Skip to content

Commit e951e56

Browse files
Lexeyandrewvk
authored andcommitted
Implemented StartingWith method
1 parent 4519e15 commit e951e56

File tree

5 files changed

+224
-105
lines changed

5 files changed

+224
-105
lines changed

Experimental/src/Collections/Suffix.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ public struct Suffix
88
{
99
/// <summary>Buffer of all added strings</summary>
1010
private readonly string _buffer;
11-
/// <summary>Offset to the beginning of the suffix in the buffer</summary>
12-
private readonly int _offset;
11+
/// <summary>Offset to the beginning of the source string in the buffer</summary>
12+
private readonly int _sourceOffset;
1313

1414
/// <summary>Constructs a new suffix</summary>
1515
/// <param name="buffer">Buffer with all added strings</param>
1616
/// <param name="sourceIndex">Source string index</param>
17-
/// <param name="offset">Offset of the suffix in the buffer</param>
17+
/// <param name="sourceOffset">Offset of the source string inside the buffer</param>
18+
/// <param name="offset">Offset of the suffix from the sourceOffset</param>
1819
/// <param name="length">Length of the suffix</param>
19-
internal Suffix([NotNull] string buffer, int sourceIndex, int offset, int length)
20+
internal Suffix([NotNull] string buffer, int sourceIndex, int sourceOffset, int offset, int length)
2021
{
2122
DebugCode.NotNull(buffer, nameof(buffer));
2223
DebugCode.ValidIndex(sourceIndex, nameof(sourceIndex));
23-
DebugCode.ValidIndexAndCount(offset, nameof(offset), length, nameof(length), buffer.Length);
24+
DebugCode.ValidIndex(sourceOffset, nameof(sourceOffset), buffer.Length);
25+
DebugCode.ValidIndexAndCount(sourceOffset + offset, nameof(offset), length, nameof(length), buffer.Length);
2426
_buffer = buffer;
2527
SourceIndex = sourceIndex;
26-
_offset = offset;
28+
_sourceOffset = sourceOffset;
29+
Offset = offset;
2730
Length = length;
2831
}
2932

@@ -32,10 +35,12 @@ internal Suffix([NotNull] string buffer, int sourceIndex, int offset, int length
3235
/// <remarks>0 - for the first added string, 1 - for the second, etc</remarks>
3336
/// </summary>
3437
public int SourceIndex { get; }
38+
/// <summary>The offset of the suffix from the beginning of the source string</summary>
39+
public int Offset { get; }
3540
/// <summary>The length of the suffix</summary>
3641
public int Length { get; }
3742
/// <summary>The suffix value</summary>
38-
public string Value => _buffer.Substring(_offset, Length);
43+
public string Value => _buffer.Substring(_sourceOffset + Offset, Length);
3944

4045
/// <summary>String conversion operator</summary>
4146
/// <param name="suffix">The suffix to convert</param>

Experimental/src/Collections/SuffixTree.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ public class SuffixTree : SuffixTreeBase
2121

2222
/// <summary>Links between nodes</summary>
2323
private Lazy<List<int>> _nodeLinks;
24-
/// <summary>Comparer for nodes against a char</summary>
25-
private Func<int, char, int> _childComparer;
2624
// state: (activeNode_, activeChild_, activeLength_), pending_
2725
/// <summary>Index of the branch node</summary>
2826
private int _branchNodeIndex;
2927
/// <summary>Index of the active edge (child node) of the branch node</summary>
30-
private int _activeEdgeIndex ;
28+
private int _activeEdgeIndex;
3129
/// <summary>The length of the current part of the active child</summary>
3230
private int activeLength_;
3331
/// <summary>Offset of the first suffix to insert</summary>
@@ -61,7 +59,6 @@ private void ResetLinks() => _nodeLinks =
6159
protected override void BuildFor(int begin, int end)
6260
{
6361
Code.AssertState(begin < end, "Invalid parameters passed");
64-
_childComparer = GetComparer();
6562
_branchNodeIndex = RootNodeIndex;
6663
_activeEdgeIndex = InvalidNodeIndex;
6764
activeLength_ = 0;
@@ -93,7 +90,6 @@ protected override void BuildFor(int begin, int end)
9390
}
9491
UpdateActiveEdgeAndCurentPosition();
9592
}
96-
_childComparer = null;
9793
}
9894

9995
/// <summary>Finds the next branching point</summary>
@@ -113,10 +109,10 @@ private void FindBranchingPoint()
113109
childNodeIndex = InvalidNodeIndex;
114110
activeEdge = default(Node);
115111
}
116-
for(;;)
112+
for (;;)
117113
{
118114
if (_activeEdgeIndex == InvalidNodeIndex)
119-
{
115+
{
120116
DebugCode.AssertState(activeLength_ == 0, "Invalid active state");
121117
if (_currentOffset == _end)
122118
{
@@ -128,7 +124,7 @@ private void FindBranchingPoint()
128124
return;
129125
}
130126
var c = InternalData[_currentOffset];
131-
var childIndex = children.LowerBound(c, _childComparer);
127+
var childIndex = children.LowerBound(c, EdgeComparer);
132128
if (childIndex == children.Count)
133129
{
134130
// a new branch
@@ -219,7 +215,7 @@ private void UpdateActiveEdgeAndCurentPosition()
219215
DebugCode.AssertState(!branchNode.IsLeaf, "Invalid active state");
220216
var index = _currentOffset - activeLength_;
221217
var children = branchNode.Children;
222-
var childIndex = children.LowerBound(InternalData[index], _childComparer);
218+
var childIndex = children.LowerBound(InternalData[index], EdgeComparer);
223219
DebugCode.AssertState(childIndex != children.Count, "Invalid active state");
224220
var edgeIndex = children[childIndex];
225221
var edgeNode = GetNode(edgeIndex);
@@ -295,7 +291,7 @@ private void InsertSuffix()
295291
{
296292
childNodeIndex = _currentOffset == _end
297293
? 0 // empty nodes always at the beginning
298-
: children.LowerBound(InternalData[_currentOffset], _childComparer);
294+
: children.LowerBound(InternalData[_currentOffset], EdgeComparer);
299295
}
300296
// now we have a non-empty children and an insertion index
301297
// just do an insert
@@ -328,7 +324,7 @@ protected override void PrintNodeText(StringBuilder sb, int nodeIndex)
328324
{
329325
var n = GetNode(nodeIndex);
330326
var nodeLink = _nodeLinks.IsValueCreated ? _nodeLinks.Value[nodeIndex] : InvalidNodeIndex;
331-
var linkText = nodeLink != InvalidNodeIndex ? $" -> {nodeLink}" : string.Empty;
327+
var linkText = nodeLink != InvalidNodeIndex ? $" -> {nodeLink}" : string.Empty;
332328
sb.AppendLine($"({nodeIndex}{linkText}, [{n.Begin}-{n.End}), {InternalData.Substring(n.Begin, n.End - n.Begin)})");
333329
}
334330
}

0 commit comments

Comments
 (0)