-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrie.hxx
573 lines (445 loc) · 11.5 KB
/
trie.hxx
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#ifndef MRR_TRIE_HXX_
#define MRR_TRIE_HXX_
#include <iterator>
#include <map>
#include <string>
#include <utility>
#include <vector>
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
namespace mrr {
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Prototypes
template <typename Key, typename T>
class trie_node;
template <typename Key, typename T>
class trie_iterator_base;
template <typename Key, typename T>
class trie_iterator;
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
class trie
{
friend class trie_iterator_base<Key,T>;
using seq_type = typename std::conditional<
std::is_same<Key, char>::value,
std::string,
std::vector<Key>
>::type;
using seq_iter_type = typename seq_type::const_iterator;
public:
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<seq_type, T>;
using iterator = trie_iterator<Key, T>;
private:
using node_type = trie_node<Key,T>;
using edge_list_type = typename node_type::edge_list_type;
using edge_list_iterator = typename edge_list_type::iterator;
private:
node_type root_;
iterator end_;
public:
trie()
: root_(), end_()
{
}
// Iterators
iterator end() const { return end_; }
// Capacity
bool empty() const { return root_.empty(); }
//--------------------------------------------------
// Modifiers
//------------------------------
// insert()
template <typename Iter>
std::pair<iterator,bool> insert(
std::pair<std::pair<Iter, Iter>, T> const& x
);
template <typename Container>
std::pair<iterator,bool> insert(
std::pair<Container, T> const& x
);
auto insert(std::pair<const char*, T> const& x)
-> typename std::enable_if<
std::is_same<Key, char>::value,
std::pair<iterator,bool>
>::type
{
return insert(
std::make_pair(
std::string(x.first),
x.second
)
);
}
//------------------------------
// propogate_insert()
template <typename Iter>
std::pair<iterator,bool> propogate_insert(
std::pair<std::pair<Iter, Iter>, T> const& x
);
template <typename Container>
std::pair<iterator,bool> propogate_insert(
std::pair<Container, T> const& x
);
auto propogate_insert(std::pair<const char*, T> const& x)
-> typename std::enable_if<
std::is_same<Key, char>::value,
std::pair<iterator,bool>
>::type
{
return propogate_insert(
std::make_pair(
std::string(x.first),
x.second
)
);
}
//------------------------------
// propogate_insert_overwrite()
template <typename Iter>
std::pair<iterator,bool> propogate_insert_overwrite(
std::pair<std::pair<Iter, Iter>, T> const& x
);
template <typename Container>
std::pair<iterator,bool> propogate_insert_overwrite(
std::pair<Container, T> const& x
);
auto propogate_insert_overwrite(std::pair<const char*, T> const& x)
-> typename std::enable_if<
std::is_same<Key, char>::value,
std::pair<iterator,bool>
>::type
{
return propogate_insert_overwrite(
std::make_pair(
std::string(x.first),
x.second
)
);
}
//------------------------------
void clear()
{
root_.clear();
}
//--------------------------------------------------
// Observers
template <typename Iter>
iterator find(Iter begin, Iter end);
template <typename Container>
iterator find(Container const& key);
auto find(const char* const& str)
-> typename std::enable_if<
std::is_same<Key, char>::value,
iterator
>::type
{
return find(std::string(str));
}
}; // class trie<Key,T>
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// mrr::trie<Key,T> member functions
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Iter>
auto trie<Key,T>::insert(std::pair<std::pair<Iter, Iter>, T> const& x)
-> std::pair<iterator,bool>
{
return insert(
value_type(
seq_type(x.first.first, x.first.second),
x.second
)
);
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Container>
auto trie<Key,T>::insert(
std::pair<Container, T> const& x
)
-> std::pair<iterator, bool>
{
using std::begin;
using std::end;
node_type* cur = &root_;
edge_list_iterator edge;
// Go through each element in the key, creating a path in the tree
for(auto const& elem : x.first)
cur = &(cur->add_edge(elem));
// If the key already exists, ignore the insert attempt
if(cur->is_final())
{
return std::make_pair(
iterator(cur, seq_type(begin(x.first), end(x.first))),
false
);
}
else
{
cur->set_value(x.second);
cur->final(true);
return std::make_pair(
iterator(cur, seq_type(begin(x.first), end(x.first))),
true
);
}
} // std::pair<iterator, bool> trie<Key,T>::insert()
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Iter>
auto trie<Key,T>::propogate_insert(
std::pair<std::pair<Iter, Iter>, T> const& x
)
-> std::pair<iterator,bool>
{
return propogate_insert(
value_type(
seq_type(x.first.first, x.first.second),
x.second
)
);
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Container>
auto trie<Key,T>::propogate_insert(
std::pair<Container, T> const& x
)
-> std::pair<iterator,bool>
{
using std::begin;
using std::end;
node_type* cur = &root_;
edge_list_iterator edge;
if(!cur->is_final())
{
cur->set_value(x.second);
cur->final(true);
}
// Go through each element in the key
for(auto const& elem : x.first)
{
// Move cur to node pointed to by elem
cur = &(cur->add_edge(elem));
// If the current node is not final, make it final and set values
if(!cur->is_final())
{
cur->set_value(x.second);
cur->final(true);
}
}
return std::make_pair(
iterator(cur, seq_type(begin(x.first), end(x.first))),
!cur->is_final()
);
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Iter>
auto trie<Key,T>::propogate_insert_overwrite(
std::pair<std::pair<Iter, Iter>, T> const& x
)
-> std::pair<iterator,bool>
{
return propogate_insert_overwrite(
value_type(
seq_type(x.first.first, x.first.second),
x.second
)
);
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Container>
auto trie<Key,T>::propogate_insert_overwrite(
std::pair<Container, T> const& x
)
-> std::pair<iterator,bool>
{
using std::begin;
using std::end;
node_type* cur = &root_;
edge_list_iterator edge;
cur->set_value(x.second);
cur->final(true);
// Go through each element in the key
for(auto const& elem : x.first)
{
// Move cur to node pointed to by elem
cur = &(cur->add_edge(elem));
cur->set_value(x.second);
cur->final(true);
}
return std::make_pair(
iterator(cur, seq_type(begin(x.first), end(x.first))),
true
);
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Iter>
auto trie<Key,T>::find(Iter first, Iter last) -> iterator
{
return find(seq_type(first, last));
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
template <typename Container>
auto trie<Key,T>::find(Container const& key) -> iterator
{
using std::begin;
using std::end;
// Start at the root
node_type* cur = &root_;
// Loop through the key
for(auto const& elem : key)
{
// Follow the correct edge
edge_list_iterator edge = cur->find_edge(elem);
// If the edge is not found, return end_, if it is, follow edge
if(edge == cur->edge_end())
return end_;
else
cur = &(edge->second);
}
// If the node we end up at is final, return an iterator to it
return cur->is_final()
? iterator(cur, seq_type(begin(key), end(key)))
: end_;
} // iterator trie<Key,T>::find()
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
class trie_node
{
friend class trie<Key,T>;
using node_type = trie_node<Key,T>;
using edge_type = Key;
using edge_list_type = std::map<edge_type, node_type>;
using edge_list_iterator = typename edge_list_type::iterator;
using key_type = typename trie<Key,T>::key_type;
using mapped_type = typename trie<Key,T>::mapped_type;
using value_type = typename trie<Key,T>::value_type;
edge_list_type edges_;
bool final_state_;
public:
trie_node()
: edges_(), final_state_(false), parent_(nullptr), value_()
{
}
void clear()
{
edges_.clear();
}
// Sub tree is empty if there are no edges and this is not a final state
bool empty() const
{
return edges_.empty() && (final_state_ == false);
}
node_type& add_edge(key_type const& x)
{
return edges_[x];
}
edge_list_iterator find_edge(key_type const& x)
{
return edges_.find(x);
}
edge_list_iterator edge_end()
{
return end(edges_);
}
bool is_final() const
{
return final_state_;
}
void final(bool s)
{
final_state_ = s;
}
void set_value(mapped_type const& val)
{
value_ = val;
}
trie_node<Key,T>* parent_;
mapped_type value_;
}; // class trie<Key,T>::trie_node
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
class trie_iterator_base
: std::iterator<std::forward_iterator_tag, T>
{
friend class trie<Key,T>;
protected:
using seq_type = typename trie<Key,T>::seq_type;
using node_type = typename trie<Key,T>::node_type;
using key_type = typename trie<Key,T>::key_type;
using value_type = typename trie<Key,T>::value_type;
public:
bool operator ==(trie_iterator_base const& iter) const
{
return node_ == iter.node_;
}
bool operator !=(trie_iterator_base const& iter) const
{
return ! (*this == iter);
}
protected:
trie_iterator_base()
: node_(nullptr)
{
}
trie_iterator_base(trie_iterator_base const& iter)
: node_(iter.node_)
{
}
trie_iterator_base(node_type* node, seq_type const& key_seq)
: node_(node), value_(std::make_pair(key_seq, node->value_))
{
}
node_type* node_;
std::pair<seq_type, T> value_;
}; // class trie_iterator_base<Key,T>
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
template <typename Key, typename T>
class trie_iterator
: public trie_iterator_base<Key,T>
{
friend class trie<Key,T>;
using seq_type = typename trie_iterator_base<Key,T>::seq_type;
using node_type = typename trie_iterator_base<Key,T>::node_type;
using key_type = typename trie_iterator_base<Key,T>::key_type;
using value_type = typename trie_iterator_base<Key,T>::value_type;
public:
trie_iterator() = default;
trie_iterator& operator =(trie_iterator const& iter)
{
this->node_ = iter.node_;
this->value_ = iter.value_;
return *this;
}
value_type& operator*()
{
return this->value_;
}
value_type* operator->()
{
return &this->value_;
}
private:
explicit trie_iterator(node_type* node, seq_type const& key_seq)
: trie_iterator_base<Key,T>(node, key_seq)
{
}
}; // class trie<Key,T>::trie_iterator
template <typename Key, typename T>
inline auto end(trie<Key,T>& a) -> decltype(a.end())
{
return a.end();
}
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
} // namespace mrr
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#endif // #ifndef MRR_TRIE_HXX_