Skip to content

Commit

Permalink
Added tests for k2-treap
Browse files Browse the repository at this point in the history
Fixed bug in dac_vector: Previously dac_vector only worked if the
bit-width of the maximum in the vector was smaller or equal to the
bit-width of the length of the vector.
  • Loading branch information
simongog committed Sep 30, 2014
1 parent 104826c commit 0ea4987
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 32 deletions.
12 changes: 12 additions & 0 deletions examples/k2_treap_in_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ int main()
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
++range_it;
}

cout<<"---"<<endl;
{
k2_rrr k2t;
construct_im(k2t, {{1,2,3},{2,3,3},{3,1,3}});
auto topk_it = top_k(k2t, {0,0}, {10,10});
while (topk_it) {
auto point_weight = *topk_it;
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
++topk_it;
}
}
}
61 changes: 36 additions & 25 deletions include/sdsl/dac_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ class dac_vector
int_vector<t_b> m_data; // block data for every level
bit_vector m_overflow; // mark non-end bytes
rank_support_type m_overflow_rank; // rank for m_overflow
int_vector<64> m_level_pointer_and_rank;
int_vector<64> m_level_pointer_and_rank = int_vector<64>(4,0);
uint8_t m_max_level; // maximum level < (log n)/b+1

void copy(const dac_vector& v) {
void copy(const dac_vector& v)
{
m_data = v.m_data;
m_overflow = v.m_overflow;
m_overflow_rank = v.m_overflow_rank;
Expand All @@ -91,25 +92,27 @@ class dac_vector
}

public:
dac_vector() {
m_level_pointer_and_rank = int_vector<64>(4,0);
}
dac_vector() = default;

dac_vector(const dac_vector& v) {
dac_vector(const dac_vector& v)
{
copy(v);
}

dac_vector(dac_vector&& v) {
dac_vector(dac_vector&& v)
{
*this = std::move(v);
}
dac_vector& operator=(const dac_vector& v) {
dac_vector& operator=(const dac_vector& v)
{
if (this != &v) {
copy(v);
}
return *this;
}

dac_vector& operator=(dac_vector&& v) {
dac_vector& operator=(dac_vector&& v)
{
if (this != &v) {
m_data = std::move(v.m_data);
m_overflow = std::move(v.m_overflow);
Expand All @@ -133,21 +136,25 @@ class dac_vector
dac_vector(int_vector_buffer<int_width>& v_buf);

//! The number of elements in the dac_vector.
size_type size()const {
size_type size()const
{
return m_level_pointer_and_rank[2];
}
//! Return the largest size that this container can ever have.
static size_type max_size() {
static size_type max_size()
{
return int_vector<>::max_size()/2;
}

//! Returns if the dac_vector is empty.
bool empty() const {
bool empty() const
{
return 0 == m_level_pointer_and_rank[2];
}

//! Swap method for dac_vector
void swap(dac_vector& v) {
void swap(dac_vector& v)
{
m_data.swap(v.m_data);
m_overflow.swap(v.m_overflow);
util::swap_support(m_overflow_rank, v.m_overflow_rank,
Expand All @@ -158,18 +165,21 @@ class dac_vector
}

//! Iterator that points to the first element of the dac_vector.
const const_iterator begin()const {
const const_iterator begin()const
{
return const_iterator(this, 0);
}


//! Iterator that points to the position after the last element of the dac_vector.
const const_iterator end()const {
const const_iterator end()const
{
return const_iterator(this, size());
}

//! []-operator
value_type operator[](size_type i)const {
value_type operator[](size_type i)const
{
uint8_t level = 1;
uint8_t offset = t_b;
size_type result = m_data[i];
Expand All @@ -189,7 +199,8 @@ class dac_vector
size_type serialize(std::ostream& out, structure_tree_node* v=nullptr, std::string name="")const;

//! Load from a stream.
void load(std::istream& in) {
void load(std::istream& in)
{
m_data.load(in);
m_overflow.load(in);
m_overflow_rank.load(in, &m_overflow);
Expand All @@ -209,13 +220,11 @@ dac_vector<t_b, t_rank>::dac_vector(const Container& c)
if (n == 0)
return;
// initialize counter
auto _size = std::max(4*bits::hi(2), 2*(((bits::hi(n)+1)+t_b-1) / t_b));
m_level_pointer_and_rank.resize(_size);
for (size_type i=0; i < m_level_pointer_and_rank.size(); ++i)
m_level_pointer_and_rank[i] = 0;
m_level_pointer_and_rank = int_vector<64>(128, 0);
m_level_pointer_and_rank[0] = n; // level 0 has n entries

uint8_t level_x_2 = 0;
uint8_t max_level_x_2 = 0;
for (size_type i=0; i < n; ++i) {
val=c[i];
val >>= t_b; // shift value b bits to the right
Expand All @@ -225,8 +234,10 @@ dac_vector<t_b, t_rank>::dac_vector(const Container& c)
++m_level_pointer_and_rank[level_x_2];
val >>= t_b; // shift value b bits to the right
level_x_2 += 2; // increase level by 1
max_level_x_2 = std::max(max_level_x_2, level_x_2);
}
}
m_level_pointer_and_rank.resize(max_level_x_2);
// (2) Determine maximum level and prefix sums of level counters
m_max_level = 0;
size_type sum_blocks = 0, last_block_size=0;
Expand Down Expand Up @@ -286,13 +297,11 @@ dac_vector<t_b, t_rank>::dac_vector(int_vector_buffer<int_width>& v_buf)
if (n == 0)
return;
// initialize counter
auto _size = std::max(4*bits::hi(2), 2*(((bits::hi(n)+1)+t_b-1) / t_b));
m_level_pointer_and_rank.resize(_size);
for (size_type i=0; i < m_level_pointer_and_rank.size(); ++i)
m_level_pointer_and_rank[i] = 0;
m_level_pointer_and_rank = int_vector<64>(128, 0);
m_level_pointer_and_rank[0] = n; // level 0 has n entries

uint8_t level_x_2 = 0;
uint8_t max_level_x_2 = 0;
for (size_type i=0; i < n; ++i) {
val=v_buf[i];
val >>= t_b; // shift value b bits to the right
Expand All @@ -302,8 +311,10 @@ dac_vector<t_b, t_rank>::dac_vector(int_vector_buffer<int_width>& v_buf)
++m_level_pointer_and_rank[level_x_2];
val >>= t_b; // shift value b bits to the right
level_x_2 += 2; // increase level by 1
max_level_x_2 = std::max(max_level_x_2, level_x_2);
}
}
m_level_pointer_and_rank.resize(max_level_x_2);
// (2) Determine maximum level and prefix sums of level counters
m_max_level = 0;
size_type sum_blocks = 0, last_block_size=0;
Expand Down
4 changes: 3 additions & 1 deletion include/sdsl/k2_treap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ class k2_treap
template<typename t_x, typename t_y, typename t_w>
k2_treap(std::vector<std::tuple<t_x, t_y, t_w>>& v, std::string temp_file_prefix="")
{
construct(v, temp_file_prefix);
if (v.size() > 0) {
construct(v, temp_file_prefix);
}
}

template<typename t_x, typename t_y, typename t_w>
Expand Down
4 changes: 2 additions & 2 deletions include/sdsl/k2_treap_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class top_k_iterator
top_k_iterator& operator=(const top_k_iterator&) = default;
top_k_iterator& operator=(top_k_iterator&&) = default;
top_k_iterator(const t_k2_treap& treap, point_type p1, point_type p2) :
m_treap(&treap), m_p1(p1), m_p2(p2), m_valid(true)
m_treap(&treap), m_p1(p1), m_p2(p2), m_valid(treap.size()>0)
{
if (m_treap->size() > 0) {
m_pq.emplace(m_treap->root(),false);
Expand Down Expand Up @@ -198,7 +198,7 @@ class range_iterator
range_iterator& operator=(const range_iterator&) = default;
range_iterator& operator=(range_iterator&&) = default;
range_iterator(const t_k2_treap& treap, point_type p1, point_type p2, range_type range) :
m_treap(&treap), m_p1(p1), m_p2(p2), m_r(range), m_valid(true)
m_treap(&treap), m_p1(p1), m_p2(p2), m_r(range), m_valid(treap.size()>0)
{
if (m_treap->size() >0) {
pq_emplace(m_treap->root(), false);
Expand Down
6 changes: 6 additions & 0 deletions test/K2TreapTest.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Inputs which should be used
EMPTY-TEST;test_cases/int-vec-k2t.0.1.0.0.x;test_cases/int-vec-k2t.0.1.0.0.y;test_cases/int-vec-k2t.0.1.0.0.w
SML-TEST;test_cases/int-vec-k2t.100.16.r.42.x;test_cases/int-vec-k2t.100.16.r.23.y;test_cases/int-vec-k2t.100.8.r.81.w
MED-TEST;test_cases/int-vec-k2t.10000.16.r.42.x;test_cases/int-vec-k2t.10000.16.r.23.y;test_cases/int-vec-k2t.10000.8.r.81.w
BIG-TEST;test_cases/int-vec-k2t.100000.39.r.42.x;test_cases/int-vec-k2t.100000.47.r.23.y;test_cases/int-vec-k2t.100000.35.r.81.w
LRG-TEST;test_cases/int-vec-k2t.1000000.52.r.42.x;test_cases/int-vec-k2t.1000000.57.r.23.y;test_cases/int-vec-k2t.1000000.45.r.81.w
Loading

0 comments on commit 0ea4987

Please sign in to comment.