TEST(RSDic, random){ RSDicBuilder bvb; vector<int> B; const uint64_t n = 100000; for (int i = 0; i < n; ++i){ int b = rand() % 2; bvb.PushBack(b); B.push_back(b); } RSDic bv; bvb.Build(bv); ASSERT_EQ(n, bv.num()); int sum = 0; for (size_t i = 0; i < bv.num(); ++i){ ASSERT_EQ(B[i] , bv.GetBit(i)) << " i=" << i; if (B[i]){ ASSERT_EQ(sum, bv.Rank(i, 1)); ASSERT_EQ(i,bv.Select(sum, 1)); } else { ASSERT_EQ(i - sum, bv.Rank(i, 0)); ASSERT_EQ(i, bv.Select(i-sum, 0)); } sum += B[i]; } ostringstream os; bv.Save(os); istringstream is(os.str()); RSDic bv_load; bv_load.Load(is); ASSERT_EQ(bv, bv_load); }
void GMR::BuildGMRBitArray(FFLCArray &fflca){ uint64_t previous = fflca[perm_.Access(0)]; RSDicBuilder bvb; for(size_t i = 0; i <= fflca[perm_.Access(0)];i++){ bvb.PushBack(1); } bvb.PushBack(0); for(size_t i = 1; i < perm_.Length();i++){ if(previous == fflca[perm_.Access(i)]){ bvb.PushBack(0); } else{ for(size_t j = previous; j < fflca[perm_.Access(i)]; j++){ bvb.PushBack(1); } bvb.PushBack(0); } previous = fflca[perm_.Access(i)]; } bvb.PushBack(1); bvb.Build(b_); }
void WaveletMatrixBuilder::BuildWithAVals(WaveletMatrix& wm, vector<uint32_t>& avals){ uint32_t max_val = GetMax(vals_); uint32_t max_depth = GetLen(max_val); wm.max_val_ = max_val; wm.layers_.resize(max_depth); vector<uint32_t> zeros; vector<uint32_t> ones; vector<uint32_t> zero_avals; vector<uint32_t> one_avals; zeros.swap(vals_); zero_avals = avals; for (uint32_t depth = 0; depth < max_depth; ++depth){ vector<uint32_t> next_zeros; vector<uint32_t> next_ones; vector<uint32_t> next_zero_avals; vector<uint32_t> next_one_avals; RSDicBuilder rsdb; FilterWithAVals(zeros, zero_avals, max_depth - depth - 1, next_zeros, next_ones, next_zero_avals, next_one_avals, rsdb); FilterWithAVals(ones, one_avals, max_depth - depth - 1, next_zeros, next_ones, next_zero_avals, next_one_avals, rsdb); zeros.swap(next_zeros); ones.swap(next_ones); zero_avals.swap(next_zero_avals); one_avals.swap(next_one_avals); rsdb.Build(wm.layers_[depth]); } copy(zero_avals.begin(), zero_avals.end(), avals.begin()); copy(one_avals.begin(), one_avals.end(), avals.begin() + zero_avals.size()); }
TEST(RSDic, trivial_one){ RSDicBuilder bvb; const uint64_t n = 10000; for (int i = 0; i < n; ++i){ bvb.PushBack(1); } RSDic bv; bvb.Build(bv); ASSERT_EQ(n, bv.num()); ASSERT_EQ(n, bv.one_num()); for (size_t i = 0; i < bv.num(); ++i){ ASSERT_EQ(1, bv.GetBit(i)); ASSERT_EQ(i, bv.Rank(i, 1)) << " i=" << i; ASSERT_EQ(i, bv.Select(i, 1)) << " i=" << i; } }
TEST(RSDic, small){ RSDicBuilder bvb; const uint64_t n = 65; for (int i = 0; i < n; ++i){ bvb.PushBack(1); } RSDic bv; bvb.Build(bv); ASSERT_EQ(n, bv.num()); ASSERT_EQ(n, bv.one_num()); for (size_t i = 0; i < bv.num(); ++i){ ASSERT_EQ(1, bv.GetBit(i)); ASSERT_EQ(i, bv.Rank(i, 1)); ASSERT_EQ(i, bv.Select(i, 1)); } }
TEST(RSDic, large){ RSDicBuilder rsdb; const uint64_t n = 26843545; vector<uint64_t> poses; for (uint64_t i = 0; i < n; ++i){ float r = (float)rand() / RAND_MAX; if (r < 0.001) { rsdb.PushBack(1); poses.push_back(i); } else rsdb.PushBack(0); } RSDic bv; rsdb.Build(bv); uint64_t one_num = bv.one_num(); for (uint64_t i = 0; i < one_num; ++i){ ASSERT_EQ(poses[i], bv.Select(i, 1)); } }
void WaveletMatrixBuilder::Build(WaveletMatrix& wm){ uint32_t max_val = GetMax(vals_); uint32_t max_depth = GetLen(max_val); wm.max_val_ = max_val; wm.layers_.resize(max_depth); vector<uint32_t> zeros; vector<uint32_t> ones; zeros.swap(vals_); for (uint32_t depth = 0; depth < max_depth; ++depth){ vector<uint32_t> next_zeros; vector<uint32_t> next_ones; RSDicBuilder rsdb; Filter(zeros, max_depth - depth - 1, next_zeros, next_ones, rsdb); Filter(ones, max_depth - depth - 1, next_zeros, next_ones, rsdb); zeros.swap(next_zeros); ones.swap(next_ones); rsdb.Build(wm.layers_[depth]); } }