BitMagic-C++
sample23.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 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 
49 using namespace std;
50 
52 
53 int main(void)
54 {
55  try
56  {
57  bm::bvector<> bv(bm::BM_GAP); // use RLE compressed vector from the start
58 
59  bv.set_range(10, 10);
60  bv.set_range(100, 110); // sets a range of 1s [100, 110] .....11111....
61  bv.set_range(777, 888);
62  bv.set_range(65536, 65536);
63 
64  bv.optimize();
65 
66 
67  {
69  if (ien.valid())
70  {
71  do
72  {
73  cout << "[" << ien.start() << ".." << ien.end() << "]";
74  } while (ien.advance());
75  cout << endl;
76  }
77  }
78 
79  // case 2: slightly less efficient, but more STL iterator conformant
80  //
81  {
84 
85  // please note prefix increment "++en" is way more efficient
86  // than possible postfix notation of "ien++" (no temp.copy)
87  //
88  for (; ien != ien_end; ++ien)
89  {
90  // also uses pair notation to represent interval
91  cout << "[" << (*ien).first << ".." << (*ien).second << "]";
92  }
93  cout << endl;
94  }
95 
96 
97  {
98  // construct enumerator to start from position 102 and
99  // extend start (to 100)
100  interval_enumerator_type ien(bv, 102, true);
101  interval_enumerator_type ien_end;
102  for (; ien != ien_end; ++ien)
103  {
104  cout << "[" << ien.get().first << ".." << ien.get().second << "]";
105  }
106  cout << endl;
107 
108  ien.go_to(105, false); // now re-position enumerator to position 105 without extend start
109  for (; ien != ien_end; ++ien)
110  {
111  cout << "[" << ien.get().first << ".." << ien.get().second << "]";
112  }
113  cout << endl;
114 
115  // now re-position enumerator to position 105 without extend start
116  ien.go_to(105, false);
117  for (; ien != ien_end; ++ien)
118  {
119  cout << "[" << ien.get().first << ".." << ien.get().second << "]";
120  }
121  cout << endl;
122 
123  // now re-position enumerator to position 115 wit extend start
124  // target position is not in the interval, so it should find the
125  // next available one automatically
126  //
127  ien.go_to(115, true); // extend_left=true but it should not matter
128  for (; ien != ien_end; ++ien)
129  {
130  cout << "[" << ien.get().first << ".." << ien.get().second << "]";
131  }
132  cout << endl;
133 
134  // go beyond end
135  ien.go_to(1150000, true);
136  if (ien.valid())
137  {
138  assert(0);
139  }
140  else
141  {
142  cout << "EMPTY" << endl;
143  }
144  }
145 
146  }
147  catch(std::exception& ex)
148  {
149  std::cerr << ex.what() << std::endl;
150  return 1;
151  }
152 
153  return 0;
154 }
155 
bm::bvector::set_range
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:2158
bm::interval_enumerator::go_to
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 ...
Definition: bmintervals.h:584
bm::pair::first
First first
Definition: bmfunc.h:123
bm::interval_enumerator::end
size_type end() const BMNOEXCEPT
Return interval end/right as bit-vector coordinate 011110 [left..right].
Definition: bmintervals.h:560
bm::bvector<>
interval_enumerator_type
bm::interval_enumerator< bm::bvector<> > interval_enumerator_type
Definition: sample23.cpp:51
bm::interval_enumerator::start
size_type start() const BMNOEXCEPT
Return interval start/left as bit-vector coordinate 011110 [left..right].
Definition: bmintervals.h:551
bm::interval_enumerator::advance
bool advance()
Definition: bmintervals.h:717
bm::interval_enumerator::get
const pair_type & get() const BMNOEXCEPT
Get interval pair.
Definition: bmintervals.h:161
bm::BM_GAP
GAP compression is ON.
Definition: bmconst.h:144
bmintervals.h
Algorithms for bit ranges and intervals.
bm::pair::second
Second second
Definition: bmfunc.h:124
bm::interval_enumerator::valid
bool valid() const BMNOEXCEPT
Returns true if enumerator is valid (false if traversal is done)
Definition: bmintervals.h:568
bm::interval_enumerator
forward iterator class to traverse bit-vector as ranges
Definition: bmintervals.h:52
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
main
int main(void)
Definition: sample23.cpp:53
bm::bvector::optimize
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:3071