The FSF has a new address.
[bpt/guile.git] / ice-9 / hcons.scm
CommitLineData
0f2d19dd
JB
1;;; installed-scm-file
2
f70b2b1e 3;;;; Copyright (C) 1995, 1996, 1998, 2001, 2003 Free Software Foundation, Inc.
0f2d19dd 4;;;;
73be1d9e
MV
5;;;; This library is free software; you can redistribute it and/or
6;;;; modify it under the terms of the GNU Lesser General Public
7;;;; License as published by the Free Software Foundation; either
8;;;; version 2.1 of the License, or (at your option) any later version.
0f2d19dd 9;;;;
73be1d9e 10;;;; This library is distributed in the hope that it will be useful,
0f2d19dd 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
0f2d19dd 14;;;;
73be1d9e
MV
15;;;; You should have received a copy of the GNU Lesser General Public
16;;;; License along with this library; if not, write to the Free Software
92205699 17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0f2d19dd
JB
18;;;;
19
20\f
1a179b03
MD
21(define-module (ice-9 hcons)
22 :export (hashq-cons-hash hashq-cons-assoc hashq-cons-get-handle
23 hashq-cons-create-handle! hashq-cons-ref hashq-cons-set! hashq-cons
24 hashq-conser make-gc-buffer))
0f2d19dd
JB
25
26\f
27;;; {Eq? hash-consing}
28;;;
29;;; A hash conser maintains a private universe of pairs s.t. if
30;;; two cons calls pass eq? arguments, the pairs returned are eq?.
31;;;
32;;; A hash conser does not contribute life to the pairs it returns.
33;;;
34
1a179b03 35(define (hashq-cons-hash pair n)
0f2d19dd
JB
36 (modulo (logxor (hashq (car pair) 4194303)
37 (hashq (cdr pair) 4194303))
38 n))
39
1a179b03 40(define (hashq-cons-assoc key l)
048a208e
JB
41 (and (not (null? l))
42 (or (and (pair? l) ; If not a pair, use its cdr?
43 (pair? (car l))
44 (pair? (caar l))
45 (eq? (car key) (caaar l))
46 (eq? (cdr key) (cdaar l))
47 (car l))
48 (hashq-cons-assoc key (cdr l)))))
0f2d19dd 49
1a179b03 50(define (hashq-cons-get-handle table key)
f70b2b1e 51 (hashx-get-handle hashq-cons-hash hashq-cons-assoc table key))
0f2d19dd 52
1a179b03 53(define (hashq-cons-create-handle! table key init)
0f2d19dd
JB
54 (hashx-create-handle! hashq-cons-hash hashq-cons-assoc table key init))
55
1a179b03 56(define (hashq-cons-ref table key)
0f2d19dd
JB
57 (hashx-ref hashq-cons-hash hashq-cons-assoc table key #f))
58
1a179b03 59(define (hashq-cons-set! table key val)
0f2d19dd
JB
60 (hashx-set! hashq-cons-hash hashq-cons-assoc table key val))
61
1a179b03 62(define (hashq-cons table a d)
0f2d19dd
JB
63 (car (hashq-cons-create-handle! table (cons a d) #f)))
64
1a179b03 65(define (hashq-conser hash-tab-or-size)
0f2d19dd
JB
66 (let ((table (if (vector? hash-tab-or-size)
67 hash-tab-or-size
68 (make-doubly-weak-hash-table hash-tab-or-size))))
69 (lambda (a d) (hashq-cons table a d))))
70
71
72\f
73
1a179b03 74(define (make-gc-buffer n)
0f2d19dd
JB
75 (let ((ring (make-list n #f)))
76 (append! ring ring)
77 (lambda (next)
78 (set-car! ring next)
79 (set! ring (cdr ring))
80 next)))