Change Guile license to LGPLv3+
[bpt/guile.git] / module / ice-9 / mapping.scm
CommitLineData
0f2d19dd
JB
1;;; installed-scm-file
2
cd5fea8d 3;;;; Copyright (C) 1996, 2001, 2006 Free Software Foundation, Inc.
0f2d19dd 4;;;;
73be1d9e
MV
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
53befeb7 8;;;; version 3 of the License, or (at your option) any later version.
0f2d19dd 9;;;;
73be1d9e 10;;;; This library is distributed in the hope that it will be useful,
0f2d19dd 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
0f2d19dd 14;;;;
73be1d9e
MV
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
92205699 17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0f2d19dd
JB
18;;;;
19
20\f
21
8bb7330c 22(define-module (ice-9 mapping)
1a179b03
MD
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))
31
32(define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
33 create-handle
34 remove)))
35
36
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))
42
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))
50
51(define (mapping-get-handle map key)
0f2d19dd 52 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
1a179b03 53(define (mapping-create-handle! map key . opts)
0f2d19dd 54 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
1a179b03 55(define (mapping-remove! map key)
0f2d19dd
JB
56 ((mapping-hooks-remove (mapping-hooks map)) map key))
57
1a179b03 58(define (mapping-ref map key . dflt)
0f2d19dd
JB
59 (cond
60 ((mapping-get-handle map key) => cdr)
61 (dflt => car)
62 (else #f)))
63
1a179b03 64(define (mapping-set! map key val)
0f2d19dd
JB
65 (set-cdr! (mapping-create-handle! map key #f) val))
66
67\f
68
1a179b03 69(define hash-table-mapping-hooks
0f2d19dd
JB
70 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
71
72 (perfect-funcq 17
73 (lambda (hash-proc assoc-proc delete-proc)
74 (let ((procs (list hash-proc assoc-proc delete-proc)))
75 (cond
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!)
87 (wrap hash-remove!)))
88 (else
89 (make-mapping-hooks (wrap
90 (lambda (table key)
91 (hashx-get-handle hash-proc assoc-proc table key)))
92 (wrap
93 (lambda (table key)
94 (hashx-create-handle hash-proc assoc-proc table key)))
95 (wrap
96 (lambda (table key)
97 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
98
1a179b03 99(define (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
0f2d19dd
JB
100 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
101
1a179b03 102(define (hash-table-mapping . options)
0f2d19dd
JB
103 (let* ((size (or (and options (number? (car options)) (car options))
104 71))
ffa04c1b
MV
105 (hash-proc (or (kw-arg-ref options #:hash-proc) hash))
106 (assoc-proc (or (kw-arg-ref options #:assoc-proc)
0f2d19dd
JB
107 (cond
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."
113 hash-proc)))))
ffa04c1b 114 (delete-proc (or (kw-arg-ref options #:delete-proc)
0f2d19dd
JB
115 (cond
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."
121 hash-proc)))))
ffa04c1b 122 (table-constructor (or (kw-arg-ref options #:table-constructor)
95b6af86 123 (lambda (len) (make-vector len '())))))
0f2d19dd
JB
124 (make-hash-table-mapping (table-constructor size)
125 hash-proc
126 assoc-proc
127 delete-proc)))
128