\r
unit bsearchtree;\r
\r
+{$ifdef fpc}\r
+ {$mode delphi}\r
+{$endif}\r
interface\r
\r
uses blinklist;\r
type\r
thashitem=class(tlinklist)\r
hash:integer;\r
- s:string;\r
+ s:ansistring;\r
p:pointer;\r
end;\r
thashtable=array[0..hashtable_size-1] of thashitem;\r
phashtable=^thashtable;\r
\r
{adds "item" to the tree for name "s". the name must not exist (no checking done)}\r
-procedure addtree(t:phashtable;s:string;item:pointer);\r
+procedure addtree(t:phashtable;s:ansistring;item:pointer);\r
\r
{removes name "s" from the tree. the name must exist (no checking done)}\r
-procedure deltree(t:phashtable;s:string);\r
+procedure deltree(t:phashtable;s:ansistring);\r
\r
{returns the item pointer for s, or nil if not found}\r
-function findtree(t:phashtable;s:string):pointer;\r
+function findtree(t:phashtable;s:ansistring):pointer;\r
+\r
+{clear a hashtable, deallocating all used resources}\r
+procedure cleartree(t:phashtable);\r
\r
implementation\r
\r
-function makehash(s:string):integer;\r
+//FNV-1a hash function\r
+function makehash(s:ansistring):integer;\r
const\r
shifter=6;\r
var\r
a,b:integer;\r
+ h:longword;\r
begin\r
result := 0;\r
b := length(s);\r
+ h := 216613626;\r
for a := 1 to b do begin\r
- result := (result shl shifter) xor byte(s[a]);\r
+ h := (h xor byte(s[a])) * 16777619;\r
end;\r
- result := (result xor result shr 16) and (hashtable_size-1);\r
+ result := h and (hashtable_size-1);\r
end;\r
\r
-procedure addtree(t:phashtable;s:string;item:pointer);\r
+procedure addtree(t:phashtable;s:ansistring;item:pointer);\r
var\r
hash:integer;\r
p:thashitem;\r
linklistadd(tlinklist(t[hash]),tlinklist(p));\r
end;\r
\r
-procedure deltree(t:phashtable;s:string);\r
+procedure deltree(t:phashtable;s:ansistring);\r
var\r
p,p2:thashitem;\r
hash:integer;\r
end;\r
\r
\r
-function findtree(t:phashtable;s:string):pointer;\r
+function findtree(t:phashtable;s:ansistring):pointer;\r
var\r
p:thashitem;\r
hash:integer;\r
end;\r
end;\r
\r
+procedure cleartree(t:phashtable);\r
+var\r
+ hash:integer;\r
+ p,p2:thashitem;\r
+begin\r
+ for hash := 0 to hashtable_size-1 do begin\r
+ p := t[hash];\r
+ while p <> nil do begin\r
+ p2 := thashitem(p.next);\r
+ linklistdel(tlinklist(t[hash]),tlinklist(p));\r
+ p.destroy;\r
+ p := thashitem(p2);\r
+ end;\r
+ end;\r
+end;\r
+\r
end.\r