-
Notifications
You must be signed in to change notification settings - Fork 4
/
table.h
292 lines (216 loc) · 5.93 KB
/
table.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef LITE__TABLE_H
#define LITE__TABLE_H
#include <lace/do_not_copy.h>
#include "link.h"
#include <lace/hash.h>
#include <lace/compare.h>
#include <lace/divider.h>
#include <cassert>
#include <cstddef>
#include <algorithm>
namespace lite {
template <class X>
struct table_link : private link<X> {
typedef table_link type;
template <class T, typename table_link<T>::type T::*L,
typename K, K T::*key, lace::compare_t (*C)(K const &, K const &),
lace::hash_t (*H)(K const &)>
friend class table;
bool bound() const { return link<X>::p; }
};
template <class X>
class table_bucket : private link<X> {
template <class T, typename table_link<T>::type T::*L,
typename K, K T::*key, lace::compare_t (*C)(K const &, K const &),
lace::hash_t (*H)(K const &)>
friend class table;
private:
X* sentinel() { return reinterpret_cast<X*>(this); }
const X* sentinel() const { return reinterpret_cast<const X*>(this); }
};
template <class T, typename table_link<T>::type T::*L,
typename K, K T::*key,
lace::compare_t (*C)(K const &, K const &) = lace::compare<K>,
lace::hash_t (*H)(K const &) = lace::hash<K> >
class table : public lace::do_not_copy {
public:
typedef table_bucket<T> bucket_t;
table(bucket_t bs[] = NULL, const size_t n = 0)
: buckets_(bs), n_buckets_(n), divider_(n)
{ assert(buckets_ || 0 == n_buckets_); set_buckets(); }
~table() { assert(empty()); reset_buckets(); }
bool empty() const {
assert(buckets_ || 0 == n_buckets_);
for (size_t i = 0 ; i < n_buckets_ ; ++i)
if (buckets_[i].p != buckets_[i].sentinel())
return false;
return true;
}
size_t buckets() const { return n_buckets_; }
bool reseat(size_t n = 0) {
if (n <= 0)
delete [] dehash();
else if (bucket_t *bs = new bucket_t[n])
delete [] rehash(bs, n);
else
return false;
return true;
}
bucket_t* rehash(bucket_t bs[], size_t n) {
assert(buckets_ || 0 == n_buckets_);
bucket_t ts;
take_all(ts);
reset_buckets();
std::swap(buckets_, bs);
std::swap(n_buckets_, n);
divider_.invert(n_buckets_);
set_buckets();
give_all(ts);
return bs;
}
bucket_t* dehash() {
assert(empty());
return rehash(NULL, 0);
}
table & set(T* t) {
assert(buckets_);
assert(!is_bound(t));
insert_at(&buckets_[index(t)].p, t);
assert(is_bound(t));
assert(!empty());
return *this;
}
T* bus(T* t) {
assert(!empty());
assert(is_bound(t));
assert(is_member(t));
bucket_t & b = buckets_[index(t)];
assert(b.p);
T ** c = &b.p;
while (t != *c)
c = &((*c)->*L).p;
take_next(c);
assert(!is_member(t));
assert(!is_bound(t));
return t;
}
T& operator[] (const K & k) const {
T* t = get(k);
assert(t);
return *t;
}
T* get(const K & k) const {
if (!n_buckets_)
return NULL;
bucket_t & b = buckets_[index(k)];
for (T ** c = &b.p ; *c != b.sentinel() ; c = &((*c)->*L).p)
if (0 == C(k, (*c)->*key))
return c == &b.p ? *c : insert_at(&b.p, take_next(c));
return NULL;
}
bool is_member(const T* t) const {
if (!n_buckets_ || !is_bound(t))
return false;
bucket_t & b = buckets_[index(t)];
for (T ** c = &b.p ; *c != b.sentinel() ; c = &((*c)->*L).p)
if (t == *c)
return true;
return false;
}
T* iterator() const {
for (size_t i = 0 ; i < n_buckets_ ; ++i)
if (buckets_[i].p != buckets_[i].sentinel())
return buckets_[i].p;
return NULL;
}
T* next(const T* t) const {
assert(is_member(t));
T* n = (t->*L).p;
if (bucket_t* b = is_bucket(n)) {
size_t o = b - buckets_;
for (size_t i = o + 1; i < n_buckets_ ; ++i)
if (buckets_[i].p != buckets_[i].sentinel())
return buckets_[i].p;
return NULL;
}
return n;
}
typedef void (T::*wiper_t)();
T* wipe(T* t, const wiper_t w = NULL) {
T* n = next(t);
bus(t);
if (w)
(t->*w)();
return n;
}
table & polish(const wiper_t w = NULL) {
for (T* n = iterator() ; n ; n = wipe(n, w)) { }
return *this;
}
private:
bucket_t * buckets_;
size_t n_buckets_;
lace::divider divider_;
static bool is_bound(const T* n) { assert(n); return (n->*L).bound(); }
size_t modulo(lace::hash_t h) const { return divider_.modulo(h, n_buckets_); }
size_t index(const K & k) const { return modulo(H(k)); }
size_t index(const T* n) const { assert(n); return index(n->*key); }
bucket_t* is_bucket(const T* n) const {
assert(n);
const bucket_t* b = reinterpret_cast<const bucket_t*>(n);
return (&buckets_[0] <= b && b < &buckets_[n_buckets_]) ? const_cast<bucket_t*>(b) : NULL;
}
static T* take_next(T ** p) {
assert(p);
assert(*p);
T* t = *p;
assert(is_bound(t));
*p = (t->*L).p;
(t->*L).p = NULL;
assert(!is_bound(t));
return t;
}
static T* insert_at(T ** p, T* t) {
assert(p);
assert(*p);
assert(t);
assert(t != *p);
assert(!is_bound(t));
(t->*L).p = *p;
*p = t;
assert(is_bound(t));
return t;
}
void take_all(bucket_t & ts) {
assert(!ts.p);
ts.p = ts.sentinel();
for (size_t i = 0 ; i < n_buckets_ ; ++i) {
bucket_t & b = buckets_[i];
while (b.p != b.sentinel())
insert_at(&ts.p, take_next(&b.p));
}
}
void give_all(bucket_t & ts) {
assert(ts.p);
while (ts.p != ts.sentinel())
set(take_next(&ts.p));
assert(ts.p == ts.sentinel());
ts.p = NULL;
}
void set_buckets() {
for (size_t i = 0 ; i < n_buckets_ ; ++i) {
assert(!buckets_[i].p);
buckets_[i].p = buckets_[i].sentinel();
}
assert(empty());
}
void reset_buckets() {
assert(empty());
for (size_t i = 0 ; i < n_buckets_ ; ++i) {
assert(buckets_[i].p == buckets_[i].sentinel());
buckets_[i].p = NULL;
}
}
};
} // namespace lite
#endif//LITE__TABLE_H