Toby
Toby

Reputation: 592

Appropriate data structure for searching in strings (.net)

I kind of toil to describe my situation, therefore my post might geht al litle longer.

I want to search for given keys in strings. The strings are lines of a text file, the comparison is to be done as the file is beeing read line by line.

There is a class which has the properties NUMBER and TYPE among others. That are the keys for which is to be searched in the line string.

A trivial solution would be to store the class instances in a list and run through that list for each and every line and see if the line string contains the keys of the current list entry.

The performance of this implementation though would be horrible, since on the average for every line the program will loop through the entire list. This is since every key in the list occures at most one time in the file. So there are a lot of lines which don't contain a key.

I hope you guys get what I'm trying to explain and get the idea.

Example for objects:

O1:
  ID - 1
  NR - 1587
  TYPE - COMPUTER

O2:
  ID - 2
  NR - 5487
  TYPE - TV

text file lines:
  bla bla \t 8745 RADIO
  fsdakfjd9 9094km d9943
  dkjd894 4003p \t 5487 TV
  sdj99  43s39 kljljkljfsd 
  ...

On line 3 the program should find the match and save the ID 2 together with the line content.

Thanks for any input ... Toby

Upvotes: 1

Views: 232

Answers (2)

Davio
Davio

Reputation: 4737

Looking up strings in your file is intensive so ideally, you only want to do this once. I think it's ideal if you store class references in a Dictionary or a Hashtable.

Then you can do something like

var myDictionary = new Dictionary<string, ObjectType>();
while(string line = reader.ReadLine())
{
    // Parse the possible key out of the line
    if (myDictionary.ContainsKey(keyFromLine) doSomething(line, myDictionary[keyFromLine]);
}

void doSomething(string line, ObjectType instance)
{
  // Unwrap the line and store appropriate values
}

Upvotes: 1

iefpw
iefpw

Reputation: 7062

Splitting, counting within strings is by nature resource and time intensive. You need parsing and searching. You have to loop through all the strings and save it, and then search it using Dictionary<key, value>. Try to loop the fewest and the way to accomplish that is by running the program on all the lines and saving it first. Don't scan lines on every search.

Upvotes: 0

Related Questions