X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/e27ef2c6aab3a2a8051314bd37bb3b2386775f36..21410d99e1361e5474a47bb0bd0bc6e939f91b7d:/bsearchtree.pas diff --git a/bsearchtree.pas b/bsearchtree.pas index d9e3ea2..9ec804c 100644 --- a/bsearchtree.pas +++ b/bsearchtree.pas @@ -32,20 +32,26 @@ procedure deltree(t:phashtable;s:ansistring); {returns the item pointer for s, or nil if not found} function findtree(t:phashtable;s:ansistring):pointer; +{clear a hashtable, deallocating all used resources} +procedure cleartree(t:phashtable); + implementation +//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:ansistring;item:pointer); @@ -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.