-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRadixTree.cs
904 lines (818 loc) · 19.7 KB
/
RadixTree.cs
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
using System;
using System.Collections.Generic;
using System.IO;
namespace Zyborg.Collections
{
// WalkFn is used when walking the tree. Takes a
// key and value, returning if iteration should
// be terminated.
//~ type WalkFn func(s string, v interface{ }) bool
public delegate bool Walker<TValue>(string s, TValue v);
//// leafNode is used to represent a value
//~ type leafNode struct {
//~ key string
//~ val interface{}
//~ }
class LeafNode<TValue>
{
internal string _key = string.Empty;
internal TValue _value;
}
//// edge is used to represent an edge node
//~ type edge struct {
//~ label byte
//~ node *node
//~ }
class Edge<TValue>
{
internal char _label;
internal Node<TValue> _node;
}
class Edges<TValue> : List<Edge<TValue>>
{
public bool IsLess(int index1, int index2)
{
return this[index1]._label < this[index2]._label;
}
//~ func (e edges) Swap(i, j int) {
//~ e[i], e[j] = e[j], e[i]
//~ }
public void Swap(int index1, int index2)
{
(this[index1], this[index2]) = (this[index2], this[index1]);
}
public void SortEdges()
{
base.Sort((Edge<TValue> x, Edge<TValue> y) => x._label - y._label);
}
// Implements semantics of:
// https://golang.org/pkg/sort/#Search
public int Search(int n, Predicate<Edge<TValue>> match)
{
var idx = FindIndex(0, n, match);
if (idx == -1)
idx = n;
return idx;
}
}
//~ type node struct {
//~ // leaf is used to store possible leaf
//~ leaf *leafNode
//~
//~ // prefix is the common prefix we ignore
//~ prefix string
//~
//~ // Edges should be stored in-order for iteration.
//~ // We avoid a fully materialized slice to save memory,
//~ // since in most cases we expect to be sparse
//~ edges edges
//~ }
class Node<TValue>
{
internal LeafNode<TValue> _leaf;
internal string _prefix = string.Empty;
internal Edges<TValue> _edges = new Edges<TValue>();
//~ func (n *node) isLeaf() bool {
//~ return n.leaf != nil
//~ }
public bool IsLeaf
{
get => _leaf != null;
}
//~ func (n *node) addEdge(e edge) {
//~ n.edges = append(n.edges, e)
//~ n.edges.Sort()
//~ }
public void AddEdge(Edge<TValue> e)
{
_edges.Add(e);
_edges.SortEdges();
}
//~ func (n *node) replaceEdge(e edge) {
//~ num := len(n.edges)
//~ idx := sort.Search(num, func(i int) bool {
//~ return n.edges[i].label >= e.label
//~ })
//~ if idx < num && n.edges[idx].label == e.label {
//~ n.edges[idx].node = e.node
//~ return
//~ }
//~ panic("replacing missing edge")
//~ }
public void ReplaceEdge(Edge<TValue> e)
{
var num = _edges.Count;
var idx = _edges.Search(num, x => x._label >= e._label);
if (idx < num && _edges[idx]._label == e._label)
{
_edges[idx]._node = e._node;
return;
}
throw new Exception("replacing missing edge");
}
//~ func (n *node) getEdge(label byte) *node {
//~ num := len(n.edges)
//~ idx := sort.Search(num, func(i int) bool {
//~ return n.edges[i].label >= label
//~ })
//~ if idx < num && n.edges[idx].label == label {
//~ return n.edges[idx].node
//~ }
//~ return nil
//~ }
public Node<TValue> GetEdge(char label)
{
var num = _edges.Count;
var idx = _edges.Search(num, x => x._label >= label);
if (idx < num && _edges[idx]._label == label)
{
return _edges[idx]._node;
}
return null;
}
//~ func (n *node) delEdge(label byte) {
//~ num := len(n.edges)
//~ idx := sort.Search(num, func(i int) bool {
//~ return n.edges[i].label >= label
//~ })
//~ if idx < num && n.edges[idx].label == label {
//~ copy(n.edges[idx:], n.edges[idx+1:])
//~ n.edges[len(n.edges)-1] = edge{}
//~ n.edges = n.edges[:len(n.edges)-1]
//~ }
//~ }
public void RemoveEdge(char label)
{
var num = _edges.Count;
var idx = _edges.Search(num, x => x._label >= label);
if (idx < num && _edges[idx]._label == label)
{
// As per https://golang.org/pkg/builtin/#copy
_edges.RemoveAt(idx);
}
}
//~ func (n *node) mergeChild() {
//~ e := n.edges[0]
//~ child := e.node
//~ n.prefix = n.prefix + child.prefix
//~ n.leaf = child.leaf
//~ n.edges = child.edges
//~ }
public void MergeChild()
{
var e = this._edges[0];
var child = e._node;
this._prefix = this._prefix + child._prefix;
this._leaf = child._leaf;
this._edges = child._edges;
}
public void Print(StreamWriter w, string prefix)
{
w.WriteLine($"{prefix}Prfx=[{this._prefix}]");
if (this._leaf != null)
w.WriteLine($"{prefix}Leaf: [{this._leaf._key}] = [{this._leaf._value}]");
foreach (var e in _edges)
{
w.WriteLine($"{prefix}Edge: label=[{e._label}]");
e._node.Print(w, prefix + " ");
}
}
}
// Tree implements a radix tree. This can be treated as a
// Dictionary abstract data type. The main advantage over
// a standard hash map is prefix-based lookups and
// ordered iteration,
//~ type Tree struct {
//~ root *node
//~ size int
//~ }
public class RadixTree<TValue>
{
private Node<TValue> _root;
private int _size;
// New returns an empty Tree
public RadixTree()
: this(null)
{ }
// NewFromMap returns a new tree containing the keys
// from an existing map
//~ func NewFromMap(m map[string]interface{}) *Tree {
//~ t := &Tree{root: &node{}}
//~ for k, v := range m {
//~ t.Insert(k, v)
//~ }
//~ return t
//~ }
public RadixTree(IReadOnlyDictionary<string, TValue> m)
{
_root = new Node<TValue>();
if (m != null)
{
foreach (var kv in m)
{
this.GoInsert(kv.Key, kv.Value);
}
}
}
// Len is used to return the number of elements in the tree
public int Count
{
get { return _size; }
}
// longestPrefix finds the length of the shared prefix
// of two strings
public static int FindLongestPrefix(string k1, string k2)
{
// max := len(k1)
// if l := len(k2); l < max {
// max = l
// }
// var i int
// for i = 0; i < max; i++ {
// if k1[i] != k2[i] {
// break
// }
// }
// return i
var max = k1.Length;
var l = k2.Length;
if (l < max)
max = l;
for (int i = 0; i < max; i++)
if (k1[i] != k2[i])
return i;
return max;
}
// Insert is used to add a newentry or update
// an existing entry. Returns if updated.
//~ func (t *Tree) Insert(s string, v interface{}) (interface{}, bool) {
public (TValue oldValue, bool updated) GoInsert(string s, TValue v)
{
//~ var parent *node
//~ n := t.root
//~ search := s
Node<TValue> parent;
var n = _root;
var search = s;
//~ for {
while (true)
{
// Handle key exhaution
//~ if len(search) == 0 {
//~ if n.isLeaf() {
//~ old := n.leaf.val
//~ n.leaf.val = v
//~ return old, true
//~ }
//~
//~ n.leaf = &leafNode{
//~ key: s,
//~ val: v,
//~ }
//~ t.size++
//~ return nil, false
//~ }
if (search.Length == 0)
{
if (n.IsLeaf)
{
var old = n._leaf._value;
n._leaf._value = v;
return (old, true);
}
n._leaf = new LeafNode<TValue>
{
_key = s,
_value = v,
};
_size++;
return (default(TValue), false);
}
// Look for the edge
//~ parent = n
//~ n = n.getEdge(search[0])
parent = n;
n = n.GetEdge(search[0]);
// No edge, create one
//~ if n == nil {
//~ e := edge{
//~ label: search[0],
//~ node: &node{
//~ leaf: &leafNode{
//~ key: s,
//~ val: v,
//~ },
//~ prefix: search,
//~ },
//~ }
//~ parent.addEdge(e)
//~ t.size++
//~ return nil, false
//~ }
if (n == null)
{
var e = new Edge<TValue>
{
_label = search[0],
_node = new Node<TValue>
{
_leaf = new LeafNode<TValue>
{
_key = s,
_value = v,
},
_prefix = search,
}
};
parent.AddEdge(e);
_size++;
return (default(TValue), false);
}
// Determine longest prefix of the search key on match
//~ commonPrefix := longestPrefix(search, n.prefix)
//~ if commonPrefix == len(n.prefix) {
//~ search = search[commonPrefix:]
//~ continue
//~ }
var commonPrefix = FindLongestPrefix(search, n._prefix);
if (commonPrefix == n._prefix.Length)
{
search = search.Substring(commonPrefix);
continue;
}
// Split the node
//~ t.size++
//~ child := &node{
//~ prefix: search[:commonPrefix],
//~ }
//~ parent.replaceEdge(edge{
//~ label: search[0],
//~ node: child,
//~ })
_size++;
var child = new Node<TValue>
{
_prefix = search.Substring(0, commonPrefix),
};
parent.ReplaceEdge(new Edge<TValue>
{
_label = search[0],
_node = child,
});
// Restore the existing node
//~ child.addEdge(edge{
//~ label: n.prefix[commonPrefix],
//~ node: n,
//~ })
//~ n.prefix = n.prefix[commonPrefix:]
child.AddEdge(new Edge<TValue>
{
_label = n._prefix[commonPrefix],
_node = n,
});
n._prefix = n._prefix.Substring(commonPrefix);
// Create a new leaf node
//~ leaf := &leafNode{
//~ key: s,
//~ val: v,
//~ }
var leaf = new LeafNode<TValue>
{
_key = s,
_value = v,
};
// If the new key is a subset, add to to this node
//~ search = search[commonPrefix:]
//~ if len(search) == 0 {
//~ child.leaf = leaf
//~ return nil, false
//~ }
search = search.Substring(commonPrefix);
if (search.Length == 0)
{
child._leaf = leaf;
return (default(TValue), false);
}
// Create a new edge for the node
//~ child.addEdge(edge{
//~ label: search[0],
//~ node: &node{
//~ leaf: leaf,
//~ prefix: search,
//~ },
//~ })
//~ return nil, false
child.AddEdge(new Edge<TValue>
{
_label = search[0],
_node = new Node<TValue>
{
_leaf = leaf,
_prefix = search,
}
});
return (default(TValue), false);
}
}
// Delete is used to delete a key, returning the previous
// value and if it was deleted
//!func (t *Tree) Delete(s string) (interface{}, bool) {
public (TValue oldValue, bool deleted) GoDelete(string s)
{
//~ var parent *node
//~ var label byte
//~ n := t.root
//~ search := s
Node<TValue> parent = null;
char label = char.MinValue;
var n = _root;
var search = s;
//! for {
while (true)
{
// Check for key exhaution
//~ if len(search) == 0 {
//~ if !n.isLeaf() {
//~ break
//~ }
//~ goto DELETE
//~ }
if (search.Length == 0)
{
if (!n.IsLeaf)
break;
goto DELETE;
}
// Look for an edge
//~ parent = n
//~ label = search[0]
//~ n = n.getEdge(label)
//~ if n == nil {
//~ break
//~ }
parent = n;
label = search[0];
n = n.GetEdge(label);
if (n == null)
break;
// Consume the search prefix
//~ if strings.HasPrefix(search, n.prefix) {
//~ search = search[len(n.prefix):]
//~ } else {
//~ break
//~ }
if (search.StartsWith(n._prefix))
search = search.Substring(n._prefix.Length);
else
break;
}
//~ return nil, false
return (default(TValue), false);
DELETE:
// Delete the leaf
//~ leaf := n.leaf
//~ n.leaf = nil
//~ t.size--
var leaf = n._leaf;
n._leaf = null;
_size--;
// Check if we should delete this node from the parent
//~ if parent != nil && len(n.edges) == 0 {
//~ parent.delEdge(label)
//~ }
if (parent != null && n._edges.Count == 0)
parent.RemoveEdge(label);
// Check if we should merge this node
//~ if n != t.root && len(n.edges) == 1 {
//~ n.mergeChild()
//~ }
if (n != _root && n._edges.Count == 1)
n.MergeChild();
// Check if we should merge the parent's other child
//~ if parent != nil && parent != t.root && len(parent.edges) == 1 && !parent.isLeaf() {
//~ parent.mergeChild()
//~ }
if (parent != null && parent != _root && parent._edges.Count == 1 && !parent.IsLeaf)
parent.MergeChild();
//~ return leaf.val, true
return (leaf._value, true);
}
// Get is used to lookup a specific key, returning
// the value and if it was found
//~ func (t *Tree) Get(s string) (interface{}, bool) {
public (TValue value, bool found) GoGet(string s)
{
// n := t.root
// search := s
var n = _root;
var search = s;
//~ for {
while (true)
{
// Check for key exhaution
//~ if len(search) == 0 {
//~ if n.isLeaf() {
//~ return n.leaf.val, true
//~ }
//~ break
//~ }
if (search.Length == 0)
{
if (n.IsLeaf)
return (n._leaf._value, true);
break;
}
// Look for an edge
//~ n = n.getEdge(search[0])
//~ if n == nil {
//~ break
//~ }
var oldN = n;
n = n.GetEdge(search[0]);
if (n == null)
break;
// Consume the search prefix
//~ if strings.HasPrefix(search, n.prefix) {
//~ search = search[len(n.prefix):]
//~ } else {
//~ break
//~ }
if (search.StartsWith(n._prefix))
search = search.Substring(n._prefix.Length);
else
break;
}
//~ return nil, false
return (default(TValue), false);
}
// LongestPrefix is like Get, but instead of an
// exact match, it will return the longest prefix match.
//~ func (t *Tree) LongestPrefix(s string) (string, interface{}, bool) {
public (string key, TValue value, bool found) LongestPrefix(string s)
{
//~ var last *leafNode
//~ n := t.root
//~ search := s
LeafNode<TValue> last = null;
var n = _root;
var search = s;
//for {
while (true)
{
// Look for a leaf node
//~ if n.isLeaf() {
//~ last = n.leaf
//~ }
if (n.IsLeaf)
last = n._leaf;
// Check for key exhaution
//~ if len(search) == 0 {
//~ break
//~ }
if (search.Length == 0)
break;
// Look for an edge
//~ n = n.getEdge(search[0])
//~ if n == nil {
//~ break
//~ }
n = n.GetEdge(search[0]);
if (n == null)
break;
// Consume the search prefix
//~ if strings.HasPrefix(search, n.prefix) {
//~ search = search[len(n.prefix):]
//~ } else {
//~ break
//~ }
if (search.StartsWith(n._prefix))
search = search.Substring(n._prefix.Length);
else
break;
}
//~ if last != nil {
//~ return last.key, last.val, true
//~ }
//~ return "", nil, false
if (last != null)
return (last._key, last._value, true);
return (string.Empty, default(TValue), false);
}
// Minimum is used to return the minimum value in the tree
//~ func (t *Tree) Minimum() (string, interface{}, bool) {
public (string key, TValue value, bool found) Minimum()
{
//!n := t.root
var n = _root;
//for {
// if n.isLeaf() {
// return n.leaf.key, n.leaf.val, true
// }
// if len(n.edges) > 0 {
// n = n.edges[0].node
// } else {
// break
// }
//}
while (true)
{
if (n.IsLeaf)
return (n._leaf._key, n._leaf._value, true);
if (n._edges.Count > 0)
n = n._edges[0]._node;
else
break;
}
//return "", nil, false
return (string.Empty, default(TValue), false);
}
// Maximum is used to return the maximum value in the tree
//~ func (t *Tree) Maximum() (string, interface{}, bool) {
public (string key, TValue value, bool found) Maximum()
{
//~ n := t.root
var n = _root;
//for {
// if num := len(n.edges); num > 0 {
// n = n.edges[num-1].node
// continue
// }
// if n.isLeaf() {
// return n.leaf.key, n.leaf.val, true
// }
// break
//}
while (true)
{
var num = n._edges.Count;
if (num > 0)
{
n = n._edges[num - 1]._node;
continue;
}
if (n.IsLeaf)
return (n._leaf._key, n._leaf._value, true);
break;
}
//return "", nil, false
return (string.Empty, default(TValue), false);
}
// Walk is used to walk the tree
//~ func (t *Tree) Walk(fn WalkFn) {
public void Walk(Walker<TValue> fn)
{
RecursiveWalk(_root, fn);
}
// WalkPrefix is used to walk the tree under a prefix
//~ func (t *Tree) WalkPrefix(prefix string, fn WalkFn) {
public void WalkPrefix(string prefix, Walker<TValue> fn)
{
//~ n := t.root
//~ search := prefix
var n = _root;
var search = prefix;
//~ for {
while (true)
{
// Check for key exhaution
//~ if len(search) == 0 {
//~ recursiveWalk(n, fn)
//~ return
//~ }
if (search.Length == 0)
{
RecursiveWalk(n, fn);
return;
}
// Look for an edge
//~ n = n.getEdge(search[0])
//~ if n == nil {
//~ break
//~ }
n = n.GetEdge(search[0]);
if (n == null)
break;
// Consume the search prefix
//~ if strings.HasPrefix(search, n.prefix) {
//~ search = search[len(n.prefix):]
//~
//~ } else if strings.HasPrefix(n.prefix, search) {
//~ // Child may be under our search prefix
//~ recursiveWalk(n, fn)
//~ return
//~ } else {
//~ break
//~ }
if (search.StartsWith(n._prefix))
{
search = search.Substring(n._prefix.Length);
}
else if (n._prefix.StartsWith(search))
{
RecursiveWalk(n, fn);
return;
}
else
{
break;
}
}
}
// WalkPath is used to walk the tree, but only visiting nodes
// from the root down to a given leaf. Where WalkPrefix walks
// all the entries *under* the given prefix, this walks the
// entries *above* the given prefix.
//~ func (t *Tree) WalkPath(path string, fn WalkFn) {
public void WalkPath(string path, Walker<TValue> fn)
{
//~ n := t.root
//~ search := path
var n = _root;
var search = path;
//~ for {
while (true)
{
// Visit the leaf values if any
//~ if n.leaf != nil && fn(n.leaf.key, n.leaf.val) {
//~ return
//~ }
if (n._leaf != null && fn(n._leaf._key, n._leaf._value))
return;
// Check for key exhaution
//~ if len(search) == 0 {
//~ return
//~ }
if (search.Length == 0)
return;
// Look for an edge
//~ n = n.getEdge(search[0])
//~ if n == nil {
//~ return
//~ }
n = n.GetEdge(search[0]);
if (n == null)
return;
// Consume the search prefix
//~ if strings.HasPrefix(search, n.prefix) {
//~ search = search[len(n.prefix):]
//~ } else {
//~ break
//~ }
if (search.StartsWith(n._prefix))
search = search.Substring(n._prefix.Length);
else
break;
}
}
// recursiveWalk is used to do a pre-order walk of a node
// recursively. Returns true if the walk should be aborted
//~ func recursiveWalk(n *node, fn WalkFn) bool {
bool RecursiveWalk(Node<TValue> n, Walker<TValue> fn)
{
// Visit the leaf values if any
//~ if n.leaf != nil && fn(n.leaf.key, n.leaf.val) {
//~ return true
//~ }
if (n._leaf != null && fn(n._leaf._key, n._leaf._value))
return true;
// Recurse on the children
//~ for _, e := range n.edges {
//~ if recursiveWalk(e.node, fn) {
//~ return true
//~ }
//~ }
foreach (var e in n._edges)
{
if (RecursiveWalk(e._node, fn))
return true;
}
//~return false
return false;
}
// ToMap is used to walk the tree and convert it into a map
//! func (t *Tree) ToMap() map[string]interface{} {
public IDictionary<string, TValue> ToMap()
{
//! out := make(map[string]interface{}, t.size)
var @out = new Dictionary<string, TValue>(_size);
//~ t.Walk(func(k string, v interface{}) bool {
//~ out[k] = v
//~ return false
//~ })
this.Walk((k, v) =>
{
@out[k] = v;
return false;
});
//~ return out
return @out;
}
public void Print(Stream s)
{
using (var sw = new StreamWriter(s))
{
this._root.Print(sw, "");
}
}
}
}