Skip to content

Commit 25bd677

Browse files
committed
Merge pull request #199 from simongog/k2_treap
Add K^2-Treap Implementation
2 parents f6b4d58 + 7767a30 commit 25bd677

File tree

9 files changed

+1454
-29
lines changed

9 files changed

+1454
-29
lines changed

examples/k2_treap_in_mem.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <tuple>
4+
#include <string>
5+
#include <complex>
6+
#include <sdsl/k2_treap.hpp>
7+
#include <sdsl/bit_vectors.hpp>
8+
9+
using namespace sdsl;
10+
using namespace std;
11+
12+
int main()
13+
{
14+
typedef k2_treap<3,rrr_vector<63>> k2_rrr;
15+
k2_rrr k2treap;
16+
17+
// Initialize treap with a vector of (x,y,weight) elements
18+
construct_im(k2treap, {{1,2,3},{2,2,6},{4,4,1},{3,3,2},{3,1,8}});
19+
20+
cout << "Points in the k2treap: " << k2treap.size() << endl;
21+
22+
cout << "Points in the rectangle from (2,1) to (3,3): " ;
23+
cout << count(k2treap, {2,1}, {3,3}) << endl;
24+
25+
cout << "Heaviest points in rectangle from (0,0) to (2,8):" << endl;
26+
auto topk_it = top_k(k2treap, {0,0}, {2,8});
27+
while (topk_it) {
28+
auto point_weight = *topk_it;
29+
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
30+
++topk_it;
31+
}
32+
33+
cout << "Report all points in rectangle from (2,2) to (10,10)" << endl;
34+
cout << "with weight in [2..6]:" << endl;
35+
auto range_it = range_3d(k2treap, {2,2}, {10,10}, {2,100});
36+
while (range_it) {
37+
auto point_weight = *range_it;
38+
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
39+
++range_it;
40+
}
41+
42+
cout<<"---"<<endl;
43+
{
44+
k2_rrr k2t;
45+
construct_im(k2t, {{1,2,3},{2,3,3},{3,1,3}});
46+
auto topk_it = top_k(k2t, {0,0}, {10,10});
47+
while (topk_it) {
48+
auto point_weight = *topk_it;
49+
cout << point_weight.first <<" weight: "<<point_weight.second << endl;
50+
++topk_it;
51+
}
52+
}
53+
}

extras/literature.bib

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Literature list of implemented data structures in SDSL
22
(not complete yet)
33
4+
// used in include/sdsl/k2_treap.hpp
5+
@INPROCEEDINGS{BRI:BER:KON:NAV:2014,
6+
author = {N. Brisaboa and G. de Bernardo and R. Konow and G. Navarro},
7+
title = {$K^2$-Treaps: Range Top-$k$ Queries in Compact Space},
8+
booktitle = {Proceedings of String Processing and Information Retrieval, 21th International
9+
Symposium (SPIRE 2014)},
10+
year = {2014},
11+
pages = {215--226}
12+
}
413

