/[cmucl]/src/code/hash.lisp
ViewVC logotype

Contents of /src/code/hash.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Thu Oct 4 14:36:09 1990 UTC (23 years, 6 months ago) by ram
Branch: MAIN
Changes since 1.2: +17 -8 lines
Changed SXHASH to use the float-bits primitives instead of integer-decode-
float for hashing float, so that SXHASH don't gag on NaNs and infinities.
Also, fixed it to not EQ hash random objects, as that is illegal.
1 ;;; -*- Log: code.log; Package: Lisp -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of Spice Lisp, please contact
7 ;;; Scott Fahlman (FAHLMAN@CMUC).
8 ;;; **********************************************************************
9 ;;;
10 ;;; Hashing and hash table functions for Spice Lisp.
11 ;;; Written by Skef Wholey.
12 ;;;
13 (in-package 'lisp)
14 (export '(hash-table hash-table-p make-hash-table
15 gethash remhash maphash clrhash
16 hash-table-count sxhash))
17
18 ;;; Vector subtype codes.
19
20 (defconstant valid-hashing 2)
21 (defconstant must-rehash 3)
22
23
24 ;;; What a hash-table is:
25
26 (defstruct (hash-table (:constructor make-hash-table-structure)
27 (:conc-name hash-table-)
28 (:print-function %print-hash-table))
29 "Structure used to implement hash tables."
30 (kind 'eq)
31 (size 65 :type fixnum)
32 (rehash-size 101) ; might be a float
33 (rehash-threshold 57 :type fixnum)
34 (number-entries 0 :type fixnum)
35 (table () :type simple-vector))
36
37 ;;; A hash-table-table is a vector of association lists. When an
38 ;;; entry is made in a hash table, a pair of (key . value) is consed onto
39 ;;; the element in the vector arrived at by hashing.
40
41 ;;; How to print one:
42
43 (defun %print-hash-table (structure stream depth)
44 (declare (ignore depth))
45 (format stream "#<~A Hash Table {~X}>"
46 (symbol-name (hash-table-kind structure))
47 (system:%primitive make-fixnum structure)))
48
49
50
51 ;;; Hashing functions for the three kinds of hash tables:
52
53 (eval-when (compile)
54
55 (defmacro eq-hash (object)
56 "Gives us a hashing of an object such that (eq a b) implies
57 (= (eq-hash a) (eq-hash b))"
58 `(truly-the (unsigned-byte 24) (%primitive make-fixnum ,object)))
59
60 (defmacro eql-hash (object)
61 "Gives us a hashing of an object such that (eql a b) implies
62 (= (eql-hash a) (eql-hash b))"
63 `(if (numberp ,object)
64 (logand (truncate ,object) most-positive-fixnum)
65 (truly-the fixnum (%primitive make-fixnum ,object))))
66
67 (defmacro equal-hash (object)
68 "Gives us a hashing of an object such that (equal a b) implies
69 (= (equal-hash a) (equal-hash b))"
70 `(sxhash ,object))
71
72 )
73
74 ;;; Rehashing functions:
75
76 (defun almost-primify (num)
77 (declare (fixnum num))
78 "Almost-Primify returns an almost prime number greater than or equal
79 to NUM."
80 (if (= (rem num 2) 0)
81 (setq num (+ 1 num)))
82 (if (= (rem num 3) 0)
83 (setq num (+ 2 num)))
84 (if (= (rem num 7) 0)
85 (setq num (+ 4 num)))
86 num)
87
88 (eval-when (compile)
89
90 (defmacro grow-size (table)
91 "Returns a fixnum for the next size of a growing hash-table."
92 `(let ((rehash-size (hash-table-rehash-size ,table)))
93 (if (floatp rehash-size)
94 (ceiling (* rehash-size (hash-table-size ,table)))
95 (+ rehash-size (hash-table-size ,table)))))
96
97 (defmacro grow-rehash-threshold (table new-length)
98 "Returns the next rehash threshold for the table."
99 table
100 `,new-length
101 ; `(ceiling (* (hash-table-rehash-threshold ,table)
102 ; (/ ,new-length (hash-table-size ,table))))
103 )
104
105 (defmacro hash-set (vector key value length hashing-function)
106 "Used for rehashing. Enters the value for the key into the vector
107 by hashing. Never grows the vector. Assumes the key is not yet
108 entered."
109 `(let ((index (rem (the fixnum (funcall ,hashing-function ,key))
110 (the fixnum ,length))))
111 (declare (fixnum index))
112 (setf (aref (the simple-vector ,vector) index)
113 (cons (cons ,key ,value)
114 (aref (the simple-vector ,vector) index)))))
115
116 )
117
118 (defun rehash (structure hash-vector new-length)
119 (declare (simple-vector hash-vector))
120 (declare (fixnum new-length))
121 "Rehashes a hash table and replaces the TABLE entry in the structure if
122 someone hasn't done so already. New vector is of NEW-LENGTH."
123 (do ((new-vector (make-array new-length))
124 (i 0 (1+ i))
125 (size (hash-table-size structure))
126 (hashing-function (case (hash-table-kind structure)
127 (eq #'(lambda (x) (eq-hash x)))
128 (eql #'(lambda (x) (eql-hash x)))
129 (equal #'(lambda (x) (equal-hash x))))))
130 ((= i size)
131 (cond ((eq hash-vector (hash-table-table structure))
132 (cond ((> new-length size)
133 (setf (hash-table-table structure) new-vector)
134 (setf (hash-table-rehash-threshold structure)
135 (grow-rehash-threshold structure new-length))
136 (setf (hash-table-size structure) new-length))
137 (t
138 (setf (hash-table-table structure) new-vector)))
139 (if (not (eq (hash-table-kind structure) 'equal))
140 (%primitive set-vector-subtype new-vector
141 valid-hashing)))))
142 (declare (fixnum i size))
143 (do ((bucket (aref hash-vector i) (cdr bucket)))
144 ((null bucket))
145 (hash-set new-vector (caar bucket) (cdar bucket) new-length
146 hashing-function))
147 (setf (aref hash-vector i) nil)))
148
149 ;;; Macros for Gethash, %Puthash, and Remhash:
150
151 (eval-when (compile)
152
153 ;;; Hashop dispatches on the kind of hash table we've got, rehashes if
154 ;;; necessary, and binds Vector to the hash vector, Index to the index
155 ;;; into that vector that the Key points to, and Size to the size of the
156 ;;; hash vector. Since Equal hash tables only need to be maybe rehashed
157 ;;; sometimes, one can tell it if it's one of those times with the
158 ;;; Equal-Needs-To-Rehash-P argument.
159
160 (defmacro hashop (equal-needs-to-rehash-p eq-body eql-body equal-body)
161 `(let* ((vector (hash-table-table hash-table))
162 (size (length vector)))
163 (declare (simple-vector vector) (fixnum size)
164 (inline assoc))
165 (case (hash-table-kind hash-table)
166 (equal
167 ,@(if equal-needs-to-rehash-p `((equal-rehash-if-needed)))
168 (let ((index (rem (the fixnum (equal-hash key)) size)))
169 (declare (fixnum index))
170 ,equal-body))
171 (eq
172 (without-gcing
173 (eq-rehash-if-needed)
174 (let ((index (rem (the fixnum (eq-hash key)) size)))
175 (declare (fixnum index))
176 ,eq-body)))
177 (eql
178 (without-gcing
179 (eq-rehash-if-needed)
180 (let ((index (rem (the fixnum (eql-hash key)) size)))
181 (declare (fixnum index))
182 ,eql-body))))))
183
184 (defmacro eq-rehash-if-needed ()
185 `(let ((subtype (truly-the (unsigned-byte 24)
186 (%primitive get-vector-subtype vector))))
187 (declare (type (unsigned-byte 24) subtype))
188 (cond ((/= subtype valid-hashing)
189 (rehash hash-table vector size)
190 (setq vector (hash-table-table hash-table)))
191 ((> (hash-table-number-entries hash-table)
192 (hash-table-rehash-threshold hash-table))
193 (rehash hash-table vector (grow-size hash-table))
194 (setq vector (hash-table-table hash-table))
195 (setq size (length vector))))))
196
197 (defmacro equal-rehash-if-needed ()
198 `(cond ((> (hash-table-number-entries hash-table)
199 (hash-table-rehash-threshold hash-table))
200 (rehash hash-table vector (grow-size hash-table))
201 (setq vector (hash-table-table hash-table))
202 (setq size (length vector)))))
203
204 (defmacro rehash-if-needed ()
205 `(let ((subtype (truly-the (unsigned-byte 24)
206 (%primitive get-vector-subtype vector)))
207 (size (length vector)))
208 (declare (type (unsigned-byte 24) subtype)
209 (fixnum size))
210 (cond ((and (not (eq (hash-table-kind hash-table) 'equal))
211 (/= subtype valid-hashing))
212 (rehash hash-table vector size)
213 (setq vector (hash-table-table hash-table))
214 (setq size (length vector)))
215 ((> (hash-table-number-entries hash-table)
216 (hash-table-rehash-threshold hash-table))
217 (rehash hash-table vector (grow-size hash-table))
218 (setq vector (hash-table-table hash-table))
219 (setq size (length vector))))))
220
221 )
222
223 ;;; Making hash tables:
224
225 (defun make-hash-table (&key (test 'eql) (size 65) (rehash-size 101)
226 rehash-threshold)
227 "Creates and returns a hash table. See manual for details."
228 (declare (fixnum size))
229 (cond ((eq test #'eq) (setq test 'eq))
230 ((eq test #'eql) (setq test 'eql))
231 ((eq test #'equal) (setq test 'equal)))
232 (if (not (member test '(eq eql equal) :test #'eq))
233 (error "~S is an illegal :Test for hash tables." test))
234 (setq size (if (<= size 37) 37 (almost-primify size)))
235 (cond ((null rehash-threshold)
236 (setq rehash-threshold size))
237 ((floatp rehash-threshold)
238 (setq rehash-threshold (ceiling (* rehash-threshold size)))))
239 (make-hash-table-structure :size size
240 :rehash-size rehash-size
241 :rehash-threshold rehash-threshold
242 :table
243 (if (eq test 'equal)
244 (make-array size)
245 (%primitive set-vector-subtype
246 (make-array size)
247 valid-hashing))
248 :kind test)))
249
250 ;;; Manipulating hash tables:
251
252 (defun gethash (key hash-table &optional default)
253 "Finds the entry in Hash-Table whose key is Key and returns the associated
254 value and T as multiple values, or returns Default and Nil if there is no
255 such entry."
256 (macrolet ((lookup (test)
257 `(let ((cons (assoc key (aref vector index) :test #',test)))
258 (declare (list cons))
259 (if cons
260 (values (cdr cons) t)
261 (values default nil)))))
262 (hashop nil
263 (lookup eq)
264 (lookup eql)
265 (lookup equal))))
266
267 (defun %puthash (key hash-table value)
268 "Create an entry in HASH-TABLE associating KEY with VALUE; if there already
269 is an entry for KEY, replace it. Returns VALUE."
270 (macrolet ((store (test)
271 `(let ((cons (assoc key (aref vector index) :test #',test)))
272 (declare (list cons))
273 (cond (cons (setf (cdr cons) value))
274 (t
275 (push (cons key value) (aref vector index))
276 (incf (hash-table-number-entries hash-table))
277 value)))))
278 (hashop t
279 (store eq)
280 (store eql)
281 (store equal))))
282
283 (defun remhash (key hash-table)
284 "Remove any entry for KEY in HASH-TABLE. Returns T if such an entry
285 existed; () otherwise."
286 (hashop nil
287 (let ((bucket (aref vector index))) ; EQ case
288 (cond ((and bucket (eq (caar bucket) key))
289 (pop (aref vector index))
290 (decf (hash-table-number-entries hash-table))
291 t)
292 (t
293 (do ((last bucket bucket)
294 (bucket (cdr bucket) (cdr bucket)))
295 ((null bucket) ())
296 (when (eq (caar bucket) key)
297 (rplacd last (cdr bucket))
298 (decf (hash-table-number-entries hash-table))
299 (return t))))))
300 (let ((bucket (aref vector index))) ; EQL case
301 (cond ((and bucket (eql (caar bucket) key))
302 (pop (aref vector index))
303 (decf (hash-table-number-entries hash-table))
304 t)
305 (t
306 (do ((last bucket bucket)
307 (bucket (cdr bucket) (cdr bucket)))
308 ((null bucket) ())
309 (when (eql (caar bucket) key)
310 (rplacd last (cdr bucket))
311 (decf (hash-table-number-entries hash-table))
312 (return t))))))
313 (let ((bucket (aref vector index))) ; EQUAL case
314 (cond ((and bucket (equal (caar bucket) key))
315 (pop (aref vector index))
316 (decf (hash-table-number-entries hash-table))
317 t)
318 (t
319 (do ((last bucket bucket)
320 (bucket (cdr bucket) (cdr bucket)))
321 ((null bucket) ())
322 (when (equal (caar bucket) key)
323 (rplacd last (cdr bucket))
324 (decf (hash-table-number-entries hash-table))
325 (return t))))))))
326
327 (defun maphash (map-function hash-table)
328 "For each entry in HASH-TABLE, calls MAP-FUNCTION on the key and value
329 of the entry; returns T."
330 (let ((vector (hash-table-table hash-table)))
331 (declare (simple-vector vector))
332 (rehash-if-needed)
333 (do ((i 0 (1+ i))
334 (size (hash-table-size hash-table)))
335 ((= i size))
336 (declare (fixnum i size))
337 (do ((bucket (aref vector i) (cdr bucket)))
338 ((null bucket))
339
340 (funcall map-function (caar bucket) (cdar bucket))))))
341
342 (defun clrhash (hash-table)
343 "Removes all entries of HASH-TABLE and returns the hash table itself."
344 (let ((vector (hash-table-table hash-table)))
345 (declare (simple-vector vector))
346 (setf (hash-table-number-entries hash-table) 0)
347 (do ((i 0 (1+ i))
348 (size (hash-table-size hash-table)))
349 ((= i size) hash-table)
350 (declare (fixnum i size))
351 (setf (aref vector i) nil))))
352
353 (defun hash-table-count (hash-table)
354 "Returns the number of entries in the given Hash-Table."
355 (hash-table-number-entries hash-table))
356
357 ;;; Primitive Hash Function
358
359 ;;; The maximum length and depth to which we hash lists.
360 (defconstant sxhash-max-len 7)
361 (defconstant sxhash-max-depth 3)
362
363 (eval-when (compile eval)
364
365 (defconstant sxhash-bits-byte (byte 23 0))
366 (defconstant sxmash-total-bits 26)
367 (defconstant sxmash-rotate-bits 7)
368
369 (defmacro sxmash (place with)
370 (let ((n-with (gensym)))
371 `(let ((,n-with ,with))
372 (declare (fixnum ,n-with))
373 (setf ,place
374 (logxor (ash ,n-with ,(- sxmash-rotate-bits sxmash-total-bits))
375 (ash (logand ,n-with
376 ,(1- (ash 1
377 (- sxmash-total-bits
378 sxmash-rotate-bits))))
379 ,sxmash-rotate-bits)
380 (the fixnum ,place))))))
381
382 (defmacro sxhash-simple-string (sequence)
383 `(truly-the index (%primitive sxhash-simple-string ,sequence)))
384
385 (defmacro sxhash-string (sequence)
386 (let ((data (gensym))
387 (start (gensym))
388 (end (gensym)))
389 `(with-array-data ((,data ,sequence)
390 (,start)
391 (,end))
392 (if (zerop ,start)
393 (truly-the index (%primitive sxhash-simple-substring ,data ,end))
394 (sxhash-simple-string (coerce (the string ,sequence)
395 'simple-string))))))
396
397 (defmacro sxhash-list (sequence depth)
398 `(if (= ,depth sxhash-max-depth)
399 0
400 (do ((sequence ,sequence (cdr (the list sequence)))
401 (index 0 (1+ index))
402 (hash 2))
403 ((or (atom sequence) (= index sxhash-max-len)) hash)
404 (declare (fixnum hash index))
405 (sxmash hash (internal-sxhash (car sequence) (1+ ,depth))))))
406
407
408 ); eval-when (compile eval)
409
410
411 (defun sxhash (s-expr)
412 "Computes a hash code for S-EXPR and returns it as an integer."
413 (internal-sxhash s-expr 0))
414
415
416 (defun internal-sxhash (s-expr depth)
417 (typecase s-expr
418 ;; The pointers and immediate types.
419 (list (sxhash-list s-expr depth))
420 (fixnum
421 (ldb sxhash-bits-byte s-expr))
422 #+nil
423 (structure ???)
424 ;; Other-pointer types.
425 (simple-string (sxhash-simple-string s-expr))
426 (symbol (sxhash-simple-string (symbol-name s-expr)))
427 (number
428 (etypecase s-expr
429 (integer (ldb sxhash-bits-byte s-expr))
430 (single-float
431 (let ((bits (vm:single-float-bits s-expr)))
432 (ldb sxhash-bits-byte
433 (logxor (ash bits (- sxmash-rotate-bits))
434 bits))))
435 (double-float
436 (let* ((val s-expr)
437 (lo (vm:double-float-low-bits val))
438 (hi (vm:double-float-high-bits val)))
439 (ldb sxhash-bits-byte
440 (logxor (ash lo (- sxmash-rotate-bits))
441 (ash hi (- sxmash-rotate-bits))
442 lo hi))))
443 (ratio (the fixnum (+ (internal-sxhash (numerator s-expr) 0)
444 (internal-sxhash (denominator s-expr) 0))))
445 (complex (the fixnum (+ (internal-sxhash (realpart s-expr) 0)
446 (internal-sxhash (imagpart s-expr) 0))))))
447 (array
448 (typecase s-expr
449 (string (sxhash-string s-expr))
450 (t (array-rank s-expr))))
451 #+nil
452 (compiled-function (%primitive header-length s-expr))
453 ;; Everything else.
454 (t 42)))

  ViewVC Help
Powered by ViewVC 1.1.5