X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/e27ef2c6aab3a2a8051314bd37bb3b2386775f36..69598fec083b67c8567293c7b8a397b64175bd45:/bsearchtree.pas?ds=inline diff --git a/bsearchtree.pas b/bsearchtree.pas index d9e3ea2..249a6ff 100644 --- a/bsearchtree.pas +++ b/bsearchtree.pas @@ -7,6 +7,9 @@ unit bsearchtree; +{$ifdef fpc} + {$mode delphi} +{$endif} interface uses blinklist; @@ -32,20 +35,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 +107,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.