The fastest way to select all rows from a list, starting with

I am looking for the fastest way to find all the rows in a collection, starting with a character set. I can use a sorted collection for this, however I cannot find a convenient way to do this in .net. Mostly I need to find low and high indexes in a collection that matches the criteria.

BinarySearch in the <T> list does not guarantee that the returned index refers to the first element, so you will need to iterate up and down to find all matching rows that are not fast if you have a large list.

There are also Linq methods (with parallel ones), but I'm not sure which data structure will provide the best results.

List example, ~ 10M entries:

aaaaaaaaaaaaaaabb
aaaaaaaaaaaaaaba
aaaaaaaaaaaaabc
...
zzzzzzzzzzzzzxx
zzzzzzzzzzzzzyzzz
zzzzzzzzzzzzzzzzzza

Search lines starting with: skk ...

Result: write indices from x to y.

UPDATE: .

+5
3

trie, .

Trie O(|S|) [ O(|S|logn)], node [ v], .

[] trie, , "" v. numberOfLeaves node, , [= ] node.

- v [ node u v - sum numberOfLeaves , u].

, - , , , , .

+4

- , , ; , .

- , 0, "abc", "abb" , "abc" . , BinarySearch , , ", abb abc".

( "abc" ), .

, , Unicode NULL, , :

// This could be done more efficiently :)
string stringJustBelow = target.Substring(0, target.Length - 1) +
                         target[target.Length - 1] + "X";
string stringJustAbove = target + "X"; // Or any character

int lowerBoundInclusive = ~list.BinarySearch(stringJustBelow);
int upperBoundExclusive = ~list.BinarySearch(stringJustAbove);

, 3 "abc", , "abbX" "abcX".

+4

SortedSet GetViewBetween.

, , , , , .

( ), GetViewBetween.

+2

All Articles