BitMagic-C++
sample5.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 sample5.cpp
20 Example demonstrates using enumerators - the fastest way to retrieve
21 indexes of 1 bits from the bitvector. This approach works faster than
22 get_first()/get_next() functions.
23
24 \sa bm::bvector<>::enumerator
25 \sa bm::bvector<>::first()
26 \sa bm::bvector<>::end()
27 \sa bm::bvector<>::get_enumerator()
28*/
29
30/*! \file sample5.cpp
31 \brief Example: bvector<>::enumerator use
32*/
33
34#include <iostream>
35#include <algorithm>
36#include "bm.h"
37
38using namespace std;
39
40inline
42{
43 cout << n << endl;;
44}
45
46int main(void)
47{
48 try
49 {
51
52 bv[10] = true;
53 bv[100] = true;
54 bv[10000] = true;
55 bv[65536] = true;
56 bv[65537] = true;
57 bv[65538] = true;
58 bv[65540] = true;
59
60 bm::bvector<>::enumerator en = bv.first();
61 bm::bvector<>::enumerator en_end = bv.end();
62
63 while (en < en_end)
64 {
65 cout << *en << ", ";
66 ++en; // Fastest way to increment enumerator
67 }
68 cout << endl;
69
70 en = bv.first();
71
72 // This is not the fastest way to do the job, because for_each
73 // often will try to calculate difference between iterators,
74 // which is expensive for enumerators.
75 // But it can be useful for some STL loyal applications.
76
77 std::for_each(en, en_end, Print);
78 cout << endl;
79
80 // example to illustrate random positioning of enumerator
81 // go to a random bit number, enumerator automatically finds the available bit
82 //
83 en.go_to(65537);
84 for (; en.valid(); ++en)
85 {
86 cout << *en << ", ";
87 }
88 cout << endl;
89 }
90 catch(std::exception& ex)
91 {
92 std::cerr << ex.what() << std::endl;
93 return 1;
94 }
95 return 0;
96}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition bm.h:1776
void Print(bm::bvector<>::size_type n)
Definition sample5.cpp:41
int main(void)
Definition sample5.cpp:46