Skip to content

Commit

Permalink
Again use the previous GetHashCode implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Feb 12, 2025
1 parent 9371fd0 commit 2fb3893
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 62 deletions.
11 changes: 0 additions & 11 deletions Megrez/src/0_CSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,5 @@ protected virtual void ThrowIfDisposed() {
throw new ObjectDisposedException(nameof(HybridPriorityQueue<T>));
}
}

public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + _storage.GetHashCode();
hash = hash * 23 + _count.GetHashCode();
hash = hash * 23 + _isReversed.GetHashCode();
hash = hash * 23 + _usingArray.GetHashCode();
return hash;
}
}
}
} // namespace Megrez
16 changes: 5 additions & 11 deletions Megrez/src/1_Compositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,11 @@ public override bool Equals(object obj) {
/// </summary>
/// <returns></returns>
public override int GetHashCode() {
unchecked { // 使用 unchecked 來允許溢位操作
int hash = 17; // 使用質數作為基礎值
hash = hash * 23 + Cursor.GetHashCode();
hash = hash * 23 + Marker.GetHashCode();
hash = hash * 23 + MaxSpanLength.GetHashCode();
hash = hash * 23 + (Separator?.GetHashCode() ?? 0);
hash = hash * 23 + Keys.GetHashCode();
hash = hash * 23 + Spans.GetHashCode();
hash = hash * 23 + WalkedNodes.GetHashCode();
return hash;
}
int[] x = {
Cursor.GetHashCode(), Marker.GetHashCode(), MaxSpanLength.GetHashCode(), (Separator.GetHashCode()),
Keys.GetHashCode(), Spans.GetHashCode(), WalkedNodes.GetHashCode(),
};
return x.GetHashCode();
}
}
} // namespace Megrez
8 changes: 2 additions & 6 deletions Megrez/src/2_Walker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ public bool Equals(SearchState? other) {
public override bool Equals(object? obj) => Equals(obj as SearchState);

public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + Node.GetHashCode();
hash = hash * 23 + Position.GetHashCode();
return hash;
}
int[] x = { Node.GetHashCode(), Position.GetHashCode() };
return x.GetHashCode();
}
}

Expand Down
11 changes: 2 additions & 9 deletions Megrez/src/3_KeyValuePaired.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,8 @@ public override bool Equals(object obj) {
/// 做為預設雜湊函式。
/// </summary>
/// <returns>目前物件的雜湊碼。</returns>
public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + KeyArray.GetHashCode();
hash = hash * 23 + Value.GetHashCode();
hash = hash * 23 + Score.GetHashCode();
return hash;
}
}
public override int GetHashCode() =>
new KeyValuePair<List<string>, Unigram>(KeyArray, new(Value, Score)).GetHashCode();
/// <summary>
/// 傳回代表目前物件的字串。
/// </summary>
Expand Down
14 changes: 4 additions & 10 deletions Megrez/src/5_Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,10 @@ public override bool Equals(object obj) {
/// </summary>
/// <returns>目前物件的雜湊碼。</returns>
public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + OverridingScore.GetHashCode();
hash = hash * 23 + KeyArray.GetHashCode();
hash = hash * 23 + SpanLength.GetHashCode();
hash = hash * 23 + Unigrams.GetHashCode();
hash = hash * 23 + CurrentUnigramIndex.GetHashCode();
hash = hash * 23 + CurrentOverrideType.GetHashCode();
return hash;
}
int[] x = { OverridingScore.GetHashCode(), KeyArray.GetHashCode(),
SpanLength.GetHashCode(), Unigrams.GetHashCode(),
CurrentUnigramIndex.GetHashCode(), CurrentOverrideType.GetHashCode() };
return x.GetHashCode();
}

// MARK: - Dynamic Variables
Expand Down
7 changes: 0 additions & 7 deletions Megrez/src/6_LangModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ public List<Unigram> UnigramsFor(List<string> keyArray) =>
TheLangModel.UnigramsFor(keyArray).StableSorted((x, y) => y.Score.CompareTo(x.Score));
/// <inheritdoc />
public bool HasUnigramsFor(List<string> keyArray) => TheLangModel.HasUnigramsFor(keyArray);
public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + TheLangModel.GetHashCode();
return hash;
}
}
}
}
} // namespace Megrez
9 changes: 1 addition & 8 deletions Megrez/src/7_Unigram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ public override bool Equals(object obj) => obj is Unigram unigram && Value == un
/// 做為預設雜湊函式。
/// </summary>
/// <returns>目前物件的雜湊碼。</returns>
public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + Value.GetHashCode();
hash = hash * 23 + Score.GetHashCode();
return hash;
}
}
public override int GetHashCode() => new KeyValuePair<string, double>(Value, Score).GetHashCode();
/// <summary>
/// 傳回代表目前物件的字串。
/// </summary>
Expand Down

0 comments on commit 2fb3893

Please sign in to comment.