|
| 1 | +#include <sdsl/bit_vectors.hpp> |
| 2 | +#include <random> |
| 3 | +#include <iostream> |
| 4 | +#include <chrono> |
| 5 | + |
| 6 | +using namespace sdsl; |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +using namespace std::chrono; |
| 10 | +using timer = std::chrono::high_resolution_clock; |
| 11 | + |
| 12 | + |
| 13 | +template<class t_vec> |
| 14 | +uint64_t test_inv_random_access(const t_vec& v, const int_vector<64>& rands, uint64_t mask, uint64_t times=100000000) |
| 15 | +{ |
| 16 | + uint64_t cnt=0; |
| 17 | + for (uint64_t i=0; i<times; ++i) { |
| 18 | + cnt += v(rands[ i&mask ]); |
| 19 | + } |
| 20 | + return cnt; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +//int main(int argc, char* argv[]){ |
| 26 | +int main() |
| 27 | +{ |
| 28 | + auto start = timer::now(); |
| 29 | + bool default_value = 0; //ID[ID.length()-1]-'0'; |
| 30 | + bit_vector bv = bit_vector(800000000, default_value); |
| 31 | + |
| 32 | + std::mt19937_64 rng; |
| 33 | + std::uniform_int_distribution<uint64_t> distribution(0, bv.size()-1); |
| 34 | + auto dice = bind(distribution, rng); |
| 35 | + // populate vectors with some other bits |
| 36 | + for (uint64_t i=0; i < bv.size()/25; ++i) { |
| 37 | + uint64_t x = dice(); |
| 38 | + bv[x] = !default_value; |
| 39 | + } |
| 40 | + auto stop = timer::now(); |
| 41 | + cout << "initialization in (ms): " << duration_cast<milliseconds>(stop-start).count() << endl; |
| 42 | + cout << "size in MiB: " << size_in_mega_bytes(bv) << endl; |
| 43 | + |
| 44 | + start = timer::now(); |
| 45 | + sd_vector<> bv_sd(bv); |
| 46 | + stop = timer::now(); |
| 47 | + cout << "sd_construction in (ms): " << duration_cast<milliseconds>(stop-start).count() << endl; |
| 48 | + { |
| 49 | + bit_vector().swap(bv); |
| 50 | + } |
| 51 | + cout << "size in MiB: " << size_in_mega_bytes(bv_sd) << endl; |
| 52 | + cout << "wl = " << (size_t) bv_sd.wl << endl; |
| 53 | + cout << "n = " << bv_sd.size() << endl; |
| 54 | + cout << "2*m = " << bv_sd.high.size()<<endl; |
| 55 | + cout <<"n/m=" << (2.0*bv_sd.size())/bv_sd.high.size()<<endl; |
| 56 | + |
| 57 | + auto zeros = sd_vector<>::rank_0_type(&bv_sd)(bv_sd.size()); |
| 58 | + auto ones = bv_sd.size()-zeros; |
| 59 | + cout << "zeros = "<< zeros << endl; |
| 60 | + { |
| 61 | + uint64_t mask = 0; |
| 62 | + auto rands = util::rnd_positions<int_vector<64>>(20, mask, zeros, 17); |
| 63 | + for (uint64_t i=0; i<rands.size(); ++i) rands[i] = rands[i]+1; |
| 64 | + sd_vector<>::select_0_type select0(&bv_sd); |
| 65 | + const uint64_t reps = 10000000; |
| 66 | + start = timer::now(); |
| 67 | + auto check = test_inv_random_access(select0, rands, mask, reps); |
| 68 | + stop = timer::now(); |
| 69 | + |
| 70 | + cout << "# select0_time = " << duration_cast<nanoseconds>(stop-start).count()/(double)reps << endl; |
| 71 | + cout << "# select_check = " << check << endl; |
| 72 | + cout << "# size_in_mega_bytes(bv_sd) = " << size_in_mega_bytes(bv_sd) << endl; |
| 73 | + cout << "# size_in_mega_bytes(select0) = " << size_in_mega_bytes(select0) << endl; |
| 74 | + } |
| 75 | + { |
| 76 | + uint64_t mask = 0; |
| 77 | + auto rands = util::rnd_positions<int_vector<64>>(20, mask, zeros, 17); |
| 78 | + for (uint64_t i=0; i<rands.size(); ++i) rands[i] = rands[i]+1; |
| 79 | + select_0_support_sd<sd_vector<>> select0(&bv_sd); |
| 80 | + const uint64_t reps = 10000000; |
| 81 | + start = timer::now(); |
| 82 | + auto check = test_inv_random_access(select0, rands, mask, reps); |
| 83 | + stop = timer::now(); |
| 84 | + |
| 85 | + cout << "# select0_time = " << duration_cast<nanoseconds>(stop-start).count()/(double)reps << endl; |
| 86 | + cout << "# select_check = " << check << endl; |
| 87 | + cout << "# size_in_mega_bytes(bv_sd) = " << size_in_mega_bytes(bv_sd) << endl; |
| 88 | + cout << "# size_in_mega_bytes(select0) = " << size_in_mega_bytes(select0) << endl; |
| 89 | + } |
| 90 | + { |
| 91 | + uint64_t mask = 0; |
| 92 | + auto rands = util::rnd_positions<int_vector<64>>(20, mask, ones, 17); |
| 93 | + for (uint64_t i=0; i<rands.size(); ++i) rands[i] = rands[i]+1; |
| 94 | + sd_vector<>::select_1_type select1(&bv_sd); |
| 95 | + const uint64_t reps = 10000000; |
| 96 | + start = timer::now(); |
| 97 | + auto check = test_inv_random_access(select1, rands, mask, reps); |
| 98 | + stop = timer::now(); |
| 99 | + |
| 100 | + cout << "# select1_time = " << duration_cast<nanoseconds>(stop-start).count()/(double)reps << endl; |
| 101 | + cout << "# select_check = " << check << endl; |
| 102 | + } |
| 103 | + { |
| 104 | + uint64_t mask = 0; |
| 105 | + auto rands = util::rnd_positions<int_vector<64>>(20, mask, bv_sd.size(), 17); |
| 106 | + cout<<"done"<<endl; |
| 107 | + cout<<(uint64_t)&(bv_sd.high_1_select)<<endl; |
| 108 | + cout<<(uint64_t)&(bv_sd.high_0_select)<<endl; |
| 109 | + sd_vector<>::rank_1_type rank1(&bv_sd); |
| 110 | + cout<<"done"<<endl; |
| 111 | + const uint64_t reps = 10000000; |
| 112 | +// for(size_t i=0; i<bv_sd.size();++i){ |
| 113 | +// cout << "i="<<i<<" rank1("<<i<<")="<<rank1(i)<<endl; |
| 114 | +// } |
| 115 | + start = timer::now(); |
| 116 | + auto check = test_inv_random_access(rank1, rands, mask, reps); |
| 117 | + stop = timer::now(); |
| 118 | + |
| 119 | + cout << "# rank1_time = " << duration_cast<nanoseconds>(stop-start).count()/(double)reps << endl; |
| 120 | + cout << "# select_check = " << check << endl; |
| 121 | + } |
| 122 | +} |
0 commit comments