BitMagic-C++
sample4.cpp

Exmaple demonstrates bitvector serialization/deserialization.

Exmaple demonstrates bitvector serialization/deserialization.For more information please visit: http://bmagic.sourceforge.net

See also
bm::serializer
bm::deserialize
/*
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 sample4.cpp
Exmaple demonstrates bitvector serialization/deserialization.
For more information please visit: http://bmagic.sourceforge.net
\sa bm::serializer
\sa bm::deserialize
*/
/*! \file sample4.cpp
\brief Example: bvector<> serialization/deserialization.
*/
#include <stdlib.h>
#include <iostream>
#include "bm.h"
#include "bmserial.h"
using namespace std;
// This exmaple demonstrates bitvector serialization/deserialization.
const unsigned MAX_VALUE = 1000000;
// This procedure creates very dense bitvector.
// The resulting set will consists mostly from ON (1) bits
// interrupted with small gaps of 0 bits.
static
{
for (unsigned i = 0; i < MAX_VALUE; ++i)
{
if (rand() % 2500)
{
bv->set_bit(i);
}
}
}
static
{
bm::bvector<>::statistics st;
bv.calc_stat(&st);
cout << "Bits count:" << bv.count() << endl;
cout << "Bit blocks:" << st.bit_blocks << endl;
cout << "GAP blocks:" << st.gap_blocks << endl;
cout << "Memory used:"<< st.memory_used << endl;
cout << "Max.serialize mem.:" << st.max_serialize_mem << endl << endl;;
}
// This is fairly low level serialization method, saves into a buffer
//
static
{
// It is reccomended to optimize vector before serialization.
bm::bvector<>::statistics st;
cout << "Bits count:" << bv.count() << endl;
cout << "Bit blocks:" << st.bit_blocks << endl;
cout << "GAP blocks:" << st.gap_blocks << endl;
cout << "Memory used:"<< st.memory_used << endl;
cout << "Max.serialize mem.:" << st.max_serialize_mem << endl;
// Allocate serialization buffer.
unsigned char* buf = new unsigned char[st.max_serialize_mem];
// Serialization to memory.
size_t len = bvs.serialize(bv, buf, st.max_serialize_mem);
cout << "Serialized size:" << len << endl << endl;
return buf;
}
int main(void)
{
unsigned char* buf1 = 0;
unsigned char* buf2 = 0;
try
{
bv2.set_new_blocks_strat(bm::BM_GAP); // set DGAP compression mode ON
fill_bvector(&bv1);
fill_bvector(&bv2);
// Prepare a serializer class
// for best performance it is best to create serilizer once and reuse it
// (saves a memory allocations). Do NOT use it concurrently.
//
// next settings provide lowest size
// 1: serializes into a new allocated buffer (needs to be deallocated)
buf1 = serialize_bvector(bvs, bv1);
// 2: use serialization buffer class (automatic RAI, freed on destruction)
{
bvs.serialize(bv2, sbuf);
buf2 = sbuf.data();
auto sz = sbuf.size();
cout << "BV2 Serialized size:" << sz << endl;
}
// Serialized bvectors (buf1 and buf2) now ready to be
// saved to a database, file or send over a network.
// ...
// Deserialization.
// As a result of desrialization bv3 will contain all bits from
// bv1 and bv3:
// bv3 = bv1 OR bv2
bm::deserialize(bv3, buf1);
bm::deserialize(bv3, buf2);
// After a complex operation we can try to optimize bv3.
bv3.optimize();
// control check using OR on the original vectors
{
int cmp = bv_c.compare(bv3);
if (cmp != 0)
{
std::cerr << "Error: bug in serialization" << std::endl;
}
// destructive serialization, "bv_c" should not be used after
bvs.optimize_serialize_destroy(bv_c, sbuf2);
cout << "BV_C Serialized size:" << sbuf2.size() << endl;
}
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
delete [] buf1;
return 1;
}
delete [] buf1;
return 0;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition bm.h:47
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs.
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition bm.h:3071
bm::bvector< Alloc > & bit_or(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode)
3-operand OR : this := bv1 OR bv2
Definition bm.h:4809
void set_new_blocks_strat(strategy strat)
Sets new blocks allocation strategy.
Definition bm.h:1810
bool set_bit(size_type n, bool val=true)
Sets bit n.
Definition bm.h:3617
int compare(const bvector< Alloc > &bvect) const BMNOEXCEPT
Lexicographical comparison with a bitvector.
Definition bm.h:3159
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
Definition bm.h:3393
Bit-vector serialization class.
Definition bmserial.h:76
void gap_length_serialization(bool value) BMNOEXCEPT
Set GAP length serialization (serializes GAP levels of the original vector)
Definition bmserial.h:1126
void byte_order_serialization(bool value) BMNOEXCEPT
Set byte-order serialization (for cross platform compatibility)
Definition bmserial.h:1132
void optimize_serialize_destroy(BV &bv, typename serializer< BV >::buffer &buf)
Bitvector serialization into buffer object (resized automatically) Input bit-vector gets optimized an...
Definition bmserial.h:1925
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition bmserial.h:2264
@ BM_GAP
GAP compression is ON.
Definition bmconst.h:144
size_t deserialize(BV &bv, const unsigned char *buf, bm::word_t *temp_block=0, const bm::bv_ref_vector< BV > *ref_vect=0)
Bitvector deserialization from a memory BLOB.
Definition bmserial.h:2688
int main(void)
Definition sample1.cpp:38
static void print_statistics(const bm::bvector<> &bv)
Definition sample3.cpp:59
static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)
Definition sample3.cpp:46
const unsigned MAX_VALUE
Definition sample3.cpp:40
static unsigned char * serialize_bvector(bm::serializer< bm::bvector<> > &bvs, bm::bvector<> &bv)
Definition sample4.cpp:76