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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 (define-module #/ice-9/mapping
23 :use-module #/ice-9/poe)
25 (define-public mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
30 (define-public make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
31 (define-public mapping-hooks? (record-predicate mapping-hooks-type))
32 (define-public mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
33 (define-public mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
34 (define-public mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
36 (define-public mapping-type (make-record-type 'mapping '(hooks data)))
37 (define-public make-mapping (record-constructor mapping-type))
38 (define-public mapping? (record-predicate mapping-type))
39 (define-public mapping-hooks (record-accessor mapping-type 'hooks))
40 (define-public mapping-data (record-accessor mapping-type 'data))
41 (define-public set-mapping-hooks! (record-modifier mapping-type 'hooks))
42 (define-public set-mapping-data! (record-modifier mapping-type 'data))
44 (define-public (mapping-get-handle map key)
45 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
46 (define-public (mapping-create-handle! map key . opts)
47 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
48 (define-public (mapping-remove! map key)
49 ((mapping-hooks-remove (mapping-hooks map)) map key))
51 (define-public (mapping-ref map key . dflt)
53 ((mapping-get-handle map key) => cdr)
57 (define-public (mapping-set! map key val)
58 (set-cdr! (mapping-create-handle! map key #f) val))
62 (define-public hash-table-mapping-hooks
63 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
66 (lambda (hash-proc assoc-proc delete-proc)
67 (let ((procs (list hash-proc assoc-proc delete-proc)))
69 ((equal? procs `(,hashq ,assq ,delq!))
70 (make-mapping-hooks (wrap hashq-get-handle)
71 (wrap hashq-create-handle!)
72 (wrap hashq-remove!)))
73 ((equal? procs `(,hashv ,assv ,delv!))
74 (make-mapping-hooks (wrap hashv-get-handle)
75 (wrap hashv-create-handle!)
76 (wrap hashv-remove!)))
77 ((equal? procs `(,hash ,assoc ,delete!))
78 (make-mapping-hooks (wrap hash-get-handle)
79 (wrap hash-create-handle!)
82 (make-mapping-hooks (wrap
84 (hashx-get-handle hash-proc assoc-proc table key)))
87 (hashx-create-handle hash-proc assoc-proc table key)))
90 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
92 (define-public (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
93 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
95 (define-public (hash-table-mapping . options)
96 (let* ((size (or (and options (number? (car options)) (car options))
98 (hash-proc (or (kw-arg-ref options :hash-proc) hash))
99 (assoc-proc (or (kw-arg-ref options :assoc-proc)
101 ((eq? hash-proc hash) assoc)
102 ((eq? hash-proc hashv) assv)
103 ((eq? hash-proc hashq) assq)
104 (else (error 'hash-table-mapping
105 "Hash-procedure specified with no known assoc function."
107 (delete-proc (or (kw-arg-ref options :delete-proc)
109 ((eq? hash-proc hash) delete!)
110 ((eq? hash-proc hashv) delv!)
111 ((eq? hash-proc hashq) delq!)
112 (else (error 'hash-table-mapping
113 "Hash-procedure specified with no known delete function."
115 (table-constructor (or (kw-arg-ref options :table-constructor) make-vector)))
116 (make-hash-table-mapping (table-constructor size)