Skip to content

Commit 305227d

Browse files
committed
Add mutable operations to Levenshtrie
1 parent abfd01b commit 305227d

10 files changed

+1022
-595
lines changed

src/Levenshtypo.Generator/CSharpStateMachineGenerator.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ public bool MoveNext(Rune c, out State next)
169169
});
170170
171171
public int Distance => DistanceData[Math.Min({{maxDistance}}, _sRune.Length - _sIndex) * {{groupMap.Count}} + _state];
172-
173-
public int MinimumDistance => MinDistanceData[_state];
174172
}
175173
""");
176174

src/Levenshtypo.Tests/LevenshtrieCustomTraversalTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public OnlyGetNChars(int numLeft)
5959

6060
public int Distance => 0;
6161

62-
public int MinimumDistance => 0;
63-
6462
public bool MoveNext(Rune c, out OnlyGetNChars next)
6563
{
6664
var nextNumLeft = _numLeft - 1;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Shouldly;
2+
3+
namespace Levenshtypo.Tests;
4+
5+
public class LevenshtrieMutationTests
6+
{
7+
[Fact]
8+
public void GeneratedTests_Guids()
9+
{
10+
RunTest(Enumerable.Range(0, 100_000).Select(_ => Guid.NewGuid().ToString()));
11+
}
12+
13+
[Fact]
14+
public void GeneratedTests_Integers()
15+
{
16+
RunTest(Enumerable.Range(0, 100_000).Select(i => i.ToString()));
17+
}
18+
19+
[Fact]
20+
public void HandWrittenTests()
21+
{
22+
RunTest([
23+
"a",
24+
"abcde", // extend head
25+
"abcdefghi", // extend tail data
26+
"az", // branch off head
27+
"abz", // branch off tail data
28+
"abc", // stop at head
29+
"abcdefg", // stop at tail data
30+
"",
31+
"z" // branch off root
32+
]);
33+
}
34+
35+
36+
private void RunTest(IEnumerable<string> keys)
37+
{
38+
var trie = Levenshtrie.CreateEmpty<string>();
39+
40+
var addedKeys = new List<string>();
41+
42+
foreach (var key in keys)
43+
{
44+
addedKeys.Add(key);
45+
46+
trie.TryGetValue(key, out _).ShouldBeFalse();
47+
48+
trie.Add(key, key);
49+
50+
trie.TryGetValue(key, out var found).ShouldBeTrue();
51+
ReferenceEquals(key, found).ShouldBeTrue();
52+
}
53+
54+
foreach (var key in addedKeys)
55+
{
56+
trie.TryGetValue(key, out var found).ShouldBeTrue();
57+
ReferenceEquals(key, found).ShouldBeTrue();
58+
}
59+
60+
foreach (var key in addedKeys)
61+
{
62+
trie.Remove(key);
63+
64+
trie.TryGetValue(key, out var found).ShouldBeFalse();
65+
found.ShouldBeNull();
66+
}
67+
}
68+
69+
}

src/Levenshtypo.Tests/LevenshtrieTryGetValueTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ public class LevenshtrieTryGetValueTests
77
[Fact]
88
public void LargeDataSet()
99
{
10-
var enFrozenTries = new List<KeyValuePair<string, int>>();
11-
var nonEnFrozenTries = new List<KeyValuePair<string, int>>();
10+
var inKeys = new List<KeyValuePair<string, int>>();
11+
var outKeys = new List<KeyValuePair<string, int>>();
1212

1313
for (int i = 0; i < 10000; i++)
1414
{
1515
if (i % 17 == 0)
1616
{
17-
nonEnFrozenTries.Add(new KeyValuePair<string, int>(i.ToString(), i));
17+
outKeys.Add(new KeyValuePair<string, int>(i.ToString(), i));
1818
}
1919
else
2020
{
21-
enFrozenTries.Add(new KeyValuePair<string, int>(i.ToString(), i));
21+
inKeys.Add(new KeyValuePair<string, int>(i.ToString(), i));
2222
}
2323
}
2424

25-
var t = Levenshtrie<int>.Create(enFrozenTries);
26-
27-
foreach (var (key, value) in enFrozenTries)
25+
var t = Levenshtrie<int>.Create(inKeys);
26+
27+
foreach (var (key, value) in inKeys)
2828
{
2929
var found = t.TryGetValue(key, out var actual);
3030
found.ShouldBeTrue();
3131
actual.ShouldBe(value);
3232
}
3333

34-
foreach (var (key, value) in nonEnFrozenTries)
34+
foreach (var (key, value) in outKeys)
3535
{
3636
var found = t.TryGetValue(key, out var actual);
3737
found.ShouldBeFalse();

src/Levenshtypo/Distance0Levenshtomaton.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,5 @@ public bool MoveNext(Rune c, out State next)
7272
public bool IsFinal => _sIndex == _sRune.Length;
7373

7474
public int Distance => 0;
75-
76-
public int MinimumDistance => 0;
7775
}
7876
}

0 commit comments

Comments
 (0)