Find sequence in IEnumerable using Linq

后端 未结 5 1387
醉酒成梦
醉酒成梦 2020-12-03 23:01

What is the most efficient way to find a sequence within a IEnumerable using LINQ

I want to be able to create an extension method which allows

5条回答
  •  被撕碎了的回忆
    2020-12-03 23:34

    The code you say you want to be able to use isn't LINQ, so I don't see why it need be implemented with LINQ.

    This is essentially the same problem as substring searching (indeed, an enumeration where order is significant is a generalisation of "string").

    Since computer science has considered this problem frequently for a long time, so you get to stand on the shoulders of giants.

    Some reasonable starting points are:

    http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

    http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm

    http://en.wikipedia.org/wiki/Rabin-karp

    Even just the pseudocode in the wikipedia articles is enough to port to C# quite easily. Look at the descriptions of performance in different cases and decide which cases are most likely to be encountered by your code.

提交回复
热议问题