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
37#include "bm.h"
38#include "bmundef.h" /* clear the pre-proc defines from BM */
39
40using namespace std;
41
42inline
44{
45 cout << n << endl;;
46}
47
48int main(void)
49{
50 try
51 {
53
54 bv[10] = true;
55 bv[100] = true;
56 bv[10000] = true;
57 bv[65536] = true;
58 bv[65537] = true;
59 bv[65538] = true;
60 bv[65540] = true;
61
63 bm::bvector<>::enumerator en_end = bv.end();
64
65 while (en < en_end)
66 {
67 cout << *en << ", ";
68 ++en; // Fastest way to increment enumerator
69 }
70 cout << endl;
71
72 en = bv.first();
73
74 // This is not the fastest way to do the job, because for_each
75 // often will try to calculate difference between iterators,
76 // which is expensive for enumerators.
77 // But it can be useful for some STL loyal applications.
78
79 std::for_each(en, en_end, Print);
80 cout << endl;
81
82 // example to illustrate random positioning of enumerator
83 // go to a random bit number, enumerator automatically finds the available bit
84 //
85 en.go_to(65537);
86 for (; en.valid(); ++en)
87 {
88 cout << *en << ", ";
89 }
90 cout << endl;
91 }
92 catch(std::exception& ex)
93 {
94 std::cerr << ex.what() << std::endl;
95 return 1;
96 }
97 return 0;
98}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool go_to(size_type pos) BMNOEXCEPT
go to a specific position in the bit-vector (or next)
Definition: bm.h:7971
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition: bm.h:283
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
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 Print(bm::bvector<>::size_type n)
Definition: sample5.cpp:43
int main(void)
Definition: sample5.cpp:48