-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSet.cs
88 lines (77 loc) · 1.92 KB
/
StringSet.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCS
{
readonly struct StringSet
{
public readonly int Index;
readonly DataSource DS;
public readonly String Source => DS.Strings[Index];
public readonly int Length => DS.Strings[Index].Length;
public readonly int End => Index + Length;
public readonly IEnumerable<byte> Data => DS.Data.Skip(Index).Take(Length);
public readonly IEnumerable<int> Starts
{
get
{
var source = DS.Strings[Index];
yield return source.Start;
var i = Array.BinarySearch(DS.ByStart, Index, DS.StartLengthIndexComparer); // byStart[i] == index
for (var j = i - 1; j > 0; j--)
{
var another = DS.ByStart[j];
if (DS.Strings[another].Start != source.Start) break;
yield return another;
}
yield return DS.ByStart[i];
for (var j = i + 1; j < DS.ByStart.Length; j++)
{
var another = DS.ByStart[j];
if (DS.Strings[another].Start != source.Start) break;
if (DS.Strings[another].Length < source.Length) break;
yield return another;
}
}
}
public readonly IEnumerable<StringView> Strings
{
get
{
var ds = DS;
var length = Length;
return Starts.Select(i => new StringView(i, length, ds));
}
}
public StringSet(int index, DataSource ds)
{
Index = index;
DS = ds ?? throw new ArgumentNullException(nameof(ds));
}
public override readonly string ToString()
{
var s = new StringBuilder();
s.Append(Utility.FormatRange(Index, Length));
if (DS.Data.Length != 0)
{
s.Append(' ');
s.Append('"');
s.Append(Utility.FormatData(DS.Data, Index, Length));
s.Append('"');
}
s.Append(' ');
s.Append('{');
var i = 0;
foreach (var start in Starts)
{
if (i > 4) break;
else i++;
s.Append(start.ToString("x"));
if (i + 1 < 4) s.Append(' ');
}
s.Append('}');
return s.ToString();
}
}
}