[gforth] / gforth / hash.fs  

gforth: gforth/hash.fs


1 : pazsan 1.1 \ Hashed dictionaries 15jul94py
2 :    
3 : anton 1.22 \ Copyright (C) 1995,1998 Free Software Foundation, Inc.
4 : anton 1.10
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 :    
21 : jwilke 1.19 [IFUNDEF] allocate
22 : jwilke 1.18 : reserve-mem here swap allot ;
23 :     \ move to a kernel/memory.fs
24 :     [ELSE]
25 :     : reserve-mem allocate throw ;
26 :     [THEN]
27 :    
28 :     [IFUNDEF] hashbits
29 : jwilke 1.19 11 Value hashbits
30 : jwilke 1.18 [THEN]
31 : anton 1.2 1 hashbits lshift Value Hashlen
32 : pazsan 1.1
33 : jwilke 1.18 \ compute hash key 15jul94py
34 :    
35 :     [IFUNDEF] hash
36 :     : hash ( addr len -- key )
37 :     hashbits (hashkey1) ;
38 :     [THEN]
39 :    
40 : pazsan 1.1 Variable insRule insRule on
41 : pazsan 1.4 Variable revealed
42 : pazsan 1.1
43 : pazsan 1.4 \ Memory handling 10oct94py
44 : pazsan 1.1
45 :     Variable HashPointer
46 : pazsan 1.4 Variable HashIndex
47 : anton 1.13 0 Value HashTable
48 : pazsan 1.1
49 : jwilke 1.18 \ forward declarations
50 :     0 Value hashsearch-map
51 : anton 1.23 Defer hash-alloc ( addr -- addr )
52 : jwilke 1.18
53 : pazsan 1.4 \ DelFix and NewFix are from bigFORTH 15jul94py
54 : pazsan 1.1
55 :     : DelFix ( addr root -- ) dup @ 2 pick ! ! ;
56 :     : NewFix ( root len # -- addr )
57 : jwilke 1.18 BEGIN 2 pick @ ?dup 0= WHILE 2dup * reserve-mem
58 : pazsan 1.1 over 0 ?DO dup 4 pick DelFix 2 pick + LOOP drop
59 :     REPEAT >r drop r@ @ rot ! r@ swap erase r> ;
60 :    
61 : anton 1.12 : bucket ( addr len wordlist -- bucket-addr )
62 :     \ @var{bucket-addr} is the address of a cell that points to the first
63 :     \ element in the list of the bucket for the string @var{addr len}
64 :     wordlist-extend @ -rot hash xor ( bucket# )
65 : anton 1.13 cells HashTable + ;
66 : anton 1.2
67 :     : hash-find ( addr len wordlist -- nfa / false )
68 : anton 1.12 >r 2dup r> bucket @ (hashfind) ;
69 : pazsan 1.1
70 :     \ hash vocabularies 16jul94py
71 :    
72 :     : lastlink! ( addr link -- )
73 :     BEGIN dup @ dup WHILE nip REPEAT drop ! ;
74 :    
75 : anton 1.14 : (reveal ( nfa wid -- )
76 : anton 1.12 over name>string rot bucket >r
77 :     HashPointer 2 Cells $400 NewFix
78 :     tuck cell+ ! r> insRule @
79 :     IF
80 :     dup @ 2 pick ! !
81 :     ELSE
82 :     lastlink!
83 :     THEN
84 :     revealed on ;
85 :    
86 : anton 1.14 : hash-reveal ( nfa wid -- )
87 :     2dup (reveal) (reveal ;
88 : pazsan 1.1
89 : jwilke 1.18 : inithash ( wid -- )
90 :     wordlist-extend
91 : pazsan 1.21 insRule @ >r insRule off hash-alloc 3 cells -
92 :     dup wordlist-id
93 : jwilke 1.18 BEGIN @ dup WHILE 2dup swap (reveal REPEAT
94 :     2drop r> insRule ! ;
95 :    
96 : pazsan 1.4 : addall ( -- )
97 :     voclink
98 : jwilke 1.18 BEGIN @ dup WHILE
99 :     dup 0 wordlist-link -
100 :     dup wordlist-map @ hashsearch-map =
101 :     IF inithash ELSE drop THEN
102 :     REPEAT drop ;
103 : pazsan 1.4
104 :     : clearhash ( -- )
105 : anton 1.13 HashTable Hashlen cells bounds
106 : pazsan 1.4 DO I @
107 : pazsan 1.15 BEGIN dup WHILE
108 : anton 1.23 dup @ swap HashPointer DelFix
109 :     REPEAT
110 :     I !
111 :     cell +LOOP
112 :     HashIndex off
113 : jwilke 1.18 voclink
114 : anton 1.23 BEGIN ( wordlist-link-addr )
115 :     @ dup
116 :     WHILE ( wordlist-link )
117 :     dup 0 wordlist-link - ( wordlist-link wid )
118 :     dup wordlist-map @ hashsearch-map =
119 :     IF ( wordlist-link wid )
120 :     0 swap wordlist-extend !
121 :     ELSE
122 :     drop
123 :     THEN
124 :     REPEAT
125 :     drop ;
126 : jwilke 1.18
127 :     : rehashall ( wid -- )
128 :     drop revealed @
129 :     IF clearhash addall revealed off
130 :     THEN ;
131 : pazsan 1.4
132 : jwilke 1.18 : (rehash) ( wid -- )
133 :     dup wordlist-extend @ 0=
134 :     IF inithash
135 :     ELSE rehashall THEN ;
136 :    
137 :     \ >rom ?!
138 :     align here ' hash-find A, ' hash-reveal A, ' (rehash) A, ' (rehash) A,
139 :     to hashsearch-map
140 : pazsan 1.4
141 :     \ hash allocate and vocabulary initialization 10oct94py
142 :    
143 : anton 1.23 :noname ( addr -- addr )
144 : jwilke 1.18 HashTable 0=
145 :     IF Hashlen cells reserve-mem TO HashTable
146 :     HashTable Hashlen cells erase THEN
147 : pazsan 1.4 HashIndex @ over ! 1 HashIndex +!
148 :     HashIndex @ Hashlen >=
149 : jwilke 1.19 [ [IFUNDEF] allocate ]
150 : jwilke 1.18 ABORT" no more space in hashtable"
151 :     [ [ELSE] ]
152 : pazsan 1.17 IF HashTable >r clearhash
153 : pazsan 1.4 1 hashbits 1+ dup to hashbits lshift to hashlen
154 : pazsan 1.17 r> free >r 0 to HashTable
155 :     addall r> throw
156 : jwilke 1.18 THEN
157 :     [ [THEN] ] ; is hash-alloc
158 : pazsan 1.1
159 :     \ Hash-Find 01jan93py
160 : jwilke 1.19 has? cross 0=
161 : jwilke 1.18 [IF]
162 : pazsan 1.16 : make-hash
163 : pazsan 1.21 hashsearch-map forth-wordlist wordlist-map !
164 : jwilke 1.18 addall ;
165 :     make-hash \ Baumsuche ist installiert.
166 :     [ELSE]
167 : pazsan 1.21 hashsearch-map forth-wordlist wordlist-map !
168 : jwilke 1.18 [THEN]
169 : pazsan 1.16
170 : jwilke 1.18 \ for ec version display that vocabulary goes hashed
171 : pazsan 1.1
172 : jwilke 1.18 : hash-cold ( -- )
173 : jwilke 1.19 [ has? ec [IF] ] ." Hashing..." [ [THEN] ]
174 : anton 1.13 HashPointer off 0 TO HashTable HashIndex off
175 : jwilke 1.18 addall
176 :     \ voclink
177 :     \ BEGIN @ dup WHILE
178 :     \ dup 0 wordlist-link - initvoc
179 :     \ REPEAT drop
180 : jwilke 1.19 [ has? ec [IF] ] ." Done" cr [ [THEN] ] ;
181 : jwilke 1.18
182 :     ' hash-cold INIT8 chained
183 : pazsan 1.5
184 : pazsan 1.1 : .words ( -- )
185 : anton 1.13 base @ >r hex HashTable Hashlen 0
186 : pazsan 1.4 DO cr i 2 .r ." : " dup i cells +
187 : pazsan 1.1 BEGIN @ dup WHILE
188 : pazsan 1.20 dup cell+ @ name>string type space REPEAT drop
189 : pazsan 1.1 LOOP drop r> base ! ;
190 :    
191 : anton 1.2 \ \ this stuff is for evaluating the hash function
192 :     \ : square dup * ;
193 :    
194 :     \ : countwl ( -- sum sumsq )
195 : pazsan 1.4 \ \ gives the number of words in the current wordlist
196 :     \ \ and the sum of squares for the sublist lengths
197 : anton 1.2 \ 0 0
198 : anton 1.13 \ hashtable Hashlen cells bounds DO
199 : pazsan 1.4 \ 0 i BEGIN
200 :     \ @ dup WHILE
201 :     \ swap 1+ swap
202 :     \ REPEAT
203 :     \ drop
204 :     \ swap over square +
205 :     \ >r + r>
206 :     \ 1 cells
207 :     \ +LOOP ;
208 : anton 1.2
209 :     \ : chisq ( -- n )
210 : pazsan 1.4 \ \ n should have about the same size as Hashlen
211 :     \ countwl Hashlen 2 pick */ swap - ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help