#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include "bm.h"
#include "bmstrsparsevec.h"
#include "bmsparsevec_algo.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);
}
static
void insertion_sort(str_sv_type& str_sv, const vector<string>& str_vec)
{
bm::sparse_vector_scanner<str_sv_type> scanner;
for (const string& s : str_vec)
{
const char* cs = s.c_str();
str_sv_type::size_type pos;
bool found = scanner.lower_bound_str(str_sv, cs, pos);
(void)found;
str_sv.insert(pos, cs);
} }
int main(void)
{
try
{
str_sv_type str_sv;
vector<string> str_vec;
generate_string_set(str_vec);
insertion_sort(str_sv, str_vec);
{
BM_DECLARE_TEMP_BLOCK(tb)
str_sv.optimize(tb);
}
std::sort(str_vec.begin(), str_vec.end());
{
vector<string>::const_iterator sit = str_vec.begin();
str_sv_type::const_iterator it = str_sv.begin();
str_sv_type::const_iterator it_end = str_sv.end();
for (; it != it_end; ++it, ++sit)
{
string s = *it;
if (*sit != s)
{
cerr << "Mismatch at:" << s << "!=" << *sit << endl;
return 1;
}
} }
cout << "Sort validation Ok." << endl;
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}