Debian Codegraph @

%3a128/128.body2.scm

raw symbols SCIP
1 ;;; Copyright (C) John Cowan (2015). All Rights Reserved.
2 ;;;
3 ;;; Permission is hereby granted, free of charge, to any person
4 ;;; obtaining a copy of this software and associated documentation
5 ;;; files (the "Software"), to deal in the Software without
6 ;;; restriction, including without limitation the rights to use,
7 ;;; copy, modify, merge, publish, distribute, sublicense, and/or
8 ;;; sell copies of the Software, and to permit persons to whom the
9 ;;; Software is furnished to do so, subject to the following
10 ;;; conditions:
11 ;;;
12 ;;; The above copyright notice and this permission notice shall be
13 ;;; included in all copies or substantial portions of the Software.
14 ;;;
15 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 ;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 ;;; OTHER DEALINGS IN THE SOFTWARE.
23
24 ;;; The default comparator
25
26 ;;; Standard comparators and their functions
27
28 ;; The unknown-object comparator, used as a fallback to everything else
29 ;; Everything compares exactly the same and hashes to 0
30 (define unknown-object-comparator
31 (make-comparator
32 (lambda (obj) #t)
33 (lambda (a b) #t)
34 (lambda (a b) #f)
35 (lambda (obj) 0)))
36
37 ;; Next index for added comparator
38
39 (define first-comparator-index 9)
40 (define *next-comparator-index* 9)
41 (define *registered-comparators* (list unknown-object-comparator))
42
43 ;; Register a new comparator for use by the default comparator.
44 (define (comparator-register-default! comparator)
45 (set! *registered-comparators* (cons comparator *registered-comparators*))
46 (set! *next-comparator-index* (+ *next-comparator-index* 1)))
47
48 ;; Return ordinal for object types: null sorts before pairs, which sort
49 ;; before booleans, etc. Implementations can extend this.
50 ;; People who call comparator-register-default! effectively do extend it.
51 (define (object-type obj)
52 (cond
53 ((null? obj) 0)
54 ((pair? obj) 1)
55 ((boolean? obj) 2)
56 ((char? obj) 3)
57 ((string? obj) 4)
58 ((symbol? obj) 5)
59 ((number? obj) 6)
60 ((vector? obj) 7)
61 ((bytevector? obj) 8)
62 ; Add more here if you want: be sure to update comparator-index variables
63 (else (registered-index obj))))
64
65 ;; Return the index for the registered type of obj.
66 (define (registered-index obj)
67 (let loop ((i 0) (registry *registered-comparators*))
68 (cond
69 ((null? registry) (+ first-comparator-index i))
70 ((comparator-test-type (car registry) obj) (+ first-comparator-index i))
71 (else (loop (+ i 1) (cdr registry))))))
72
73 ;; Given an index, retrieve a registered conductor.
74 ;; Index must be >= first-comparator-index.
75 (define (registered-comparator i)
76 (list-ref *registered-comparators* (- i first-comparator-index)))
77
78 (define (dispatch-equality type a b)
79 (case type
80 ((0) #t) ; All empty lists are equal
81 ((1) ((make-pair=? (make-default-comparator) (make-default-comparator)) a b))
82 ((2) (boolean=? a b))
83 ((3) (char=? a b))
84 ((4) (string=? a b))
85 ((5) (symbol=? a b))
86 ((6) (= a b))
87 ((7) ((make-vector=? (make-default-comparator)
88 vector? vector-length vector-ref) a b))
89 ((8) ((make-vector=? (make-comparator exact-integer? = < default-hash)
90 bytevector? bytevector-length bytevector-u8-ref) a b))
91 ; Add more here
92 (else (binary=? (registered-comparator type) a b))))
93
94 (define (dispatch-ordering type a b)
95 (case type
96 ((0) 0) ; All empty lists are equal
97 ((1) ((make-pair<? (make-default-comparator) (make-default-comparator)) a b))
98 ((2) (boolean<? a b))
99 ((3) (char<? a b))
100 ((4) (string<? a b))
101 ((5) (symbol<? a b))
102 ((6) (complex<? a b))
103 ((7) ((make-vector<? (make-default-comparator) vector? vector-length vector-ref) a b))
104 ((8) ((make-vector<? (make-comparator exact-integer? = < default-hash)
105 bytevector? bytevector-length bytevector-u8-ref) a b))
106 ; Add more here
107 (else (binary<? (registered-comparator type) a b))))
108
109 ;;; The author of SRFI 128 has suggested a post-finalization note
110 ;;; saying the first and third bullet items stating "must" requirements
111 ;;; for default-hash may be weakened. That allows a much faster hash
112 ;;; function to be used for lists and vectors.
113
114 (define (default-hash obj)
115 (case (object-type obj)
116 ((0 1 7) ; empty list, pair, or vector
117 ((make-hasher) (equal-hash obj)))
118 ((2) (boolean-hash obj))
119 ((3) (char-hash obj))
120 ((4) (string-hash obj))
121 ((5) (symbol-hash obj))
122 ((6) (number-hash obj))
123 ((8) ((make-vector-hash (make-default-comparator)
124 bytevector? bytevector-length bytevector-u8-ref) obj))
125 ; Add more here
126 (else (comparator-hash (registered-comparator (object-type obj)) obj))))
127
128 (define (default-ordering a b)
129 (let ((a-type (object-type a))
130 (b-type (object-type b)))
131 (cond
132 ((< a-type b-type) #t)
133 ((> a-type b-type) #f)
134 (else (dispatch-ordering a-type a b)))))
135
136 (define (default-equality a b)
137 (let ((a-type (object-type a))
138 (b-type (object-type b)))
139 (if (= a-type b-type) (dispatch-equality a-type a b) #f)))
140
141 (define (make-default-comparator)
142 (make-comparator
143 (lambda (obj) #t)
144 default-equality
145 default-ordering
146 default-hash))
147