3 Orders and Ordered Dictionaries
(require data/order) | package: data-lib |
This library defines orders and the ordered dictionary generic interface.
value
value
e/c = (->i ([d ordered-dict?]) [_ (d) (or/c #f (dict-iter-contract d))]) s/c = (->i ([d ordered-dict?] [k (d) (dict-key-contract d)]) [_ (d) (or/c #f (dict-iter-contract d))])
The methods are implementations of the following generic functions:
A struct type that implements gen:ordered-dict must also implement gen:dict.
value
prop:ordered-dict :
(struct-type-property/c (vectorof e/c e/c s/c s/c s/c s/c))
procedure
(ordered-dict? x) → boolean?
x : any/c
procedure
(dict-iterate-least dict) → (or/c (dict-iter-contract dict) #f)
dict : ordered-dict?
procedure
(dict-iterate-greatest dict)
→ (or/c (dict-iter-contract dict) #f) dict : ordered-dict?
procedure
(dict-iterate-least/>? dict key)
→ (or/c (dict-iter-contract dict) #f) dict : ordered-dict? key : any/c
procedure
(dict-iterate-least/>=? dict key)
→ (or/c (dict-iter-contract dict) #f) dict : ordered-dict? key : any/c
procedure
(dict-iterate-greatest/<? dict key)
→ (or/c (dict-iter-contract dict) #f) dict : ordered-dict? key : any/c
procedure
(dict-iterate-greatest/<=? dict key)
→ (or/c (dict-iter-contract dict) #f) dict : ordered-dict? key : any/c
procedure
(order name domain-contract comparator)
→ (and/c order? procedure?) name : symbol? domain-contract : contract? comparator : (-> any/c any/c ordering/c) (order name domain-contract =? <? [>?]) → (and/c order? procedure?) name : symbol? domain-contract : contract? =? : (-> any/c any/c boolean?) <? : (-> any/c any/c boolean?) >? : (-> any/c any/c boolean?) = (lambda (x y) (<? y x))
The domain-contract is not applied to the comparison function; rather, clients of the order are advised to incorporate the domain contracts into their own contracts. For example, when a splay-tree (see data/splay-tree) is constructed with an order, it applies the domain-contract to its keys. Thus the contract is checked once per dictionary procedure call, rather than on every comparison.
An order object is applicable as a procedure; it behaves as its comparator.
> (define string-order (order 'string-order string? string=? string<?))
> (string-order "abc" "acdc") '<
> (string-order "x" 12) string=?: contract violation
expected: string?
given: 12
argument position: 2nd
other arguments...:
"x"
procedure
(order-comparator ord) → (-> any/c any/c ordering/c)
ord : order?
procedure
(order-domain-contract ord) → contract?
ord : order?
value
> (real-order 1.0 1) '=
> (real-order 5 7) '<
> (real-order 9.0 3.4) '>
> (real-order 1 +inf.0) '<
> (real-order 5 -inf.0) '>
value
Programs should not rely on the ordering of different data types, since it may change in future versions of Racket to improve comparison performance. The ordering of non-prefab struct types may change between one execution of a program and the next.
The order is guaranteed, however, to lexicographically sort proper lists, vectors, prefab structs, and fully-transparent structs. Improper lists sort lexicographically considered as pairs, but the ordering of an improper list and its proper prefix, such as '(a b . c) and '(a b), is not specified.
The datum-order comparator does not perform cycle-detection; comparisons involving cyclic data may diverge.
Warning: datum-order is not compatible with the standard numeric order; all exact numbers are ordered separately from all inexact numbers. Thus 1 is considered distinct from 1.0, for example.
The following data types are currently supported: numbers, strings, bytes, keywords, symbols, booleans, characters, paths, null, pairs, vectors, boxes, prefab structs, and fully-transparent structs.
> (datum-order 1 2) '<
> (datum-order 8.0 5.0) '>
> (datum-order 'apple 'candy) '<
> (datum-order '(a #:b c) '(a #:c d c)) '<
> (datum-order '(5 . 4) '(3 2 1)) '>
> (datum-order '(a b . c) '(a b . z)) '<
> (datum-order "apricot" "apple") '>
> (datum-order '#(1 2 3) '#(1 2)) '>
> (datum-order '#(1 2 3) '#(1 3)) '<
> (datum-order (box 'car) (box 'candy)) '>
> (datum-order '#s(point a 1) '#s(point b 0)) '<
> (datum-order '#s(A 1 2) '#s(Z 3 4 5)) '<
> (struct fish (name) #:transparent)
> (datum-order (fish 'alewife) (fish 'sockeye)) '<
(datum-order 1 2.0) (datum-order 3+5i 3+2i) (datum-order 'apple "zucchini") (datum-order '(a b) '(a b . c)) (datum-order 0 'zero)
(struct fowl (name) #:transparent) (datum-order (fish 'alewife) (fowl 'dodo))