BitMagic-C++
sample15.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 sample15.cpp
20Example for finding first and last bits in bit-vector (dynamic range).
21
22Ranges of bit-vectors can be used to find probability of intersection.
23For instance, in some corner cases AND product can be predicted empty if vectors
24belong to different ranges.
25
26 @sa bm::bvector::find()
27 @sa bm::bvector::find_reverse()
28 @sa bm::bvector::find_range()
29*/
30
31/*! \file sample15.cpp
32 \brief Example: bvector<> methods to find last bit and bit-vectors effective range
33*/
34
35#include <stdlib.h>
36#include <iostream>
37#include <vector>
38#include <cassert>
39
40#include "bm.h"
41#include "bmundef.h" /* clear the pre-proc defines from BM */
42
43using namespace std;
44
45
46const unsigned MAX_VALUE = 10000000;
47
48static
50{
51 unsigned start = MAX_VALUE / (unsigned)(rand()%10);
52 for (unsigned i = start; i < MAX_VALUE; ++i)
53 {
54 if ((rand() % 10))
55 {
56 bv->set(i);
57 }
58 }
59}
60
61
62int main(void)
63{
64 try
65 {
66 bm::bvector<> bv1;
67 bm::bvector<> bv2;
68
69 fill_bvector(&bv1);
70 fill_bvector(&bv2);
71
72 cout << "bv1 count = " << bv1.count() << endl;
73 cout << "bv2 count = " << bv2.count() << endl;
74
75 bool found;
76 bm::bvector<>::size_type first, last, pos, second;
77
78 found = bv1.find(first);
79 if (found)
80 cout << "bv1 first = " << first << endl;
81
82 // make a repeat find starting on a discovered position
83 //
84 // find will return the same position (if it is set)
85 found = bv1.find(first, pos);
86 assert (found);
87 {
88 cout << "bv1 pos = " << pos << endl; // will be same as first
89 assert(pos == first);
90 }
91
92 // Q: what if we need a second set position?
93 // A: increament previously found index and find() again
94 //
95 found = bv1.find(first+1, second); // use first + 1
96 assert (found);
97 {
98 cout << "bv1 second = " << second << endl;
99 assert(second > first);
100 assert(bv1.test(second));
101 }
102
103 found = bv1.find_reverse(last);
104 if (found)
105 cout << "bv1 last = " << last << endl;
106
107 found = bv2.find(first);
108 if (found)
109 cout << "bv2 first = " << first << endl;
110
111 found = bv2.find_reverse(last);
112 if (found)
113 cout << "bv2 last = " << last << endl;
114
115 found = bv1.find_range(first, last);
116 if (found)
117 cout << "bv1 range = [" << first << ", " << last << "]" << endl;
118
119 found = bv2.find_range(first, last);
120 if (found)
121 cout << "bv2 range = [" << first << ", " << last << "]" << endl;
122
123 }
124 catch(std::exception& ex)
125 {
126 std::cerr << ex.what() << std::endl;
127 return 1;
128 }
129
130
131 return 0;
132}
133
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
bool test(size_type n) const BMNOEXCEPT
returns true if bit n is set and false is bit n is 0.
Definition: bm.h:1480
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
bool find_range(size_type &first, size_type &last) const BMNOEXCEPT
Finds dynamic range of bit-vector [first, last].
Definition: bm.h:4901
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_reverse(size_type &pos) const BMNOEXCEPT
Finds last index of 1 bit.
Definition: bm.h:4673
static void fill_bvector(bm::bvector<> *bv)
Definition: sample15.cpp:49
int main(void)
Definition: sample15.cpp:62
const unsigned MAX_VALUE
Definition: sample15.cpp:46