How can I speed this loop up? Is there a class for replacing multiple terms at at time?

时光总嘲笑我的痴心妄想 提交于 2020-01-22 19:50:27

问题


The loop:

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;

It just does a repeated find-and-replace on a bunch of keys. The dictionary is just <string,string>.

I can see 2 improvements to this.

  1. 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.
  2. The pattern != before bit is how I check if anything was replaced during that iteration. If the pattern.Replace function returned how many or if any replaces actually occured, I wouldn't need this.

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?


Full Class

Thanks to Elian Ebbing and 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();
    }
}

Purpose

For building complex regular expressions that may extend eachother. Namely, I'm trying to implement the css spec.


回答1:


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);

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}"?

Edit: 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.

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.




回答2:


You should be able to use a regular expression to find the matches. Then you can also make use of the fast lookup of the dictionary and not just use it as a list.

var pattern = _dict[key];
bool replaced = false;
do {
  pattern = Regex.Replace(pattern, @"\{([^\}]+)\}", m => {
    string k = m.Groups[1].Value;
    string value;
    if (k != key && _dict.TryGetValue(k, out value) {
      replaced = true;
      return "(" + value + ")";
    } else {
      return "{" + k + "}";
    }
  });
} while (replaced);
return pattern;



回答3:


You can implement the following algorithm:

  1. Search for { in source string
  2. Copy everything upto { to StringBuilder
  3. Find matching } (the search is done from last fond position)
  4. Compare value between { and } to keys in your dictionary
    • If it matches copy to String builder ( + Value + )
    • Else copy from source string
  5. If source string end is not reached go to step 1



回答4:


Could you use PLINQ at all?

Something along the lines of:

var keys = dict.KeyCollection.Where(k => k != key);
bool replacementMade = keys.Any();

foreach(var k in keys.AsParallel(), () => {replacement code})


来源:https://stackoverflow.com/questions/5364309/how-can-i-speed-this-loop-up-is-there-a-class-for-replacing-multiple-terms-at-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!