Skip to content

Commit

Permalink
Merge pull request #199 from simongog/k2_treap
Browse files Browse the repository at this point in the history
Add K^2-Treap Implementation
  • Loading branch information
simongog committed Sep 30, 2014
2 parents f6b4d58 + 7767a30 commit 25bd677
Show file tree
Hide file tree
Showing 9 changed files with 1,454 additions and 29 deletions.
53 changes: 53 additions & 0 deletions examples/k2_treap_in_mem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <vector>
#include <tuple>
#include <string>
#include <complex>
#include <sdsl/k2_treap.hpp>
#include <sdsl/bit_vectors.hpp>

using namespace sdsl;
using namespace std;

int main()
{
typedef k2_treap<3,rrr_vector<63>> k2_rrr;
k2_rrr k2treap;

// Initialize treap with a vector of (x,y,weight) elements
construct_im(k2treap, {{1,2,3},{2,2,6},{4,4,1},{3,3,2},{3,1,8}});

cout << "Points in the k2treap: " << k2treap.size() << endl;

cout << "Points in the rectangle from (2,1) to (3,3): " ;
cout << count(k2treap, {2,1}, {3,3}) << endl;

cout << "Heaviest points in rectangle from (0,0) to (2,8):" << endl;
auto topk_it = top_k(k2treap, {0,0}, {2,8});
while (topk_it) {
auto point_weight = *topk_it;
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
++topk_it;
}

cout << "Report all points in rectangle from (2,2) to (10,10)" << endl;
cout << "with weight in [2..6]:" << endl;
auto range_it = range_3d(k2treap, {2,2}, {10,10}, {2,100});
while (range_it) {
auto point_weight = *range_it;
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;
}
}
}
9 changes: 9 additions & 0 deletions extras/literature.bib
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Literature list of implemented data structures in SDSL
(not complete yet)
// used in include/sdsl/k2_treap.hpp
@INPROCEEDINGS{BRI:BER:KON:NAV:2014,
author = {N. Brisaboa and G. de Bernardo and R. Konow and G. Navarro},
title = {$K^2$-Treaps: Range Top-$k$ Queries in Compact Space},
booktitle = {Proceedings of String Processing and Information Retrieval, 21th International
Symposium (SPIRE 2014)},
year = {2014},
pages = {215--226}
}

// used in include/sdsl/construct_sa.hpp
@inproceedings{BEL:ZWE:GOG:OHL:2013,
Expand Down
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 = 4;
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 = 4;
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
Loading

0 comments on commit 25bd677

Please sign in to comment.