#include <iostream>
#include <algorithm>
#include "bm.h"
#include "bmsparsevec.h"
#include "bmundef.h"
using namespace std;
typedef bm::sparse_vector<unsigned, bm::bvector<> > sparse_vector_u32;
typedef bm::sparse_vector<int, bm::bvector<> > sparse_vector_i32;
static
void Demo1()
{
using value_type = bm::sparse_vector<int, bm::bvector<> >::value_type;
sparse_vector_u32 sv1;
unsigned arr[3] = {1,2,3};
sv1.import(arr, 3);
{
BM_DECLARE_TEMP_BLOCK(tb)
sv1.optimize(tb);
}
cout << "sv1.size() = " << sv1.size() << endl;
cout << "sv[]:";
for (unsigned i = 0; i < sv1.size(); ++i)
{
cout << sv1.at(i) << ",";
}
cout << endl;
unsigned arr2[5] = {10, 20, 30, 40, 50};
sv1.import(arr2, 5, sv1.size());
cout << "sv1.size() = " << sv1.size() << endl;
cout << "sv[]:";
std::for_each(sv1.begin(), sv1.end(),
[](value_type n) { cout << n << ", "; });
cout << endl;
}
static
void Demo2()
{
sparse_vector_i32 sv1;
int arr[3] = {1,-2,3};
sv1.import(arr, 3);
int arr2[5] = {-10, 20, 30, 40, -50};
sv1.import(arr2, 5, sv1.size());
{
BM_DECLARE_TEMP_BLOCK(tb)
sv1.optimize(tb);
}
cout << "sv1.size() = " << sv1.size() << endl;
cout << "sv[]:";
std::for_each(sv1.begin(), sv1.end(), [] (auto n) { cout << n << ", "; }
);
cout << endl;
}
int main(void)
{
try
{
Demo1();
cout << endl << endl;
Demo2();
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}