Inline helpers into slot-ref, slot-set!, etc
[bpt/guile.git] / module / ice-9 / mapping.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1996, 2001, 2006, 2013 Free Software Foundation, Inc.
4 ;;;;
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 3 of the License, or (at your option) any later version.
9 ;;;;
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.
14 ;;;;
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
18 ;;;;
19
20 \f
21
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))
31
32 (issue-deprecation-warning
33 "(ice-9 mapping) is deprecated. Use srfi-69 or rnrs hash tables instead.")
34
35 (define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
36 create-handle
37 remove)))
38
39
40 (define make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
41 (define mapping-hooks? (record-predicate mapping-hooks-type))
42 (define mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
43 (define mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
44 (define mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
45
46 (define mapping-type (make-record-type 'mapping '(hooks data)))
47 (define make-mapping (record-constructor mapping-type))
48 (define mapping? (record-predicate mapping-type))
49 (define mapping-hooks (record-accessor mapping-type 'hooks))
50 (define mapping-data (record-accessor mapping-type 'data))
51 (define set-mapping-hooks! (record-modifier mapping-type 'hooks))
52 (define set-mapping-data! (record-modifier mapping-type 'data))
53
54 (define (mapping-get-handle map key)
55 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
56 (define (mapping-create-handle! map key init)
57 ((mapping-hooks-create-handle (mapping-hooks map)) map key init))
58 (define (mapping-remove! map key)
59 ((mapping-hooks-remove (mapping-hooks map)) map key))
60
61 (define* (mapping-ref map key #:optional dflt)
62 (cond
63 ((mapping-get-handle map key) => cdr)
64 (else dflt)))
65
66 (define (mapping-set! map key val)
67 (set-cdr! (mapping-create-handle! map key #f) val))
68
69 \f
70
71 (define hash-table-mapping-hooks
72 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
73
74 (perfect-funcq 17
75 (lambda (hash-proc assoc-proc)
76 (let ((procs (list hash-proc assoc-proc)))
77 (cond
78 ((equal? procs `(,hashq ,assq))
79 (make-mapping-hooks (wrap hashq-get-handle)
80 (wrap hashq-create-handle!)
81 (wrap hashq-remove!)))
82 ((equal? procs `(,hashv ,assv))
83 (make-mapping-hooks (wrap hashv-get-handle)
84 (wrap hashv-create-handle!)
85 (wrap hashv-remove!)))
86 ((equal? procs `(,hash ,assoc))
87 (make-mapping-hooks (wrap hash-get-handle)
88 (wrap hash-create-handle!)
89 (wrap hash-remove!)))
90 (else
91 (make-mapping-hooks (wrap
92 (lambda (table key)
93 (hashx-get-handle hash-proc assoc-proc table key)))
94 (wrap
95 (lambda (table key init)
96 (hashx-create-handle! hash-proc assoc-proc table key init)))
97 (wrap
98 (lambda (table key)
99 (hashx-remove! hash-proc assoc-proc table key)))))))))))
100
101 (define (make-hash-table-mapping table hash-proc assoc-proc)
102 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc) table))
103
104 (define* (hash-table-mapping #:optional (size 71) #:key
105 (hash-proc hash)
106 (assoc-proc
107 (or (assq-ref `((,hashq . ,assq)
108 (,hashv . ,assv)
109 (,hash . ,assoc))
110 hash-proc)
111 (error 'hash-table-mapping
112 "Hash-procedure specified with no known assoc function."
113 hash-proc)))
114 (table-constructor
115 (lambda (len) (make-vector len '()))))
116 (make-hash-table-mapping (table-constructor size)
117 hash-proc
118 assoc-proc))