X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..1a8d0bce39fe4edf7443bde884fefe78412fe521:/bsearchtree.pas diff --git a/bsearchtree.pas b/bsearchtree.pas old mode 100755 new mode 100644 index ad61751..9ec804c --- a/bsearchtree.pas +++ b/bsearchtree.pas @@ -17,38 +17,44 @@ 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; +//FNV-1a hash function +function makehash(s:ansistring):integer; const shifter=6; var a,b:integer; + h:longword; begin result := 0; b := length(s); + h := 216613626; for a := 1 to b do begin - result := (result shl shifter) xor byte(s[a]); + h := (h xor byte(s[a])) * 16777619; end; - result := (result xor result shr 16) and (hashtable_size-1); + result := h 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 +67,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 +87,7 @@ begin end; -function findtree(t:phashtable;s:string):pointer; +function findtree(t:phashtable;s:ansistring):pointer; var p:thashitem; hash:integer; @@ -98,4 +104,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.