BitMagic-C++
sample10.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For 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
32#include "bm.h"
33#include "bmrandom.h"
34#include "bmundef.h" /* clear the pre-proc defines from BM */
35
36using namespace std;
37
38template<class T> void PrintContainer(T first, T last)
39{
40 if (first == last)
41 cout << "<EMPTY SET>";
42 else
43 for(;first != last; ++first)
44 cout << *first << ";";
45 cout << endl;
46}
47
48
49int main(void)
50{
51 try
52 {
54 // -----------------------------------------------
55 // set some bits
56 //
58 for (i = 0; i < 30000; i+=10)
59 {
60 bv.set(i);
61 }
62
63 for (i = 300000; i < 400000; i+=100)
64 {
65 bv.set(i);
66 }
67
68 bm::bvector<> bvsubset1;
69 bm::bvector<> bvsubset2;
70
71 // random sampler instance can be shared between calls
72 //
74 rand_sampler.sample(bvsubset1, bv, 20);
75 rand_sampler.sample(bvsubset2, bv, 20);
76
77 PrintContainer(bvsubset1.first(), bvsubset1.end());
78 cout << endl;
79 PrintContainer(bvsubset2.first(), bvsubset2.end());
80 }
81 catch(std::exception& ex)
82 {
83 std::cerr << ex.what() << std::endl;
84 return 1;
85 }
86
87 return 0;
88}
89
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Generation of random subset.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
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:4153
bvector_size_type size_type
Definition: bm.h:121
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1849
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1855
void sample(BV &bv_out, const BV &bv_in, size_type sample_count)
Get random subset of input vector.
Definition: bmrandom.h:140
void PrintContainer(T first, T last)
Definition: sample10.cpp:38
int main(void)
Definition: sample10.cpp:49