BitMagic-C++
sample10.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 For more information please visit: http://bitmagic.io
17 */
18 
19 /** \example sample10.cpp
20  Example of how to get random subset of a bit-vector
21 
22  \sa bm::random_subset
23  */
24 
25 /*! \file sample10.cpp
26  \brief Example: bvector<> generation of random sub-set
27 */
28 
29 
30 #include <iostream>
31 #include "bm.h"
32 #include "bmrandom.h"
33 
34 using namespace std;
35 
36 template<class T> void PrintContainer(T first, T last)
37 {
38  if (first == last)
39  cout << "<EMPTY SET>";
40  else
41  for(;first != last; ++first)
42  cout << *first << ";";
43  cout << endl;
44 }
45 
46 
47 int main(void)
48 {
49  try
50  {
51  bm::bvector<> bv;
52  // -----------------------------------------------
53  // set some bits
54  //
56  for (i = 0; i < 30000; i+=10)
57  {
58  bv.set(i);
59  }
60 
61  for (i = 300000; i < 400000; i+=100)
62  {
63  bv.set(i);
64  }
65 
66  bm::bvector<> bvsubset1;
67  bm::bvector<> bvsubset2;
68 
69  // random sampler instance can be shared between calls
70  //
71  bm::random_subset<bm::bvector<> > rand_sampler;
72  rand_sampler.sample(bvsubset1, bv, 20);
73  rand_sampler.sample(bvsubset2, bv, 20);
74 
75  PrintContainer(bvsubset1.first(), bvsubset1.end());
76  cout << endl;
77  PrintContainer(bvsubset2.first(), bvsubset2.end());
78  }
79  catch(std::exception& ex)
80  {
81  std::cerr << ex.what() << std::endl;
82  return 1;
83  }
84 
85  return 0;
86 }
87 
bmrandom.h
Generation of random subset.
bm::bvector<>
bm::random_subset::sample
void sample(BV &bv_out, const BV &bv_in, size_type sample_count)
Get random subset of input vector.
Definition: bmrandom.h:140
bm::bvector::set
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:3581
PrintContainer
void PrintContainer(T first, T last)
Definition: sample10.cpp:36
main
int main(void)
Definition: sample10.cpp:47
bm::random_subset
Definition: bmrandom.h:56
bm::bvector::end
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1776
bm::bvector::first
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1770
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.