BitMagic-C++: sample2.cpp Source File

BitMagic-C++

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 // product of XOR is a mismatch vector (definition of XOR)
103 //
104 {
105 bm::bvector<>::size_type pos;
106 bool f = bv3.find(pos);
107 if (f)
108 {
109 cout << "XOR mismatch position = " << pos << endl;
110 }
111
112 // if we need to find only first mismatch we don't need a full
113 // XOR product, we can use bvector<>::find_first_mismatch()
114 //
115 f = bv2.find_first_mismatch(bv1, pos);
116 if (f)
117 {
118 cout << "search mismatch position = " << pos << endl;
119 }
120 }
121
122 bv2 ^= bv1; // You can also use: bv2.bit_xor(bv1);
123 print_bvector(bv2);
124
125 // For lexicographical comparison there is set of overloaded
126 // operators and function compare (see also bvector<>::equal() )
127
128 if (bv2 == bv3)
129 {
130 cerr << "Equivalent. Comparison result = "
131 << bv2.compare(bv3) << endl;
132 }
133 else
134 {
135 cout << "Error." << endl;
136 return 1;
137 }
138 }
139 catch(std::exception& ex)
140 {
141 std::cerr << ex.what() << std::endl;
142 }
143
144
145 return 0;
146 }
main
int main(void)
Definition: sample2.cpp:53
bm::bvector::find_first_mismatch
bool find_first_mismatch(const bvector< Alloc > &bvect, size_type &pos, size_type search_to=bm::id_max) const BMNOEXCEPT
Find index of first bit different between this and the agr vector.
Definition: bm.h:3277
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:3581
bm::bvector::find
bool find(size_type &pos) const BMNOEXCEPT
Finds index of first 1 bit.
Definition: bm.h:4035
print_bvector
static void print_bvector(const bm::bvector<> &bv)
Definition: sample2.cpp:34
bm::bvector::get_next
size_type get_next(size_type prev) const BMNOEXCEPT
Finds the number of the next bit ON.
Definition: bm.h:1540
bm::bvector::get_first
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:1531
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.