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

Contents of /src/code/hash.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.3 - (hide annotations) (vendor branch)
Sun May 6 05:34:43 1990 UTC (23 years, 11 months ago) by wlott
Changes since 1.1.1.2: +2 -1 lines
Use (ldb (byte 32 0) s-expr) instead of (abs s-expr) in sxhash of fixnums,
cause the abs of a fixnum can be a bignum.
1 ram 1.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 wlott 1.1.1.1 ;;; Vector subtype codes.
19    
20     (defconstant valid-hashing 2)
21     (defconstant must-rehash 3)
22    
23    
24 ram 1.1 ;;; 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 lisp::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     `(%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     (%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 wlott 1.1.1.1 valid-hashing)))))
142 ram 1.1 (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 (%primitive get-vector-subtype vector)))
186     (declare (fixnum subtype))
187 wlott 1.1.1.1 (cond ((/= subtype valid-hashing)
188 ram 1.1 (rehash hash-table vector size)
189     (setq vector (hash-table-table hash-table)))
190     ((> (hash-table-number-entries hash-table)
191     (hash-table-rehash-threshold hash-table))
192     (rehash hash-table vector (grow-size hash-table))
193     (setq vector (hash-table-table hash-table))
194     (setq size (length vector))))))
195    
196     (defmacro equal-rehash-if-needed ()
197     `(cond ((> (hash-table-number-entries hash-table)
198     (hash-table-rehash-threshold hash-table))
199     (rehash hash-table vector (grow-size hash-table))
200     (setq vector (hash-table-table hash-table))
201     (setq size (length vector)))))
202    
203     (defmacro rehash-if-needed ()
204     `(let ((subtype (%primitive get-vector-subtype vector))
205     (size (length vector)))
206     (declare (fixnum subtype size))
207     (cond ((and (not (eq (hash-table-kind hash-table) 'equal))
208 wlott 1.1.1.1 (/= subtype valid-hashing))
209 ram 1.1 (rehash hash-table vector size)
210     (setq vector (hash-table-table hash-table))
211     (setq size (length vector)))
212     ((> (hash-table-number-entries hash-table)
213     (hash-table-rehash-threshold hash-table))
214     (rehash hash-table vector (grow-size hash-table))
215     (setq vector (hash-table-table hash-table))
216     (setq size (length vector))))))
217    
218     )
219    
220     ;;; Making hash tables:
221    
222     (defun make-hash-table (&key (test 'eql) (size 65) (rehash-size 101)
223     rehash-threshold)
224     "Creates and returns a hash table. See manual for details."
225     (declare (fixnum size))
226     (cond ((eq test #'eq) (setq test 'eq))
227     ((eq test #'eql) (setq test 'eql))
228     ((eq test #'equal) (setq test 'equal)))
229 wlott 1.1.1.1 (if (not (member test '(eq eql equal) :test #'eq))
230 ram 1.1 (error "~S is an illegal :Test for hash tables." test))
231     (setq size (if (<= size 37) 37 (almost-primify size)))
232     (cond ((null rehash-threshold)
233     (setq rehash-threshold size))
234     ((floatp rehash-threshold)
235     (setq rehash-threshold (ceiling (* rehash-threshold size)))))
236     (make-hash-table-structure :size size
237     :rehash-size rehash-size
238     :rehash-threshold rehash-threshold
239     :table
240     (if (eq test 'equal)
241     (make-array size)
242     (%primitive set-vector-subtype
243 wlott 1.1.1.1 (make-array size)
244     valid-hashing))
245 ram 1.1 :kind test)))
246    
247     ;;; Manipulating hash tables:
248    
249     (defun gethash (key hash-table &optional default)
250     "Finds the entry in Hash-Table whose key is Key and returns the associated
251     value and T as multiple values, or returns Default and Nil if there is no
252     such entry."
253     (macrolet ((lookup (test)
254     `(let ((cons (assoc key (aref vector index) :test #',test)))
255     (declare (list cons))
256     (if cons
257     (values (cdr cons) t)
258     (values default nil)))))
259     (hashop nil
260     (lookup eq)
261     (lookup eql)
262     (lookup equal))))
263    
264     (defun %puthash (key hash-table value)
265     "Create an entry in HASH-TABLE associating KEY with VALUE; if there already
266     is an entry for KEY, replace it. Returns VALUE."
267     (macrolet ((store (test)
268     `(let ((cons (assoc key (aref vector index) :test #',test)))
269     (declare (list cons))
270     (cond (cons (setf (cdr cons) value))
271     (t
272     (push (cons key value) (aref vector index))
273     (incf (hash-table-number-entries hash-table))
274     value)))))
275     (hashop t
276     (store eq)
277     (store eql)
278     (store equal))))
279    
280     (defun remhash (key hash-table)
281     "Remove any entry for KEY in HASH-TABLE. Returns T if such an entry
282     existed; () otherwise."
283     (hashop nil
284     (let ((bucket (aref vector index))) ; EQ case
285     (cond ((and bucket (eq (caar bucket) key))
286     (pop (aref vector index))
287     (decf (hash-table-number-entries hash-table))
288     t)
289     (t
290     (do ((last bucket bucket)
291     (bucket (cdr bucket) (cdr bucket)))
292     ((null bucket) ())
293     (when (eq (caar bucket) key)
294     (rplacd last (cdr bucket))
295     (decf (hash-table-number-entries hash-table))
296     (return t))))))
297     (let ((bucket (aref vector index))) ; EQL case
298     (cond ((and bucket (eql (caar bucket) key))
299     (pop (aref vector index))
300     (decf (hash-table-number-entries hash-table))
301     t)
302     (t
303     (do ((last bucket bucket)
304     (bucket (cdr bucket) (cdr bucket)))
305     ((null bucket) ())
306     (when (eql (caar bucket) key)
307     (rplacd last (cdr bucket))
308     (decf (hash-table-number-entries hash-table))
309     (return t))))))
310     (let ((bucket (aref vector index))) ; EQUAL case
311     (cond ((and bucket (equal (caar bucket) key))
312     (pop (aref vector index))
313     (decf (hash-table-number-entries hash-table))
314     t)
315     (t
316     (do ((last bucket bucket)
317     (bucket (cdr bucket) (cdr bucket)))
318     ((null bucket) ())
319     (when (equal (caar bucket) key)
320     (rplacd last (cdr bucket))
321     (decf (hash-table-number-entries hash-table))
322     (return t))))))))
323    
324     (defun maphash (map-function hash-table)
325     "For each entry in HASH-TABLE, calls MAP-FUNCTION on the key and value
326     of the entry; returns T."
327     (let ((vector (hash-table-table hash-table)))
328     (declare (simple-vector vector))
329     (rehash-if-needed)
330     (do ((i 0 (1+ i))
331     (size (hash-table-size hash-table)))
332     ((= i size))
333     (declare (fixnum i size))
334     (do ((bucket (aref vector i) (cdr bucket)))
335     ((null bucket))
336    
337     (funcall map-function (caar bucket) (cdar bucket))))))
338    
339     (defun clrhash (hash-table)
340     "Removes all entries of HASH-TABLE and returns the hash table itself."
341     (let ((vector (hash-table-table hash-table)))
342     (declare (simple-vector vector))
343     (setf (hash-table-number-entries hash-table) 0)
344     (do ((i 0 (1+ i))
345     (size (hash-table-size hash-table)))
346     ((= i size) hash-table)
347     (declare (fixnum i size))
348     (setf (aref vector i) nil))))
349    
350     (defun hash-table-count (hash-table)
351     "Returns the number of entries in the given Hash-Table."
352     (hash-table-number-entries hash-table))
353    
354     ;;; Primitive Hash Function
355    
356     ;;; The maximum length and depth to which we hash lists.
357     (defconstant sxhash-max-len 7)
358     (defconstant sxhash-max-depth 3)
359    
360     (eval-when (compile eval)
361    
362    
363 wlott 1.1.1.2 (defconstant sxmash-total-bits 26)
364     (defconstant sxmash-rotate-bits 7)
365    
366     (defmacro sxmash (place with)
367     (let ((n-with (gensym)))
368     `(let ((,n-with ,with))
369     (declare (fixnum ,n-with))
370     (setf ,place
371     (logxor (ash ,n-with ,(- sxmash-rotate-bits sxmash-total-bits))
372     (ash (logand ,n-with
373     ,(1- (ash 1
374     (- sxmash-total-bits
375     sxmash-rotate-bits))))
376     ,sxmash-rotate-bits)
377     (the fixnum ,place))))))
378    
379 ram 1.1 (defmacro sxhash-simple-string (sequence)
380     `(%primitive sxhash-simple-string ,sequence))
381    
382     (defmacro sxhash-string (sequence)
383     (let ((data (gensym))
384     (start (gensym))
385     (end (gensym)))
386     `(with-array-data ((,data ,sequence)
387     (,start)
388 wlott 1.1.1.1 (,end))
389 ram 1.1 (if (zerop ,start)
390     (%primitive sxhash-simple-substring ,data ,end)
391     (sxhash-simple-string (coerce (the string ,sequence)
392     'simple-string))))))
393    
394     (defmacro sxhash-list (sequence depth)
395     `(if (= ,depth sxhash-max-depth)
396     0
397     (do ((sequence ,sequence (cdr (the list sequence)))
398     (index 0 (1+ index))
399     (hash 2))
400     ((or (atom sequence) (= index sxhash-max-len)) hash)
401     (declare (fixnum hash index))
402 wlott 1.1.1.2 (sxmash hash (internal-sxhash (car sequence) (1+ ,depth))))))
403 ram 1.1
404 wlott 1.1.1.2
405 ram 1.1 ); eval-when (compile eval)
406    
407    
408     (defun sxhash (s-expr)
409     "Computes a hash code for S-EXPR and returns it as an integer."
410     (internal-sxhash s-expr 0))
411    
412 wlott 1.1.1.2
413 ram 1.1 (defun internal-sxhash (s-expr depth)
414     (typecase s-expr
415 wlott 1.1.1.2 ;; The pointers and immediate types.
416 ram 1.1 (list (sxhash-list s-expr depth))
417 wlott 1.1.1.3 (fixnum
418     (ldb (byte 23 0) s-expr))
419 wlott 1.1.1.2 #+nil
420     (structure ???)
421     ;; Other-pointer types.
422     (simple-string (sxhash-simple-string s-expr))
423     (symbol (sxhash-simple-string (symbol-name s-expr)))
424 ram 1.1 (number
425     (etypecase s-expr
426     (integer (ldb (byte 23 0) s-expr))
427     (float (multiple-value-bind (significand exponent)
428     (integer-decode-float s-expr)
429     (logxor (the fixnum (ldb (byte 23 0) significand))
430     (the fixnum (ldb (byte 23 0) exponent)))))
431     (ratio (the fixnum (+ (internal-sxhash (numerator s-expr) 0)
432     (internal-sxhash (denominator s-expr) 0))))
433     (complex (the fixnum (+ (internal-sxhash (realpart s-expr) 0)
434     (internal-sxhash (imagpart s-expr) 0))))))
435 wlott 1.1.1.2 (array
436     (typecase s-expr
437     (string (sxhash-string s-expr))
438     (t (array-rank s-expr))))
439 wlott 1.1.1.1 #+nil
440 ram 1.1 (compiled-function (%primitive header-length s-expr))
441 wlott 1.1.1.2 ;; Everything else.
442 ram 1.1 (t (%primitive make-fixnum s-expr))))

  ViewVC Help
Powered by ViewVC 1.1.5