BitMagic-C++
sample23.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 sample23.cpp
20
21 Example on intervals enumarator.
22
23 BitMagic bvector<> implements high performance operation on RLE
24 coded bit-vectors, transparently supporting all logical operations
25 like intersections.Serialization uses compressive encoding
26 (binary interpolative codes) to efficiently store collections
27 of intervals.
28
29 This example illustrates use of inetrval_enumerator<> to interpret
30 bit-vector as a sequence of 011110 ranges/intervals.
31
32 \sa bm::bvector::set_range
33 \sa bm::interval_enumerator
34
35 \sa sample22.cpp
36 \sa bvintervals
37*/
38
39/*! \file sample23.cpp
40 \brief Example: interval_enumerator<> - interator class for intervals
41*/
42
43#include <iostream>
44#include <assert.h>
45
46#include "bm.h"
47#include "bmintervals.h"
48#include "bmundef.h" /* clear the pre-proc defines from BM */
49
50using namespace std;
51
53
54int main(void)
55{
56 try
57 {
58 bm::bvector<> bv(bm::BM_GAP); // use RLE compressed vector from the start
59
60 bv.set_range(10, 10);
61 bv.set_range(100, 110); // sets a range of 1s [100, 110] .....11111....
62 bv.set_range(777, 888);
63 bv.set_range(65536, 65536);
64
65 bv.optimize();
66
67
68 {
70 if (ien.valid())
71 {
72 do
73 {
74 cout << "[" << ien.start() << ".." << ien.end() << "]";
75 } while (ien.advance());
76 cout << endl;
77 }
78 }
79
80 // case 2: slightly less efficient, but more STL iterator conformant
81 //
82 {
85
86 // please note prefix increment "++en" is way more efficient
87 // than possible postfix notation of "ien++" (no temp.copy)
88 //
89 for (; ien != ien_end; ++ien)
90 {
91 // also uses pair notation to represent interval
92 cout << "[" << (*ien).first << ".." << (*ien).second << "]";
93 }
94 cout << endl;
95 }
96
97
98 {
99 // construct enumerator to start from position 102 and
100 // extend start (to 100)
101 interval_enumerator_type ien(bv, 102, true);
103 for (; ien != ien_end; ++ien)
104 {
105 cout << "[" << ien.get().first << ".." << ien.get().second << "]";
106 }
107 cout << endl;
108
109 ien.go_to(105, false); // now re-position enumerator to position 105 without extend start
110 for (; ien != ien_end; ++ien)
111 {
112 cout << "[" << ien.get().first << ".." << ien.get().second << "]";
113 }
114 cout << endl;
115
116 // now re-position enumerator to position 105 without extend start
117 ien.go_to(105, false);
118 for (; ien != ien_end; ++ien)
119 {
120 cout << "[" << ien.get().first << ".." << ien.get().second << "]";
121 }
122 cout << endl;
123
124 // now re-position enumerator to position 115 wit extend start
125 // target position is not in the interval, so it should find the
126 // next available one automatically
127 //
128 ien.go_to(115, true); // extend_left=true but it should not matter
129 for (; ien != ien_end; ++ien)
130 {
131 cout << "[" << ien.get().first << ".." << ien.get().second << "]";
132 }
133 cout << endl;
134
135 // go beyond end
136 ien.go_to(1150000, true);
137 if (ien.valid())
138 {
139 assert(0);
140 }
141 else
142 {
143 cout << "EMPTY" << endl;
144 }
145 }
146
147 }
148 catch(std::exception& ex)
149 {
150 std::cerr << ex.what() << std::endl;
151 return 1;
152 }
153
154 return 0;
155}
156
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for bit ranges and intervals.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:115
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition bm.h:3635
bvector< Alloc > & set_range(size_type left, size_type right, bool value=true)
Sets all bits in the specified closed interval [left,right] Interval must be inside the bvector's siz...
Definition bm.h:2368
forward iterator class to traverse bit-vector as ranges
Definition bmintervals.h:53
const pair_type & get() const BMNOEXCEPT
Get interval pair.
size_type end() const BMNOEXCEPT
Return interval end/right as bit-vector coordinate 011110 [left..right].
bool valid() const BMNOEXCEPT
Returns true if enumerator is valid (false if traversal is done).
size_type start() const BMNOEXCEPT
Return interval start/left as bit-vector coordinate 011110 [left..right].
bool go_to(size_type pos, bool extend_start=true)
Go to inetrval at specified position Jump to position with interval. If interval is not available at ...
@ BM_GAP
GAP compression is ON.
Definition bmconst.h:148
bm::interval_enumerator< bm::bvector<> > interval_enumerator_type
Definition sample23.cpp:52
int main(void)
Definition sample23.cpp:54
Second second
Definition bmfunc.h:156
First first
Definition bmfunc.h:155