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 function makehash(s:ansistring):integer;
\r
48 for a := 1 to b do begin
\r
49 result := (result shl shifter) xor byte(s[a]);
\r
51 result := (result xor result shr 16) and (hashtable_size-1);
\r
54 procedure addtree(t:phashtable;s:ansistring;item:pointer);
\r
59 hash := makehash(s);
\r
60 p := thashitem.create;
\r
64 linklistadd(tlinklist(t[hash]),tlinklist(p));
\r
67 procedure deltree(t:phashtable;s:ansistring);
\r
72 hash := makehash(s);
\r
75 while p <> nil do begin
\r
76 if p.s = s then begin
\r
80 p := thashitem(p.next);
\r
82 linklistdel(tlinklist(t[hash]),tlinklist(p2));
\r
87 function findtree(t:phashtable;s:ansistring):pointer;
\r
93 hash := makehash(s);
\r
95 while p <> nil do begin
\r
96 if p.s = s then begin
\r
100 p := thashitem(p.next);
\r
104 procedure cleartree(t:phashtable);
\r
109 for hash := 0 to hashtable_size-1 do begin
\r
111 while p <> nil do begin
\r
112 p2 := thashitem(p.next);
\r
113 linklistdel(tlinklist(t[hash]),tlinklist(p));
\r
115 p := thashitem(p2);
\r