#include <stdlib.h>
#include <iostream>
#include <vector>
#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() % 10))
{
bv->set(i);
}
}
}
int main(void)
{
bm::operation_deserializer<bm::bvector<> > od;
try
{
bm::bvector<> bv1;
bm::bvector<> bv2;
fill_bvector(&bv1);
fill_bvector(&bv2);
cout << "bv1 count = " << bv1.count() << endl;
cout << "bv2 count = " << bv2.count() << endl;
BM_DECLARE_TEMP_BLOCK(tb)
bm::serializer<bm::bvector<> > bvs(tb);
bvs.set_compression_level(4);
bm::bvector<>::statistics st1;
bm::bvector<>::statistics st2;
bv1.optimize(tb, bm::bvector<>::opt_compress, &st1);
bv1.optimize(tb, bm::bvector<>::opt_compress, &st2);
bm::serializer<bm::bvector<> >::buffer sbuf1;
bm::serializer<bm::bvector<> >::buffer sbuf2;
bvs.serialize(bv1, sbuf1, &st1);
bvs.serialize(bv2, sbuf2, &st2);
std::vector<unsigned char> vect1;
std::vector<unsigned char> vect2;
vect1.resize(sbuf1.size());
vect2.resize(sbuf2.size());
::memcpy(vect1.data(), sbuf1.buf(), sbuf1.size());
::memcpy(vect2.data(), sbuf2.buf(), sbuf2.size());
bm::bvector<> bv3;
bm::deserialize(bv3, sbuf1.buf());
bm::deserialize(bv3, sbuf2.buf());
bv3.optimize(tb);
bm::bvector<> bv4(bv3);
od.deserialize(bv4, vect1.data(), bm::set_AND);
cout << "bv4 count = " << bv4.count() << endl;
bool eq = bv1.equal(bv4);
if (!eq)
{
cerr << "Logical error detected!" << endl;
return 1;
}
else
cout << "bv4 is equal to bv1" << endl;
bm::bvector<> bv5(bv3);
auto cnt_sub =
od.deserialize(bv3, sbuf1.buf(), bm::set_COUNT_SUB_AB);
cout << "minus count = " << cnt_sub << endl;
od.deserialize(bv5, sbuf1.buf(), tb, bm::set_SUB);
auto bv5_cnt = bv5.count();
cout << "bv5 count = " << bv5_cnt << endl;
if (cnt_sub != bv5_cnt)
{
cerr << "Logical error!" << endl;
return 1;
}
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}