From c73794ef62afb3e0094019a16c310d6b94e2e355 Mon Sep 17 00:00:00 2001 From: toolgood Date: Mon, 22 Jan 2018 22:39:52 +0800 Subject: [PATCH] =?UTF-8?q?2.0.1=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ToolGood.Words/TextSearch/StringSearchEx.cs | 33 +++++++++---------- ToolGood.Words/TextSearch/WordsSearchEx.cs | 35 +++++++++++---------- ToolGood.Words/ToolGood.Words.csproj | 1 + 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/ToolGood.Words/TextSearch/StringSearchEx.cs b/ToolGood.Words/TextSearch/StringSearchEx.cs index 1d493cb..3e52836 100644 --- a/ToolGood.Words/TextSearch/StringSearchEx.cs +++ b/ToolGood.Words/TextSearch/StringSearchEx.cs @@ -20,8 +20,8 @@ class TrieNode internal List Results; internal Dictionary m_values; internal Dictionary merge_values; - private uint minflag = uint.MaxValue; - private uint maxflag = uint.MinValue; + private int minflag = int.MaxValue; + private int maxflag = 0; internal int Next; private int Count; @@ -34,7 +34,7 @@ public TrieNode() public bool TryGetValue(char c, out TrieNode node) { - if (minflag <= (uint)c && maxflag >= (uint)c) { + if (minflag <= (int)c && maxflag >= (int)c) { return m_values.TryGetValue(c, out node); } node = null; @@ -111,47 +111,51 @@ public int GetMaxLength() public int Rank(TrieNode[] has) { bool[] seats = new bool[has.Length]; - int maxCount = 1; int start = 1; has[0] = this; - Rank(ref maxCount, ref start, seats, has); + Rank(ref start, seats, has); + int maxCount = has.Length - 1; + while (has[maxCount] == null) { maxCount--; } return maxCount; } - private void Rank(ref int maxCount, ref int start, bool[] seats, TrieNode[] has) + private void Rank(ref int start, bool[] seats, TrieNode[] has) { if (maxflag == 0) return; var keys = m_values.Select(q => (int)q.Key).ToList(); keys.AddRange(merge_values.Select(q => (int)q.Key).ToList()); while (has[start] != null) { start++; } - for (int i = start; i < has.Length; i++) { + var s = start < (int)minflag ? (int)minflag : start; + + for (int i = s; i < has.Length; i++) { if (has[i] == null) { var next = i - (int)minflag; - if (next < 0) continue; + //if (next < 0) continue; if (seats[next]) continue; var isok = true; foreach (var item in keys) { - if (has[i - minflag + item] != null) { isok = false; break; } + if (has[next + item] != null) { isok = false; break; } } if (isok) { - SetSeats(next, ref maxCount, seats, has); + SetSeats(next, seats, has); break; } } } + start += keys.Count; var keys2 = m_values.OrderByDescending(q => q.Value.Count).ThenByDescending(q => q.Value.maxflag - q.Value.minflag); foreach (var key in keys2) { - key.Value.Rank(ref maxCount, ref start, seats, has); + key.Value.Rank(ref start, seats, has); } } - private void SetSeats(int next, ref int maxCount, bool[] seats, TrieNode[] has) + private void SetSeats(int next, bool[] seats, TrieNode[] has) { Next = next; seats[next] = true; @@ -165,10 +169,7 @@ private void SetSeats(int next, ref int maxCount, bool[] seats, TrieNode[] has) var position = next + item.Key; has[position] = item.Value; } - var position2 = next + (int)maxflag; - if (maxCount <= position2) { - maxCount = position2; - } + } diff --git a/ToolGood.Words/TextSearch/WordsSearchEx.cs b/ToolGood.Words/TextSearch/WordsSearchEx.cs index 1c1b587..dfb4dae 100644 --- a/ToolGood.Words/TextSearch/WordsSearchEx.cs +++ b/ToolGood.Words/TextSearch/WordsSearchEx.cs @@ -20,8 +20,8 @@ class TrieNode internal List Results; internal Dictionary m_values; internal Dictionary merge_values; - private uint minflag = uint.MaxValue; - private uint maxflag = uint.MinValue; + private int minflag = int.MaxValue; + private int maxflag = 0; internal int Next; private int Count; @@ -34,7 +34,7 @@ public TrieNode() public bool TryGetValue(char c, out TrieNode node) { - if (minflag <= (uint)c && maxflag >= (uint)c) { + if (minflag <= (int)c && maxflag >= (int)c) { return m_values.TryGetValue(c, out node); } node = null; @@ -111,47 +111,51 @@ public int GetMaxLength() public int Rank(TrieNode[] has) { bool[] seats = new bool[has.Length]; - int maxCount = 1; int start = 1; has[0] = this; - Rank(ref maxCount, ref start, seats, has); + Rank(ref start, seats, has); + int maxCount = has.Length - 1; + while (has[maxCount] == null) { maxCount--; } return maxCount; } - private void Rank(ref int maxCount, ref int start, bool[] seats, TrieNode[] has) + private void Rank(ref int start, bool[] seats, TrieNode[] has) { if (maxflag == 0) return; var keys = m_values.Select(q => (int)q.Key).ToList(); keys.AddRange(merge_values.Select(q => (int)q.Key).ToList()); while (has[start] != null) { start++; } - for (int i = start; i < has.Length; i++) { + var s = start < (int)minflag ? (int)minflag : start; + + for (int i = s; i < has.Length; i++) { if (has[i] == null) { var next = i - (int)minflag; - if (next < 0) continue; + //if (next < 0) continue; if (seats[next]) continue; var isok = true; foreach (var item in keys) { - if (has[i - minflag + item] != null) { isok = false; break; } + if (has[next + item] != null) { isok = false; break; } } if (isok) { - SetSeats(next, ref maxCount, seats, has); + SetSeats(next, seats, has); break; } } } + start += keys.Count; var keys2 = m_values.OrderByDescending(q => q.Value.Count).ThenByDescending(q => q.Value.maxflag - q.Value.minflag); foreach (var key in keys2) { - key.Value.Rank(ref maxCount, ref start, seats, has); + key.Value.Rank(ref start, seats, has); } } - - private void SetSeats(int next, ref int maxCount, bool[] seats, TrieNode[] has) + + private void SetSeats(int next, bool[] seats, TrieNode[] has) { Next = next; seats[next] = true; @@ -165,10 +169,7 @@ private void SetSeats(int next, ref int maxCount, bool[] seats, TrieNode[] has) var position = next + item.Key; has[position] = item.Value; } - var position2 = next + (int)maxflag; - if (maxCount <= position2) { - maxCount = position2; - } + } diff --git a/ToolGood.Words/ToolGood.Words.csproj b/ToolGood.Words/ToolGood.Words.csproj index 65d7bb9..9b89a5b 100644 --- a/ToolGood.Words/ToolGood.Words.csproj +++ b/ToolGood.Words/ToolGood.Words.csproj @@ -24,6 +24,7 @@ ToolGood.Words true true + 2.0.1.0