Skip to content

Commit

Permalink
略微优化了分割字符类的方法。
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJB committed Apr 16, 2013
1 parent a547da9 commit d6a2731
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions Cyjb.Compiler/Lexer/CharClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,19 @@ public HashSet<int> GetCharClass(string charClass)
// 不包含任何字符类。
return result;
}
CharSet setClone = new CharSet();
setClone.UnionWith(set);
for (int i = 0; i < cnt; i++)
CharSet setClone = new CharSet(set);
for (int i = 0; i < cnt && set.Count > 0; i++)
{
CharSet cc = charClassList[i];
setClone.ExceptWith(cc);
if (setClone.Count == set.Count)
set.ExceptWith(cc);
if (set.Count == setClone.Count)
{
// 当前字符类与 set 没有重叠。
continue;
}
// 得到当前字符类与 set 重叠的部分。
set.ExceptWith(setClone);
if (set.Count == cc.Count)
setClone.ExceptWith(set);
if (setClone.Count == cc.Count)
{
// 完全被当前字符类包含,直接添加。
result.Add(i);
Expand All @@ -94,11 +93,11 @@ public HashSet<int> GetCharClass(string charClass)
else
{
// 从当前的字符类中剔除被分割的部分。
cc.ExceptWith(set);
cc.ExceptWith(setClone);
// 更新字符类。
int newCC = charClassList.Count;
result.Add(newCC);
charClassList.Add(set);
charClassList.Add(setClone);
if (set.Count == 1)
{
charClassRecord.Add(null);
Expand All @@ -118,14 +117,8 @@ public HashSet<int> GetCharClass(string charClass)
charClassRecord[newCC].Add(tmpSet);
}
}
if (setClone.Count == 0)
{
break;
}
// 更新 set。
set = setClone;
setClone = new CharSet();
setClone.UnionWith(set);
// 重新复制 set。
setClone = new CharSet(set);
}
return result;
}
Expand Down

0 comments on commit d6a2731

Please sign in to comment.