Debian Codegraph @

%3a146/gleckler/hamt-map.sls

raw
1 ;;;; Persistent Hash Map
2
3 ;;; Copyright MMXV-MMXVII Arthur A. Gleckler. All rights reserved.
4
5 ;; Permission is hereby granted, free of charge, to any person
6 ;; obtaining a copy of this software and associated documentation
7 ;; files (the "Software"), to deal in the Software without
8 ;; restriction, including without limitation the rights to use, copy,
9 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
10 ;; of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
12
13 ;; The above copyright notice and this permission notice shall be
14 ;; included in all copies or substantial portions of the Software.
15
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 ;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 ;; DEALINGS IN THE SOFTWARE.
24 (library (srfi :146 gleckler hamt-map)
25 (export make-phm phm?
26 phm->alist
27 phm/add-alist phm/add-alist!
28 phm/contains?
29 phm/count
30 phm/empty?
31 phm/for-each
32 phm/get
33 phm/immutable
34 phm/keys
35 phm/mutable phm/mutable?
36 phm/put
37 phm/put!
38 phm/remove phm/remove!
39 phm/replace phm/replace!
40
41 ;; This is only needed by tests:
42 phm/data)
43 (import (except (rnrs) assert)
44 (only (srfi :1) fold)
45 (srfi :16)
46 (srfi :146 gleckler hamt)
47 (srfi :146 gleckler hamt-misc))
48
49
50 ;;; Public protocol (API)
51
52 ;; (phm? datum)
53
54 ;; Return true iff `datum' is a persistent hash map.
55
56 ;; (make-phm hash = [alist])
57
58 ;; Return a new immutable persistent hash map that uses the `hash'
59 ;; procedure to hash its keys and `=' to compare them. If `alist'
60 ;; is supplied, include all its keys and data in the result. Later
61 ;; occurrences of the same key override earlier ones.
62
63 ;; (phm/count phm)
64
65 ;; Return the number of elements in `phm'.
66
67 ;; (phm/empty? phm)
68
69 ;; Return true iff `phm' is empty.
70
71 ;; (phm/immutable phm)
72 ;; (phm/immutable phm replace)
73
74 ;; Return a PHM equivalent to `phm', but that is immutable. Even if
75 ;; `phm' is mutable, no change to it will affect the returned one.
76 ;; If `replace' is supplied, replace the datum associated with each
77 ;; key whose value has been modified since the PHM was made mutable
78 ;; with the result of calling `replace' on that key and datum. This
79 ;; is useful for converting PHSs (sets) stored as values in a PHM
80 ;; back into immutable ones when the containing PHM is made
81 ;; immutable.
82
83 ;; (phm/mutable phm)
84
85 ;; Return a PHM equivalent to `phm', but that is mutable. If `phm'
86 ;; was immutable, no change to the returned PHM will affect `phm'.
87
88 ;; (phm/mutable? phm)
89
90 ;; Return true iff `phm' is mutable.
91
92 ;; (phm/put phm key datum)
93
94 ;; Return a PHM equivalent to `phm' except that `datum' is at `key'.
95
96 ;; (phm/put! phm key datum)
97
98 ;; Return a PHM equivalent to `phm' except that `datum' is at `key'.
99 ;; Modify `phm', which must be mutable, in the process.
100
101 ;; (phm/replace phm key replace)
102
103 ;; Return a PHM equivalent to `phm' except that whatever value is at
104 ;; `key' has been replaced by `(replace datum)', or `(replace
105 ;; hamt-null)' if there was no value there already. If `replace'
106 ;; returns `hamt-null', the value is removed.
107
108 ;; (phm/replace! phm key replace)
109
110 ;; Return a PHM equivalent to `phm' except that whatever value is at
111 ;; `key' has been replaced by `(replace datum)', or `(replace
112 ;; hamt-null)' if there was no value there already. If `replace'
113 ;; returns `hamt-null', the value is removed. Modify `phm', which
114 ;; must be mutable, in the process.
115
116 ;; (phm/get phm key [default])
117
118 ;; Return the datum stored at `key' in `phm'. If none is present,
119 ;; return `default' if it was supplied, or #f if it was not.
120
121 ;; (phm/contains? phm key)
122
123 ;; Return true iff `phm' has a datum at `key'.
124
125 ;; (phm/remove phm key)
126
127 ;; Return a PHM equivalent to `phm' except that there is no datum at
128 ;; `key'.
129
130 ;; (phm/remove! phm key)
131
132 ;; Return a PHM equivalent to `phm' except that there is no datum at
133 ;; `key'. Modify `phm', which must be mutable, in the process.
134
135 ;; (phm/add-alist phm alist)
136
137 ;; Return a PHM equivalent to `phm' except that, for every pair in
138 ;; `alist', the datum in its cdr is stored in the new PHM at the key
139 ;; in its car. Later occurrences of the same key override earlier
140 ;; ones.
141
142 ;; (phm/add-alist! phm alist)
143
144 ;; Return a PHM equivalent to `phm' except that, for every pair in
145 ;; `alist', the datum in its cdr is stored in the new PHM at the key
146 ;; in its car. Later occurrences of the same key override earlier
147 ;; ones. Modify `phm', which must be mutable, in the process.
148
149 ;; (phm->alist phm)
150
151 ;; Return an alist mapping the keys in `phm' to their values.
152
153 ;; (phm/keys phm)
154
155 ;; Return a list of the keys in `phm'.
156
157 ;; (phm/for-each procedure phm)
158
159 ;; Run `procedure' on each key and datum in `phm'.
160
161 ;;; Implementation of public protocol (API)
162
163 (define (phm? datum)
164 (and (hash-array-mapped-trie? datum)
165 (hamt/payload? datum)))
166
167 (define (make-phm-inner hash = alist)
168 (let ((phm (make-hamt = hash #t)))
169 (if (null? alist)
170 phm
171 (let ((phm-1 (phm/mutable phm)))
172 (phm/add-alist! phm-1 alist)
173 (phm/immutable phm-1)))))
174
175 (define make-phm
176 (case-lambda
177 ((hash =) (make-phm-inner hash = '()))
178 ((hash = alist) (make-phm-inner hash = alist))))
179
180 (define (phm/count phm)
181 (assert (phm? phm))
182 (hamt/count phm))
183
184 (define (phm/empty? phm)
185 (assert (phm? phm))
186 (hamt/empty? phm))
187
188 (define phm/immutable
189 (case-lambda
190 ((phm)
191 (assert (phm? phm))
192 (hamt/immutable phm))
193 ((phm replace)
194 (assert (phm? phm))
195 (hamt/immutable phm replace))))
196
197 (define (phm/mutable phm)
198 (assert (phm? phm))
199 (hamt/mutable phm))
200
201 (define (phm/mutable? phm)
202 (assert (phm? phm))
203 (hamt/mutable? phm))
204
205 (define (phm/put phm key datum)
206 (assert (phm? phm))
207 (hamt/put phm key datum))
208
209 (define (phm/put! phm key datum)
210 (assert (phm? phm))
211 (hamt/put! phm key datum))
212
213 (define (phm/replace phm key replace)
214 (assert (phm? phm))
215 (hamt/replace phm key replace))
216
217 (define (phm/replace! phm key replace)
218 (assert (phm? phm))
219 (hamt/replace! phm key replace))
220
221 (define (phm/get-inner phm key default)
222 (assert (phm? phm))
223 (let ((result (hamt-fetch phm key)))
224 (if (hamt-null? result)
225 default
226 result)))
227
228 (define phm/get
229 (case-lambda
230 ((phm key) (phm/get-inner phm key #f))
231 ((phm key default) (phm/get-inner phm key default))))
232
233 (define (phm/contains? phm key)
234 (assert (phm? phm))
235 (not (hamt-null? (hamt-fetch phm key))))
236
237 (define (phm/remove phm key)
238 (assert (phm? phm))
239 (phm/put phm key hamt-null))
240
241 (define (phm/remove! phm key)
242 (assert (phm? phm))
243 (assert (hamt/mutable? phm))
244 (phm/put! phm key hamt-null))
245
246 (define (phm/add-alist phm alist)
247 (assert (phm? phm))
248 (fold (lambda (a phm) (phm/put phm (car a) (cdr a))) phm alist))
249
250 (define (phm/add-alist! phm alist)
251 (assert (phm? phm))
252 (do-list (a alist)
253 (phm/put! phm (car a) (cdr a)))
254 phm)
255
256 (define (phm->alist phm)
257 (assert (phm? phm))
258 (hamt->list phm cons))
259
260 (define (phm/data phm)
261 (assert (phm? phm))
262 (hamt->list phm (lambda (k d) d)))
263
264 (define (phm/keys phm)
265 (assert (phm? phm))
266 (hamt->list phm (lambda (k d) k)))
267
268 (define (phm/for-each procedure phm)
269 (assert (phm? phm))
270 (hamt/for-each procedure phm)))