BitMagic-C++: sample18.cpp Source File

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
40 using namespace std;
41
42 int main(void)
43 {
44 try
45 {
46 bm::bvector<> bv;
47
48 // use regular insert iterator to add bits to vector
49 bm::bvector<>::insert_iterator iit = bv.inserter();
50 for (unsigned i = 5; i != 0; --i)
51 {
52 iit = i;
53 cout << bv.count() << ", "; // note that bits are added immediately
54 }
55 cout << endl;
56
57 bv.clear(); // clear the vector
58
59 // bulk insert adds bits faster (on sorted data), but provides no guarantee
60 // that it happens immediately. Instead it uses an internal buffer to accumulate it first
61 // and periodically flushes it into the bit-vector.
62 //
63 {
64 bm::bvector<>::bulk_insert_iterator bulk_iit(bv);
65 for (bm::bvector<>::size_type i = 5; i != 0; --i)
66 {
67 bulk_iit = i;
68 cout << bv.count() << ", "; // note that bits are NOT added immediately
69 }
70 cout << endl;
71 } // <= scope is a point when data flush happens (on destruction)
72 cout << bv.count() << endl; // now all bits are added
73
74 bv.clear();
75
76 // bulk content flash can be forced, before going out-of-scope
77 // which may be problematic due to possible exceptions from the destructor.
78 //
79 {
80 bm::bvector<>::bulk_insert_iterator bulk_iit(bv);
81 for (bm::bvector<>::size_type i = 0; i < 5; ++i)
82 bulk_iit = i;
83 cout << bv.count() << endl; // not added yet
84 bulk_iit.flush(); // force bulk iterator to submit the data
85 cout << bv.count() << endl; // now added!
86 }
87
88 }
89 catch(std::exception& ex)
90 {
91 std::cerr << ex.what() << std::endl;
92 return 1;
93 }
94
95 return 0;
96 }
97
98
bm::bvector::insert_iterator
Output iterator iterator designed to set "ON" bits based on input sequence of integers (bit indeces).
Definition: bm.h:377
main
int main(void)
Definition: sample18.cpp:42
bm::bvector<>
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2194
bm::bvector::clear
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:3546
bm::bvector::inserter
insert_iterator inserter()
Definition: bm.h:1245
bm::bvector::bulk_insert_iterator::flush
void flush()
Definition: bm.h:563
bm::bvector::bulk_insert_iterator
Output iterator iterator designed to set "ON" bits based on input sequence of integers.
Definition: bm.h:461
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.