This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
/
SuffixTree.cs
862 lines (787 loc) · 31.4 KB
/
SuffixTree.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
/* A suffix tree based on Ukkonen's algorithm for byte values. I followed the ideas based
* in this helpful article:
* http://stackoverflow.com/questions/9452701/ukkonens-suffix-tree-algorithm-in-plain-english/9513423#9513423
*
* The code written here is based on a C++ implementation given here:
* http://pastie.org/5925809
*
* A note on memory management:
* Try creating as few new suffix tree classes as possible. Every time a new node is created, it has to
* initialize an array of child nodes of size equal to the alphabet size (257). For large sets of data, this
* operation becomes expensive and time-consuming. To counter the performance hit, the SuffixTree class
* can be reused. The class keeps a list of every used node. When the class is reset to implement a new tree
* structure, it simply erases all of the nodes information and reference data without letting the class itself
* be erased from memory. This way, the next tree creation (if another one will be made) does not lose time
* on allocating new memory. The performance gains are very significant.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Helper
{
/// <summary>
/// Specifies a generalized suffix tree for <see cref="Byte"/> arrays.
/// </summary>
/// <remarks>
/// The suffix tree is generated using Ukkonen's algorithm.
/// </remarks>
/// <seealso cref="SubstringPointer"/>
public class SuffixTree
{
/// <summary>
/// A value that is outside of the standard <see cref="Byte"/> alphabet that signifies
/// the end of a suffix branch.
/// This field is constant.
/// </summary>
public const int TerminationValue = AlphabetSize;
/// <summary>
/// The total size of the alphabet, including <see cref="TerminationValue"/>.
/// This field is constant.
/// </summary>
public const int AlphabetSize = Byte.MaxValue + 1;
/// <summary>
/// Specifies an index that extends to <see cref="Position"/>.
/// This field is constant.
/// </summary>
private const int EndOfData = SubstringPointer.EndOfString;
/// <summary>
/// The default size of <see cref="Nodes"/>.
/// </summary>
private const int FallbakNodeCollectionSize = 0x1000;
/// <summary>
/// The <see cref="Node"/> containing the beginning of each suffix string.
/// </summary>
private RootNode Root
{
get;
set;
}
/// <summary>
/// Container for every <see cref="Node"/> created in <see cref="SuffixTree"/>.
/// Used for recycling purposes.
/// </summary>
private NodeCollection Nodes
{
get;
set;
}
/// <summary>
/// Current position in <see cref="Data"/>.
/// </summary>
private int Position
{
get;
set;
}
/// <summary>
/// The <see cref="Node"/> that <see cref="ActiveNode"/> links to.
/// </summary>
private Node ActiveLinkNode
{
get;
set;
}
/// <summary>
/// The number of <see cref="Node"/>s left to add.
/// </summary>
private int Remainder
{
get;
set;
}
/// <summary>
/// Is <see cref="Root"/> if we are doing a direct insertion.
/// Otherwise, it is the branch of a child node where a matching substring
/// pattern exists.
/// </summary>
private Node ActiveNode
{
get;
set;
}
/// <summary>
/// The data position of <see cref="ActiveNode"/>.
/// </summary>
private int ActivePosition
{
get;
set;
}
/// <summary>
/// Substring length at <see cref="ActivePosition"/>.
/// </summary>
private int ActiveLength
{
get;
set;
}
/// <summary>
/// A copy of all the data of the tree.
/// </summary>
private int[] Data
{
get;
set;
}
/// <summary>
/// Gets the value of the passed tree data at <paramref name="index"/>.
/// </summary>
/// <param name="index">
/// The zero-based index to read from.
/// </param>
/// <returns>
/// The value of <see cref="Data"/> at <paramref name="index"/>.
/// </returns>
public int this[int index] => Data[index];
/// <summary>
/// The size of the data passed to this <see cref="SuffixTree"/>, including
/// <see cref="TerminationValue"/>.
/// </summary>
public int Size
{
get;
private set;
}
/// <summary>
/// Initialize a new instance of the <see cref="SuffixTree"/> class.
/// </summary>
public SuffixTree()
{
//Create a large node collection now to save time in the future.
Nodes = new NodeCollection(this, FallbakNodeCollectionSize);
Root = new RootNode(this);
}
/// <summary>
/// Initializes the parameters of this <see cref="SuffixTree"/> with a given
/// data size.
/// </summary>
/// <param name="size">
/// The size of <see cref="Data"/> (excluding the termination value).
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="size"/> is less than zero.
/// </exception>
private void Initialize(int size)
{
if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size),
SR.ErrorLowerBoundExclusive(nameof(size), size, 0));
Nodes.Clear();
size++; // +1 for the termination value.
Data = new int[size];
Size = size;
Position = -1;
Remainder = 0;
ActiveLength = 0;
ActivePosition = 0;
Root.Reset();
ActiveNode = Root;
}
/// <summary>
/// Creates a suffix tree of <paramref name="data"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="Byte"/>s to create a suffix tree of.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is null.
/// </exception>
public void CreateTree(byte[] data)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
CreateTree(data, 0, data.Length);
}
/// <summary>
/// Creates a suffix tree of <paramref name="data"/> at <paramref name="start"/>,
/// with range specified by <paramref name="size"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="Byte"/>s to create a suffix tree of.
/// </param>
/// <param name="start">
/// The starting index to read data from.
/// </param>
/// <param name="size">
/// The number of bytes to read from <paramref name="data"/> after <paramref name="start"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is null.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> is less than zero. -or-
/// <paramref name="start"/> + <paramref name="size"/> is greater than length of
/// <paramref name="data"/>
/// </exception>
public void CreateTree(byte[] data, int start, int size)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
unsafe
{
fixed (byte* ptr = &data[start])
CreateTree(ptr, data.Length, start, size);
}
}
/// <summary>
/// Creates a suffix tree of <paramref name="data"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="Byte"/>s to create a suffix tree of.
/// </param>
/// <param name="length">
/// The size, in bytes, of <paramref name="data"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is null.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="length"/> is less than zero.
/// </exception>
public void CreateTree(IntPtr data, int length)
{
unsafe
{
CreateTree((byte*)data, length, 0, length);
}
}
/// <summary>
/// Creates a suffix tree of <paramref name="data"/> at <paramref name="start"/>,
/// with range specified by <paramref name="size"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="Byte"/>s to create a suffix tree of.
/// </param>
/// <param name="length">
/// The size, in bytes, of <paramref name="data"/>.
/// </param>
/// <param name="start">
/// The starting index to read data from.
/// </param>
/// <param name="size">
/// The number of bytes to read from <paramref name="data"/> after <paramref name="start"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is null.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> is less than zero. -or-
/// <paramref name="start"/> + <paramref name="size"/> is greater than <paramref name="length"/>.
/// </exception>
public void CreateTree(IntPtr data, int length, int start, int size)
{
unsafe
{
CreateTree((byte*)data, length, start, size);
}
}
/// <summary>
/// Creates a suffix tree of <paramref name="data"/> at <paramref name="start"/>,
/// with range specified by <paramref name="size"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="Byte"/>s to create a suffix tree of.
/// </param>
/// <param name="length">
/// The size, in bytes, of <paramref name="data"/>.
/// </param>
/// <param name="start">
/// The starting index to read data from.
/// </param>
/// <param name="size">
/// The number of bytes to read from <paramref name="data"/> after <paramref name="start"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is null.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> is less than zero. -or-
/// <paramref name="start"/> + <paramref name="size"/> is greater than <paramref name="length"/>.
/// </exception>
private unsafe void CreateTree(byte* data, int length, int start, int size)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (start < 0)
throw new ArgumentOutOfRangeException(nameof(start),
SR.ErrorLowerBoundExclusive(nameof(start), start, 0));
if (start + size > length)
throw new ArgumentOutOfRangeException(nameof(size),
SR.ErrorArrayRange(nameof(size), size, nameof(data), length, start));
Initialize(size);
data += start;
for (int i = 0; i < size; i++)
Add(data[i]);
Add(TerminationValue);
Position++;
}
/// <summary>
/// Adds <paramref name="value"/> to <see cref="Data"/> and updates the tree.
/// </summary>
/// <param name="value">
/// The value to add to <see cref="Data"/>.
/// </param>
private void Add(int value)
{
// Update position and write new value.
Data[++Position] = value;
// [?] Why do we do this?
ActiveLinkNode = null;
// [?] Is there a smarter way to go through this loop?
Remainder++;
do
{
// [?] Why do we do this?
if (ActiveLength == 0)
ActivePosition = Position;
// Get the active child node.
var activeValue = Data[ActivePosition];
var stem = ActiveNode[activeValue];
// Create a new child node if it does not currently exist
if (stem == null)
{
var leaf = Nodes.Add(Position);
ActiveNode[activeValue] = leaf;
AddLink(ActiveNode);
}
else
{
// Determine if current substring exceeds the size of the active child node's.
var edge = stem.Length;
if (ActiveLength >= edge)
{
ActivePosition += edge;
ActiveLength -= edge;
ActiveNode = stem;
continue;
}
// If the node and substring still match, update active point and terminate sequence.
if (Data[stem.Start + ActiveLength] == value)
{
ActiveLength++;
// If we are in an internal node needing a suffix link, we chain this node to those.
AddLink(ActiveNode);
break;
}
// Redefine active node as a branch. It will branch to the original substring and the new substring.
var branch = Nodes.Add(stem.Start, stem.Start + ActiveLength);
ActiveNode[activeValue] = branch;
var leaf = Nodes.Add(Position, EndOfData);
branch[value] = leaf;
stem.Start += ActiveLength;
branch[Data[stem.Start]] = stem;
AddLink(branch);
}
Remainder--;
if (ActiveNode == Root && ActiveLength > 0)
{
ActiveLength--;
ActivePosition = Position - Remainder + 1;
}
else
ActiveNode = ActiveNode.Link ?? Root;
} while (Remainder > 0);
}
/// <summary>
/// Add <paramref name="node"/> to the active link chain.
/// </summary>
/// <param name="node">
/// The <see cref="Node"/> to link to.
/// </param>
private void AddLink(Node node)
{
if (ActiveLinkNode != null)
ActiveLinkNode.Link = node;
ActiveLinkNode = node;
}
/// <summary>
/// Gets a <see cref="SubstringPointer"/> specifying the index of length of the longest
/// substring before <paramref name="index"/> that matches the substring starting at
/// <paramref name="index"/>.
/// </summary>
/// <param name="index">
/// The index within <see cref="Data"/> to look at.
/// </param>
/// <returns>
/// A <see cref="SubstringPointer"/> whose start position is less than
/// <paramref name="index"/> and also specifies a substring that matches
/// the substring starting at <paramref name="index"/> within <see cref="Data"/>.
/// It's length is specified by the returning <see cref="SubstringPointer"/>'s length and
/// is the longest such substring if more than one match was found. If no matching substring
/// was found, <see cref="SubstringPointer.Empty"/> is returned.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is less than 0. -or-
/// <paramref name="index"/> is greater than or equal to <see cref="Size"/>.
/// </exception>
public SubstringPointer GetLongestInternalSubstring(int index)
{
if (index < 0 || index >= Size)
throw new ArgumentOutOfRangeException(nameof(index),
SR.ErrorArrayBounds(nameof(index), index, Size));
unsafe
{
fixed (int* ptr = Data)
{
var node = (Node)Root;
var result = SubstringPointer.Empty;
for (int i = index, length = 0; i < Size;)
{
// update to current node.
int value = ptr[i];
node = node[value];
// If no node specifies the current value, then our substring has reached max.
if (node == null)
return result;
// if node goes to end of data, then this is the longest match.
if (node.End > index + length || node.End == -1)
return result;
// update status to current node's position.
i += node.Length;
length += node.Length;
result = SubstringPointer.FromLengthAndEnd(length, node.End);
}
return result;
}
}
}
/// <summary>
/// A class that contains the substring information of each node in a <see cref="SuffixTree"/>.
/// </summary>
[DebuggerDisplay("Start = {Start}, Length = {Length}")]
private class Node
{
/// <summary>
/// The size of <see cref="Children"/>.
/// </summary>
private const int AllocationSize = AlphabetSize + 1;
/// <summary>
/// The <see cref="SuffixTree"/> that owns this <see cref="Node"/>.
/// </summary>
public SuffixTree Tree
{
get;
internal set;
}
/// <summary>
/// Gets or sets the start index of the substring that describes this <see cref="Node"/>.
/// </summary>
public int Start
{
get;
set;
}
/// <summary>
/// Gets or sets the end index of the substring that describes this <see cref="Node"/>.
/// </summary>
public int End
{
get;
set;
}
/// <summary>
/// Gets the length of the substring that describes this <see cref="Node"/>.
/// </summary>
public int Length => (End == -1 ? Tree.Position : End) - Start; /// <summary>
/// Gets the <see cref="SubstringPointer"/> that describes this <see cref="Node"/>.
/// </summary>
public SubstringPointer SubstringPointer => SubstringPointer.FromStartAndLength(Start, Length);
/// <summary>
/// The <see cref="Node"/> that links to this <see cref="Node"/>
/// </summary>
/// <remarks>
/// Link nodes are necessary when we split off of a non-root node to help
/// us chain back to the root node.
/// </remarks>
public Node Link
{
get;
set;
}
/// <summary>
/// The children of this <see cref="Node"/>.
/// </summary>
/// <remarks>
/// This array has a constant size of <see cref="AllocationSize"/>, and is indexed by the
/// alphabet values. It is better to think of this array as a node dictionary.
/// </remarks>
private Node[] Children
{
get;
set;
}
/// <summary>
/// Collection of index of every active child node.
/// </summary>
/// <remarks>
/// When resetting the node, we remove the reference to every child
/// whose index is in this array. This saves a substantial number of
/// cycles as every node in <see cref="Children"/> is typically not
/// set.
/// </remarks>
private int[] Active
{
get;
set;
}
/// <summary>
/// The number of index values in <see cref="Active"/>.
/// </summary>
private int ActiveSize
{
get;
set;
}
/// <summary>
/// Gets or sets the <see cref="Node"/> whose substring described by this
/// <see cref="Node"/> has <paramref name="key"/> as its next value.
/// </summary>
/// <param name="key">
/// A value contained by the suffix tree alphabet.
/// </param>
/// <returns>
/// The <see cref="Node"/> whose substring described by this <see cref="Node"/> has
/// <paramref name="key"/> as its next value.
/// </returns>
public Node this[int key]
{
get => Children[key];
set
{
// If we are setting a non-null node to a child index that is currently
// null, then we need to update the active index array to include this new index.
if (value != null)
{
if (this[key] == null)
Active[ActiveSize++] = key;
}
Children[key] = value;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class with
/// <paramref name="tree"/> as its owner.
/// </summary>
/// <param name="tree">
/// The <see cref="SuffixTree"/> of the <see cref="Node"/>.
/// </param>
private Node(SuffixTree tree)
{
Debug.Assert(tree != null);
Tree = tree;
Active = new int[AllocationSize];
Children = new Node[AllocationSize];
}
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class with
/// <paramref name="tree"/> as its owner and with the specified
/// <paramref name="start"/> and <paramref name="end"/> values.
/// </summary>
/// <param name="tree">
/// The <see cref="SuffixTree"/> of the <see cref="Node"/>.
/// </param>
/// <param name="start">
/// The start index of the substring describing this <see cref="Node"/>.
/// </param>
/// <param name="end">
/// The end index of the substring describing this <see cref="Node"/>.
/// </param>
public Node(SuffixTree tree, int start, int end) : this(tree)
{
Start = start;
End = end;
}
/// <summary>
/// Resets the <see cref="Node"/> information with the given
/// <paramref name="start"/> and <paramref name="end"/> values.
/// </summary>
/// <param name="start">
/// The new value of <see cref="Start"/>.
/// </param>
/// <param name="end">
/// The new value of <see cref="End"/>.
/// </param>
/// <returns>
/// This <see cref="Node"/>.
/// </returns>
/// <remarks>
/// <see cref="Link"/> and every child in <see cref="Children"/> are
/// dereferenced.
/// </remarks>
public Node Reset(int start, int end)
{
Start = start;
End = end;
Link = null;
// Dereference every child node that we have set. A much smarter alternative
// than iterating through every single child and seeing if null.
for (int i = ActiveSize; --i >= 0;)
this[Active[i]] = null;
ActiveSize = 0;
return this;
}
/// <summary>
/// Converts this <see cref="Node"/> to a human-readable <see cref="String"/>.
/// </summary>
/// <returns>
/// A <see cref="String"/> the represent this <see cref="Node"/>.
/// </returns>
public override string ToString()
{
return SubstringPointer.ToString();
}
}
/// <summary>
/// Specifies the root <see cref="Node"/> of a <see cref="SuffixTree"/>.
/// </summary>
[DebuggerDisplay("Root")]
private class RootNode : Node
{
/// <summary>
/// Initializes a new instance of the <see cref="RootNode"/> class with
/// <paramref name="tree"/> as its owner.
/// </summary>
/// <param name="tree">
/// The <see cref="SuffixTree"/> of the <see cref="RootNode"/>.
/// </param>
public RootNode(SuffixTree tree) : base(tree, -1, -1)
{
}
// Remove public access to this method.
private new void Reset(int start, int end)
{
base.Reset(start, end);
}
/// <summary>
/// Dereferences <see cref="Node.Link"/> and children in <see cref="Node.Children"/>.
/// </summary>
public void Reset()
{
Reset(-1, -1);
}
/// <summary>
/// Converts this <see cref="RootNode"/> to a human-readable <see cref="String"/>.
/// </summary>
/// <returns>
/// A <see cref="String"/> the represent this <see cref="RootNode"/>.
/// </returns>
public override string ToString()
{
return "Root";
}
}
/// <summary>
/// Represents a collection of <see cref="Node"/>s that doesn't dispose of items
/// in its collection.
/// </summary>
/// <remarks>
/// <see cref="Node"/>s added to <see cref="NodeCollection"/> are not removed when calling
/// <see cref="Clear"/>(). This way, if <see cref="Add(Int32)"/> or <see cref="Add(Int32, Int32)"/>
/// is called, a previously "cleared" <see cref="Node"/> is reset with the specified parameters.
/// This saves time against instantiating a new <see cref="Node"/> due to its large alphabet size.
/// </remarks>
private class NodeCollection : List<Node>
{
/// <summary>
/// The <see cref="SuffixTree"/> that this <see cref="NodeCollection"/> belongs to.
/// </summary>
public SuffixTree Tree
{
get;
private set;
}
/// <summary>
/// Gets the number of elements that are accessible in <see cref="NodeCollection"/>.
/// </summary>
public new int Count
{
get;
private set;
}
/// <summary>
/// Initialize a new instance of the <see cref="NodeCollection"/> with the given
/// <see cref="SuffixTree"/> as its owner and has the specified initial capacity.
/// </summary>
/// <param name="tree">
/// The <see cref="SuffixTree"/> that will own this <see cref="NodeCollection"/>.
/// </param>
/// <param name="capacity">
/// The number of new elements that <see cref="NodeCollection"/> can initially store.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="tree"/> is null.
/// </exception>
/// <remarks>
/// When instantiated, <see cref="NodeCollection"/> will not be empty. Each element
/// up to <paramref name="capacity"/> will be initialized. This implies there is a heavy
/// initial cost, but with increased performance during use.
/// </remarks>
public NodeCollection(SuffixTree tree, int capacity) : base(capacity)
{
Tree = tree;
for (int i = capacity; --i >= 0;)
Add(-1);
Clear();
}
/// <summary>
/// Adds a <see cref="Node"/> with <see cref="Node.Start"/> set to
/// <paramref name="position"/> at the end of the <see cref="NodeCollection"/>.
/// </summary>
/// <param name="position">
/// The value <see cref="Node.Start"/> will have.
/// </param>
/// <returns>
/// The resulting <see cref="Node"/> in <see cref="NodeCollection"/>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="position"/> is less than 0.
/// </exception>
/// <remarks>
/// <see cref="Node.End"/> is set to <see cref="EndOfData"/>.
/// </remarks>
public Node Add(int position)
{
return Add(position, EndOfData);
}
/// <summary>
/// Adds a <see cref="Node"/> with <see cref="Node.Start"/> set to
/// <paramref name="start"/> and <see cref="Node.End"/> set to <paramref name="end"/>
/// at the end of the <see cref="NodeCollection"/>.
/// </summary>
/// <param name="start">
/// The value <see cref="Node.Start"/> will have.
/// </param>
/// <param name="end">
/// The values <see cref="Node.End"/> will have.
/// </param>
/// <returns>
/// The resulting <see cref="Node"/> in <see cref="NodeCollection"/>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> is less than 0.
/// </exception>
public Node Add(int start, int end)
{
// If a node already exists in the list, then we simply reset its value.
if (Count++ < base.Count)
return this[Count - 1].Reset(start, end);
// Otherwise, we create a new node and add it to the list.
var node = new Node(Tree, start, end);
Add(node);
return node;
}
/// <summary>
/// Updates <see cref="Count"/> to 0.
/// </summary>
/// <remarks>
/// The <see cref="Node"/>s are not actually removed from <see cref="NodeCollection"/>.
/// </remarks>
public new void Clear()
{
// Rather than removing every existing node from the list, we set count to zero
// and keep every node for future use.
Count = 0;
}
}
}
}