BitMagic-C++
samples
bvsample02
sample2.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 sample2.cpp
20
Example demonstrates using set operations AND, OR, XOR, etc.
21
\sa bvsetalgebra.cpp
22
*/
23
24
/*! \file sample2.cpp
25
\brief Example: bvector<> set algebra operations AND, OR, XOR, etc.
26
*/
27
28
#include <iostream>
29
#include "
bm.h
"
30
31
using namespace
std;
32
33
static
34
void
print_bvector
(
const
bm::bvector<>
& bv)
35
{
36
bm::bvector<>::size_type
value = bv.
get_first
();
37
do
38
{
39
cout << value;
40
value = bv.
get_next
(value);
41
if
(value)
42
{
43
cout <<
","
;
44
}
45
else
46
{
47
break
;
48
}
49
}
while
(1);
50
cout << endl;
51
}
52
53
int
main
(
void
)
54
{
55
try
56
{
57
bm::bvector<>
bv1;
58
bm::bvector<>
bv2;
59
bm::bvector<>
bv3;
60
61
bv1.
set
(10);
62
bv1.
set
(100);
63
bv1.
set
(1000000);
64
65
66
bv2.
set
(10);
67
bv2.
set
(100);
68
69
// Logical AND operation on bv2 (bv1 is the argument)
70
// bv2 = bv2 AND bv1
71
72
bv3 = bv1 & bv2;
73
print_bvector
(bv3);
74
75
bv2 &= bv1;
// You also can use: bv2.bit_and(bv1);
76
print_bvector
(bv2);
77
78
// bv2 = bv2 OR bv1
79
80
bv3 = bv1 | bv2;
81
print_bvector
(bv3);
82
83
bv2 |= bv1;
// You can also use: bv2.bit_or(bv1);
84
print_bvector
(bv2);
85
86
87
bv1.
set
(1000000,
false
);
88
89
// bv2 = bv2 SUB bv1
90
91
bv3 = bv2 - bv1;
92
print_bvector
(bv3);
93
94
bv2 -= bv1;
// You can also use: bv2.bit_sub(bv1);
95
print_bvector
(bv2);
96
97
// bv2 XOR bv1
98
99
bv3 = bv2 ^ bv1;
100
print_bvector
(bv3);
101
102
bv2 ^= bv1;
// You can also use: bv2.bit_xor(bv1);
103
print_bvector
(bv2);
104
105
// For lexicographical comparison there is set of overloaded
106
// operators and function compare.
107
108
if
(bv2 == bv3)
109
{
110
cerr <<
"Equivalent. Comparison result = "
111
<< bv2.compare(bv3) << endl;
112
}
113
else
114
{
115
cout <<
"Error."
<< endl;
116
return
1;
117
}
118
}
119
catch
(std::exception& ex)
120
{
121
std::cerr << ex.what() << std::endl;
122
}
123
124
125
return
0;
126
}
main
int main(void)
Definition:
sample2.cpp:53
bm::bvector::get_next
size_type get_next(size_type prev) const
Finds the number of the next bit ON.
Definition:
bm.h:1897
bm::bvector<>
bm::bvector::set
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:3539
print_bvector
static void print_bvector(const bm::bvector<> &bv)
Definition:
sample2.cpp:34
bm::bvector::get_first
size_type get_first() const
find first 1 bit in vector. Function may return 0 and this requires an extra check if bit 0 is actual...
Definition:
bm.h:1888
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.
Generated by
1.8.17