Skip to content

Commit

Permalink
Fix an issue that update() can ruin a span unit object. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Mar 7, 2023
1 parent 7c8d672 commit 0855802
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Megrez.Tests/Megrez.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ReleaseVersion>2.5.2</ReleaseVersion>
<ReleaseVersion>2.5.3</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 10 additions & 2 deletions Megrez.Tests/MegrezTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void Test01_SpanUnitInternalAbilities() {

[Test]
public void Test02_RankedLanguageModel() {
Compositor.LangModelRanked lmRanked = new Compositor.LangModelRanked(langModel: new TestLMForRanked());
LangModelProtocol lmTest = new TestLMForRanked();
Compositor.LangModelRanked lmRanked = new(langModel: ref lmTest);
Assert.IsTrue(lmRanked.HasUnigramsFor(new() { "foo" }));
Assert.IsFalse(lmRanked.HasUnigramsFor(new() { "bar" }));
Assert.IsEmpty(lmRanked.UnigramsFor(new() { "bar" }));
Expand Down Expand Up @@ -315,7 +316,7 @@ public void Test12_LongGridInsertion() {
[Test]
public void Test13_WalkerBenchMark() {
Console.WriteLine("// Stress test preparation begins.");
Compositor compositor = new Compositor(langModel: new SimpleLM(input: StrStressData));
Compositor compositor = new(langModel: new SimpleLM(input: StrStressData));
foreach (int _ in new BRange(0, 1919)) compositor.InsertKey("yi1");
Console.WriteLine("// Stress test preparation started with keys inserted: " + compositor.Keys.Count);
DateTime startTime = DateTime.Now;
Expand Down Expand Up @@ -516,5 +517,12 @@ public void Test20_Compositor_updateUnigramData() {
string newResult = compositor.Walk().WalkedNodes.Values().Joined();
Assert.AreEqual(actual: new List<string> { oldResult, newResult },
expected: new List<string> { "年中獎金", "年終獎金" });
compositor.Cursor = 4;
compositor.DropKey(direction: Compositor.TypingDirection.ToRear);
compositor.DropKey(direction: Compositor.TypingDirection.ToRear);
theLM.Trim(key: "nian2zhong1", value: "年終");
compositor.Update(updateExisting: true);
string newResult2 = compositor.Walk().WalkedNodes.Values().Joined(separator: ",");
Assert.AreEqual(actual: newResult2, expected: "年,中");
}
}
2 changes: 1 addition & 1 deletion Megrez.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ Global
$0.DotNetNamingPolicy = $4
$4.DirectoryNamespaceAssociation = PrefixedHierarchical
$0.StandardHeader = $5
version = 2.5.2
version = 2.5.3
EndGlobalSection
EndGlobal
8 changes: 4 additions & 4 deletions Megrez/Megrez.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ReleaseVersion>2.5.2</ReleaseVersion>
<ReleaseVersion>2.5.3</ReleaseVersion>
<PackageId>vChewing.Megrez</PackageId>
<Authors>Shiki Suen</Authors>
<Company>Atelier Inmu</Company>
<Copyright>(c) 2022 and onwards The vChewing Project for Megrez-specific changes; (c) 2022 and onwards Lukhnos Liu for upstream contents.</Copyright>
<RepositoryUrl>https://github.com/ShikiSuen/MegrezNT</RepositoryUrl>
<NeutralLanguage>zh-TW</NeutralLanguage>
<AssemblyVersion>2.5.2</AssemblyVersion>
<FileVersion>2.5.2</FileVersion>
<Version>2.5.2</Version>
<AssemblyVersion>2.5.3</AssemblyVersion>
<FileVersion>2.5.3</FileVersion>
<Version>2.5.3</Version>
<Product>Megrez</Product>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion Megrez/src/1_Compositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public int Update(bool updateExisting = false) {
List<Unigram> unigramsA = TheLangModel.UnigramsFor(joinedKeyArray);
if (unigramsA.IsEmpty()) {
if (theNode.KeyArray.Count == 1) continue;
Spans[position].Nodes.RemoveAll(x => Equals(x, theNode));
Spans[position].Nullify(givenNode: theNode);
} else {
theNode.SyncingUnigramsFrom(unigramsA);
}
Expand Down
18 changes: 18 additions & 0 deletions Megrez/src/4_SpanUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public void Clear() {
MaxLength = 0;
}

/// <summary>
/// 丟掉任何與給定節點完全雷同的節點。
/// </summary>
/// <remarks>
/// Swift 不像 C# 那樣有容量鎖定型陣列,
/// 對某個位置的內容的刪除行為都可能會導致其它內容錯位、繼發其它不可知故障。
/// 於是就提供了這個專門的工具函式。
/// </remarks>
/// <param name="givenNode">要參照的節點。</param>
public void Nullify(Node givenNode) {
BRange theRange = new(lowerbound: 0, upperbound: Nodes.Count);
foreach (int theIndex in theRange) {
Node? currentNode = Nodes[theIndex];
if (!Equals(currentNode, givenNode)) continue;
Nodes[theIndex] = null;
}
}

/// <summary>
/// 往該幅位塞入一個節點。
/// </summary>
Expand Down

0 comments on commit 0855802

Please sign in to comment.