BitMagic-C++: sample5.cpp Source File

BitMagic-C++

sample5.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 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
38 using namespace std;
39
40 inline
41 void Print(bm::bvector<>::size_type n)
42 {
43 cout << n << endl;;
44 }
45
46 int main(void)
47 {
48 try
49 {
50 bm::bvector<> bv;
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 }
Print
void Print(bm::bvector<>::size_type n)
Definition: sample5.cpp:41
bm::bvector::enumerator
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:599
bm::bvector::enumerator::go_to
bool go_to(size_type pos) BMNOEXCEPT
go to a specific position in the bit-vector (or next)
Definition: bm.h:6870
bm::bvector<>
bm::bvector::iterator_base::valid
bool valid() const BMNOEXCEPT
Checks if iterator is still valid. Analog of != 0 comparison for pointers.
Definition: bm.h:280
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
main
int main(void)
Definition: sample5.cpp:46
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.