[gforth] / gforth / hash.fs  

gforth: gforth/hash.fs


1 : pazsan 1.1 \ Hashed dictionaries 15jul94py
2 :    
3 : anton 1.10 \ Copyright (C) 1995 Free Software Foundation, Inc.
4 :    
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 : anton 1.9 11 value hashbits
22 : anton 1.2 1 hashbits lshift Value Hashlen
23 : pazsan 1.1
24 :     Variable insRule insRule on
25 : pazsan 1.4 Variable revealed
26 : pazsan 1.1
27 : pazsan 1.4 \ Memory handling 10oct94py
28 : pazsan 1.1
29 :     Variable HashPointer
30 : pazsan 1.4 Variable HashIndex
31 : anton 1.13 0 Value HashTable
32 : pazsan 1.1
33 : pazsan 1.4 \ DelFix and NewFix are from bigFORTH 15jul94py
34 : pazsan 1.1
35 :     : DelFix ( addr root -- ) dup @ 2 pick ! ! ;
36 :     : NewFix ( root len # -- addr )
37 :     BEGIN 2 pick @ ?dup 0= WHILE 2dup * allocate throw
38 :     over 0 ?DO dup 4 pick DelFix 2 pick + LOOP drop
39 :     REPEAT >r drop r@ @ rot ! r@ swap erase r> ;
40 :    
41 :     \ compute hash key 15jul94py
42 :    
43 : anton 1.2 : hash ( addr len -- key )
44 :     hashbits (hashkey1) ;
45 :     \ (hashkey)
46 :     \ Hashlen 1- and ;
47 : pazsan 1.1
48 : anton 1.12 : bucket ( addr len wordlist -- bucket-addr )
49 :     \ @var{bucket-addr} is the address of a cell that points to the first
50 :     \ element in the list of the bucket for the string @var{addr len}
51 :     wordlist-extend @ -rot hash xor ( bucket# )
52 : anton 1.13 cells HashTable + ;
53 : anton 1.2
54 :     : hash-find ( addr len wordlist -- nfa / false )
55 : anton 1.12 >r 2dup r> bucket @ (hashfind) ;
56 : pazsan 1.1
57 :     \ hash vocabularies 16jul94py
58 :    
59 :     : lastlink! ( addr link -- )
60 :     BEGIN dup @ dup WHILE nip REPEAT drop ! ;
61 :    
62 : anton 1.14 : (reveal ( nfa wid -- )
63 : anton 1.12 dup wordlist-extend @ 0<
64 :     IF
65 :     2drop EXIT
66 :     THEN
67 :     over name>string rot bucket >r
68 :     HashPointer 2 Cells $400 NewFix
69 :     tuck cell+ ! r> insRule @
70 :     IF
71 :     dup @ 2 pick ! !
72 :     ELSE
73 :     lastlink!
74 :     THEN
75 :     revealed on ;
76 :    
77 : anton 1.14 : hash-reveal ( nfa wid -- )
78 :     2dup (reveal) (reveal ;
79 : pazsan 1.1
80 : pazsan 1.4 : addall ( -- )
81 :     voclink
82 :     BEGIN @ dup @ WHILE dup 'initvoc REPEAT drop ;
83 :    
84 :     : clearhash ( -- )
85 : anton 1.13 HashTable Hashlen cells bounds
86 : pazsan 1.4 DO I @
87 : pazsan 1.15 BEGIN dup WHILE
88 :     dup @ swap HashPointer DelFix
89 : pazsan 1.4 REPEAT I !
90 :     cell +LOOP HashIndex off ;
91 :    
92 : pazsan 1.7 : re-hash clearhash addall ;
93 : pazsan 1.4 : (rehash) ( addr -- )
94 : pazsan 1.7 drop revealed @ IF re-hash revealed off THEN ;
95 : pazsan 1.4
96 : anton 1.12 Create hashsearch-map ( -- wordlist-map )
97 :     ' hash-find A, ' hash-reveal A, ' (rehash) A,
98 : pazsan 1.4
99 :     \ hash allocate and vocabulary initialization 10oct94py
100 :    
101 : anton 1.13 : hash-alloc ( addr -- addr ) HashTable 0= IF
102 :     Hashlen cells allocate throw TO HashTable
103 :     HashTable Hashlen cells erase THEN
104 : pazsan 1.4 HashIndex @ over ! 1 HashIndex +!
105 :     HashIndex @ Hashlen >=
106 : pazsan 1.17 IF HashTable >r clearhash
107 : pazsan 1.4 1 hashbits 1+ dup to hashbits lshift to hashlen
108 : pazsan 1.17 r> free >r 0 to HashTable
109 :     addall r> throw
110 : pazsan 1.4 THEN ;
111 : pazsan 1.1
112 : pazsan 1.4 : (initvoc) ( addr -- )
113 : pazsan 1.15 cell+ dup @ 0< IF drop EXIT THEN
114 : pazsan 1.16 dup 2 cells - @ hashsearch-map <> IF drop EXIT THEN
115 :     insRule @ >r insRule off hash-alloc 3 cells - dup
116 : anton 1.2 BEGIN @ dup WHILE 2dup swap (reveal REPEAT
117 :     2drop r> insRule ! ;
118 : pazsan 1.1
119 : pazsan 1.16 ' (initvoc) IS 'initvoc
120 : pazsan 1.1
121 :     \ Hash-Find 01jan93py
122 :    
123 : pazsan 1.16 : make-hash
124 :     Root hashsearch-map context @ cell+ !
125 :     Forth hashsearch-map context @ cell+ !
126 :     addall \ Baum aufbauen
127 :     ;
128 :    
129 :     make-hash \ Baumsuche ist installiert.
130 : pazsan 1.1
131 : pazsan 1.5 : hash-cold ( -- ) Defers 'cold
132 : anton 1.13 HashPointer off 0 TO HashTable HashIndex off
133 : pazsan 1.6 voclink
134 :     BEGIN @ dup @ WHILE
135 :     dup cell - @ >r
136 :     dup 'initvoc
137 :     r> over cell - !
138 :     REPEAT drop ;
139 : anton 1.13 ' hash-cold ' 'cold >body !
140 : pazsan 1.5
141 : pazsan 1.1 : .words ( -- )
142 : anton 1.13 base @ >r hex HashTable Hashlen 0
143 : pazsan 1.4 DO cr i 2 .r ." : " dup i cells +
144 : pazsan 1.1 BEGIN @ dup WHILE
145 :     dup cell+ @ .name REPEAT drop
146 :     LOOP drop r> base ! ;
147 :    
148 : anton 1.2 \ \ this stuff is for evaluating the hash function
149 :     \ : square dup * ;
150 :    
151 :     \ : countwl ( -- sum sumsq )
152 : pazsan 1.4 \ \ gives the number of words in the current wordlist
153 :     \ \ and the sum of squares for the sublist lengths
154 : anton 1.2 \ 0 0
155 : anton 1.13 \ hashtable Hashlen cells bounds DO
156 : pazsan 1.4 \ 0 i BEGIN
157 :     \ @ dup WHILE
158 :     \ swap 1+ swap
159 :     \ REPEAT
160 :     \ drop
161 :     \ swap over square +
162 :     \ >r + r>
163 :     \ 1 cells
164 :     \ +LOOP ;
165 : anton 1.2
166 :     \ : chisq ( -- n )
167 : pazsan 1.4 \ \ n should have about the same size as Hashlen
168 :     \ countwl Hashlen 2 pick */ swap - ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help