BitMagic-C++
sample18.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 sample18.cpp
20  Example of bulk insert iterator
21  \sa bm::bvector::insert_iterator
22  \sa bm::bvector::bulk_insert_iterator
23 
24 */
25 
26 /*! \file sample18.cpp
27  \brief Example: bulk insert iterator
28 
29  Bulk insert iterator uses internal buffer to accumulate a batch
30  of bit indexes and flush it faster.
31  This delay makes its behavior different from classic
32  deterministic STL iterators.
33 */
34 
35 #include <stdlib.h>
36 #include <iostream>
37 
38 #include "bm.h"
39 #include "bmundef.h" /* clear the pre-proc defines from BM */
40 
41 using namespace std;
42 
43 int main(void)
44 {
45  try
46  {
47  bm::bvector<> bv;
48 
49  // use regular insert iterator to add bits to vector
51  for (unsigned i = 5; i != 0; --i)
52  {
53  iit = i;
54  cout << bv.count() << ", "; // note that bits are added immediately
55  }
56  cout << endl;
57 
58  bv.clear(); // clear the vector
59 
60  // bulk insert adds bits faster (on sorted data), but provides no guarantee
61  // that it happens immediately. Instead it uses an internal buffer to accumulate it first
62  // and periodically flushes it into the bit-vector.
63  //
64  {
66  for (bm::bvector<>::size_type i = 5; i != 0; --i)
67  {
68  bulk_iit = i;
69  cout << bv.count() << ", "; // note that bits are NOT added immediately
70  }
71  cout << endl;
72  } // <= scope is a point when data flush happens (on destruction)
73  cout << bv.count() << endl; // now all bits are added
74 
75  bv.clear();
76 
77  // bulk content flash can be forced, before going out-of-scope
78  // which may be problematic due to possible exceptions from the destructor.
79  //
80  {
82  for (bm::bvector<>::size_type i = 0; i < 5; ++i)
83  bulk_iit = i;
84  cout << bv.count() << endl; // not added yet
85  bulk_iit.flush(); // force bulk iterator to submit the data
86  cout << bv.count() << endl; // now added!
87  }
88 
89  }
90  catch(std::exception& ex)
91  {
92  std::cerr << ex.what() << std::endl;
93  return 1;
94  }
95 
96  return 0;
97 }
98 
99 
Output iterator iterator designed to set "ON" bits based on input sequence of integers.
Definition: bm.h:461
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2220
bm::id_t size_type
Definition: bm.h:117
insert_iterator inserter()
Definition: bm.h:1259
pre-processor un-defines to avoid global space pollution (internal)
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:3570
int main(void)
Definition: sample18.cpp:43
Output iterator iterator designed to set "ON" bits based on input sequence of integers (bit indeces)...
Definition: bm.h:377