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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
#pragma once
#include "base/assert.hpp"
#include "base/macros.hpp"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <utility>
#include <vector>
namespace base
{
template <typename Char, typename Subtree>
class MapMoves
{
public:
template <typename ToDo>
void ForEach(ToDo && toDo) const
{
for (auto const & subtree : m_subtrees)
toDo(subtree.first, *subtree.second);
}
template <typename ToDo>
void ForEach(ToDo && toDo)
{
for (auto const & subtree : m_subtrees)
toDo(subtree.first, *subtree.second);
}
Subtree * GetSubtree(Char const & c) const
{
auto const it = m_subtrees.find(c);
if (it == m_subtrees.end())
return nullptr;
return it->second.get();
}
Subtree & GetOrCreateSubtree(Char const & c, bool & created)
{
auto & node = m_subtrees[c];
if (!node)
{
node = std::make_unique<Subtree>();
created = true;
}
else
{
created = false;
}
return *node;
}
void AddSubtree(Char const & c, std::unique_ptr<Subtree> subtree)
{
ASSERT(!GetSubtree(c), ());
m_subtrees.emplace(c, move(subtree));
}
void EraseSubtree(Char const & c) { m_subtrees.erase(c); }
size_t Size() const { return m_subtrees.size(); }
bool Empty() const { return Size() == 0; }
void Clear() { m_subtrees.clear(); }
void Swap(MapMoves & rhs) { m_subtrees.swap(rhs.m_subtrees); }
private:
std::map<Char, std::unique_ptr<Subtree>> m_subtrees;
};
template <typename Char, typename Subtree>
class VectorMoves
{
public:
template <typename ToDo>
void ForEach(ToDo && toDo) const
{
for (auto const & subtree : m_subtrees)
toDo(subtree.first, *subtree.second);
}
Subtree * GetSubtree(Char const & c) const
{
auto const it = Find(c);
return it != m_subtrees.cend() ? it->second.get() : nullptr;
}
Subtree & GetOrCreateSubtree(Char const & c, bool & created)
{
if (auto * const subtree = GetSubtree(c))
{
created = false;
return *subtree;
}
created = true;
m_subtrees.emplace_back(c, std::make_unique<Subtree>());
return *m_subtrees.back().second;
}
void AddSubtree(Char const & c, std::unique_ptr<Subtree> subtree)
{
ASSERT(!GetSubtree(c), ());
m_subtrees.emplace_back(c, std::move(subtree));
}
void EraseSubtree(Char const & c)
{
auto const it = Find(c);
if (it != m_subtrees.end())
m_subtrees.erase(it);
}
size_t Size() const { return m_subtrees.size(); }
bool Empty() const { return Size() == 0; }
void Clear() { m_subtrees.clear(); }
void Swap(VectorMoves & rhs) { m_subtrees.swap(rhs.m_subtrees); }
private:
using Entry = std::pair<Char, std::unique_ptr<Subtree>>;
using Entries = std::vector<Entry>;
typename Entries::iterator Find(Char const & c)
{
return std::find_if(m_subtrees.begin(), m_subtrees.end(),
[&c](Entry const & entry) { return entry.first == c; });
}
typename Entries::const_iterator Find(Char const & c) const
{
return std::find_if(m_subtrees.cbegin(), m_subtrees.cend(),
[&c](Entry const & entry) { return entry.first == c; });
}
Entries m_subtrees;
};
template <typename V>
struct VectorValues
{
using value_type = V;
using Value = V;
template <typename... Args>
void Add(Args &&... args)
{
m_values.emplace_back(std::forward<Args>(args)...);
}
void Erase(Value const & v)
{
auto const it = std::find(m_values.begin(), m_values.end(), v);
if (it != m_values.end())
m_values.erase(it);
}
template <typename ToDo>
void ForEach(ToDo && toDo) const
{
for (auto const & value : m_values)
toDo(value);
}
bool Empty() const { return m_values.empty(); }
void Clear() { m_values.clear(); }
void Swap(VectorValues & rhs) { m_values.swap(rhs.m_values); }
std::vector<Value> m_values;
};
// This class is a simple in-memory trie which allows to add
// key-value pairs and then traverse them in a sorted order.
template <typename String, class ValuesHolder, template <typename...> class Moves = MapMoves>
class MemTrie
{
private:
struct Node;
public:
using Char = typename String::value_type;
using Value = typename ValuesHolder::value_type;
MemTrie() = default;
MemTrie(MemTrie && rhs) { *this = std::move(rhs); }
MemTrie & operator=(MemTrie && rhs)
{
m_root = std::move(rhs.m_root);
rhs.Clear();
return *this;
}
// A read-only iterator wrapping a Node. Any modification to the
// underlying trie is assumed to invalidate the iterator.
class Iterator
{
public:
using Char = MemTrie::Char;
Iterator(MemTrie::Node const & node) : m_node(node) {}
// Iterates over all possible moves from this Iterator's node
// and calls |toDo| with two arguments:
// (Char of the move, Iterator wrapping the node of the move).
template <typename ToDo>
void ForEachMove(ToDo && toDo) const
{
m_node.m_moves.ForEach([&](Char const & c, Node const & node) { toDo(c, Iterator(node)); });
}
// Calls |toDo| for every value in this Iterator's node.
template <typename ToDo>
void ForEachInNode(ToDo && toDo) const
{
m_node.m_values.ForEach(std::forward<ToDo>(toDo));
}
String GetLabel() const { return m_node.m_edge.template As<String>(); }
ValuesHolder const & GetValues() const { return m_node.m_values; }
private:
MemTrie::Node const & m_node;
};
// Adds a key-value pair to the trie.
template <typename... Args>
void Add(String const & key, Args &&... args)
{
auto * curr = &m_root;
auto it = key.begin();
while (it != key.end())
{
bool created;
curr = &curr->GetOrCreateMove(*it++, created);
auto & edge = curr->m_edge;
if (created)
{
edge.Assign(it, key.end());
it = key.end();
continue;
}
size_t i = 0;
SkipEqual(edge, key.end(), i, it);
if (i == edge.Size())
{
// We may directly add value to the |curr| values, when edge
// equals to the rest of the |key|. Otherwise we need to jump
// to the next iteration of the loop and continue traversal of
// the trie.
continue;
}
// We need to split the edge to |curr|.
auto node = std::make_unique<Node>();
ASSERT_LESS(i, edge.Size(), ());
node->m_edge = edge.DropFirst(i);
ASSERT(!edge.Empty(), ());
auto const next = edge[0];
edge.DropFirst(1);
node->Swap(*curr);
curr->AddChild(next, std::move(node));
}
curr->AddValue(std::forward<Args>(args)...);
}
void Erase(String const & key, Value const & value)
{
Erase(m_root, key.begin(), key.end(), value);
}
// Traverses all key-value pairs in the trie and calls |toDo| on each of them.
template <typename ToDo>
void ForEachInTrie(ToDo && toDo) const
{
String prefix;
ForEachInSubtree(m_root, prefix, std::forward<ToDo>(toDo));
}
// Calls |toDo| for each key-value pair in the node that is reachable
// by |prefix| from the trie root. Does nothing if such node does
// not exist.
template <typename ToDo>
void ForEachInNode(String const & prefix, ToDo && toDo) const
{
MoveTo(prefix, true /* fullMatch */,
[&](Node const & node, Edge const & /* edge */, size_t /* offset */) {
node.m_values.ForEach(std::forward<ToDo>(toDo));
});
}
template <typename ToDo>
void WithValuesHolder(String const & prefix, ToDo && toDo) const
{
MoveTo(prefix, true /* fullMatch */, [&toDo](Node const & node, Edge const & /* edge */,
size_t /* offset */) { toDo(node.m_values); });
}
// Calls |toDo| for each key-value pair in a subtree that is
// reachable by |prefix| from the trie root. Does nothing if such
// subtree does not exist.
template <typename ToDo>
void ForEachInSubtree(String const & prefix, ToDo && toDo) const
{
MoveTo(prefix, false /* fullMatch */, [&](Node const & node, Edge const & edge, size_t offset) {
String p = prefix;
for (; offset < edge.Size(); ++offset)
p.push_back(edge[offset]);
ForEachInSubtree(node, p, std::forward<ToDo>(toDo));
});
}
bool HasKey(String const & key) const
{
bool exists = false;
MoveTo(key, true /* fullMatch */,
[&](Node const & node, Edge const & /* edge */, size_t /* offset */) {
exists = !node.m_values.Empty();
});
return exists;
}
bool HasPrefix(String const & prefix) const
{
bool exists = false;
MoveTo(prefix, false /* fullMatch */, [&](Node const & node, Edge const & /* edge */,
size_t /* offset */) { exists = !node.Empty(); });
return exists;
}
void Clear() { m_root.Clear(); }
size_t GetNumNodes() const { return m_root.GetNumNodes(); }
Iterator GetRootIterator() const { return Iterator(m_root); }
Node const & GetRoot() const { return m_root; }
private:
// Stores tail (all characters except the first one) of an edge from
// a parent node to a child node. Characters are stored in reverse
// order, because we need to efficiently add and cut prefixes.
class Edge
{
public:
Edge() = default;
template <typename It>
Edge(It begin, It end)
{
Assign(begin, end);
}
template <typename It>
void Assign(It begin, It end)
{
m_label.assign(begin, end);
std::reverse(m_label.begin(), m_label.end());
}
// Drops first |n| characters from the edge.
//
// Complexity: amortized O(n).
Edge DropFirst(size_t n)
{
ASSERT_LESS_OR_EQUAL(n, Size(), ());
Edge const prefix(m_label.rbegin(), m_label.rbegin() + n);
m_label.erase(m_label.begin() + Size() - n, m_label.end());
return prefix;
}
// Prepends |c| to the edge.
//
// Complexity: amortized O(1).
void Prepend(Char const & c) { m_label.push_back(c); }
// Prepends |prefix| to the edge.
//
// Complexity: amortized O(|prefix|)
void Prepend(Edge const & prefix)
{
m_label.insert(m_label.end(), prefix.m_label.cbegin(), prefix.m_label.cend());
}
Char operator[](size_t i) const
{
ASSERT_LESS(i, Size(), ());
return *(m_label.rbegin() + i);
}
size_t Size() const { return m_label.size(); }
bool Empty() const { return Size() == 0; }
void Swap(Edge & rhs) { m_label.swap(rhs.m_label); }
template <typename Sequence>
Sequence As() const { return {m_label.rbegin(), m_label.rend()}; }
friend std::string DebugPrint(Edge const & edge) { return edge.template As<std::string>(); }
private:
std::vector<Char> m_label;
};
struct Node
{
Node() = default;
Node(Node && /* rhs */) = default;
Node & operator=(Node && /* rhs */) = default;
Node & GetOrCreateMove(Char const & c, bool & created)
{
return m_moves.GetOrCreateSubtree(c, created);
}
Node * GetMove(Char const & c) const { return m_moves.GetSubtree(c); }
void AddChild(Char const & c, std::unique_ptr<Node> node)
{
m_moves.AddSubtree(c, std::move(node));
}
void EraseMove(Char const & c) { m_moves.EraseSubtree(c); }
template <typename... Args>
void AddValue(Args &&... args)
{
m_values.Add(std::forward<Args>(args)...);
}
void EraseValue(Value const & value)
{
m_values.Erase(value);
}
bool Empty() const { return m_moves.Empty() && m_values.Empty(); }
void Clear()
{
m_moves.Clear();
m_values.Clear();
}
size_t GetNumNodes() const
{
size_t size = 1;
m_moves.ForEach(
[&size](Char const & /* c */, Node const & child) { size += child.GetNumNodes(); });
return size;
}
void Swap(Node & rhs)
{
m_moves.Swap(rhs.m_moves);
m_edge.Swap(rhs.m_edge);
m_values.Swap(rhs.m_values);
}
Moves<Char, Node> m_moves;
Edge m_edge;
ValuesHolder m_values;
DISALLOW_COPY(Node);
};
template <typename Fn>
void MoveTo(String const & prefix, bool fullMatch, Fn && fn) const
{
auto const * cur = &m_root;
auto it = prefix.begin();
if (it == prefix.end())
{
fn(*cur, cur->m_edge, 0 /* offset */);
return;
}
while (true)
{
ASSERT(it != prefix.end(), ());
cur = cur->GetMove(*it++);
if (!cur)
return;
auto const & edge = cur->m_edge;
size_t i = 0;
SkipEqual(edge, prefix.end(), i, it);
if (i < edge.Size())
{
if (it != prefix.end() || fullMatch)
return;
}
ASSERT(i == edge.Size() || (it == prefix.end() && !fullMatch), ());
if (it == prefix.end())
{
fn(*cur, edge, i /* offset */);
return;
}
ASSERT(it != prefix.end() && i == edge.Size(), ());
}
}
template <typename It>
void Erase(Node & root, It cur, It end, Value const & value)
{
if (cur == end)
{
root.EraseValue(value);
if (root.m_values.Empty() && root.m_moves.Size() == 1)
{
Node child;
Char c;
root.m_moves.ForEach([&](Char const & mc, Node & mn) {
c = mc;
child.Swap(mn);
});
child.m_edge.Prepend(c);
child.m_edge.Prepend(root.m_edge);
root.Swap(child);
}
return;
}
auto const symbol = *cur++;
auto * child = root.GetMove(symbol);
if (!child)
return;
auto const & edge = child->m_edge;
size_t i = 0;
SkipEqual(edge, end, i, cur);
if (i == edge.Size())
{
Erase(*child, cur, end, value);
if (child->Empty())
root.EraseMove(symbol);
}
}
// Calls |toDo| for each key-value pair in subtree where |node| is a
// root of the subtree. |prefix| is a path from the trie root to the
// |node|. Because we need to accumulate labels from the root to the
// current |node| somewhere, |prefix| is passed by reference here,
// but after the call the contents of the |prefix| will be the same
// as before the call.
template <typename ToDo>
void ForEachInSubtree(Node const & node, String & prefix, ToDo && toDo) const
{
node.m_values.ForEach([&prefix, &toDo](Value const & value) {
toDo(static_cast<String const &>(prefix), value);
});
node.m_moves.ForEach([&](Char const & c, Node const & node)
{
auto const size = prefix.size();
auto const edge = node.m_edge.template As<String>();
prefix.push_back(c);
prefix.insert(prefix.end(), edge.begin(), edge.end());
ForEachInSubtree(node, prefix, toDo);
prefix.resize(size);
});
}
template <typename It>
void SkipEqual(Edge const & edge, It end, size_t & i, It & cur) const
{
while (i < edge.Size() && cur != end && edge[i] == *cur)
{
++i;
++cur;
}
}
Node m_root;
DISALLOW_COPY(MemTrie);
};
} // namespace base
|