3 ;;;; Copyright (C) 1996, 2001, 2006 Free Software Foundation, Inc.
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.
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
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
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 (define-module (ice-9 mapping)
23 :use-module (ice-9 poe)
24 :export (mapping-hooks-type make-mapping-hooks mapping-hooks?
25 mapping-hooks-get-handle mapping-hooks-create-handle
26 mapping-hooks-remove mapping-type make-mapping mapping?
27 mapping-hooks mapping-data set-mapping-hooks! set-mapping-data!
28 mapping-get-handle mapping-create-handle! mapping-remove!
29 mapping-ref mapping-set! hash-table-mapping-hooks
30 make-hash-table-mapping hash-table-mapping))
32 (define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
37 (define make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
38 (define mapping-hooks? (record-predicate mapping-hooks-type))
39 (define mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
40 (define mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
41 (define mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
43 (define mapping-type (make-record-type 'mapping '(hooks data)))
44 (define make-mapping (record-constructor mapping-type))
45 (define mapping? (record-predicate mapping-type))
46 (define mapping-hooks (record-accessor mapping-type 'hooks))
47 (define mapping-data (record-accessor mapping-type 'data))
48 (define set-mapping-hooks! (record-modifier mapping-type 'hooks))
49 (define set-mapping-data! (record-modifier mapping-type 'data))
51 (define (mapping-get-handle map key)
52 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
53 (define (mapping-create-handle! map key . opts)
54 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
55 (define (mapping-remove! map key)
56 ((mapping-hooks-remove (mapping-hooks map)) map key))
58 (define (mapping-ref map key . dflt)
60 ((mapping-get-handle map key) => cdr)
64 (define (mapping-set! map key val)
65 (set-cdr! (mapping-create-handle! map key #f) val))
69 (define hash-table-mapping-hooks
70 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
73 (lambda (hash-proc assoc-proc delete-proc)
74 (let ((procs (list hash-proc assoc-proc delete-proc)))
76 ((equal? procs `(,hashq ,assq ,delq!))
77 (make-mapping-hooks (wrap hashq-get-handle)
78 (wrap hashq-create-handle!)
79 (wrap hashq-remove!)))
80 ((equal? procs `(,hashv ,assv ,delv!))
81 (make-mapping-hooks (wrap hashv-get-handle)
82 (wrap hashv-create-handle!)
83 (wrap hashv-remove!)))
84 ((equal? procs `(,hash ,assoc ,delete!))
85 (make-mapping-hooks (wrap hash-get-handle)
86 (wrap hash-create-handle!)
89 (make-mapping-hooks (wrap
91 (hashx-get-handle hash-proc assoc-proc table key)))
94 (hashx-create-handle hash-proc assoc-proc table key)))
97 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
99 (define (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
100 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
102 (define (hash-table-mapping . options)
103 (let* ((size (or (and options (number? (car options)) (car options))
105 (hash-proc (or (kw-arg-ref options #:hash-proc) hash))
106 (assoc-proc (or (kw-arg-ref options #:assoc-proc)
108 ((eq? hash-proc hash) assoc)
109 ((eq? hash-proc hashv) assv)
110 ((eq? hash-proc hashq) assq)
111 (else (error 'hash-table-mapping
112 "Hash-procedure specified with no known assoc function."
114 (delete-proc (or (kw-arg-ref options #:delete-proc)
116 ((eq? hash-proc hash) delete!)
117 ((eq? hash-proc hashv) delv!)
118 ((eq? hash-proc hashq) delq!)
119 (else (error 'hash-table-mapping
120 "Hash-procedure specified with no known delete function."
122 (table-constructor (or (kw-arg-ref options #:table-constructor)
123 (lambda (len) (make-vector len '())))))
124 (make-hash-table-mapping (table-constructor size)