我怎样才能加速这个循环呢?是否有在在一次更换多个方面的一类? [英] How can I speed this loop up? Is there a class for replacing multiple terms at at time?
问题描述
循环:
var pattern = _dict[key];
string before;
do
{
before = pattern;
foreach (var pair in _dict)
if (key != pair.Key)
pattern = pattern.Replace(string.Concat("{", pair.Key, "}"), string.Concat("(", pair.Value, ")"));
} while (pattern != before);
return pattern;
它只是重复上一串钥匙查找和替换。这本字典就是<字符串,字符串方式>
我可以看到2改进了这一点。
I can see 2 improvements to this.
- 每次我们
pattern.Replace
其从字符串的开始时间搜索再次。它会更好,如果当它击中第一个{
,它只是浏览键进行匹配(可能使用二进制搜索)的列表,然后更换合适之一。 - 的
模式!=前
位是我如何检查,如果任何迭代过程中被换下。如果pattern.Replace
函数返回的多少或任何替换实际发生,我不需要这个。
- Every time we do
pattern.Replace
it searches from the beginning of the string again. It would be better if when it hit the first{
, it would just look through the list of keys for a match (perhaps using a binary search), and then replace the appropriate one. - The
pattern != before
bit is how I check if anything was replaced during that iteration. If thepattern.Replace
function returned how many or if any replaces actually occured, I wouldn't need this.
不过......我真的不想写一个大的讨厌的事情类来实现这一切。这必须是一个相当常见的场景?是否有任何existng解决方案?
However... I don't really want to write a big nasty thing class to do all that. This must be a fairly common scenario? Are there any existng solutions?
谢谢。为埃利安消退和 ChrisWue
class FlexDict : IEnumerable<KeyValuePair<string,string>>
{
private Dictionary<string, string> _dict = new Dictionary<string, string>();
private static readonly Regex _re = new Regex(@"{([_a-z][_a-z0-9-]*)}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public void Add(string key, string pattern)
{
_dict[key] = pattern;
}
public string Expand(string pattern)
{
pattern = _re.Replace(pattern, match =>
{
string key = match.Groups[1].Value;
if (_dict.ContainsKey(key))
return "(" + Expand(_dict[key]) + ")";
return match.Value;
});
return pattern;
}
public string this[string key]
{
get { return Expand(_dict[key]); }
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
foreach (var p in _dict)
yield return new KeyValuePair<string,string>(p.Key, this[p.Key]);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
使用示例
Example Usage
class Program
{
static void Main(string[] args)
{
var flex = new FlexDict
{
{"h", @"[0-9a-f]"},
{"nonascii", @"[\200-\377]"},
{"unicode", @"\\{h}{1,6}(\r\n|[ \t\r\n\f])?"},
{"escape", @"{unicode}|\\[^\r\n\f0-9a-f]"},
{"nmstart", @"[_a-z]|{nonascii}|{escape}"},
{"nmchar", @"[_a-z0-9-]|{nonascii}|{escape}"},
{"string1", @"""([^\n\r\f\\""]|\\{nl}|{escape})*"""},
{"string2", @"'([^\n\r\f\\']|\\{nl}|{escape})*'"},
{"badstring1", @"""([^\n\r\f\\""]|\\{nl}|{escape})*\\?"},
{"badstring2", @"'([^\n\r\f\\']|\\{nl}|{escape})*\\?"},
{"badcomment1", @"/\*[^*]*\*+([^/*][^*]*\*+)*"},
{"badcomment2", @"/\*[^*]*(\*+[^/*][^*]*)*"},
{"baduri1", @"url\({w}([!#$%&*-\[\]-~]|{nonascii}|{escape})*{w}"},
{"baduri2", @"url\({w}{string}{w}"},
{"baduri3", @"url\({w}{badstring}"},
{"comment", @"/\*[^*]*\*+([^/*][^*]*\*+)*/"},
{"ident", @"-?{nmstart}{nmchar}*"},
{"name", @"{nmchar}+"},
{"num", @"[0-9]+|[0-9]*\.[0-9]+"},
{"string", @"{string1}|{string2}"},
{"badstring", @"{badstring1}|{badstring2}"},
{"badcomment", @"{badcomment1}|{badcomment2}"},
{"baduri", @"{baduri1}|{baduri2}|{baduri3}"},
{"url", @"([!#$%&*-~]|{nonascii}|{escape})*"},
{"s", @"[ \t\r\n\f]+"},
{"w", @"{s}?"},
{"nl", @"\n|\r\n|\r|\f"},
{"A", @"a|\\0{0,4}(41|61)(\r\n|[ \t\r\n\f])?"},
{"C", @"c|\\0{0,4}(43|63)(\r\n|[ \t\r\n\f])?"},
{"D", @"d|\\0{0,4}(44|64)(\r\n|[ \t\r\n\f])?"},
{"E", @"e|\\0{0,4}(45|65)(\r\n|[ \t\r\n\f])?"},
{"G", @"g|\\0{0,4}(47|67)(\r\n|[ \t\r\n\f])?|\\g"},
{"H", @"h|\\0{0,4}(48|68)(\r\n|[ \t\r\n\f])?|\\h"},
{"I", @"i|\\0{0,4}(49|69)(\r\n|[ \t\r\n\f])?|\\i"},
{"K", @"k|\\0{0,4}(4b|6b)(\r\n|[ \t\r\n\f])?|\\k"},
{"L", @"l|\\0{0,4}(4c|6c)(\r\n|[ \t\r\n\f])?|\\l"},
{"M", @"m|\\0{0,4}(4d|6d)(\r\n|[ \t\r\n\f])?|\\m"},
{"N", @"n|\\0{0,4}(4e|6e)(\r\n|[ \t\r\n\f])?|\\n"},
{"O", @"o|\\0{0,4}(4f|6f)(\r\n|[ \t\r\n\f])?|\\o"},
{"P", @"p|\\0{0,4}(50|70)(\r\n|[ \t\r\n\f])?|\\p"},
{"R", @"r|\\0{0,4}(52|72)(\r\n|[ \t\r\n\f])?|\\r"},
{"S", @"s|\\0{0,4}(53|73)(\r\n|[ \t\r\n\f])?|\\s"},
{"T", @"t|\\0{0,4}(54|74)(\r\n|[ \t\r\n\f])?|\\t"},
{"U", @"u|\\0{0,4}(55|75)(\r\n|[ \t\r\n\f])?|\\u"},
{"X", @"x|\\0{0,4}(58|78)(\r\n|[ \t\r\n\f])?|\\x"},
{"Z", @"z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z"},
{"Z", @"z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z"},
{"CDO", @"<!--"},
{"CDC", @"-->"},
{"INCLUDES", @"~="},
{"DASHMATCH", @"\|="},
{"STRING", @"{string}"},
{"BAD_STRING", @"{badstring}"},
{"IDENT", @"{ident}"},
{"HASH", @"#{name}"},
{"IMPORT_SYM", @"@{I}{M}{P}{O}{R}{T}"},
{"PAGE_SYM", @"@{P}{A}{G}{E}"},
{"MEDIA_SYM", @"@{M}{E}{D}{I}{A}"},
{"CHARSET_SYM", @"@charset\b"},
{"IMPORTANT_SYM", @"!({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T}"},
{"EMS", @"{num}{E}{M}"},
{"EXS", @"{num}{E}{X}"},
{"LENGTH", @"{num}({P}{X}|{C}{M}|{M}{M}|{I}{N}|{P}{T}|{P}{C})"},
{"ANGLE", @"{num}({D}{E}{G}|{R}{A}{D}|{G}{R}{A}{D})"},
{"TIME", @"{num}({M}{S}|{S})"},
{"PERCENTAGE", @"{num}%"},
{"NUMBER", @"{num}"},
{"URI", @"{U}{R}{L}\({w}{string}{w}\)|{U}{R}{L}\({w}{url}{w}\)"},
{"BAD_URI", @"{baduri}"},
{"FUNCTION", @"{ident}\("},
};
var testStrings = new[] { @"""str""", @"'str'", "5", "5.", "5.0", "a", "alpha", "url(hello)",
"url(\"hello\")", "url(\"blah)", @"\g", @"/*comment*/", @"/**/", @"<!--", @"-->", @"~=",
"|=", @"#hash", "@import", "@page", "@media", "@charset", "!/*iehack*/important"};
foreach (var pair in flex)
{
Console.WriteLine("{0}\n\t{1}\n", pair.Key, pair.Value);
}
var sw = Stopwatch.StartNew();
foreach (var str in testStrings)
{
Console.WriteLine("{0} matches: ", str);
foreach (var pair in flex)
{
if (Regex.IsMatch(str, "^(" + pair.Value + ")$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
Console.WriteLine(" {0}", pair.Key);
}
}
Console.WriteLine("\nRan in {0} ms", sw.ElapsedMilliseconds);
Console.ReadLine();
}
}
目的
有关建设一个可以扩展的海誓山盟复杂的正则表达式。也就是说,我试图实现 CSS规范。
推荐答案
我认为这将是更快,如果你找任何出现 {富}使用
正则表达式,然后使用 MatchEvaluator 它取代了 {富}
如果富
恰好是在字典中的关键。
I think it would be faster if you look for any occurrences of {foo}
using a regular expression, and then use a MatchEvaluator that replaces the {foo}
if foo
happens to be a key in the dictionary.
我目前在这里没有视觉工作室,但我想这是你的代码的例子功能相同:
I have currently no visual studio here, but I guess this is functionally equivalent with your code example:
var pattern = _dict[key];
bool isChanged = false;
do
{
isChanged = false;
pattern = Regex.Replace(pattern, "{([^}]+)}", match => {
string matchKey = match.Groups[1].Value;
if (matchKey != key && _dict.ContainsKey(matchKey))
{
isChanged = true;
return "(" + _dict[matchKey] + ")";
}
return match.Value;
});
} while (isChanged);
我可以问你为什么你需要的do / while循环?在字典中键的值可以再次包含 {占位符}
已更换?你可以肯定你不要停留在一个无限循环,关键A
包含Blahblah {B}
和键b
包含Blahblah {A}
?
Can I ask you why you need the do/while loop? Can the value of a key in the dictionary again contain {placeholders}
that have to be replaced? Can you be sure you don't get stuck in an infinite loop where key "A"
contains "Blahblah {B}"
and key "B"
contains "Blahblah {A}"
?
编辑:进一步改善将是:
further improvements would be:
- Using a precompiled Regex.
- Using recursion instead of a loop (see ChrisWue's comment).
- Using
_dict.TryGetValue()
, as in Guffa's code.
您将最终获得一个O(n)的算法,其中n为输出的大小,所以你不能这样做比这更好
You will end up with an O(n) algorithm where n is the size of the output, so you can't do much better than this.
这篇关于我怎样才能加速这个循环呢?是否有在在一次更换多个方面的一类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!