dictionary binary access
- Posted by Carlos Valdis <cvaldes at NCS.COM> Apr 21, 1998
- 781 views
Some 2 weeks ago there was a discussion on if it was possible to do a binary access to the dictionary file containing variable length records (words). Yes, it is possible using some little trick. I am sending the code sample. It can be posted in the archives if it is considered useful (if not optimum, for sure). Carlos Valdes <cvaldes at ncs.com> Mexico -- This program adds a first register to the words.txt file --(why? see next binary access program) and copies the rest -- You need to rename the output file, again to words.txt include file.e include graphics.e object s atom f1,f2 --back_color(blue) f1=open("words.txt","r") f2=open("words.tyt","w") puts(f2,"AAAAAAAAAA\n") while 1 do s=gets(f1) if atom(s) then exit end if puts(f2,s) end while puts(1,"END of file,FIN de archivo\n") close(f1) close(f2) -- This is the program to do the binary search into the dictionary --It calculates a new position, reads only part of the register where it is positioned --so you need to do a second access to get the whole next register. include get.e include sort.e include file.e include wildcard.e integer up,dn,md,tr,f1,x,cc object ret,da,db,dc da={} db={} dc={} f1=open("words.txt","r") while 1 do dn=1 ret=seek(f1,-1) if ret!=0 then abort(1) end if up=where(f1) md=floor((up-dn)/2) tr=0 puts(1,"\nkey-in word:") da=gets(0) if length(da)<2 then exit else da=upper(da[1..length(da)-1]) end if while 1 do printf(1,"\ndn=%d,md=%d,up=%d\n",{dn,md,up}) ret=seek(f1,md) if ret!=0 then exit end if db=gets(f1) db=gets(f1) db=db[1..length(db)-1] if atom(db) then exit end if x=compare(db,da) puts(1,da & ' ') puts(1,db & '\n') tr=tr+1 if x=0 then printf(1,"found in %d trials",tr) exit elsif x>0 then up=md elsif x<0 then dn=md end if x=compare(db,dc) if x=0 then printf(1,"not found in %d trials",tr) dc={} ret=seek(f1,1) while 1 do db=gets(f1) if atom(db) then exit else db=db[1..length(db)-1] x=compare(db,da) if x=0 then puts(1,"but it is there.......") elsif x>0 then puts(1,'\n' & dc & '-' & db & '\n') exit end if end if dc=db end while exit end if dc=db md=floor((up+dn)/2) end while end while close(f1) cc=wait_key()