#include <stdlib.h>
#include <iostream>
#include "bm.h"
#include "bmserial.h"
#include "bmundef.h"
using namespace std;
const unsigned MAX_VALUE = 1000000;
static
void fill_bvector(bm::bvector<>* bv)
{
for (unsigned i = 0; i < MAX_VALUE; ++i)
{
if (rand() % 2500)
{
bv->set_bit(i);
}
}
}
static
void print_statistics(const bm::bvector<>& bv)
{
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;;
}
static
unsigned char* serialize_bvector(bm::serializer<bm::bvector<> >& bvs,
bm::bvector<>& bv)
{
BM_DECLARE_TEMP_BLOCK(tb)
bm::bvector<>::statistics st;
bv.optimize(tb, bm::bvector<>::opt_compress, &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;
unsigned char* buf = new unsigned char[st.max_serialize_mem];
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
{
bm::bvector<> bv1;
bm::bvector<> bv2;
bv2.set_new_blocks_strat(bm::BM_GAP);
fill_bvector(&bv1);
fill_bvector(&bv2);
bm::serializer<bm::bvector<> > bvs;
bvs.byte_order_serialization(false);
bvs.gap_length_serialization(false);
buf1 = serialize_bvector(bvs, bv1);
bm::serializer<bm::bvector<> >::buffer sbuf;
{
bvs.serialize(bv2, sbuf);
buf2 = sbuf.data();
auto sz = sbuf.size();
cout << "BV2 Serialized size:" << sz << endl;
}
bm::bvector<> bv3;
bm::deserialize(bv3, buf1);
bm::deserialize(bv3, buf2);
print_statistics(bv3);
bv3.optimize();
print_statistics(bv3);
{
bm::bvector<> bv_c;
bv_c.bit_or(bv1, bv2, bm::bvector<>::opt_compress);
int cmp = bv_c.compare(bv3);
if (cmp != 0)
{
std::cerr << "Error: bug in serialization" << std::endl;
}
bm::serializer<bm::bvector<> >::buffer sbuf2;
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;
}