RE: Sequence Slicing (Was RE: Tough ciphers?)
- Posted by bensler at mail.com Mar 15, 2002
- 418 views
This is fast and works on any slice of a sequence. Sanity checks could be added. function range_find(object x, sequence s, integer pos1, integer pos2) atom found found = 0 while not found and pos1 <= pos2 do found = equal(x,s[pos1]) * pos1 pos1 +=1 end while return found end function This is the test that I used. I wasn't concerned with accuracy, because the routine beats find() by a long shot. <TEST> include file.e integer fn,fl fn = open("words.txt","rb") fl = seek(fn,-1) fl = where(fn) if seek(fn,0) then end if sequence temp temp = repeat(0,fl) for i = 1 to fl do temp[i] = getc(fn) end for close(fn) function range_find(object x, sequence s, integer pos1, integer pos2) atom found found = 0 while not found and pos1 <= pos2 do found = equal(x,s[pos1]) * pos1 pos1 +=1 end while return found end function atom t t=time() integer count count = 0 integer f,found f=0 found=find('\n',temp) while found != 0 do count +=1 f = found+1 found = range_find('\n',temp,f,length(temp)) end while ? f ? count ? time()-t while get_key()=-1 do end while <END TEST> The control test used: found = find('\n',temp[f..length(temp)] instead of: found = range_find('\n',temp,f,length(temp)) I didn't try with shorter sequences. Chris C. K. Lester wrote: > bensler at mail.com wrote: > > The problem that Derek is pointing out, is that you still > > have to slice your sequence to do consecutive finds. > > I guess what he's suggesting is "give us an optimized find on a slice," > right? > > Rob, why not?! :) > >