Delphi - THashedStringList incremental search -
i have thashedstringlist 200k items on it(strings , objects). list heavy used on searching items on it. in cases need incremental search on it. i've wrote rudimentary example incremental search using startstext routine
unit unit1; interface uses winapi.windows, winapi.messages, system.sysutils, system.variants, system.classes, vcl.graphics, vcl.controls, vcl.forms, vcl.dialogs, system.inifiles, vcl.stdctrls, system.strutils; type tform1 = class(tform) memo1: tmemo; edit1: tedit; memo2: tmemo; procedure formcreate(sender: tobject); procedure edit1keyup(sender: tobject; var key: word; shift: tshiftstate); private { private declarations } public ahsstrlst : thashedstringlist; { public declarations } end; var form1: tform1; implementation {$r *.dfm} function generate(cantidad: integer): string; const letras_mi = 'abcdefghijklmnopqrstuvwxyz'; var i: integer; begin result := ''; := 0 cantidad-1 result := result + letras_mi[random(length(letras_mi)) + 1]; end; procedure tform1.edit1keyup(sender: tobject; var key: word; shift: tshiftstate); var ipos: integer; begin memo2.clear; memo2.update; ipos := 0 ahsstrlst.count-1 if startstext(edit1.text,ahsstrlst[ipos]) memo2.lines.add(ahsstrlst[ipos]) end; procedure tform1.formcreate(sender: tobject); var ipos:integer; begin ahsstrlst := thashedstringlist.create; ipos := 0 50000 ahsstrlst.addobject(generate(4),tobject(ipos));//here there diffent type of objects ipos := 0 50000 ahsstrlst.addobject(generate(6),tobject(ipos)); ipos := 0 50000 ahsstrlst.addobject(generate(8),tobject(ipos)); ipos := 0 50000 ahsstrlst.addobject(generate(10),tobject(ipos)); memo1.lines.addstrings(ahsstrlst); end; end.
is there way speed incremental search?
for start, should stop using thashedstringlist
. once you've got delphi generics should use tdictionary<k,v>
instead. presents cleaner interface, , performs better. however, in case, demand partial matching renders hash based lookup helpless. need different data structure.
for efficient partial match lookup can consider maintaining ordered list. can perform lookup using bisection. because performing partial matches you'll have craft own bisection because facilities provided rtl search single items.
suppose searching 'abc'
. first of find largest value < 'abc'
. partial matches starts item follows. find largest value begins 'abc'
. partial matches end item. if no such item exists, there no matches.
Comments
Post a Comment