1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
2 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
3 which is included in the package
\r
4 ----------------------------------------------------------------------------- }
\r
6 {actually a hashtable. it was a tree in earlier versions}
\r
15 hashtable_size=$4000;
\r
18 thashitem=class(tlinklist)
\r
23 thashtable=array[0..hashtable_size-1] of thashitem;
\r
24 phashtable=^thashtable;
\r
26 {adds "item" to the tree for name "s". the name must not exist (no checking done)}
\r
27 procedure addtree(t:phashtable;s:ansistring;item:pointer);
\r
29 {removes name "s" from the tree. the name must exist (no checking done)}
\r
30 procedure deltree(t:phashtable;s:ansistring);
\r
32 {returns the item pointer for s, or nil if not found}
\r
33 function findtree(t:phashtable;s:ansistring):pointer;
\r
35 {clear a hashtable, deallocating all used resources}
\r
36 procedure cleartree(t:phashtable);
\r
40 //FNV-1a hash function
\r
41 function makehash(s:ansistring):integer;
\r
51 for a := 1 to b do begin
\r
52 h := (h xor byte(s[a])) * 16777619;
\r
54 result := h and (hashtable_size-1);
\r
57 procedure addtree(t:phashtable;s:ansistring;item:pointer);
\r
62 hash := makehash(s);
\r
63 p := thashitem.create;
\r
67 linklistadd(tlinklist(t[hash]),tlinklist(p));
\r
70 procedure deltree(t:phashtable;s:ansistring);
\r
75 hash := makehash(s);
\r
78 while p <> nil do begin
\r
79 if p.s = s then begin
\r
83 p := thashitem(p.next);
\r
85 linklistdel(tlinklist(t[hash]),tlinklist(p2));
\r
90 function findtree(t:phashtable;s:ansistring):pointer;
\r
96 hash := makehash(s);
\r
98 while p <> nil do begin
\r
99 if p.s = s then begin
\r
103 p := thashitem(p.next);
\r
107 procedure cleartree(t:phashtable);
\r
112 for hash := 0 to hashtable_size-1 do begin
\r
114 while p <> nil do begin
\r
115 p2 := thashitem(p.next);
\r
116 linklistdel(tlinklist(t[hash]),tlinklist(p));
\r
118 p := thashitem(p2);
\r