/
boundedhashtable.h
304 lines (284 loc) · 8.2 KB
/
boundedhashtable.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
293
294
295
296
297
298
299
300
301
302
303
304
/* Copyright: Matthew Brewer (mbrewer@smalladventures.net) 2017-02-12
*
* This is much like a standard open chaining hashtable implementation except
* 1) It uses AVL trees instead of lists, this bounds worst case performance
* for most operations
* 2) Resizes rehash incrementally, thus bounding the worst-case performance
* for resize as well. To make finding the next element constant time we
* additionally put elements in a list.
* 3) "zeroing" of new tables is constant time instead of linear, see
* zero_array.h for details
* When to use this:
* Don't, it's REALLY slow, just a neat proof of concept
* Use btree.h or avl.h for good bounds, ochashtable.h for good average runtime
*
* Assumption: malloc/free (not realloc or calloc) are constant time
* Given this assumption:
* All operations on BoundedHashTable have the same worst-case big O run-time
* as an AVL tree, but with average case runtimes (hopefully) similar to that of
* a hashtable
*
* This means worst case: O(log(n)) insert, remove, get
* Average case: O(1) insert, remove, get
*
* resizes up when size is < x data it contains
* resizes down when size is > 2x data it contains
*
* Threadsafety:
* thead compatible
*/
#include <stdio.h>
#include <utility>
#include "panic.h"
#include "zero_array.h"
#include "avl.h"
#include "dlist.h"
#ifndef BOUNDED_HASHTABLE_H
#define BOUNDED_HASHTABLE_H
#define MINSIZE 4
template <typename Node_T, typename Val_T>
class FixedHashTable {
private:
ZeroArray<AVL<Node_T, Val_T>> table;
DList<Node_T, Val_T> l;
size_t next;
public:
typename DList<Node_T, Val_T>::Iterator begin(){
return l.begin();
}
typename DList<Node_T, Val_T>::Iterator end(){
return l.end();
}
FixedHashTable();
~FixedHashTable();
void reset(size_t s);
bool insert(Node_T *n);
Node_T* get(Val_T key);
Node_T* getOne(void);
Node_T* remove(Node_T *n);
void print(void);
size_t size(void);
};
template <typename Node_T, typename Val_T>
FixedHashTable<Node_T,Val_T>::FixedHashTable() {
// Warning: There's a trick happening here!!
// We create one empty AVL tree. That empty AVL tree
// is *copied* in to a node whenever one is accessed
// that hasn't been accessed before.
// Then, we actually never call the destructor!
// This is okay because an empty AVL tree has nothing to
// free... but it's an API violation, so be careful
table.set_zero(AVL<Node_T, Val_T>());
}
template <typename Node_T, typename Val_T>
FixedHashTable<Node_T,Val_T>::~FixedHashTable() {};
template <typename Node_T, typename Val_T>
void FixedHashTable<Node_T,Val_T>::reset(size_t s) {
table.reset(s); // this is constant time
}
template <typename Node_T, typename Val_T>
bool FixedHashTable<Node_T, Val_T>::insert(Node_T *new_node) {
// Hash it
size_t i = Node_T::hash(new_node->val()) % table.size();
bool b = table[i].insert(new_node);
if (b) {
l.insert(new_node);
}
return b;
}
template <typename Node_T, typename Val_T>
Node_T* FixedHashTable<Node_T,Val_T>::get(Val_T key) {
size_t i = Node_T::hash(key) % table.size();
return table[i].get(key);
}
template <typename Node_T, typename Val_T>
Node_T* FixedHashTable<Node_T,Val_T>::getOne(void) {
return l.peak();
}
template <typename Node_T, typename Val_T>
Node_T* FixedHashTable<Node_T,Val_T>::remove(Node_T *n) {
Val_T v = n->val();
size_t i = Node_T::hash(v) % table.size();
table[i].remove(n);
l.remove(n);
return n;
}
template <typename Node_T, typename Val_T>
void FixedHashTable<Node_T,Val_T>::print(void) {
printf("[\n");
for (size_t i=0; i<table.size(); ++i) {
printf(" ");
table[i].print();
}
printf("]\n");
}
template <typename Node_T, typename Val_T>
size_t FixedHashTable<Node_T,Val_T>::size(void) {
return table.size();
}
template <typename Node_T, typename Val_T>
class BoundedHashTable {
private:
FixedHashTable<Node_T, Val_T> at1;
FixedHashTable<Node_T, Val_T> at2;
FixedHashTable<Node_T, Val_T> *t1;
FixedHashTable<Node_T, Val_T> *t2;
size_t count;
void resize(size_t s) {
if (t2->getOne()) {
PANIC("t2 not empty and we're trying to reset!\n");
}
t2->reset(s);
// Swap the tables
auto tmp = t2;
t2 = t1;
t1 = tmp;
}
void inc() {
// Frumious downcast... This lets us get around
// issues with multiple inheritance
Node_T *n = t2->getOne();
if (n) {
t2->remove(n);
n->ht = t1;
t1->insert(n);
}
}
public:
class Iterator {
private:
typename DList<Node_T, Val_T>::Iterator it;
typename DList<Node_T, Val_T>::Iterator jump;
typename DList<Node_T, Val_T>::Iterator jumpto;
bool jumped;
public:
Iterator(typename DList<Node_T, Val_T>::Iterator _it,
typename DList<Node_T, Val_T>::Iterator _jump,
typename DList<Node_T, Val_T>::Iterator _jumpto,
bool _jumped) {
it = _it;
jump = _jump;
jumpto = _jumpto;
jumped = _jumped;
}
Iterator(const Iterator& other) {
it = other.it;
jump = other.jump;
jumpto = other.jumpto;
jumped = other.jumped;
}
Iterator& operator=(const Iterator& other) {
it = other.it;
jump = other.jump;
jumpto = other.jumpto;
jumped = other.jumped;
return *this;
}
bool operator==(const Iterator& other) {
return it == other.it;
}
bool operator!=(const Iterator& other) {
return !((*this) == other);
}
Iterator operator++() {
it++;
if (!jumped && it == jump) {
jumped = true;
it = jumpto;
}
return *this;
}
Iterator operator++(int) {
Iterator tmp(*this);
++(*this);
return tmp;
}
Node_T& operator*() {
// Get what's inside the iterator
return *it;
}
Node_T* operator->() {
// Get a reference to what's inside the iterator (lol)
return &(*it);
}
};
Iterator begin() {
return Iterator(t1->begin(), t1->end(), t2->begin(), false);
}
Iterator end() {
return Iterator(t2->end(), t1->end(), t2->begin(), true);
}
BoundedHashTable() {
count = 0;
t1 = &at1;
t2 = &at2;
t1->reset(MINSIZE);
t2->reset(MINSIZE);
};
~BoundedHashTable() {}
bool insert(Node_T *n) {
inc();
// if it's over full resize up
if (t1->size() < count+1) {
resize(t1->size()*2);
}
// Make sure it's not in t2 already
if (t2->get(n->val())) {
return false;
}
n->ht = t1;
if(t1->insert(n)) {
count++;
return true;
}
return false;
}
Node_T* get(Val_T key) {
Node_T *n = t1->get(key);
if (!n) {
n = t2->get(key);
}
return n;
}
Node_T* remove(Node_T *n) {
// Only half the elements have to be removed
// before we resize, but all have to be
// out of t2, so we call inc() twice
inc();
inc();
n->ht->remove(n);
count--;
// if it's under half full resize down
if (t1->size() > 2*count && !t2->getOne()) {
resize(t1->size()/2);
}
return n;
}
bool isempty(void) const {
return count == 0;
}
void print() {
printf("[");
printf("t1=");
t1->print();
printf("t2=");
t2->print();
printf("]\n");
}
};
// We pull a bit of a frumious hack here
// Note the multiple inheritance, to give DList access to the DListNode data,
// we actually upcast to that type before putting it in the list, then downcast
// again when we take it out...
// This emulates the behavior of a classic C-style externally allocated structure
// with less macro magic, at the cost of a little loss in type-checking.
template <typename Node_T, typename Val_T>
class BoundedHashTableNode_base: public AVLNode_base<Node_T, Val_T>, public DListNode_base<Node_T> {
public:
FixedHashTable<Node_T,Val_T> *ht;
// subclass must implement:
// Val_T val(void);
// void print(void);
// static size_t hash(Val_T v);
};
#endif