Immutable bit-vectors.This example generates sparse bit-vectors and shows how to make them use less memory via optimization (after optimization bit-vector remains mutable) and freezing it into immutable, where memory all edit reservations are getting dropped. Freezing also allocates memory blocks together, reducing the heap fragmentation.
#include <stdlib.h>
#include <iostream>
#include <cassert>
using namespace std;
static
{
const unsigned fill_factor1 = 2500;
const unsigned fill_factor2 = 150;
if (unsigned(rand()) % fill_factor1)
for (
unsigned i = 0; i <
MAX_VALUE; i+=fill_factor2)
}
static
{
}
{
try
{
cout << "Statistics before memory optimization" << endl;
cout << "-------------------------------------" << endl << endl;
{
}
cout << "Statistics after memory optimization" << endl;
cout << "-------------------------------------" << endl;
assert(bv1_ro.is_ro());
cout << bv2.
is_ro() << endl;
cout << "Statistics after freezing" << endl;
cout << "-------------------------------------" << endl;
{
assert(bv11.is_ro());
bool eq = bv11.equal(bv1_ro);
assert(eq);
}
{
}
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
return 0;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
void freeze()
Turn current vector to read-only (immutable vector).
bool is_ro() const BMNOEXCEPT
Returns true if vector is read-only.
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
@ READWRITE
mutable (read-write object)
@ READONLY
immutable (read-only object)
static void print_statistics(const bm::bvector<> &bv)
static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)
Fill bit-vectors with values using dense and sparse distrubutions.
size_t gap_cap_overhead
gap memory overhead between length and capacity
size_t gap_blocks
Number of GAP blocks.
size_t bit_blocks
Number of bit blocks.
size_t max_serialize_mem
estimated maximum memory for serialization
size_t memory_used
memory usage for all blocks and service tables
Statistical information about bitset's memory allocation details.