Rewrite %initialize-object in Scheme
[bpt/guile.git] / module / ice-9 / mapping.scm
CommitLineData
0f2d19dd
JB
1;;; installed-scm-file
2
72ad03fc 3;;;; Copyright (C) 1996, 2001, 2006, 2013 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
72ad03fc
AW
32(issue-deprecation-warning
33 "(ice-9 mapping) is deprecated. Use srfi-69 or rnrs hash tables instead.")
34
1a179b03
MD
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)
0f2d19dd 55 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
1746b8ff
DH
56(define (mapping-create-handle! map key init)
57 ((mapping-hooks-create-handle (mapping-hooks map)) map key init))
1a179b03 58(define (mapping-remove! map key)
0f2d19dd
JB
59 ((mapping-hooks-remove (mapping-hooks map)) map key))
60
1746b8ff 61(define* (mapping-ref map key #:optional dflt)
0f2d19dd 62 (cond
1746b8ff
DH
63 ((mapping-get-handle map key) => cdr)
64 (else dflt)))
0f2d19dd 65
1a179b03 66(define (mapping-set! map key val)
0f2d19dd
JB
67 (set-cdr! (mapping-create-handle! map key #f) val))
68
69\f
70
1a179b03 71(define hash-table-mapping-hooks
0f2d19dd
JB
72 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
73
74 (perfect-funcq 17
1746b8ff
DH
75 (lambda (hash-proc assoc-proc)
76 (let ((procs (list hash-proc assoc-proc)))
0f2d19dd 77 (cond
1746b8ff 78 ((equal? procs `(,hashq ,assq))
0f2d19dd
JB
79 (make-mapping-hooks (wrap hashq-get-handle)
80 (wrap hashq-create-handle!)
81 (wrap hashq-remove!)))
1746b8ff 82 ((equal? procs `(,hashv ,assv))
0f2d19dd
JB
83 (make-mapping-hooks (wrap hashv-get-handle)
84 (wrap hashv-create-handle!)
85 (wrap hashv-remove!)))
1746b8ff 86 ((equal? procs `(,hash ,assoc))
0f2d19dd
JB
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
1746b8ff
DH
95 (lambda (table key init)
96 (hashx-create-handle! hash-proc assoc-proc table key init)))
0f2d19dd
JB
97 (wrap
98 (lambda (table key)
1746b8ff
DH
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))