X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/055fa6bf18e0733d1bf2f97075d6bb33c76e72b5..1272fe8df1c3cc91ea478e2d2d44a0ec4c80f57b:/bsearchtree.pas?ds=sidebyside diff --git a/bsearchtree.pas b/bsearchtree.pas index ad61751..9dc355e 100644 --- a/bsearchtree.pas +++ b/bsearchtree.pas @@ -17,24 +17,27 @@ const type thashitem=class(tlinklist) hash:integer; - s:string; + s:ansistring; p:pointer; end; thashtable=array[0..hashtable_size-1] of thashitem; phashtable=^thashtable; {adds "item" to the tree for name "s". the name must not exist (no checking done)} -procedure addtree(t:phashtable;s:string;item:pointer); +procedure addtree(t:phashtable;s:ansistring;item:pointer); {removes name "s" from the tree. the name must exist (no checking done)} -procedure deltree(t:phashtable;s:string); +procedure deltree(t:phashtable;s:ansistring); {returns the item pointer for s, or nil if not found} -function findtree(t:phashtable;s:string):pointer; +function findtree(t:phashtable;s:ansistring):pointer; + +{clear a hashtable, deallocating all used resources} +procedure cleartree(t:phashtable); implementation -function makehash(s:string):integer; +function makehash(s:ansistring):integer; const shifter=6; var @@ -48,7 +51,7 @@ begin result := (result xor result shr 16) and (hashtable_size-1); end; -procedure addtree(t:phashtable;s:string;item:pointer); +procedure addtree(t:phashtable;s:ansistring;item:pointer); var hash:integer; p:thashitem; @@ -61,7 +64,7 @@ begin linklistadd(tlinklist(t[hash]),tlinklist(p)); end; -procedure deltree(t:phashtable;s:string); +procedure deltree(t:phashtable;s:ansistring); var p,p2:thashitem; hash:integer; @@ -81,7 +84,7 @@ begin end; -function findtree(t:phashtable;s:string):pointer; +function findtree(t:phashtable;s:ansistring):pointer; var p:thashitem; hash:integer; @@ -98,4 +101,20 @@ begin end; end; +procedure cleartree(t:phashtable); +var + hash:integer; + p,p2:thashitem; +begin + for hash := 0 to hashtable_size-1 do begin + p := t[hash]; + while p <> nil do begin + p2 := thashitem(p.next); + linklistdel(tlinklist(t[hash]),tlinklist(p)); + p.destroy; + p := thashitem(p2); + end; + end; +end; + end.