(system-error-errno): New.
[bpt/guile.git] / ice-9 / mapping.scm
CommitLineData
0f2d19dd
JB
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
15328041
JB
17;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18;;;; Boston, MA 02111-1307 USA
a482f2cc
MV
19;;;;
20;;;; As a special exception, the Free Software Foundation gives permission
21;;;; for additional uses of the text contained in its release of GUILE.
22;;;;
23;;;; The exception is that, if you link the GUILE library with other files
24;;;; to produce an executable, this does not by itself cause the
25;;;; resulting executable to be covered by the GNU General Public License.
26;;;; Your use of that executable is in no way restricted on account of
27;;;; linking the GUILE library code into it.
28;;;;
29;;;; This exception does not however invalidate any other reasons why
30;;;; the executable file might be covered by the GNU General Public License.
31;;;;
32;;;; This exception applies only to the code released by the
33;;;; Free Software Foundation under the name GUILE. If you copy
34;;;; code from other Free Software Foundation releases into a copy of
35;;;; GUILE, as the General Public License permits, the exception does
36;;;; not apply to the code that you add in this way. To avoid misleading
37;;;; anyone as to the status of such modified files, you must delete
38;;;; this exception notice from them.
39;;;;
40;;;; If you write modifications of your own for GUILE, it is your choice
41;;;; whether to permit this exception to apply to your modifications.
42;;;; If you do not wish that, delete this exception notice.
0f2d19dd
JB
43;;;;
44
45\f
46
8bb7330c
JB
47(define-module (ice-9 mapping)
48 :use-module (ice-9 poe))
0f2d19dd
JB
49
50(define-public mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
51 create-handle
52 remove)))
53
54
55(define-public make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
56(define-public mapping-hooks? (record-predicate mapping-hooks-type))
57(define-public mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
58(define-public mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
59(define-public mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
60
61(define-public mapping-type (make-record-type 'mapping '(hooks data)))
62(define-public make-mapping (record-constructor mapping-type))
63(define-public mapping? (record-predicate mapping-type))
64(define-public mapping-hooks (record-accessor mapping-type 'hooks))
65(define-public mapping-data (record-accessor mapping-type 'data))
66(define-public set-mapping-hooks! (record-modifier mapping-type 'hooks))
67(define-public set-mapping-data! (record-modifier mapping-type 'data))
68
69(define-public (mapping-get-handle map key)
70 ((mapping-hooks-get-handle (mapping-hooks map)) map key))
71(define-public (mapping-create-handle! map key . opts)
72 (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
73(define-public (mapping-remove! map key)
74 ((mapping-hooks-remove (mapping-hooks map)) map key))
75
76(define-public (mapping-ref map key . dflt)
77 (cond
78 ((mapping-get-handle map key) => cdr)
79 (dflt => car)
80 (else #f)))
81
82(define-public (mapping-set! map key val)
83 (set-cdr! (mapping-create-handle! map key #f) val))
84
85\f
86
87(define-public hash-table-mapping-hooks
88 (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
89
90 (perfect-funcq 17
91 (lambda (hash-proc assoc-proc delete-proc)
92 (let ((procs (list hash-proc assoc-proc delete-proc)))
93 (cond
94 ((equal? procs `(,hashq ,assq ,delq!))
95 (make-mapping-hooks (wrap hashq-get-handle)
96 (wrap hashq-create-handle!)
97 (wrap hashq-remove!)))
98 ((equal? procs `(,hashv ,assv ,delv!))
99 (make-mapping-hooks (wrap hashv-get-handle)
100 (wrap hashv-create-handle!)
101 (wrap hashv-remove!)))
102 ((equal? procs `(,hash ,assoc ,delete!))
103 (make-mapping-hooks (wrap hash-get-handle)
104 (wrap hash-create-handle!)
105 (wrap hash-remove!)))
106 (else
107 (make-mapping-hooks (wrap
108 (lambda (table key)
109 (hashx-get-handle hash-proc assoc-proc table key)))
110 (wrap
111 (lambda (table key)
112 (hashx-create-handle hash-proc assoc-proc table key)))
113 (wrap
114 (lambda (table key)
115 (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
116
117(define-public (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
118 (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
119
120(define-public (hash-table-mapping . options)
121 (let* ((size (or (and options (number? (car options)) (car options))
122 71))
123 (hash-proc (or (kw-arg-ref options :hash-proc) hash))
124 (assoc-proc (or (kw-arg-ref options :assoc-proc)
125 (cond
126 ((eq? hash-proc hash) assoc)
127 ((eq? hash-proc hashv) assv)
128 ((eq? hash-proc hashq) assq)
129 (else (error 'hash-table-mapping
130 "Hash-procedure specified with no known assoc function."
131 hash-proc)))))
132 (delete-proc (or (kw-arg-ref options :delete-proc)
133 (cond
134 ((eq? hash-proc hash) delete!)
135 ((eq? hash-proc hashv) delv!)
136 ((eq? hash-proc hashq) delq!)
137 (else (error 'hash-table-mapping
138 "Hash-procedure specified with no known delete function."
139 hash-proc)))))
95b6af86
JB
140 (table-constructor (or (kw-arg-ref options :table-constructor)
141 (lambda (len) (make-vector len '())))))
0f2d19dd
JB
142 (make-hash-table-mapping (table-constructor size)
143 hash-proc
144 assoc-proc
145 delete-proc)))
146