forked from ytakano/radix_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_tree.hpp
536 lines (402 loc) · 12.5 KB
/
radix_tree.hpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#ifndef RADIX_TREE_HPP
#define RADIX_TREE_HPP
#include <cassert>
#include <string>
#include <utility>
#include <vector>
#include "radix_tree_it.hpp"
#include "radix_tree_node.hpp"
template<typename K>
K radix_substr(const K &key, int begin, int num);
template<>
inline std::string radix_substr<std::string>(const std::string &key, int begin, int num)
{
return key.substr(begin, num);
}
template<typename K>
K radix_join(const K &key1, const K &key2);
template<>
inline std::string radix_join<std::string>(const std::string &key1, const std::string &key2)
{
return key1 + key2;
}
template<typename K>
int radix_length(const K &key);
template<>
inline int radix_length<std::string>(const std::string &key)
{
return key.size();
}
template <typename K, typename T>
class radix_tree {
public:
typedef K key_type;
typedef T mapped_type;
typedef std::pair<const K, T> value_type;
typedef radix_tree_it<K, T> iterator;
typedef std::size_t size_type;
radix_tree() : m_size(0), m_root(NULL) { }
~radix_tree() {
delete m_root;
}
size_type size() const {
return m_size;
}
bool empty() const {
return m_size == 0;
}
void clear() {
delete m_root;
m_root = NULL;
m_size = 0;
}
iterator find(const K &key);
iterator begin();
iterator end();
std::pair<iterator, bool> insert(const value_type &val);
bool erase(const K &key);
void erase(iterator it);
void prefix_match(const K &key, std::vector<iterator> &vec);
void greedy_match(const K &key, std::vector<iterator> &vec);
iterator longest_match(const K &key);
T& operator[] (const K &lhs);
private:
size_type m_size;
radix_tree_node<K, T>* m_root;
radix_tree_node<K, T>* begin(radix_tree_node<K, T> *node);
radix_tree_node<K, T>* find_node(const K &key, radix_tree_node<K, T> *node, int depth);
radix_tree_node<K, T>* append(radix_tree_node<K, T> *parent, const value_type &val);
radix_tree_node<K, T>* prepend(radix_tree_node<K, T> *node, const value_type &val);
void greedy_match(radix_tree_node<K, T> *node, std::vector<iterator> &vec);
radix_tree(const radix_tree& other); // delete
radix_tree& operator =(const radix_tree other); // delete
};
template <typename K, typename T>
void radix_tree<K, T>::prefix_match(const K &key, std::vector<iterator> &vec)
{
vec.clear();
if (m_root == NULL)
return;
radix_tree_node<K, T> *node;
K key_sub1, key_sub2;
node = find_node(key, m_root, 0);
if (node->m_is_leaf)
node = node->m_parent;
int len = radix_length(key) - node->m_depth;
key_sub1 = radix_substr(key, node->m_depth, len);
key_sub2 = radix_substr(node->m_key, 0, len);
if (key_sub1 != key_sub2)
return;
greedy_match(node, vec);
}
template <typename K, typename T>
typename radix_tree<K, T>::iterator radix_tree<K, T>::longest_match(const K &key)
{
if (m_root == NULL)
return iterator(NULL);
radix_tree_node<K, T> *node;
K key_sub;
node = find_node(key, m_root, 0);
if (node->m_is_leaf)
return iterator(node);
key_sub = radix_substr(key, node->m_depth, radix_length(node->m_key));
if (! (key_sub == node->m_key))
node = node->m_parent;
K nul = radix_substr(key, 0, 0);
while (node != NULL) {
typename radix_tree_node<K, T>::it_child it;
it = node->m_children.find(nul);
if (it != node->m_children.end() && it->second->m_is_leaf)
return iterator(it->second);
node = node->m_parent;
}
return iterator(NULL);
}
template <typename K, typename T>
typename radix_tree<K, T>::iterator radix_tree<K, T>::end()
{
return iterator(NULL);
}
template <typename K, typename T>
typename radix_tree<K, T>::iterator radix_tree<K, T>::begin()
{
radix_tree_node<K, T> *node;
if (m_root == NULL)
node = NULL;
else
node = begin(m_root);
return iterator(node);
}
template <typename K, typename T>
radix_tree_node<K, T>* radix_tree<K, T>::begin(radix_tree_node<K, T> *node)
{
if (node->m_is_leaf)
return node;
assert(!node->m_children.empty());
return begin(node->m_children.begin()->second);
}
template <typename K, typename T>
T& radix_tree<K, T>::operator[] (const K &lhs)
{
iterator it = find(lhs);
if (it == end()) {
std::pair<K, T> val;
val.first = lhs;
std::pair<iterator, bool> ret;
ret = insert(val);
assert(ret.second == true);
it = ret.first;
}
return it->second;
}
template <typename K, typename T>
void radix_tree<K, T>::greedy_match(const K &key, std::vector<iterator> &vec)
{
radix_tree_node<K, T> *node;
vec.clear();
if (m_root == NULL)
return;
node = find_node(key, m_root, 0);
if (node->m_is_leaf)
node = node->m_parent;
greedy_match(node, vec);
}
template <typename K, typename T>
void radix_tree<K, T>::greedy_match(radix_tree_node<K, T> *node, std::vector<iterator> &vec)
{
if (node->m_is_leaf) {
vec.push_back(iterator(node));
return;
}
typename std::map<K, radix_tree_node<K, T>*>::iterator it;
for (it = node->m_children.begin(); it != node->m_children.end(); ++it) {
greedy_match(it->second, vec);
}
}
template <typename K, typename T>
void radix_tree<K, T>::erase(iterator it)
{
erase(it->first);
}
template <typename K, typename T>
bool radix_tree<K, T>::erase(const K &key)
{
if (m_root == NULL)
return 0;
radix_tree_node<K, T> *child;
radix_tree_node<K, T> *parent;
radix_tree_node<K, T> *grandparent;
K nul = radix_substr(key, 0, 0);
child = find_node(key, m_root, 0);
if (! child->m_is_leaf)
return 0;
parent = child->m_parent;
parent->m_children.erase(nul);
delete child;
m_size--;
if (parent == m_root)
return 1;
if (parent->m_children.size() > 1)
return 1;
if (parent->m_children.empty()) {
grandparent = parent->m_parent;
grandparent->m_children.erase(parent->m_key);
delete parent;
} else {
grandparent = parent;
}
if (grandparent == m_root) {
return 1;
}
if (grandparent->m_children.size() == 1) {
// merge grandparent with the uncle
typename std::map<K, radix_tree_node<K, T>*>::iterator it;
it = grandparent->m_children.begin();
radix_tree_node<K, T> *uncle = it->second;
if (uncle->m_is_leaf)
return 1;
uncle->m_depth = grandparent->m_depth;
uncle->m_key = radix_join(grandparent->m_key, uncle->m_key);
uncle->m_parent = grandparent->m_parent;
grandparent->m_children.erase(it);
grandparent->m_parent->m_children.erase(grandparent->m_key);
grandparent->m_parent->m_children[uncle->m_key] = uncle;
delete grandparent;
}
return 1;
}
template <typename K, typename T>
radix_tree_node<K, T>* radix_tree<K, T>::append(radix_tree_node<K, T> *parent, const value_type &val)
{
int depth;
int len;
K nul = radix_substr(val.first, 0, 0);
radix_tree_node<K, T> *node_c, *node_cc;
depth = parent->m_depth + radix_length(parent->m_key);
len = radix_length(val.first) - depth;
if (len == 0) {
node_c = new radix_tree_node<K, T>(val);
node_c->m_depth = depth;
node_c->m_parent = parent;
node_c->m_key = nul;
node_c->m_is_leaf = true;
parent->m_children[nul] = node_c;
return node_c;
} else {
node_c = new radix_tree_node<K, T>(val);
K key_sub = radix_substr(val.first, depth, len);
parent->m_children[key_sub] = node_c;
node_c->m_depth = depth;
node_c->m_parent = parent;
node_c->m_key = key_sub;
node_cc = new radix_tree_node<K, T>(val);
node_c->m_children[nul] = node_cc;
node_cc->m_depth = depth + len;
node_cc->m_parent = node_c;
node_cc->m_key = nul;
node_cc->m_is_leaf = true;
return node_cc;
}
}
template <typename K, typename T>
radix_tree_node<K, T>* radix_tree<K, T>::prepend(radix_tree_node<K, T> *node, const value_type &val)
{
int count;
int len1, len2;
len1 = radix_length(node->m_key);
len2 = radix_length(val.first) - node->m_depth;
for (count = 0; count < len1 && count < len2; count++) {
if (! (node->m_key[count] == val.first[count + node->m_depth]) )
break;
}
assert(count != 0);
node->m_parent->m_children.erase(node->m_key);
radix_tree_node<K, T> *node_a = new radix_tree_node<K, T>;
node_a->m_parent = node->m_parent;
node_a->m_key = radix_substr(node->m_key, 0, count);
node_a->m_depth = node->m_depth;
node_a->m_parent->m_children[node_a->m_key] = node_a;
node->m_depth += count;
node->m_parent = node_a;
node->m_key = radix_substr(node->m_key, count, len1 - count);
node->m_parent->m_children[node->m_key] = node;
K nul = radix_substr(val.first, 0, 0);
if (count == len2) {
radix_tree_node<K, T> *node_b;
node_b = new radix_tree_node<K, T>(val);
node_b->m_parent = node_a;
node_b->m_key = nul;
node_b->m_depth = node_a->m_depth + count;
node_b->m_is_leaf = true;
node_b->m_parent->m_children[nul] = node_b;
return node_b;
} else {
radix_tree_node<K, T> *node_b, *node_c;
node_b = new radix_tree_node<K, T>;
node_b->m_parent = node_a;
node_b->m_depth = node->m_depth;
node_b->m_key = radix_substr(val.first, node_b->m_depth, len2 - count);
node_b->m_parent->m_children[node_b->m_key] = node_b;
node_c = new radix_tree_node<K, T>(val);
node_c->m_parent = node_b;
node_c->m_depth = radix_length(val.first);
node_c->m_key = nul;
node_c->m_is_leaf = true;
node_c->m_parent->m_children[nul] = node_c;
return node_c;
}
}
template <typename K, typename T>
std::pair<typename radix_tree<K, T>::iterator, bool> radix_tree<K, T>::insert(const value_type &val)
{
if (m_root == NULL) {
K nul = radix_substr(val.first, 0, 0);
m_root = new radix_tree_node<K, T>;
m_root->m_key = nul;
}
radix_tree_node<K, T> *node = find_node(val.first, m_root, 0);
if (node->m_is_leaf) {
return std::pair<iterator, bool>(node, false);
} else if (node == m_root) {
m_size++;
return std::pair<iterator, bool>(append(m_root, val), true);
} else {
m_size++;
int len = radix_length(node->m_key);
K key_sub = radix_substr(val.first, node->m_depth, len);
if (key_sub == node->m_key) {
return std::pair<iterator, bool>(append(node, val), true);
} else {
return std::pair<iterator, bool>(prepend(node, val), true);
}
}
}
template <typename K, typename T>
typename radix_tree<K, T>::iterator radix_tree<K, T>::find(const K &key)
{
if (m_root == NULL)
return iterator(NULL);
radix_tree_node<K, T> *node = find_node(key, m_root, 0);
// if the node is a internal node, return NULL
if (! node->m_is_leaf)
return iterator(NULL);
return iterator(node);
}
template <typename K, typename T>
radix_tree_node<K, T>* radix_tree<K, T>::find_node(const K &key, radix_tree_node<K, T> *node, int depth)
{
if (node->m_children.empty())
return node;
typename radix_tree_node<K, T>::it_child it;
int len_key = radix_length(key) - depth;
for (it = node->m_children.begin(); it != node->m_children.end(); ++it) {
if (len_key == 0) {
if (it->second->m_is_leaf)
return it->second;
else
continue;
}
if (! it->second->m_is_leaf && key[depth] == it->first[0] ) {
int len_node = radix_length(it->first);
K key_sub = radix_substr(key, depth, len_node);
if (key_sub == it->first) {
return find_node(key, it->second, depth+len_node);
} else {
return it->second;
}
}
}
return node;
}
/*
(root)
|
|---------------
| | |
abcde bcdef c
| | | |------
| | $3 | | |
f ge d e $6
| | | |
$1 $2 $4 $5
find_node():
bcdef -> $3
bcdefa -> bcdef
c -> $6
cf -> c
abch -> abcde
abc -> abcde
abcde -> abcde
abcdef -> $1
abcdeh -> abcde
de -> (root)
(root)
|
abcd
|
$
(root)
|
$
*/
#endif // RADIX_TREE_HPP