#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include <fstream>
#include "bm.h"
#include "bmstrsparsevec.h"
#include "bmsparsevec_serial.h"
#include "bmundef.h"
using namespace std;
typedef bm::bvector<> bvector_type;
typedef bm::str_sparse_vector<char, bvector_type, 32> str_sv_type;
static
void generate_string_set(vector<string>& str_vec)
{
const unsigned max_coll = 50000;
str_vec.resize(0);
string str;
for (unsigned i = 10; i < max_coll; i += rand() % 3)
{
str = to_string(i);
str_vec.emplace_back(str);
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(str_vec.begin(), str_vec.end(), g);
}
int main(void)
{
try
{
str_sv_type str_sv;
vector<string> str_vec;
generate_string_set(str_vec);
std::sort(str_vec.begin(), str_vec.end());
{
size_t vect_size = 0; str_sv_type str_sv_tmp; {
str_sv_type::back_insert_iterator bi =
str_sv_tmp.get_back_inserter();
for (auto str : str_vec)
{
bi = str;
size_t str_size = str.size() + sizeof(str);
vect_size += str_size;
}
bi.flush();
cout << "STL vector<string> approx.memory consumption:"
<< vect_size << endl;
}
str_sv_type::statistics st;
str_sv_tmp.calc_stat(&st);
cout << "Used memory: " << st.memory_used << std::endl;
str_sv.remap_from(str_sv_tmp);
BM_DECLARE_TEMP_BLOCK(tb)
str_sv.optimize(tb);
str_sv.calc_stat(&st);
cout << "Used memory after remap and optimization: "
<< st.memory_used
<< std::endl;
}
{
std::string fname = "test.sv";
bm::sparse_vector_serial_layout<str_sv_type> sv_lay;
BM_DECLARE_TEMP_BLOCK(tb)
bm::sparse_vector_serialize(str_sv, sv_lay, tb);
std::ofstream fout(fname.c_str(), std::ios::binary);
if (!fout.good())
{
return -1;
}
const char* buf = (char*)sv_lay.buf();
fout.write(buf, (unsigned)sv_lay.size());
if (!fout.good())
{
return -1;
}
fout.close();
cout << "Saved size: " << sv_lay.size() << endl;
}
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}