I need to do some look-up operations against a collection of items.
First I need to see if there is a direct match. This is quite simple as I have the entries in a Dictionary<String,MyObjectType>
, so I can just go dictionary["valuetofind"]
.
If however there is no direct match, then I need to do a starts-with match, but it has to be the longest match that is returned:
Record Examples:
String Record
0 A
01 B
012 D
02 B
03 C
Query examples:
Query Result
0 A - Because 0 is the longest match
01 B - Because 01 is the longest match
023456 B - Because 02 is the longest match
012 D - Because 012 is the longest match
0123456 D - Because 012 is the longest match
03456 C - Because 03 is the longest match
04 A - Because 0 is the longest match
0456 A - Because 0 is the longest match
1 Null - No Match
Are there classes in the framework that have hashes or tree structures in the background implementation for doing something like this, or am I needing to write something myself?
EDIT What I have so far is the list sorted by length of the pattern string and then I go over the entries one by one to see if the query starts with the record. This works OK for most of the situations as we do not have large lists (yet), but does have an expensive cost for the situations where there is no match.
I lack the vocabulary to get google to give me pages not relating to hash-sets, lists and dictionaries. All the research I found points at tree based structures, but none point out if there is already an implementation in the .NET Framework or not.