maintainer changed: was lord, now jimb; first import
[bpt/guile.git] / ice-9 / mapping.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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.
18 ;;;;
19
20 \f
21
22 (define-module #/ice-9/mapping
23 :use-module #/ice-9/poe)
24
25 (define-public mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
26 create-handle
27 remove)))
28
29
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))
35
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))
43
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))
50
51 (define-public (mapping-ref map key . dflt)
52 (cond
53 ((mapping-get-handle map key) => cdr)
54 (dflt => car)
55 (else #f)))
56
57 (define-public (mapping-set! map key val)
58 (set-cdr! (mapping-create-handle! map key #f) val))
59
60 \f
61
62 (define-public hash-table-mapping-hooks
63 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
64
65 (perfect-funcq 17
66 (lambda (hash-proc assoc-proc delete-proc)
67 (let ((procs (list hash-proc assoc-proc delete-proc)))
68 (cond
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!)
80 (wrap hash-remove!)))
81 (else
82 (make-mapping-hooks (wrap
83 (lambda (table key)
84 (hashx-get-handle hash-proc assoc-proc table key)))
85 (wrap
86 (lambda (table key)
87 (hashx-create-handle hash-proc assoc-proc table key)))
88 (wrap
89 (lambda (table key)
90 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
91
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))
94
95 (define-public (hash-table-mapping . options)
96 (let* ((size (or (and options (number? (car options)) (car options))
97 71))
98 (hash-proc (or (kw-arg-ref options :hash-proc) hash))
99 (assoc-proc (or (kw-arg-ref options :assoc-proc)
100 (cond
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."
106 hash-proc)))))
107 (delete-proc (or (kw-arg-ref options :delete-proc)
108 (cond
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."
114 hash-proc)))))
115 (table-constructor (or (kw-arg-ref options :table-constructor) make-vector)))
116 (make-hash-table-mapping (table-constructor size)
117 hash-proc
118 assoc-proc
119 delete-proc)))
120