BitMagic-C++
sample2.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 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
30#include "bm.h"
31#include "bmundef.h" /* clear the pre-proc defines from BM */
32
33using namespace std;
34
35static
37{
39 do
40 {
41 cout << value;
42 value = bv.get_next(value);
43 if (value)
44 {
45 cout << ",";
46 }
47 else
48 {
49 break;
50 }
51 } while(1);
52 cout << endl;
53}
54
55int main(void)
56{
57 try
58 {
59 bm::bvector<> bv1;
60 bm::bvector<> bv2;
61 bm::bvector<> bv3;
62
63 bv1.set(10);
64 bv1.set(100);
65 bv1.set(1000000);
66
67
68 bv2.set(10);
69 bv2.set(100);
70
71 // Logical AND operation on bv2 (bv1 is the argument)
72 // bv2 = bv2 AND bv1
73
74 bv3 = bv1 & bv2;
75 print_bvector(bv3);
76
77 bv2 &= bv1; // You also can use: bv2.bit_and(bv1);
78 print_bvector(bv2);
79
80 // bv2 = bv2 OR bv1
81
82 bv3 = bv1 | bv2;
83 print_bvector(bv3);
84
85 bv2 |= bv1; // You can also use: bv2.bit_or(bv1);
86 print_bvector(bv2);
87
88
89 bv1.set(1000000, false);
90
91 // bv2 = bv2 SUB bv1
92
93 bv3 = bv2 - bv1;
94 print_bvector(bv3);
95
96 bv2 -= bv1; // You can also use: bv2.bit_sub(bv1);
97 print_bvector(bv2);
98
99 // bv2 XOR bv1
100
101 bv3 = bv2 ^ bv1;
102 print_bvector(bv3);
103
104 // product of XOR is a mismatch vector (definition of XOR)
105 //
106 {
108 bool f = bv3.find(pos);
109 if (f)
110 {
111 cout << "XOR mismatch position = " << pos << endl;
112 }
113
114 // if we need to find only first mismatch we don't need a full
115 // XOR product, we can use bvector<>::find_first_mismatch()
116 //
117 f = bv2.find_first_mismatch(bv1, pos);
118 if (f)
119 {
120 cout << "search mismatch position = " << pos << endl;
121 }
122 }
123
124 bv2 ^= bv1; // You can also use: bv2.bit_xor(bv1);
125 print_bvector(bv2);
126
127 // For lexicographical comparison there is set of overloaded
128 // operators and function compare (see also bvector<>::equal() )
129
130 if (bv2 == bv3)
131 {
132 cerr << "Equivalent. Comparison result = "
133 << bv2.compare(bv3) << endl;
134 }
135 else
136 {
137 cout << "Error." << endl;
138 return 1;
139 }
140 }
141 catch(std::exception& ex)
142 {
143 std::cerr << ex.what() << std::endl;
144 }
145
146
147 return 0;
148}
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 find(size_type &pos) const BMNOEXCEPT
Finds index of first 1 bit.
Definition: bm.h:4855
size_type get_next(size_type prev) const BMNOEXCEPT
Finds the number of the next bit ON.
Definition: bm.h:1587
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:4153
bvector_size_type size_type
Definition: bm.h:121
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:3827
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:1578
int compare(const bvector< Alloc > &bvect) const BMNOEXCEPT
Lexicographical comparison with a bitvector.
Definition: bm.h:3709
int main(void)
Definition: sample2.cpp:55
static void print_bvector(const bm::bvector<> &bv)
Definition: sample2.cpp:36