514
// used in include/sdsl/construct_sa.hpp
615
@inproceedings{BEL:ZWE:GOG:OHL:2013,

include/sdsl/dac_vector.hpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ class dac_vector
7878
int_vector<t_b> m_data; // block data for every level
7979
bit_vector m_overflow; // mark non-end bytes
8080
rank_support_type m_overflow_rank; // rank for m_overflow
81-
int_vector<64> m_level_pointer_and_rank;
81+
int_vector<64> m_level_pointer_and_rank = int_vector<64>(4,0);
8282
uint8_t m_max_level; // maximum level < (log n)/b+1
8383

84-
void copy(const dac_vector& v) {
84+
void copy(const dac_vector& v)
85+
{
8586
m_data = v.m_data;
8687
m_overflow = v.m_overflow;
8788
m_overflow_rank = v.m_overflow_rank;
@@ -91,25 +92,27 @@ class dac_vector
9192
}
9293

9394
public:
94-
dac_vector() {
95-
m_level_pointer_and_rank = int_vector<64>(4,0);
96-
}
95+
dac_vector() = default;
9796

98-
dac_vector(const dac_vector& v) {
97+
dac_vector(const dac_vector& v)
98+
{
9999
copy(v);
100100
}
101101

102-
dac_vector(dac_vector&& v) {
102+
dac_vector(dac_vector&& v)
103+
{
103104
*this = std::move(v);
104105
}
105-
dac_vector& operator=(const dac_vector& v) {
106+
dac_vector& operator=(const dac_vector& v)
107+
{
106108
if (this != &v) {
107109
copy(v);
108110
}
109111
return *this;
110112
}
111113

112-
dac_vector& operator=(dac_vector&& v) {
114+
dac_vector& operator=(dac_vector&& v)
115+
{
113116
if (this != &v) {
114117
m_data = std::move(v.m_data);
115118
m_overflow = std::move(v.m_overflow);
@@ -133,21 +136,25 @@ class dac_vector
133136
dac_vector(int_vector_buffer<int_width>& v_buf);
134137

135138
//! The number of elements in the dac_vector.
136-
size_type size()const {
139+
size_type size()const
140+
{
137141
return m_level_pointer_and_rank[2];
138142
}
139143
//! Return the largest size that this container can ever have.
140-
static size_type max_size() {
144+
static size_type max_size()
145+
{
141146
return int_vector<>::max_size()/2;
142147
}
143148

144149
//! Returns if the dac_vector is empty.
145-
bool empty() const {
150+
bool empty() const
151+
{
146152
return 0 == m_level_pointer_and_rank[2];
147153
}
148154

149155
//! Swap method for dac_vector
150-
void swap(dac_vector& v) {
156+
void swap(dac_vector& v)
157+
{
151158
m_data.swap(v.m_data);
152159
m_overflow.swap(v.m_overflow);
153160
util::swap_support(m_overflow_rank, v.m_overflow_rank,
@@ -158,18 +165,21 @@ class dac_vector
158165
}
159166

160167
//! Iterator that points to the first element of the dac_vector.
161-
const const_iterator begin()const {
168+
const const_iterator begin()const
169+
{
162170
return const_iterator(this, 0);
163171
}
164172

165173

166174
//! Iterator that points to the position after the last element of the dac_vector.
167-
const const_iterator end()const {
175+
const const_iterator end()const
176+
{
168177
return const_iterator(this, size());
169178
}
170179

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

191201
//! Load from a stream.
192-
void load(std::istream& in) {
202+
void load(std::istream& in)
203+
{
193204
m_data.load(in);
194205
m_overflow.load(in);
195206
m_overflow_rank.load(in, &m_overflow);
@@ -209,13 +220,11 @@ dac_vector<t_b, t_rank>::dac_vector(const Container& c)
209220
if (n == 0)
210221
return;
211222
// initialize counter
212-
auto _size = std::max(4*bits::hi(2), 2*(((bits::hi(n)+1)+t_b-1) / t_b));
213-
m_level_pointer_and_rank.resize(_size);
214-
for (size_type i=0; i < m_level_pointer_and_rank.size(); ++i)
215-
m_level_pointer_and_rank[i] = 0;
223+
m_level_pointer_and_rank = int_vector<64>(128, 0);
216224
m_level_pointer_and_rank[0] = n; // level 0 has n entries
217225

218226
uint8_t level_x_2 = 0;
227+
uint8_t max_level_x_2 = 4;
219228
for (size_type i=0; i < n; ++i) {
220229
val=c[i];
221230
val >>= t_b; // shift value b bits to the right
@@ -225,8 +234,10 @@ dac_vector<t_b, t_rank>::dac_vector(const Container& c)
225234
++m_level_pointer_and_rank[level_x_2];
226235
val >>= t_b; // shift value b bits to the right
227236
level_x_2 += 2; // increase level by 1
237+
max_level_x_2 = std::max(max_level_x_2, level_x_2);
228238
}
229239
}
240+
m_level_pointer_and_rank.resize(max_level_x_2);
230241
// (2) Determine maximum level and prefix sums of level counters
231242
m_max_level = 0;
232243
size_type sum_blocks = 0, last_block_size=0;
@@ -286,13 +297,11 @@ dac_vector<t_b, t_rank>::dac_vector(int_vector_buffer<int_width>& v_buf)
286297
if (n == 0)
287298
return;
288299
// initialize counter
289-
auto _size = std::max(4*bits::hi(2), 2*(((bits::hi(n)+1)+t_b-1) / t_b));
290-
m_level_pointer_and_rank.resize(_size);
291-
for (size_type i=0; i < m_level_pointer_and_rank.size(); ++i)
292-
m_level_pointer_and_rank[i] = 0;
300+
m_level_pointer_and_rank = int_vector<64>(128, 0);
293301
m_level_pointer_and_rank[0] = n; // level 0 has n entries
294302

295303
uint8_t level_x_2 = 0;
304+
uint8_t max_level_x_2 = 4;
296305
for (size_type i=0; i < n; ++i) {
297306
val=v_buf[i];
298307
val >>= t_b; // shift value b bits to the right
@@ -302,8 +311,10 @@ dac_vector<t_b, t_rank>::dac_vector(int_vector_buffer<int_width>& v_buf)
302311
++m_level_pointer_and_rank[level_x_2];
303312
val >>= t_b; // shift value b bits to the right
304313
level_x_2 += 2; // increase level by 1
314+
max_level_x_2 = std::max(max_level_x_2, level_x_2);
305315
}
306316
}
317+
m_level_pointer_and_rank.resize(max_level_x_2);
307318
// (2) Determine maximum level and prefix sums of level counters
308319
m_max_level = 0;
309320
size_type sum_blocks = 0, last_block_size=0;

0 commit comments

Comments
 (0)