tags:

views:

34

answers:

1

I'm traversing a text file line by line adjusting the text so that the file eventually will match the syntax required to be run on a MIPS simulator (MARS). My issue is that whenever I see a line with the string "blezl" in it, I want to do several things. First I need to insert some lines of text following the line containing this word. That's easy enough using insert. I then have to insert some lines of text after the line that originally followed the found line. The problem is, I need to then search the entire document for a string that was at the end of the found string and then insert a few lines before any lines that contain the 2nd string found. So..

  #1 blezl v0,#10
  #2 addu s1,s0,s5
  #3 lw v1,0(s8)
  ...
  #10 addu s1,s0,s5

i need to find "blezl", and then I insert some lines between "#1" and "#2". Then I insert some lines between "#2" and "#3", and then I need to search the entire document for "#10" and when I find any, I insert some lines before it. The problem is that the last step requires that I search the entire document (in the middle of traversing line by line until blezl was found). This is because "#10" could occur anywhere before or after "#1". This is going to take forever (there are 80k lines in my file and probably about 2% of them have "blezl" in them.) How can I do this without the massive redundant step?

A: 

80k lines isn't large enough that you can't load it into RAM. Searches are fairly fast once you have data in ram.

If you are concerned about performance, you could make a b-tree to store lines based on label. This would give you log(n) search time for each line you need to find.

Adam Shiemke
I'm thinking maybe fill a map using the first characters as a key and the iterator as the data element. Then I can do a quick search if I need to while traversing line by line.
Dan Snyder
Sure, that would give the same performance and probably be easier to implement.
Adam Shiemke