Skip to content

Commit

Permalink
Turn SpanUnit.MaxLength into a dynamic variable. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Mar 7, 2023
1 parent 0855802 commit 03f469e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 58 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.3</ReleaseVersion>
<ReleaseVersion>2.5.4</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions Megrez.Tests/MegrezTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void Test01_SpanUnitInternalAbilities() {
Compositor.Node n3 = new(keyArray: new() { "gao1", "ke1", "ji4" }, spanLength: 3,
unigrams: langModel.UnigramsFor(new() { "gao1ke1ji4" }));
Assert.AreEqual(actual: span.MaxLength, expected: 0);
span.Add(node: n1);
span.Append(node: n1);
Assert.AreEqual(actual: span.MaxLength, expected: 1);
span.Add(node: n3);
span.Append(node: n3);
Assert.AreEqual(actual: span.MaxLength, expected: 3);
Assert.AreEqual(actual: span.NodeOf(length: 1), expected: n1);
Assert.AreEqual(actual: span.NodeOf(length: 2), expected: null);
Expand All @@ -33,8 +33,8 @@ public void Test01_SpanUnitInternalAbilities() {
Assert.AreEqual(actual: span.NodeOf(length: 2), expected: null);
Assert.AreEqual(actual: span.NodeOf(length: 3), expected: null);

span.Add(node: n1);
span.Add(node: n3);
span.Append(node: n1);
span.Append(node: n3);
span.DropNodesOfOrBeyond(length: 2);
Assert.AreEqual(actual: span.MaxLength, expected: 1);
Assert.AreEqual(actual: span.NodeOf(length: 1), expected: n1);
Expand All @@ -44,7 +44,7 @@ public void Test01_SpanUnitInternalAbilities() {
Assert.AreEqual(actual: span.MaxLength, expected: 0);
Assert.AreEqual(actual: span.NodeOf(length: 1), expected: null);
Compositor.Node n114514 = new(new(), 114_514, new());
Assert.IsFalse(span.Add(n114514));
Assert.IsFalse(span.Append(n114514));
Assert.IsNull(span.NodeOf(length: 0));
Assert.IsNull(span.NodeOf(length: Compositor.MaxSpanLength + 1));
}
Expand Down
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.3
version = 2.5.4
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.3</ReleaseVersion>
<ReleaseVersion>2.5.4</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.3</AssemblyVersion>
<FileVersion>2.5.3</FileVersion>
<Version>2.5.3</Version>
<AssemblyVersion>2.5.4</AssemblyVersion>
<FileVersion>2.5.4</FileVersion>
<Version>2.5.4</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 @@ -430,7 +430,7 @@ public int Update(bool updateExisting = false) {
}
List<Unigram> unigramsB = TheLangModel.UnigramsFor(joinedKeyArray);
if (unigramsB.IsEmpty()) continue;
Spans[position].Add(new(joinedKeyArray, theLength, unigramsB));
Spans[position].Append(new(joinedKeyArray, theLength, unigramsB));
nodesChanged += 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Megrez/src/2_Walker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial struct Compositor {
List<List<Vertex>> vertexSpans = new();
foreach (SpanUnit _ in Spans) vertexSpans.Add(new());
foreach ((int i, SpanUnit span) in Spans.Enumerated()) {
foreach (int j in new BRange(1, span.MaxLength + 1)) {
foreach (int j in new BRange(1, Math.Max(1, span.MaxLength) + 1)) {
if (span.NodeOf(j) is not {} theNode) continue;
vertexSpans[i].Add(new(theNode));
}
Expand Down
59 changes: 18 additions & 41 deletions Megrez/src/4_SpanUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class SpanUnit {
/// <summary>
/// 節點陣列。每個位置上的節點可能是 null。
/// </summary>
public List<Node?> Nodes = new();
public Dictionary<int, Node> Nodes = new();
/// <summary>
/// 該幅位單元內的所有節點當中持有最長幅位的節點長度。
/// 該變數受該幅位的自身操作函式而被動更新。
/// </summary>
public int MaxLength { get; private set; }
public int MaxLength => Nodes.Keys.Count > 0? Nodes.Keys.Max() : 0;
/// <summary>
/// 該幅位單元內的節點的幅位長度上限。
/// </summary>
Expand All @@ -30,17 +30,22 @@ public class SpanUnit {
/// <summary>
/// 幅位乃指一組共享起點的節點。
/// </summary>
public SpanUnit() {
Clear(); // 該函式會自動給 MaxLength 賦值。
}
public SpanUnit() => Clear(); // 該函式會自動給 MaxLength 賦值。

/// <summary>
/// 清除該幅位單元內的全部的節點,且重設最長節點長度為 0,然後再在節點陣列內預留空位。
/// </summary>
public void Clear() {
Nodes.Clear();
foreach (int _ in new BRange(0, MaxSpanLength)) Nodes.Add(null);
MaxLength = 0;
public void Clear() => Nodes.Clear();

/// <summary>
/// 往該幅位塞入一個節點。
/// </summary>
/// <param name="node">要塞入的節點。</param>
/// <returns>該操作是否成功執行。</returns>
public bool Append(Node node) {
if (!AllowedLengths.Contains(node.SpanLength)) return false;
Nodes[node.SpanLength] = node;
return true;
}

/// <summary>
Expand All @@ -52,26 +57,7 @@ public void Clear() {
/// 於是就提供了這個專門的工具函式。
/// </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>
/// <param name="node">要塞入的節點。</param>
/// <returns>該操作是否成功執行。</returns>
public bool Add(Node node) {
if (!AllowedLengths.Contains(node.SpanLength)) return false;
Nodes[node.SpanLength - 1] = node;
MaxLength = Math.Max(MaxLength, node.SpanLength);
return true;
}
public void Nullify(Node givenNode) => Nodes.Remove(givenNode.SpanLength);

/// <summary>
/// 丟掉任何不小於給定幅位長度的節點。
Expand All @@ -80,18 +66,9 @@ public bool Add(Node node) {
/// <returns>該操作是否成功執行。</returns>
public bool DropNodesOfOrBeyond(int length) {
if (!AllowedLengths.Contains(length)) return false;
length = Math.Min(length, MaxSpanLength);
foreach (int i in new BRange(length, MaxSpanLength + 1)) {
if (i > Nodes.Count) continue; // 防呆
Nodes[i - 1] = null;
}
MaxLength = 0;
if (length <= 1) return false;
int maxR = length - 2;
foreach (int i in new BRange(0, maxR + 1)) {
BRange countRange = new(0, maxR - i + 1);
if (!countRange.Contains(maxR - i)) continue; // 防呆
MaxLength = maxR - i + 1;
break;
Nodes.Remove(i);
}
return true;
}
Expand All @@ -101,7 +78,7 @@ public bool DropNodesOfOrBeyond(int length) {
/// </summary>
/// <param name="length">給定的幅位長度。</param>
/// <returns>查詢結果。</returns>
public Node? NodeOf(int length) => AllowedLengths.Contains(length) ? Nodes[length - 1] : null;
public Node? NodeOf(int length) => Nodes.ContainsKey(length) ? Nodes[length] : null;
}

// MARK: - Internal Implementations.
Expand Down
8 changes: 4 additions & 4 deletions Megrez/src/5_Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class Vertex {
/// 這是一個可變的數據結構,用於有向無環圖的構建和單源最短路徑的計算。
/// </summary>
/// <param name="node">字詞節點。</param>
public Vertex(Node node) { Node = node; }
public Vertex(Node node) => Node = node;

/// <summary>
/// 讓一個 Vertex 順藤摸瓜地將自己的所有的連帶的 Vertex 都摧毀,再摧毀自己。
Expand All @@ -62,7 +62,7 @@ public void Destroy() {
/// </summary>
/// <param name="u">參照頂點,會在必要時成為 v 的前述頂點。</param>
/// <param name="v">要影響的頂點。</param>
internal void Relax(Vertex u, ref Vertex v) {
private void Relax(Vertex u, ref Vertex v) {
// 從 u 到 w 的距離,也就是 v 的權重。
double w = v.Node.Score;
// 這裡計算最大權重:
Expand All @@ -76,7 +76,7 @@ internal void Relax(Vertex u, ref Vertex v) {
/// <summary>
/// 位相幾何排序處理時的處理狀態。
/// </summary>
internal class TopoSortState {
private class TopoSortState {
public int IterIndex { get; set; }
public Vertex Vertex { get; }
public TopoSortState(Vertex vertex, int iterIndex = 0) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public TopoSortState(Vertex vertex, int iterIndex = 0) {
/// </summary>
/// <param name="root">根頂點。</param>
/// <returns>排序結果(頂點陣列)。</returns>
internal List<Vertex> TopoSort(ref Vertex root) {
private List<Vertex> TopoSort(ref Vertex root) {
List<Vertex> result = new();
List<TopoSortState> stack = new();
stack.Add(new(root));
Expand Down

0 comments on commit 03f469e

Please sign in to comment.