3 ;;;; Copyright (C) 1996, 2001 Free Software Foundation, Inc.
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.
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.
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, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
47 (define-module (ice-9 mapping)
48 :use-module (ice-9 poe)
49 :export (mapping-hooks-type make-mapping-hooks mapping-hooks?
50 mapping-hooks-get-handle mapping-hooks-create-handle
51 mapping-hooks-remove mapping-type make-mapping mapping?
52 mapping-hooks mapping-data set-mapping-hooks! set-mapping-data!
53 mapping-get-handle mapping-create-handle! mapping-remove!
54 mapping-ref mapping-set! hash-table-mapping-hooks
55 make-hash-table-mapping hash-table-mapping))
57 (define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
62 (define make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
63 (define mapping-hooks? (record-predicate mapping-hooks-type))
64 (define mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
65 (define mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
66 (define mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
68 (define mapping-type (make-record-type 'mapping '(hooks data)))
69 (define make-mapping (record-constructor mapping-type))
70 (define mapping? (record-predicate mapping-type))
71 (define mapping-hooks (record-accessor mapping-type 'hooks))
72 (define mapping-data (record-accessor mapping-type 'data))
73 (define set-mapping-hooks! (record-modifier mapping-type 'hooks))
74 (define set-mapping-data! (record-modifier mapping-type 'data))
76 (define (mapping-get-handle map key)
77 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
78 (define (mapping-create-handle! map key . opts)
79 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
80 (define (mapping-remove! map key)
81 ((mapping-hooks-remove (mapping-hooks map)) map key))
83 (define (mapping-ref map key . dflt)
85 ((mapping-get-handle map key) => cdr)
89 (define (mapping-set! map key val)
90 (set-cdr! (mapping-create-handle! map key #f) val))
94 (define hash-table-mapping-hooks
95 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
98 (lambda (hash-proc assoc-proc delete-proc)
99 (let ((procs (list hash-proc assoc-proc delete-proc)))
101 ((equal? procs `(,hashq ,assq ,delq!))
102 (make-mapping-hooks (wrap hashq-get-handle)
103 (wrap hashq-create-handle!)
104 (wrap hashq-remove!)))
105 ((equal? procs `(,hashv ,assv ,delv!))
106 (make-mapping-hooks (wrap hashv-get-handle)
107 (wrap hashv-create-handle!)
108 (wrap hashv-remove!)))
109 ((equal? procs `(,hash ,assoc ,delete!))
110 (make-mapping-hooks (wrap hash-get-handle)
111 (wrap hash-create-handle!)
112 (wrap hash-remove!)))
114 (make-mapping-hooks (wrap
116 (hashx-get-handle hash-proc assoc-proc table key)))
119 (hashx-create-handle hash-proc assoc-proc table key)))
122 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
124 (define (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
125 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
127 (define (hash-table-mapping . options)
128 (let* ((size (or (and options (number? (car options)) (car options))
130 (hash-proc (or (kw-arg-ref options :hash-proc) hash))
131 (assoc-proc (or (kw-arg-ref options :assoc-proc)
133 ((eq? hash-proc hash) assoc)
134 ((eq? hash-proc hashv) assv)
135 ((eq? hash-proc hashq) assq)
136 (else (error 'hash-table-mapping
137 "Hash-procedure specified with no known assoc function."
139 (delete-proc (or (kw-arg-ref options :delete-proc)
141 ((eq? hash-proc hash) delete!)
142 ((eq? hash-proc hashv) delv!)
143 ((eq? hash-proc hashq) delq!)
144 (else (error 'hash-table-mapping
145 "Hash-procedure specified with no known delete function."
147 (table-constructor (or (kw-arg-ref options :table-constructor)
148 (lambda (len) (make-vector len '())))))
149 (make-hash-table-mapping (table-constructor size)