Skip to content

Commit

Permalink
2.0.1版本
Browse files Browse the repository at this point in the history
  • Loading branch information
toolgood committed Jan 22, 2018
1 parent 43ab500 commit c73794e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
33 changes: 17 additions & 16 deletions ToolGood.Words/TextSearch/StringSearchEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class TrieNode
internal List<int> Results;
internal Dictionary<char, TrieNode> m_values;
internal Dictionary<char, TrieNode> 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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

}


Expand Down
35 changes: 18 additions & 17 deletions ToolGood.Words/TextSearch/WordsSearchEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class TrieNode
internal List<int> Results;
internal Dictionary<char, TrieNode> m_values;
internal Dictionary<char, TrieNode> 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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

}


Expand Down
1 change: 1 addition & 0 deletions ToolGood.Words/ToolGood.Words.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<RootNamespace>ToolGood.Words</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<FileVersion>2.0.1.0</FileVersion>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
Expand Down

0 comments on commit c73794e

Please sign in to comment.