BitMagic-C++
sample12.cpp

Example of how to use various bit setting techniques.Several techniques benchmarked to better illustrate relative performance.

See also
bm::bvector<>::set()
bm::bvector<>::set_bit()
bm::bvector<>::set_bit_conditional()
bm::bvector<>::set_range()
bm::bvector<>::clear_bit()
bm::bvector<>::reset()
bm::bvector<>::flip()
bm::bvector<>::swap()
bm::bvector<>::extract_next()
bm::bvector<>::set_bit_no_check()
bm::combine_or()
/*
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 sample12.cpp
Example of how to use various bit setting techniques.
Several techniques benchmarked to better illustrate relative performance.
\sa bm::bvector<>::set()
\sa bm::bvector<>::set_bit()
\sa bm::bvector<>::set_bit_conditional()
\sa bm::bvector<>::set_range()
\sa bm::bvector<>::clear_bit()
\sa bm::bvector<>::reset()
\sa bm::bvector<>::flip()
\sa bm::bvector<>::swap()
\sa bm::bvector<>::extract_next()
\sa bm::bvector<>::set_bit_no_check()
\sa bm::combine_or()
*/
/*! \file sample12.cpp
\brief Example: bvector<> analysis of bit setting methods
*/
#include <iostream>
#include <vector>
#define BM64ADDR
#include "bm.h"
#include "bmalgo.h"
#include "bmtimer.h"
using namespace std;
// timing storage for benchmarking
// depending of the build it may be "unsigned int" or 64-bit "unsigned long long"
const unsigned benchmark_count = 1000;
// Utility template function used to print container
template<class T> void PrintContainer(T first, T last)
{
if (first == last)
std::cout << "<EMPTY SET>";
else
for (; first != last; ++first)
std::cout << *first << ";";
std::cout << std::endl;
}
static
void generate_test_vectors(std::vector<bm_size_type> &v1,
std::vector<bm_size_type> &v2,
std::vector<bm_size_type> &v3)
{
for (j = 0; j < vector_max; j += 2)
{
v1.push_back(j);
}
for (j = 0; j < vector_max; j += 5)
{
v2.push_back(j);
}
for (j = 0; j < vector_max; j += 120)
{
v3.push_back(j);
}
}
// stress test for bm::bvector<>::set_bit()
//
static
{
bm::chrono_taker tt1("1. bvector<>::set_bit()", benchmark_count, &timing_map);
bm::bvector<> bv1, bv2, bv3;
for (unsigned i = 0; i < benchmark_count; ++i)
{
for (j = 0; j < vector_max; j += 2)
{
bv1.set_bit(j, true);
}
for (j = 0; j < vector_max; j += 10)
{
bv2.set_bit(j, true);
}
for (j = 0; j < vector_max; j += 120)
{
bv3.set_bit(j, true);
}
bv1.reset();
bv2.reset();
bv3.reset();
} // for
}
// stress test for bm::bvector<>::set_bit()
//
static
{
bm::chrono_taker tt1("2. bvector<>::set_bit_no_check()", benchmark_count, &timing_map);
bm::bvector<> bv1, bv2, bv3;
bv1.init();
bv2.init();
bv3.init();
for (unsigned i = 0; i < benchmark_count; ++i)
{
for (j = 0; j < vector_max; j += 2)
{
}
for (j = 0; j < vector_max; j += 10)
{
}
for (j = 0; j < vector_max; j += 120)
{
}
}
}
static
void combine_or_test(std::vector<bm_size_type> &v1,
std::vector<bm_size_type> &v2,
std::vector<bm_size_type> &v3)
{
bm::chrono_taker tt1("3. combine_or()", benchmark_count, &timing_map);
bm::bvector<> bv1, bv2, bv3;
for (unsigned i = 0; i < benchmark_count; ++i)
{
bm::combine_or(bv1, v1.begin(), v1.end());
bm::combine_or(bv2, v2.begin(), v2.end());
bm::combine_or(bv3, v3.begin(), v3.end());
}
}
static
void bvector_bulk_set_test(std::vector<bm_size_type> &v1,
std::vector<bm_size_type> &v2,
std::vector<bm_size_type> &v3)
{
bm::chrono_taker tt1("3. bvector<>::set() array", benchmark_count, &timing_map);
bm::bvector<> bv1, bv2, bv3;
for (unsigned i = 0; i < benchmark_count; ++i)
{
bv1.set(&v1[0], bm_size_type(v1.size()));
bv2.set(&v2[0], bm_size_type(v2.size()));
bv3.set(&v3[0], bm_size_type(v3.size()));
}
}
int main(void)
{
try
{
// 0. create bvector, use brace initialization to add some initial data
//
bm::bvector<> bv1 { 2, 3, 4 };
PrintContainer(bv1.first(), bv1.end()); // 2, 3, 4
bv1.clear();
// 1. Set some bits using regular bvector<>::set() method
//
bv1.set(10);
bv1.set(256);
bv1.set(1000000);
PrintContainer(bv1.first(), bv1.end()); // 10, 256, 1000000
// 2. now use bvector<>::set_bit()
// it returns a report if target actually changed
//
bm::id_t bits[] = { 256, 512, 10 };
unsigned cnt = 0;
for (unsigned i = 0; i < sizeof(bits) / sizeof(bits[0]); ++i)
{
bool b = bv1.set_bit(bits[i], true);
cnt += b;
}
std::cout << "Number of bits changed:" << cnt << std::endl;
PrintContainer(bv1.first(), bv1.end());
// 3. set and clear some bits using bvector<>::set_bit_conditional()
// method sets bit n only if current value equals the condition
//
bool b;
b = bv1.set_bit_conditional(5, true, false); // set bit 5 to true if it is false (yes)
std::cout << "Bit 5 set:" << (b ? " yes " : " no ") << std::endl; // (yes)
b = bv1.set_bit_conditional(256, true, false); // set bit 256 to true if it is false
std::cout << "Bit 256 set:" << (b ? " yes " : " no ") << std::endl; // (no)
b = bv1.set_bit_conditional(256, true, true); // set bit 256 to true if it is true
std::cout << "Bit 256 set:" << (b ? " yes " : " no ") << std::endl; // (no)
PrintContainer(bv1.first(), bv1.end());
// 4. set or clear multiple bits using bvector::set_range()
// This method is faster than calling bvector::set() many times in a row
//
bv1.set_range(10, 15, true); // set all bits in [10..15] closed interval
PrintContainer(bv1.first(), bv1.end());
bv1.set_range(10, 12, false); // clear all bits in [10..15] closed interval
PrintContainer(bv1.first(), bv1.end());
// 5. bvector::clear_bit() - same as set_bit() just a syntax sugar
//
b = bv1.clear_bit(13);
std::cout << "Bit 13 set:" << (b ? " yes " : " no ") << std::endl; // (yes)
// 6. bvector<>::reset() - clears all the bits, frees the blocks memory
// same as bvector<>::clear(true);
//
bv1.reset();
PrintContainer(bv1.first(), bv1.end()); // <EMPTY>
// 7. use bm::combine_or() to set bits
//
bm::combine_or(bv1, &bits[0], &bits[0] + (sizeof(bits) / sizeof(bits[0])));
PrintContainer(bv1.first(), bv1.end()); // 10, 256, 512
// 8. use bvector<>::flip( ) to flip a bit
//
bv1.flip(256);
bv1.flip(257);
PrintContainer(bv1.first(), bv1.end()); // 10, 257, 512
// 9. bvector<>::swap() to flip content of two bit-vectors
//
bv1.swap(bv2);
PrintContainer(bv1.first(), bv1.end()); // <EMPTY>
PrintContainer(bv2.first(), bv2.end()); // 10, 257, 512
// 10. use bvector<>::extract_next() to find ON bit and turn it to 0
// this function is useful for building FIFO queues on bvector
//
bm_size_type p = 1;
for (p = bv2.extract_next(p); p != 0; p = bv2.extract_next(p))
{
std::cout << "Extracted p = " << p << std::endl;
}
PrintContainer(bv2.first(), bv2.end()); // <EMPTY>
// 11. use bvector<>::set_bit_no_check() to set bits faster
//
bv3.init(); // the key here you MUST call init() before setting bits this way
bv3.set_bit_no_check(100);
bv3.set_bit_no_check(1000);
PrintContainer(bv3.first(), bv3.end()); // 10, 100, 1000
std::vector<bm_size_type> v1, v2, v3;
generate_test_vectors(v1, v2, v3);
for (unsigned k = 0; k < 1000; ++k)
{
// run a few CPU "pre-heat" loops to get consistent results
bm_size_type s = 0;
for (i = 0; i < v1.size(); ++i)
s = s + s* v1[i] + i;
for (i = 0; i < v2.size(); ++i)
s = s + s* v2[i] + i;
std::cout << s << "\r";
}
std::cout << std::endl << "Running benchmarks..." << std::endl;
combine_or_test(v1, v2, v3);
bvector_bulk_set_test(v1, v2, v3);
// print all test timing results
//
std::cout << std::endl;
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
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:2172
bv_set_bit_no_check_test
static void bv_set_bit_no_check_test()
Definition: sample12.cpp:125
main
int main(void)
Definition: sample12.cpp:186
bm::bvector::flip
bvector< Alloc > & flip(size_type n)
Flips bit n.
Definition: bm.h:1245
bm::chrono_taker
Utility class to collect performance measurements and statistics.
Definition: bmtimer.h:39
bm::bvector::reset
bvector< Alloc > & reset()
Clears every bit in the bitvector.
Definition: bm.h:1239
bv_set_bit_test
static void bv_set_bit_test()
Definition: sample12.cpp:95
vector_max
bm_size_type vector_max
Definition: sample12.cpp:56
bm::bvector::set_bit_no_check
void set_bit_no_check(size_type n)
Set bit without checking preconditions (size, etc)
Definition: bm.h:3482
bm_size_type
bm::bvector ::size_type bm_size_type
Definition: sample12.cpp:53
combine_or_test
static void combine_or_test(std::vector< bm_size_type > &v1, std::vector< bm_size_type > &v2, std::vector< bm_size_type > &v3)
Definition: sample12.cpp:153
bmalgo.h
Algorithms for bvector<> (main include)
bm::bvector::swap
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
Definition: bm.h:3395
bm::bvector<>
bm::bvector::set
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:3595
timing_map
bm::chrono_taker::duration_map_type timing_map
Definition: sample12.cpp:50
bm::bvector::init
void init()
Explicit post-construction initialization.
Definition: bm.h:2137
bmtimer.h
Timing utilities for benchmarking (internal)
bm::chrono_taker::ct_all
@ ct_all
Definition: bmtimer.h:60
bm::id_t
unsigned int id_t
Definition: bmconst.h:37
bm::bvector::set_bit_conditional
bool set_bit_conditional(size_type n, bool val, bool condition)
Sets bit n only if current value equals the condition.
Definition: bm.h:3604
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:3560
bvector_bulk_set_test
static void bvector_bulk_set_test(std::vector< bm_size_type > &v1, std::vector< bm_size_type > &v2, std::vector< bm_size_type > &v3)
Definition: sample12.cpp:169
bm::combine_or
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1016
bm::chrono_taker::duration_map_type
std::map< std::string, statistics > duration_map_type
test name to duration map
Definition: bmtimer.h:65
bm::bvector::end
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1790
bm::chrono_taker::print_duration_map
static void print_duration_map(const duration_map_type &dmap, format fmt=ct_time)
Definition: bmtimer.h:127
bm::bvector::first
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1784
bm::bvector::clear_bit
bool clear_bit(size_type n)
Clears bit n.
Definition: bm.h:1219
PrintContainer
void PrintContainer(T first, T last)
Definition: sample12.cpp:60
bm::bvector::extract_next
size_type extract_next(size_type prev)
Finds the number of the next bit ON and sets it to 0.
Definition: bm.h:1564
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
benchmark_count
const unsigned benchmark_count
Definition: sample12.cpp:55
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::bvector::set_bit
bool set_bit(size_type n, bool val=true)
Sets bit n.
Definition: bm.h:3631
generate_test_vectors
static void generate_test_vectors(std::vector< bm_size_type > &v1, std::vector< bm_size_type > &v2, std::vector< bm_size_type > &v3)
Definition: sample12.cpp:72