BitMagic-C++
sample1.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 sample1.cpp
20 Example how to use bvector<> to set bits and then retrieve indexes of ON bits
21
22
23 \sa bm::bvector<>::get_next()
24 \sa bm::bvector<>::get_first()
25 \sa bm::bvector<>::set()
26 \sa bm::bvector<>::count()
27 \sa bm::bvector<>::clear()
28 \sa bm::bvector<>::swap()
29
30 */
31
32/*! \file sample1.cpp
33 \brief Example: bvector<> set bits and then retrieve indexes of ON bits
34*/
35#include <cassert>
36#include <iostream>
37
38#include "bm.h"
39#include "bmundef.h" /* clear the pre-proc defines from BM */
40
41using namespace std;
42
43int main(void)
44{
45 try
46 {
47 bm::bvector<> bv { 1, 2, 3 }; // Bitvector variable declaration with init list
48
49 cout << "1. bitcount: " << bv.count() << endl;
50
51 // Set some bits.
52
53 bv.set(10);
54 bv.set(100);
55 bv.set(1000000);
56
57 // New bitvector's count.
58
59 cout << "2. bitcount: " << bv.count() << endl;
60
61
62 // Print the bitvector.
63
64 auto value = bv.get_first();
65 do
66 {
67 cout << value;
68 value = bv.get_next(value);
69 if (value)
70 {
71 cout << ",";
72 }
73 else
74 {
75 break;
76 }
77 } while(1);
78
79 cout << endl;
80
81 bv.clear(); // Clean up.
82
83 cout << "3. bitcount: " << bv.count() << endl;
84
85 // We also can use operators to set-clear bits;
86
87 bv[10] = true;
88 bv[100] = true;
89 bv[10000] = true;
90
91 cout << "4. bitcount: " << bv.count() << endl;
92
93 if (bv[10])
94 {
95 bv[10] = false;
96 }
97
98 cout << "5. bitcount: " << bv.count() << endl; // 2
99
100 // swap two values
101
102 bv.swap(10, 100);
103
104 cout << "6. bitcount: " << bv.count() << endl; // 2 again
105
106 assert(bv.test(10)); // make sure values swapped ok
107 assert(!bv.test(100));
108
109 }
110 catch(std::exception& ex)
111 {
112 std::cerr << ex.what() << std::endl;
113 return 1;
114 }
115
116
117 return 0;
118}
119
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:115
bool test(size_type n) const BMNOEXCEPT
returns true if bit n is set and false is bit n is 0.
Definition bm.h:1502
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition bm.h:2401
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:4188
size_type get_next(size_type prev) const BMNOEXCEPT
Finds the number of the next bit ON.
Definition bm.h:1609
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
Definition bm.h:3966
size_type get_first() const BMNOEXCEPT
find first 1 bit in vector. Function may return 0 and this requires an extra check if bit 0 is actual...
Definition bm.h:1600
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition bm.h:4149
int main(void)
Definition sample1.cpp:43