Example of how to use bm::str_sparse_vector<> - succinct container for bit-transposed string collections
- See also
- bm::str_sparse_vector
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include <fstream>
static
{
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);
}
{
try
{
vector<string> str_vec;
std::sort(str_vec.begin(), str_vec.end());
{
size_t vect_size = 0;
{
for (auto str : str_vec)
{
bi = str;
size_t str_size = str.size() + sizeof(str);
vect_size += str_size;
}
cout << "STL vector<string> approx.memory consumption:"
<< vect_size << endl;
}
cout <<
"Used memory: " << st.
memory_used << std::endl;
cout << "Used memory after remap and optimization: "
<< std::endl;
}
{
std::string fname = "test.sv";
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;
}