Debian Codegraph @

%3a125/125.body.scm

raw symbols SCIP
1 ;;; Copyright 2015 William D Clinger.
2 ;;;
3 ;;; Permission to copy this software, in whole or in part, to use this
4 ;;; software for any lawful purpose, and to redistribute this software
5 ;;; is granted subject to the restriction that all copies made of this
6 ;;; software must include this copyright and permission notice in full.
7 ;;;
8 ;;; I also request that you send me a copy of any improvements that you
9 ;;; make to this software so that they may be incorporated within it to
10 ;;; the benefit of the Scheme community.
11 ;;;
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
14 ;;; Private stuff, not exported.
15
16 ;;; Ten of the SRFI 125 procedures are deprecated, and another
17 ;;; two allow alternative arguments that are deprecated.
18
19 (define (issue-deprecated-warnings?) #t)
20
21 (define (issue-warning-deprecated name-of-deprecated-misfeature)
22 (if (not (memq name-of-deprecated-misfeature already-warned))
23 (begin
24 (set! already-warned
25 (cons name-of-deprecated-misfeature already-warned))
26 (if (issue-deprecated-warnings?)
27 (let ((out (current-error-port)))
28 (display "WARNING: " out)
29 (display name-of-deprecated-misfeature out)
30 (newline out)
31 (display " is deprecated by SRFI 125. See" out)
32 (newline out)
33 (display " " out)
34 (display url:deprecated out)
35 (newline out))))))
36
37 (define url:deprecated
38 "http://srfi.schemers.org/srfi-125/srfi-125.html")
39
40 ; List of deprecated features for which a warning has already
41 ; been issued.
42
43 (define already-warned '())
44
45 ;;; Comparators contain a type test predicate, which implementations
46 ;;; of the hash-table-set! procedure can use to reject invalid keys.
47 ;;; That's hard to do without sacrificing interoperability with R6RS
48 ;;; and/or SRFI 69 and/or SRFI 126 hash tables.
49 ;;;
50 ;;; Full interoperability means the hash tables implemented here are
51 ;;; interchangeable with the SRFI 126 hashtables used to implement them.
52 ;;; SRFI 69 and R6RS and SRFI 126 hashtables don't contain comparators,
53 ;;; so any association between a hash table and its comparator would have
54 ;;; to be maintained outside the representation of hash tables themselves,
55 ;;; which is problematic unless weak pointers are available.
56 ;;;
57 ;;; Not all of the hash tables implemented here will have comparators
58 ;;; associated with them anyway, because an equivalence procedure
59 ;;; and hash function can be used to create a hash table instead of
60 ;;; a comparator (although that usage is deprecated by SRFI 125).
61 ;;;
62 ;;; One way to preserve interoperability while enforcing a comparator's
63 ;;; type test is to incorporate that test into a hash table's hash
64 ;;; function. The advantage of doing that should be weighed against
65 ;;; these disadvantages:
66 ;;;
67 ;;; If the type test is slow, then hashing would also be slower.
68 ;;;
69 ;;; The R6RS, SRFI 69, and SRFI 126 APIs allow extraction of
70 ;;; a hash function from some hash tables.
71 ;;; Some programmers might expect that hash function to be the
72 ;;; hash function encapsulated by the comparator (in the sense
73 ;;; of eq?, perhaps) even though this API makes no such guarantee
74 ;;; (and extraction of that hash function from an existing hash
75 ;;; table can only be done by calling a deprecated procedure).
76
77 ;;; If %enforce-comparator-type-tests is true, then make-hash-table,
78 ;;; when passed a comparator, will use a hash function that enforces
79 ;;; the comparator's type test.
80
81 (define %enforce-comparator-type-tests #t)
82
83 ;;; Given a comparator, return its hash function, possibly augmented
84 ;;; by the comparator's type test.
85
86 (define (%comparator-hash-function comparator)
87 (let ((okay? (comparator-type-test-predicate comparator))
88 (hash-function (comparator-hash-function comparator)))
89 (if %enforce-comparator-type-tests
90 (lambda (x . rest)
91 (cond ((not (okay? x))
92 (error #f "key rejected by hash-table comparator"
93 x
94 comparator))
95 ((null? rest)
96 (hash-function x))
97 (else
98 (apply hash-function x rest))))
99 hash-function)))
100
101 ;;; A unique (in the sense of eq?) value that will never be found
102 ;;; within a hash-table.
103
104 (define %not-found (list '%not-found))
105
106 ;;; A unique (in the sense of eq?) value that escapes only as an irritant
107 ;;; when a hash-table key is not found.
108
109 (define %not-found-irritant (list 'not-found))
110
111 ;;; The error message used when a hash-table key is not found.
112
113 (define %not-found-message "hash-table key not found")
114
115 ;;; We let SRFI 126 decide which weakness is supported
116 (define (%check-optional-arguments procname args)
117 (if (memq 'thread-safe args)
118 (error (string-append (symbol->string procname)
119 ": unsupported optional argument(s)")
120 args)))
121
122 (define (%get-hash-table-weakness args)
123 (cond
124 ((memq 'ephemeral-values args)
125 (if (or (memq 'ephemeral-keys args)
126 (memq 'weak-keys args))
127 'ephemeral-key-and-value
128 'ephemeral-value))
129 ((memq 'ephemeral-keys args)
130 (if (memq 'weak-values args)
131 'ephemeral-key-and-value
132 'ephemeral-key))
133 ((memq 'weak-keys args)
134 (if (memq 'weak-values args)
135 'weak-key-and-value
136 'weak-key))
137 ((memq 'weak-values args)
138 'weak-value)
139 (else #f)))
140
141 (define (%get-hash-table-capacity args)
142 (find fixnum? args))
143
144 ;;; This was exported by an earlier draft of SRFI 125,
145 ;;; and is still used by hash-table=?
146
147 (define (hash-table-every proc ht)
148 (call-with-values
149 (lambda () (hashtable-entries ht))
150 (lambda (keys vals)
151 (let ((size (vector-length keys)))
152 (let loop ((i 0))
153 (or (fx>=? i size)
154 (let* ((key (vector-ref keys i))
155 (val (vector-ref vals i)))
156 (and (proc key val)
157 (loop (fx+ i 1))))))))))
158
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 ;;;
161 ;;; Exported procedures
162 ;;;
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164
165 ;;; Constructors.
166
167 ;;; The first argument can be a comparator or an equality predicate.
168 ;;;
169 ;;; If the first argument is a comparator, any remaining arguments
170 ;;; are implementation-dependent, but a non-negative exact integer
171 ;;; should be interpreted as an initial capacity and the symbols
172 ;;; thread-safe, weak-keys, ephemeral-keys, weak-values, and
173 ;;; emphemeral-values should be interpreted specially. (These
174 ;;; special symbols are distinct from the analogous special symbols
175 ;;; in SRFI 126.)
176 ;;;
177 ;;; If the first argument is not a comparator, then it had better
178 ;;; be an equality predicate (which is deprecated by SRFI 125).
179 ;;; If a second argument is present and is a procedure, then it's
180 ;;; a hash function (which is allowed only for the deprecated case
181 ;;; in which the first argument is an equality predicate). If a
182 ;;; second argument is not a procedure, then it's some kind of
183 ;;; implementation-dependent optional argument, as are all arguments
184 ;;; beyond the second.
185 ;;;
186 ;;; SRFI 128 defines make-eq-comparator, make-eqv-comparator, and
187 ;;; make-equal-comparator procedures whose hash function is the
188 ;;; default-hash procedure of SRFI 128, which is inappropriate
189 ;;; for use with eq? and eqv? unless the object being hashed is
190 ;;; never mutated. Neither SRFI 125 nor 128 provide any way to
191 ;;; define a comparator whose hash function is truly compatible
192 ;;; with the use of eq? or eqv? as an equality predicate.
193 ;;;
194 ;;; That would make SRFI 125 almost as bad as SRFI 69 if not for
195 ;;; the following paragraph of SRFI 125:
196 ;;;
197 ;;; Implementations are permitted to ignore user-specified
198 ;;; hash functions in certain circumstances. Specifically,
199 ;;; if the equality predicate, whether passed as part of a
200 ;;; comparator or explicitly, is more fine-grained (in the
201 ;;; sense of R7RS-small section 6.1) than equal?, the
202 ;;; implementation is free — indeed, is encouraged — to
203 ;;; ignore the user-specified hash function and use something
204 ;;; implementation-dependent. This allows the use of addresses
205 ;;; as hashes, in which case the keys must be rehashed if
206 ;;; they are moved by the garbage collector. Such a hash
207 ;;; function is unsafe to use outside the context of
208 ;;; implementation-provided hash tables. It can of course be
209 ;;; exposed by an implementation as an extension, with
210 ;;; suitable warnings against inappropriate uses.
211 ;;;
212 ;;; That gives implementations permission to do something more
213 ;;; useful, but when should implementations take advantage of
214 ;;; that permission? This implementation uses the superior
215 ;;; solution provided by SRFI 126 whenever:
216 ;;;
217 ;;; A comparator is passed as first argument and its equality
218 ;;; predicate is eq? or eqv?.
219 ;;;
220 ;;; The eq? or eqv? procedure is passed as first argument
221 ;;; (which is a deprecated usage).
222
223 (define (make-hash-table comparator/equiv . rest)
224 (if (comparator? comparator/equiv)
225 (let ((equiv (comparator-equality-predicate comparator/equiv))
226 (hash-function (%comparator-hash-function comparator/equiv)))
227 (%make-hash-table equiv hash-function rest))
228 (let* ((equiv comparator/equiv)
229 (hash-function (if (and (not (null? rest))
230 (procedure? (car rest)))
231 (car rest)
232 #f))
233 (rest (if hash-function (cdr rest) rest)))
234 (issue-warning-deprecated 'srfi-69-style:make-hash-table)
235 (%make-hash-table equiv hash-function rest))))
236
237 (define (%make-hash-table equiv hash-function opts)
238 (%check-optional-arguments 'make-hash-table opts)
239 (let ((weakness (%get-hash-table-weakness opts))
240 (capacity (%get-hash-table-capacity opts)))
241 ;; Use SRFI :126 make-hashtable to handle capacity and weakness
242 (cond ((equal? equiv eq?)
243 (make-eq-hashtable capacity weakness))
244 ((equal? equiv eqv?)
245 (make-eqv-hashtable capacity weakness))
246 (hash-function
247 (make-hashtable hash-function equiv capacity weakness))
248 ((equal? equiv equal?)
249 (make-hashtable equal-hash equiv capacity weakness))
250 ((equal? equiv string=?)
251 (make-hashtable string-hash equiv capacity weakness))
252 ((equal? equiv string-ci=?)
253 (make-hashtable string-ci-hash equiv capacity weakness))
254 ((equal? equiv symbol=?)
255 (make-hashtable symbol-hash equiv capacity weakness))
256 (else
257 (error "make-hash-table: unable to infer hash function"
258 equiv)))))
259
260 (define (hash-table comparator . rest)
261 (let ((ht (apply make-hash-table comparator rest)))
262 (let loop ((kvs rest))
263 (cond
264 ((null? kvs) #f)
265 ((null? (cdr kvs)) (error #f "hash-table: wrong number of arguments"))
266 ((hashtable-contains? ht (car kvs))
267 (error "hash-table: two equivalent keys were provided"
268 (car kvs)))
269 (else (hashtable-set! ht (car kvs) (cadr kvs))
270 (loop (cddr kvs)))))
271 (hash-table-copy ht #f)))
272
273 (define (hash-table-unfold stop? mapper successor seed comparator . rest)
274 (let ((ht (apply make-hash-table comparator rest)))
275 (let loop ((seed seed))
276 (if (stop? seed)
277 ht
278 (call-with-values
279 (lambda () (mapper seed))
280 (lambda (key val)
281 (hash-table-set! ht key val)
282 (loop (successor seed))))))))
283
284 (define (alist->hash-table alist comparator/equiv . rest)
285 (if (and (not (null? rest))
286 (procedure? (car rest)))
287 (issue-warning-deprecated 'srfi-69-style:alist->hash-table))
288 (let ((ht (apply make-hash-table comparator/equiv rest))
289 (entries (reverse alist)))
290 (for-each (lambda (entry)
291 (hash-table-set! ht (car entry) (cdr entry)))
292 entries)
293 ht))
294
295 ;;; Predicates.
296
297 ;; (define (hash-table? obj)
298 ;; (hashtable? obj))
299
300 ;; (define (hash-table-contains? ht key)
301 ;; (hashtable-contains? ht key))
302
303 ;; (define (hash-table-empty? ht)
304 ;; (hashtable-empty? ht))
305
306 (define (hash-table=? value-comparator ht1 ht2)
307 (let ((val=? (comparator-equality-predicate value-comparator))
308 (n1 (hash-table-size ht1))
309 (n2 (hash-table-size ht2)))
310 (and (= n1 n2)
311 (eq? (hashtable-equivalence-function ht1)
312 (hashtable-equivalence-function ht2))
313 (hash-table-every (lambda (key val1)
314 (and (hash-table-contains? ht2 key)
315 (val=? val1
316 (hashtable-ref ht2 key 'ignored))))
317 ht1))))
318
319 (define (hash-table-mutable? ht)
320 (hashtable-mutable? ht))
321
322 ;;; Accessors.
323
324 (define hash-table-ref
325 (case-lambda
326 ((ht key) (hashtable-ref ht key))
327 ((ht key failure)
328 (let ((val (hashtable-ref ht key %not-found)))
329 (if (eq? val %not-found)
330 (failure)
331 val)))
332 ((ht key failure success)
333 (let ((val (hashtable-ref ht key %not-found)))
334 (if (eq? val %not-found)
335 (failure)
336 (success val))))))
337
338 (define (hash-table-ref/default ht key default)
339 (hashtable-ref ht key default))
340
341 ;;; Mutators.
342
343 (define hash-table-set!
344 (case-lambda
345 ((ht) #f)
346 ((ht key val) (hashtable-set! ht key val))
347 ((ht key1 val1 key2 val2 . others)
348 (hashtable-set! ht key1 val1)
349 (hashtable-set! ht key2 val2)
350 (apply hash-table-set! ht others))))
351
352 (define (hash-table-delete! ht . keys)
353 (let ((count 0))
354 (for-each (lambda (key)
355 (when (hashtable-contains? ht key)
356 (set! count (fx+ 1 count))
357 (hashtable-delete! ht key)))
358 keys)
359 count))
360
361 ;; (define (hash-table-intern! ht key failure)
362 ;; (hashtable-intern! ht key failure))
363
364 (define hash-table-update!
365 (case-lambda
366 ((ht key updater)
367 (hashtable-update! ht key updater))
368
369 ((ht key updater failure)
370 (let ((updater* (lambda (val)
371 (if (eq? %not-found val)
372 (updater (failure))
373 (updater val)))))
374 (hashtable-update! ht key updater* %not-found)))
375
376 ((ht key updater failure success)
377 (let* ((updater* (lambda (val)
378 (if (eq? %not-found val)
379 (updater (failure))
380 (success (updater val))))))
381 (hashtable-update! ht key updater* %not-found)))))
382
383 (define (hash-table-update!/default ht key updater default)
384 (hashtable-update! ht key updater default))
385
386 ;; (define (hash-table-pop! ht)
387 ;; (hashtable-pop! ht))
388
389 ;; (define (hash-table-clear! ht)
390 ;; (hashtable-clear! ht))
391
392 ;;; The whole hash table.
393
394 ;; (define (hash-table-size ht)
395 ;; (hashtable-size ht))
396
397 (define (hash-table-keys ht)
398 (vector->list (hashtable-keys ht)))
399
400 (define (hash-table-values ht)
401 (vector->list (hashtable-values ht)))
402
403 (define (hash-table-entries ht)
404 (call-with-values
405 (lambda () (hashtable-entries ht))
406 (lambda (keys vals)
407 (values (vector->list keys)
408 (vector->list vals)))))
409
410 (define (hash-table-find proc ht failure)
411 (call-with-values
412 (lambda () (hashtable-entries ht))
413 (lambda (keys vals)
414 (let ((size (vector-length keys)))
415 (let loop ((i 0))
416 (if (fx>=? i size)
417 (failure)
418 (let* ((key (vector-ref keys i))
419 (val (vector-ref vals i))
420 (x (proc key val)))
421 (or x (loop (fx+ i 1))))))))))
422
423 (define (hash-table-count pred ht)
424 (let ((count 0))
425 (call-with-values
426 (lambda () (hashtable-entries ht))
427 (lambda (keys vals)
428 (vector-for-each (lambda (key val)
429 (if (pred key val) (set! count (fx+ count 1))))
430 keys vals)))
431 count))
432
433 ;;; Mapping and folding.
434
435 (define (hash-table-map proc comparator ht)
436 (let ((result (make-hash-table comparator)))
437 (hash-table-for-each
438 (lambda (key val)
439 (hash-table-set! result key (proc val)))
440 ht)
441 result))
442
443 (define (hash-table-map->list proc ht)
444 (call-with-values
445 (lambda () (hash-table-entries ht))
446 (lambda (keys vals)
447 (map proc keys vals))))
448
449 ;;; With this particular implementation, the proc can safely mutate ht.
450 ;;; That property is not guaranteed by the specification, but can be
451 ;;; relied upon by procedures defined in this file.
452
453 (define (hash-table-for-each proc ht)
454 (hashtable-walk ht proc))
455
456 (define (hash-table-map! proc ht)
457 (hashtable-update-all! ht proc))
458
459 (define (hash-table-fold proc init ht)
460 (if (hashtable? proc)
461 (deprecated:hash-table-fold proc init ht)
462 (hashtable-sum ht init proc)))
463
464 (define (hash-table-prune! proc ht)
465 (hashtable-prune! ht proc))
466
467 ;;; Copying and conversion.
468
469 ;; (define hash-table-copy hashtable-copy)
470
471 (define (hash-table-empty-copy ht)
472 (let* ((ht2 (hash-table-copy ht #t))
473 (ignored (hash-table-clear! ht2)))
474 ht2))
475
476 (define (hash-table->alist ht)
477 (call-with-values
478 (lambda () (hash-table-entries ht))
479 (lambda (keys vals)
480 (map cons keys vals))))
481
482 ;;; Hash tables as sets.
483
484 (define (hash-table-union! ht1 ht2)
485 (hash-table-for-each
486 (lambda (key2 val2)
487 (if (not (hashtable-contains? ht1 key2))
488 (hashtable-set! ht1 key2 val2)))
489 ht2)
490 ht1)
491
492 (define (hash-table-intersection! ht1 ht2)
493 (hash-table-for-each
494 (lambda (key1 val1)
495 (if (not (hashtable-contains? ht2 key1))
496 (hashtable-delete! ht1 key1)))
497 ht1)
498 ht1)
499
500 (define (hash-table-difference! ht1 ht2)
501 (hash-table-for-each
502 (lambda (key1 val1)
503 (if (hashtable-contains? ht2 key1)
504 (hashtable-delete! ht1 key1)))
505 ht1)
506 ht1)
507
508 (define (hash-table-xor! ht1 ht2)
509 (hash-table-for-each
510 (lambda (key2 val2)
511 (if (hashtable-contains? ht1 key2)
512 (hashtable-delete! ht1 key2)
513 (hashtable-set! ht1 key2 val2)))
514 ht2)
515 ht1)
516
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
518 ;;;
519 ;;; The following procedures are deprecated by SRFI 125, but must
520 ;;; be exported nonetheless.
521 ;;;
522 ;;; Programs that import the (srfi 125) library must rename the
523 ;;; deprecated string-hash and string-ci-hash procedures to avoid
524 ;;; conflict with the string-hash and string-ci-hash procedures
525 ;;; exported by SRFI 126 and SRFI 128.
526 ;;;
527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528
529 (define (deprecated:hash obj . rest)
530 (issue-warning-deprecated 'hash)
531 (default-hash obj))
532
533 (define (deprecated:string-hash obj . rest)
534 (issue-warning-deprecated 'srfi-125:string-hash)
535 (string-hash obj))
536
537 (define (deprecated:string-ci-hash obj . rest)
538 (issue-warning-deprecated 'srfi-125:string-ci-hash)
539 (string-ci-hash obj))
540
541 (define (deprecated:hash-by-identity obj . rest)
542 (issue-warning-deprecated 'hash-by-identity)
543 (deprecated:hash obj))
544
545 (define (deprecated:hash-table-equivalence-function ht)
546 (issue-warning-deprecated 'hash-table-equivalence-function)
547 (hashtable-equivalence-function ht))
548
549 (define (deprecated:hash-table-hash-function ht)
550 (issue-warning-deprecated 'hash-table-hash-function)
551 (hashtable-hash-function ht))
552
553 (define (deprecated:hash-table-exists? ht key)
554 (issue-warning-deprecated 'hash-table-exists?)
555 (hash-table-contains? ht key))
556
557 (define (deprecated:hash-table-walk ht proc)
558 (issue-warning-deprecated 'hash-table-walk)
559 (hash-table-for-each proc ht))
560
561 (define (deprecated:hash-table-fold ht proc seed)
562 (issue-warning-deprecated 'srfi-69-style:hash-table-fold)
563 (hash-table-fold proc seed ht))
564
565 (define (deprecated:hash-table-merge! ht1 ht2)
566 (issue-warning-deprecated 'hash-table-merge!)
567 (hash-table-union! ht1 ht2))
568
569 ; eof