BitMagic-C++
sample18.cpp

Example of bulk insert iterator.

Example of bulk insert iterator

See also
bm::bvector::insert_iterator
bm::bvector::bulk_insert_iterator
/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/** \example sample18.cpp
Example of bulk insert iterator
\sa bm::bvector::insert_iterator
\sa bm::bvector::bulk_insert_iterator
*/
/*! \file sample18.cpp
\brief Example: bulk insert iterator
Bulk insert iterator uses internal buffer to accumulate a batch
of bit indexes and flush it faster.
This delay makes its behavior different from classic
deterministic STL iterators.
*/
#include <stdlib.h>
#include <iostream>
#include "bm.h"
using namespace std;
int main(void)
{
try
{
// use regular insert iterator to add bits to vector
for (unsigned i = 5; i != 0; --i)
{
iit = i;
cout << bv.count() << ", "; // note that bits are added immediately
}
cout << endl;
bv.clear(); // clear the vector
// bulk insert adds bits faster (on sorted data), but provides no guarantee
// that it happens immediately. Instead it uses an internal buffer to accumulate it first
// and periodically flushes it into the bit-vector.
//
{
for (bm::bvector<>::size_type i = 5; i != 0; --i)
{
bulk_iit = i;
cout << bv.count() << ", "; // note that bits are NOT added immediately
}
cout << endl;
} // <= scope is a point when data flush happens (on destruction)
cout << bv.count() << endl; // now all bits are added
bv.clear();
// bulk content flash can be forced, before going out-of-scope
// which may be problematic due to possible exceptions from the destructor.
//
{
for (bm::bvector<>::size_type i = 0; i < 5; ++i)
bulk_iit = i;
cout << bv.count() << endl; // not added yet
bulk_iit.flush(); // force bulk iterator to submit the data
cout << bv.count() << endl; // now added!
}
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Output iterator iterator designed to set "ON" bits based on input sequence of integers.
Definition bm.h:462
Output iterator iterator designed to set "ON" bits based on input sequence of integers (bit indeces).
Definition bm.h:378
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
bm::id_t size_type
Definition bm.h:117
insert_iterator inserter()
Definition bm.h:1245
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
int main(void)
Definition sample1.cpp:38