summaryrefslogtreecommitdiff
path: root/Lisp/weak-hash-tables.lisp
blob: ae8ad0cd6e02cbc2b12302c43af6660051ad9c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(in-package #:mulk.objective-cl)


#+cmu
(progn
  (declaim (inline make-weak-value-hash-table))

  (defun make-weak-value-hash-table ()
    (make-hash-table :test 'eql))

  (defun weak-gethash (key hash-table &optional (default nil))
    (let ((pointer (gethash key hash-table default)))
      (or (and (trivial-garbage:weak-pointer-p pointer)
               (trivial-garbage:weak-pointer-value pointer))
          (prog1 default
            ;; Clean up.
            (remhash key hash-table)))))

  (defun (setf weak-gethash) (value key hash-table)
    (setf (gethash key hash-table)
          (trivial-garbage:make-weak-pointer value))))


#-cmu
(progn
  (declaim (inline make-weak-value-hash-table (setf weak-gethash)))

  (defun make-weak-value-hash-table ()
    (trivial-garbage:make-weak-hash-table :weakness :value
                                          :test 'eql))

  (setf (fdefinition 'weak-gethash) (fdefinition 'gethash))

  (defun (setf weak-gethash) (value key hash-table)
    (setf (gethash key hash-table) value)))