-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathveb.h
119 lines (95 loc) · 2.41 KB
/
veb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef veb_h
#define veb_h
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <memory>
#include "util.h"
#define WORD_BITS 8
template< typename E>
class veb
{
typedef typename std::unordered_map<E, std::shared_ptr<veb<E> > > map_type;
map_type clusters;
std::shared_ptr<veb<E> > summary;
E min; // not stored in a cluster
E max; // copy of element stored in cluster
bool isEmpty;
E w_size; // effective size
// provide working space
std::shared_ptr<veb<E> > getSummary()
{
if(summary.get() == NULL)
{
summary = std::make_shared<veb<E> >(w_size/2);
}
return summary;
}
public:
veb()
: isEmpty(true), w_size(sizeof(E)*WORD_BITS)
{
}
explicit veb(E w_size_)
: isEmpty(true), w_size(w_size_)
{
}
E successor(E x)
{
E w2 = w_size/2;
E c = high(x, w_size);
E i = low(x, w_size);
if(x < min)
{
return min;
}
// If a cluster which would contain x exists and
// if i would be less than it's max we find successor
// inside of that cluster
typename map_type::const_iterator c_it = clusters.find(c);
if(c_it != clusters.end() && i < (*c_it).second->max)
{
return (c<<w2)|(*c_it).second->successor(i);
}
else
{
E c_prime = getSummary()->successor(c);
return (c_prime << w2) | clusters[c_prime]->min;
}
}
// E predecessor(E x);
void insert(E x)
{
if(isEmpty)
{
min = x;
max = x;
isEmpty = false;
return;
}
if(x < min)
{
std::swap(x, min);
}
if(x > max)
{
max = x;
}
//std::cout << "x:" << x << std::endl;
E c = high(x, w_size);
//std::cout << "high:" << c << std::endl;
E i = low(x, w_size);
//std::cout << "low:" << i << std::endl;
if(clusters.count(c) == 0)
{
getSummary()->insert(c);
clusters[c] = std::make_shared<veb<E> >(w_size/2);
}
clusters[c]->insert(i);
}
// E erase(E x);
E get_max() {return max;}
E get_min() {return min;}
bool get_isEmpty() {return isEmpty;}
};
#endif // veb_h