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