1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
#lang racket/base
(require racket/dict
racket/contract/base
racket/string
ffi/unsafe/atomic
racket/private/generic)
(define ordering/c
(or/c '= '< '>))
(provide ordering/c)
;; we use the private version here because we need to
;; provide a backwards compatible interface (just in case)
;; i.e., exporting prop:ordered-dict as opposed to using a
;; generated hidden property.
(define-primitive-generics
(ordered-dict gen:ordered-dict
prop:ordered-dict
ordered-methods
ordered-dict?
ordered-dict-implements?)
#:fast-defaults ()
#:defaults ()
#:fallbacks ()
#:derive-properties ()
(dict-iterate-least ordered-dict)
(dict-iterate-greatest ordered-dict)
(dict-iterate-least/>? ordered-dict key)
(dict-iterate-least/>=? ordered-dict key)
(dict-iterate-greatest/<? ordered-dict key)
(dict-iterate-greatest/<=? ordered-dict key))
(define extreme-contract
(->i ([d ordered-dict?])
[_r (d) (or/c #f (dict-iter-contract d))]))
(define search-contract
(->i ([d ordered-dict?]
[k (d) (dict-key-contract d)])
[_r (d) (or/c #f (dict-iter-contract d))]))
(define prop:ordered-dict-contract
(let ([e (or/c extreme-contract #f)] ;; generics initializes with #f,
; then sets the methods
[s (or/c search-contract #f)])
(vector/c e ;; iterate-least
e ;; iterate-greatest
s ;; iterate-least/>?
s ;; iterate-least/>=?
s ;; iterate-greatest/<?
s)));; iterate-greatest/<=?
;; --------
(provide gen:ordered-dict)
(provide/contract
[prop:ordered-dict
(struct-type-property/c prop:ordered-dict-contract)]
[ordered-dict? (-> any/c boolean?)]
[dict-iterate-least extreme-contract]
[dict-iterate-greatest extreme-contract]
[dict-iterate-least/>? search-contract]
[dict-iterate-least/>=? search-contract]
[dict-iterate-greatest/<? search-contract]
[dict-iterate-greatest/<=? search-contract])
;; ============================================================
(struct order (name domain-contract comparator =? <?)
#:property prop:procedure (struct-field-index comparator))
(define order*
(let ([order
(case-lambda
[(name ctc cmp)
(order name ctc cmp
(lambda (x y) (eq? (cmp x y) '=))
(lambda (x y) (eq? (cmp x y) '<)))]
[(name ctc = <)
(order name ctc
(lambda (x y)
(cond [(= x y) '=]
[(< x y) '<]
[(< y x) '>]
[else (incomparable name x y)]))
= <)]
[(name ctc = < >)
(order name ctc
(lambda (x y)
(cond [(= x y) '=]
[(< x y) '<]
[(> x y) '>]
[else (incomparable name x y)]))
= <)])])
order))
(define (incomparable name x y)
(error name "values are incomparable: ~e ~e" x y))
(provide/contract
[rename order* order
(->* (symbol? contract? procedure?) (procedure? procedure?)
order?)]
[order? (-> any/c boolean?)]
[order-comparator
(-> order? procedure?)]
[order-<?
(-> order? procedure?)]
[order-=?
(-> order? procedure?)]
[order-domain-contract
(-> order? contract?)])
;; ============================================================
(define (real/not-NaN? x) (and (real? x) (not (eqv? x +nan.0))))
(define real-order
(order* 'real-order real/not-NaN? = < >))
(provide/contract
[real-order order?])
;; ============================================================
#|
natural-cmp : Comparator
datum-cmp : Comparator
comparators for (most) built-in values
!! May diverge on cyclical input.
natural-cmp:
* restriction to reals equiv to <,=
real (exact and inexact, #e1 = #i1, +nan.0 not allowed!)
< complex
< Other
datum-cmp:
* restriction to reals NOT EQUIV to <,= (separates exact, inexact)
exact real
< inexact real (+nan.0 > +inf.0)
< complex
< Other
Other:
string
< bytes
< keyword
< symbol
< bool
< char
< path
< null
< pair
< vector
< box
< prefab-struct
< fully-transparent-struct
;; FIXME: What else to add? regexps (4 kinds?), syntax, ...
|#
;; not exported because I'm not sure it's a good idea and I'm not sure
;; how to characterize it
(define (natural-cmp x y)
(gen-cmp x y #t))
(define (datum-cmp x y)
(gen-cmp x y #f))
(define (gen-cmp x y natural?)
(define-syntax-rule (recur x* y*)
(gen-cmp x* y* natural?))
(cond [(eq? x y) '=]
#|
[(T? x) ...]
;; at this point, Type(x) > T
[(T? y)
;; Type(x) > T = Type(y), so:
'>]
Assumes arguments are legal.
|#
[(real? x)
(if (real? y)
(cond [natural?
(cmp* < = x y)]
[else ;; exact < inexact
(cond [(and (exact? x) (exact? y))
(cmp* < = x y)]
[(exact? x) ;; inexact y
'<]
[(exact? y) ;; inexact x
'>]
[(and (eqv? x +nan.0) (eqv? y +nan.0))
'=]
[(eqv? x +nan.0)
'>]
[(eqv? y +nan.0)
'<]
[else ;; inexact x, inexact y
(cmp* < = x y)])])
'<)]
[(real? y) '>]
[(complex? x)
(if (complex? y)
(lexico (recur (real-part x) (real-part y))
(recur (imag-part x) (imag-part y)))
'<)]
[(complex? y) '>]
[(string? x)
(if (string? y)
(cmp* string<? string=? x y)
'<)]
[(string? y) '>]
[(bytes? x)
(if (bytes? y)
(cmp* bytes<? bytes=? x y)
'<)]
[(bytes? y) '>]
[(keyword? x)
(if (keyword? y)
(cmp* keyword<? eq? x y)
'<)]
[(keyword? y) '>]
[(symbol? x)
(if (symbol? y)
(cmp* symbol<? eq? x y)
'<)]
[(symbol? y) '>]
[(boolean? x)
(if (boolean? y)
(cond [(eq? x y) '=]
[y '<]
[else '>])
'<)]
[(boolean? y) '>]
[(char? x)
(if (char? y)
(cmp* char<? char=? x y)
'<)]
[(char? y)
'>]
[(path-for-some-system? x)
(if (path-for-some-system? y)
(cmp* bytes<? bytes=? (path->bytes x) (path->bytes y))
'<)]
[(path-for-some-system? y)
'>]
[(null? x)
(if (null? y)
'=
'<)]
[(null? y) '>]
[(pair? x)
(if (pair? y)
(lexico (recur (car x) (car y)) (recur (cdr x) (cdr y)))
'<)]
[(pair? y) '>]
[(vector? x)
(if (vector? y)
(vector-cmp x y 0 natural?)
'<)]
[(vector? y) '>]
[(box? x)
(if (box? y)
(recur (unbox x) (unbox y))
'<)]
[(box? y) '>]
[(prefab-struct-key x)
(if (prefab-struct-key y)
(lexico (recur (prefab-struct-key x) (prefab-struct-key y))
;; FIXME: use struct-ref to avoid allocation?
(vector-cmp (struct->vector x) (struct->vector y) 1 natural?))
'<)]
[(prefab-struct-key y)
'>]
[(fully-transparent-struct-type x)
=> (lambda (xtype)
(cond [(fully-transparent-struct-type y)
=> (lambda (ytype)
;; could also do another lexico with object-name first
(lexico (object-cmp xtype ytype)
;; FIXME: use struct-ref to avoid allocation?
(vector-cmp (struct->vector x) (struct->vector y)
1 natural?)))]
[else '<]))]
[(fully-transparent-struct-type y)
'>]
[else
(raise-type-error
(if natural? 'natural-cmp 'datum-cmp)
(string-join '("number" "string" "bytes" "keyword" "symbol" "boolean" "character"
"path" "null" "pair" "vector" "box"
"prefab struct" "or fully-transparent struct")
", ")
0 x y)]))
(define-syntax-rule (cmp* <? =? xe ye)
(let ([x xe] [y ye])
(if (=? x y) '= (if (<? x y) '< '>))))
(define-syntax-rule (lexico c1 c2)
(case c1
((<) '<)
((=) c2)
((>) '>)))
(define (vector-cmp x y i natural?)
(cond [(< i (vector-length x))
(if (< i (vector-length y))
(lexico (gen-cmp (vector-ref x i) (vector-ref y i) natural?)
(vector-cmp x y (add1 i) natural?))
'>)]
[(< i (vector-length y))
'<]
[else '=]))
;; fully-transparent-struct-type : any -> struct-type or #f
(define (fully-transparent-struct-type x)
(parameterize ((current-inspector weak-inspector))
(let-values ([(x-type x-skipped?) (struct-info x)])
(and (not x-skipped?) x-type))))
;; weak inspector controls no struct types;
;; so if it can inspect, must be transparent
(define weak-inspector (make-inspector))
;; Impose an arbitrary (but consistent) ordering on eq?-compared
;; objects. Use eq? and eq-hash-code for common fast path. Fall back
;; to table when comparing struct-types *same eq-hash-code* but *not
;; eq?*. That should be rare.
(define object-order-table (make-weak-hasheq))
(define object-order-next 0)
(define (object-cmp x y)
(cond [(eq? x y) '=]
[else
(lexico
(cmp* < = (eq-hash-code x) (eq-hash-code y))
(call-as-atomic
(lambda ()
(let ([xi (hash-ref object-order-table x #f)]
[yi (hash-ref object-order-table y #f)])
(cond [(and xi yi)
;; x not eq? y, so xi != yi
(if (< xi yi) '< '>)]
[xi '<]
[yi '>]
[else ;; neither one is in table; we only need to add one
(hash-set! object-order-table x object-order-next)
(set! object-order-next (add1 object-order-next))
'<])))))]))
(define datum-order
(order* 'datum-order any/c datum-cmp))
(provide/contract
[datum-order order?])
|