3 ;;;; Copyright (C) 1996 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))
50 (define-public mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
55 (define-public make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
56 (define-public mapping-hooks? (record-predicate mapping-hooks-type))
57 (define-public mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
58 (define-public mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
59 (define-public mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
61 (define-public mapping-type (make-record-type 'mapping '(hooks data)))
62 (define-public make-mapping (record-constructor mapping-type))
63 (define-public mapping? (record-predicate mapping-type))
64 (define-public mapping-hooks (record-accessor mapping-type 'hooks))
65 (define-public mapping-data (record-accessor mapping-type 'data))
66 (define-public set-mapping-hooks! (record-modifier mapping-type 'hooks))
67 (define-public set-mapping-data! (record-modifier mapping-type 'data))
69 (define-public (mapping-get-handle map key)
70 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
71 (define-public (mapping-create-handle! map key . opts)
72 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
73 (define-public (mapping-remove! map key)
74 ((mapping-hooks-remove (mapping-hooks map)) map key))
76 (define-public (mapping-ref map key . dflt)
78 ((mapping-get-handle map key) => cdr)
82 (define-public (mapping-set! map key val)
83 (set-cdr! (mapping-create-handle! map key #f) val))
87 (define-public hash-table-mapping-hooks
88 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
91 (lambda (hash-proc assoc-proc delete-proc)
92 (let ((procs (list hash-proc assoc-proc delete-proc)))
94 ((equal? procs `(,hashq ,assq ,delq!))
95 (make-mapping-hooks (wrap hashq-get-handle)
96 (wrap hashq-create-handle!)
97 (wrap hashq-remove!)))
98 ((equal? procs `(,hashv ,assv ,delv!))
99 (make-mapping-hooks (wrap hashv-get-handle)
100 (wrap hashv-create-handle!)
101 (wrap hashv-remove!)))
102 ((equal? procs `(,hash ,assoc ,delete!))
103 (make-mapping-hooks (wrap hash-get-handle)
104 (wrap hash-create-handle!)
105 (wrap hash-remove!)))
107 (make-mapping-hooks (wrap
109 (hashx-get-handle hash-proc assoc-proc table key)))
112 (hashx-create-handle hash-proc assoc-proc table key)))
115 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
117 (define-public (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
118 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
120 (define-public (hash-table-mapping . options)
121 (let* ((size (or (and options (number? (car options)) (car options))
123 (hash-proc (or (kw-arg-ref options :hash-proc) hash))
124 (assoc-proc (or (kw-arg-ref options :assoc-proc)
126 ((eq? hash-proc hash) assoc)
127 ((eq? hash-proc hashv) assv)
128 ((eq? hash-proc hashq) assq)
129 (else (error 'hash-table-mapping
130 "Hash-procedure specified with no known assoc function."
132 (delete-proc (or (kw-arg-ref options :delete-proc)
134 ((eq? hash-proc hash) delete!)
135 ((eq? hash-proc hashv) delv!)
136 ((eq? hash-proc hashq) delq!)
137 (else (error 'hash-table-mapping
138 "Hash-procedure specified with no known delete function."
140 (table-constructor (or (kw-arg-ref options :table-constructor)
141 (lambda (len) (make-vector len '())))))
142 (make-hash-table-mapping (table-constructor size)