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
39#include "bm.h"
40
41using namespace std;
42
43
44const unsigned MAX_VALUE = 10000000;
45
46static
48{
49 unsigned start = MAX_VALUE / (rand()%10);
50 for (unsigned i = start; i < MAX_VALUE; ++i)
51 {
52 if ((rand() % 10))
53 {
54 bv->set(i);
55 }
56 }
57}
58
59
60int main(void)
61{
62 try
63 {
64 bm::bvector<> bv1;
65 bm::bvector<> bv2;
66
67 fill_bvector(&bv1);
68 fill_bvector(&bv2);
69
70 cout << "bv1 count = " << bv1.count() << endl;
71 cout << "bv2 count = " << bv2.count() << endl;
72
73 bool found;
74 bm::bvector<>::size_type first, last;
75
76 found = bv1.find(first);
77 if (found)
78 cout << "bv1 first = " << first << endl;
79
80 found = bv1.find_reverse(last);
81 if (found)
82 cout << "bv1 last = " << last << endl;
83
84 found = bv2.find(first);
85 if (found)
86 cout << "bv2 first = " << first << endl;
87
88 found = bv2.find_reverse(last);
89 if (found)
90 cout << "bv2 last = " << last << endl;
91
92 found = bv1.find_range(first, last);
93 if (found)
94 cout << "bv1 range = [" << first << ", " << last << "]" << endl;
95
96 found = bv2.find_range(first, last);
97 if (found)
98 cout << "bv2 range = [" << first << ", " << last << "]" << endl;
99
100 }
101 catch(std::exception& ex)
102 {
103 std::cerr << ex.what() << std::endl;
104 return 1;
105 }
106
107
108 return 0;
109}
110
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bool find(size_type &pos) const BMNOEXCEPT
Finds index of first 1 bit.
Definition bm.h:4035
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
bool find_range(size_type &first, size_type &last) const BMNOEXCEPT
Finds dynamic range of bit-vector [first, last].
Definition bm.h:4081
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
bool find_reverse(size_type &pos) const BMNOEXCEPT
Finds last index of 1 bit.
Definition bm.h:3978
static void fill_bvector(bm::bvector<> *bv)
Definition sample15.cpp:47
int main(void)
Definition sample15.cpp:60
const unsigned MAX_VALUE
Definition sample15.cpp:44