4.13 Sorted Maps
(require rebellion/collection/sorted-map) | |
package: rebellion |
A sorted map is a collection of key-value mappings whose keys are unique and sorted according to some comparator. Sorted maps may be mutable, immutable, or unmodifiable. Two immutable sorted maps are equal? if they contain the same entries and use equal? comparators. Two mutable sorted maps are equal? if they will always contain the same entries and use equal? comparators, meaning that they share the same mutable state. This is not necessarily the same as being eq?, as some sorted maps may be views of others.
All sorted maps are sequences. When iterated, a sorted map traverses its entries in ascending order as defined by its comparator. To traverse a sorted map in descending order, either use in-sorted-map with #:descending? set to true, or reverse the sorted map with sorted-map-reverse. Note that sorted-map-reverse returns a view of the original map, not a copy, so it constructs the view in constant time regardless of the size of the original map.
procedure
(sorted-map? v) → boolean?
v : any/c
procedure
(mutable-sorted-map? v) → boolean?
v : any/c
procedure
v : any/c
4.13.1 Constructing Sorted Maps
procedure
(sorted-map key value ... ... #:key-comparator key-comparator) → immutable-sorted-map? key : any/c value : any/c key-comparator : comparator?
> (sorted-map 3 'c 1 'a 4 'd 2 'b 5 'e #:key-comparator natural<=>) (immutable-sorted-map 1 'a 2 'b 3 'c 4 'd 5 'e)
procedure
(make-mutable-sorted-map [ initial-entries] #:key-comparator key-comparator) → mutable-sorted-map? initial-entries : (sequence/c entry?) = '() key-comparator : comparator?
> (make-mutable-sorted-map #:key-comparator natural<=>) (mutable-sorted-map
> (make-mutable-sorted-map (in-hash-entries (hash 3 'c 1 'a 4 'd 2 'b)) #:key-comparator natural<=>) (mutable-sorted-map 1 'a 2 'b 3 'c 4 'd)
procedure
(entry-sequence->sorted-map entries #:key-comparator key-comparator) → immutable-sorted-map? entries : (sequence/c entry?) key-comparator : comparator?
(define h (hash 3 'c 1 'a 4 'd 2 'b 5 'e))
> (entry-sequence->sorted-map (in-hash-entries h) #:key-comparator natural<=>) (immutable-sorted-map 1 'a 2 'b 3 'c 4 'd 5 'e)
procedure
(into-sorted-map key-comparator)
→ (reducer/c entry? immutable-sorted-map?) key-comparator : comparator?
(define h (hash 3 'c 1 'a 4 'd 2 'b 5 'e))
> (transduce (in-hash-entries h) #:into (into-sorted-map natural<=>)) (immutable-sorted-map 1 'a 2 'b 3 'c 4 'd 5 'e)
syntax
(for/sorted-map #:key-comparator key-comparator (for-clause ...) body-or-break ... body)
key-comparator : comparator?
body : entry?
> (for/sorted-map #:key-comparator char<=> ([char (in-string "cat")] [i (in-naturals)]) (entry char i)) (immutable-sorted-map #\a 1 #\c 0 #\t 2)
syntax
(for*/sorted-map #:key-comparator key-comparator (for-clause ...) body-or-break ... body)
key-comparator : comparator?
body : entry?
> (for*/sorted-map #:key-comparator char<=> ([str (in-list (list "abc" "tuv" "xyz"))] [char (in-string str)]) (entry char str))
(immutable-sorted-map
#\a "abc"
#\b "abc"
#\c "abc"
#\t "tuv"
#\u "tuv"
#\v "tuv"
#\x "xyz"
#\y "xyz"
#\z "xyz")
4.13.2 Iterating Sorted Maps
procedure
(in-sorted-map map [ #:descending? descending?]) → (sequence/c entry?) map : sorted-map? descending? : boolean? = #false
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sequence->list (in-sorted-map map)) (list (entry 1 'a) (entry 2 'b) (entry 3 'c))
procedure
(in-sorted-map-keys map [ #:descending? descending?]) → (sequence/c any/c) map : sorted-map? descending? : boolean? = #false
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sequence->list (in-sorted-map-keys map)) '(1 2 3)
procedure
(in-sorted-map-values map [ #:descending? descending?]) → (sequence/c any/c) map : sorted-map? descending? : boolean? = #false
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sequence->list (in-sorted-map-values map)) '(a b c)
4.13.3 Querying Sorted Maps
procedure
(sorted-map-empty? map) → boolean?
map : sorted-map?
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-empty? map) #f
> (sorted-map-empty? (sorted-submap map (less-than-range 1 #:comparator natural<=>))) #t
procedure
(sorted-map-size map) → natural?
map : sorted-map?
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-size map) 3
> (sorted-map-size (sorted-submap map (at-least-range 2 #:comparator natural<=>))) 2
procedure
(sorted-map-key-comparator map) → comparator?
map : sorted-map?
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-key-comparator map) #<comparator:natural<=>>
procedure
(sorted-map-contains-key? map key) → boolean?
map : sorted-map? key : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-contains-key? map 2) #t
> (sorted-map-contains-key? map 4) #f
procedure
(sorted-map-contains-value? map value) → boolean?
map : sorted-map? value : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-contains-value? map 'b) #t
> (sorted-map-contains-value? map 'z) #f
procedure
(sorted-map-contains-entry? map entry) → boolean?
map : sorted-map? entry : entry?
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-contains-entry? map (entry 1 'a)) #t
> (sorted-map-contains-entry? map (entry 2 'b)) #t
> (sorted-map-contains-entry? map (entry 1 'b)) #f
procedure
(sorted-map-get map key [failure-result]) → any/c
map : sorted-map? key : any/c failure-result : failure-result/c = (λ () (raise ...))
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-get map 2) 'b
> (sorted-map-get map 4) sorted-map-get: no value for key;
key: 4
map: (immutable-sorted-map 1 'a 2 'b 3 'c)
> (sorted-map-get map 4 'missing) 'missing
procedure
(sorted-map-get-option map key) → option?
map : sorted-map? key : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-get-option map 2) (present 'b)
> (sorted-map-get-option map 4) #<absent>
procedure
(sorted-map-get-entry map key [ failure-result]) → entry? map : sorted-map? key : any/c failure-result : failure-result/c = (λ () (raise ...))
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-get-entry map 2) (entry 2 'b)
> (sorted-map-get-entry map 4) sorted-map-get-entry: no value for key;
key: 4
map: (immutable-sorted-map 1 'a 2 'b 3 'c)
> (sorted-map-get-entry map 4 'missing) (entry 4 'missing)
procedure
(sorted-map-least-key map) → option?
map : sorted-map?
> (sorted-map-least-key (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (present 1)
> (sorted-map-least-key (sorted-map #:key-comparator natural<=>)) #<absent>
procedure
(sorted-map-greatest-key map) → option?
map : sorted-map?
> (sorted-map-greatest-key (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (present 3)
> (sorted-map-greatest-key (sorted-map #:key-comparator natural<=>)) #<absent>
procedure
(sorted-map-key-less-than map upper-bound) → option?
map : sorted-map? upper-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-key-less-than map 2) (present 1)
> (sorted-map-key-less-than map 1) #<absent>
procedure
(sorted-map-key-greater-than map lower-bound) → option? map : sorted-map? lower-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-key-greater-than map 2) (present 3)
> (sorted-map-key-greater-than map 3) #<absent>
procedure
(sorted-map-key-at-most map upper-bound) → option?
map : sorted-map? upper-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-key-at-most map 5) (present 3)
> (sorted-map-key-at-most map 0) #<absent>
procedure
(sorted-map-key-at-least map lower-bound) → option?
map : sorted-map? lower-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-key-at-least map 0) (present 1)
> (sorted-map-key-at-least map 5) #<absent>
procedure
(sorted-map-least-entry map) → (option/c entry?)
map : sorted-map?
> (sorted-map-least-entry (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (present (entry 1 'a))
> (sorted-map-least-entry (sorted-map #:key-comparator natural<=>)) #<absent>
procedure
(sorted-map-greatest-entry map) → (option/c entry?)
map : sorted-map?
> (sorted-map-greatest-entry (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (present (entry 3 'c))
> (sorted-map-greatest-entry (sorted-map #:key-comparator natural<=>)) #<absent>
procedure
(sorted-map-entry-less-than map upper-key-bound) → (option/c entry?) map : sorted-map? upper-key-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-entry-less-than map 2) (present (entry 1 'a))
> (sorted-map-entry-less-than map 1) #<absent>
procedure
(sorted-map-entry-greater-than map lower-key-bound) → (option/c entry?) map : sorted-map? lower-key-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-entry-greater-than map 2) (present (entry 3 'c))
> (sorted-map-entry-greater-than map 3) #<absent>
procedure
(sorted-map-entry-at-most map upper-key-bound) → (option/c entry?) map : sorted-map? upper-key-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-entry-at-most map 5) (present (entry 3 'c))
> (sorted-map-entry-at-most map 0) #<absent>
procedure
(sorted-map-entry-at-least map lower-key-bound) → (option/c entry?) map : sorted-map? lower-key-bound : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-entry-at-least map 0) (present (entry 1 'a))
> (sorted-map-entry-at-least map 5) #<absent>
4.13.4 Sorted Map Views
procedure
(sorted-submap map key-range) → sorted-map?
map : sorted-map? key-range : range?
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-submap map (at-most-range 2 #:comparator natural<=>)) (immutable-sorted-map 1 'a 2 'b)
When used on mutable sorted maps, the returned map is also a write-through view —
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>)) (define map<=2 (sorted-submap map (at-most-range 2 #:comparator natural<=>)))
> map<=2 (mutable-sorted-map 1 'a 2 'b)
> (sorted-map-remove! map<=2 1) > map<=2 (mutable-sorted-map 2 'b)
> map (mutable-sorted-map 2 'b 3 'c)
procedure
(sorted-map-reverse map) → sorted-map?
map : sorted-map?
When used on mutable sorted maps, the returned map is also a write-through view —
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-reverse map) (immutable-sorted-map 3 'c 2 'b 1 'a)
procedure
(sorted-map-keys map) → sorted-set?
map : sorted-map?
> (sorted-map-keys (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (immutable-sorted-set 1 2 3)
When used on mutable sorted maps, the returned set is also a write-through view —
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-set-remove! (sorted-map-keys map) 2) > map (mutable-sorted-map 1 'a 3 'c)
> (sorted-set-add! (sorted-map-keys map) 5) sorted-set-add!: sorted map key sets do not support
insertion
key set: (sorted-set 1 3)
element: 5
procedure
(sorted-map-entries map) → sorted-set?
map : sorted-map?
> (sorted-map-entries (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>)) (sorted-set (entry 1 'a) (entry 2 'b) (entry 3 'c))
When used on mutable sorted maps, the returned set is also a write-through view —
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-set-remove! (sorted-map-entries map) (entry 2 'b)) > map (mutable-sorted-map 1 'a 3 'c)
> (sorted-set-add! (sorted-map-entries map) (entry 5 'b)) > map (mutable-sorted-map 1 'a 3 'c 5 'b)
4.13.5 Modifying Sorted Maps
procedure
(sorted-map-get! map key failure-result) → any/c
map : mutable-sorted-map? key : any/c failure-result : failure-result/c
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-map-get! map 2 'missing) 'b
> (sorted-map-get! map 5 'missing) 'missing
> map (mutable-sorted-map 1 'a 2 'b 3 'c 5 'missing)
procedure
(sorted-map-get-entry! map key failure-result) → entry? map : sorted-map? key : any/c failure-result : failure-result/c
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-map-get-entry! map 2 'missing) (entry 2 'b)
> (sorted-map-get-entry! map 5 'missing) (entry 5 'missing)
> map (mutable-sorted-map 1 'a 2 'b 3 'c 5 'missing)
procedure
(sorted-map-put map key value) → immutable-sorted-map?
map : immutable-sorted-map? key : any/c value : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-put map 4 'd) (immutable-sorted-map 1 'a 2 'b 3 'c 4 'd)
> (sorted-map-put map 2 'x) (immutable-sorted-map 1 'a 2 'x 3 'c)
procedure
(sorted-map-put! map key value) → void?
map : mutable-sorted-map? key : any/c value : any/c
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-map-put! map 2 'x) > map (mutable-sorted-map 1 'a 2 'x 3 'c)
> (sorted-map-put! map 4 'd) > map (mutable-sorted-map 1 'a 2 'x 3 'c 4 'd)
procedure
(sorted-map-put-all map entries) → immutable-sorted-map?
map : immutable-sorted-map? entries : (sequence/c entry?)
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-put-all map (in-hash-entries (hash 2 'x 3 'x 4 'x))) (immutable-sorted-map 1 'a 2 'x 3 'x 4 'x)
procedure
(sorted-map-put-all! map entries) → void?
map : mutable-sorted-map? entries : (sequence/c entry?)
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-map-put-all! map (in-hash-entries (hash 2 'x 3 'x 4 'x))) > map (mutable-sorted-map 1 'a 2 'x 3 'x 4 'x)
procedure
(sorted-map-put-if-absent map key value)
→ (result/c immutable-sorted-map? any/c) map : immutable-sorted-map? key : any/c value : any/c
(define map (sorted-map 1 'a 2 'b 3 'c #:key-comparator natural<=>))
> (sorted-map-put-if-absent map 4 'd) (success (immutable-sorted-map 1 'a 2 'b 3 'c 4 'd))
> (sorted-map-put-if-absent map 2 'x) (failure 'b)
procedure
(sorted-map-put-if-absent! map key value) → option?
map : mutable-sorted-map? key : any/c value : any/c
(define map (make-mutable-sorted-map (list (entry 1 'a) (entry 2 'b) (entry 3 'c)) #:key-comparator natural<=>))
> (sorted-map-put-if-absent! map 4 'd) #<absent>
> map (mutable-sorted-map 1 'a 2 'b 3 'c 4 'd)
> (sorted-map-put-if-absent! map 2 'x) (present 'b)
> map (mutable-sorted-map 1 'a 2 'b 3 'c 4 'd)
procedure
(sorted-map-update map key updater [ failure-result]) → immutable-sorted-map? map : immutable-sorted-map? key : any/c updater : (-> any/c any/c) failure-result : failure-result/c = (λ () (raise ...))
(define map (sorted-map 'a 1 'b 1 'c 1 #:key-comparator symbol<=>))
> (sorted-map-update map 'b add1) (immutable-sorted-map 'a 1 'b 2 'c 1)
> (sorted-map-update map 'd add1) sorted-map-update: no value for key;
key: 'd
map: (immutable-sorted-map 'a 1 'b 1 'c 1)
> (sorted-map-update map 'd add1 0) (immutable-sorted-map 'a 1 'b 1 'c 1 'd 1)
procedure
(sorted-map-update! map key updater [ failure-result]) → void? map : mutable-sorted-map? key : any/c updater : (-> any/c any/c) failure-result : failure-result/c = (λ () (raise ...))
(define map (make-mutable-sorted-map (list (entry 'a 1) (entry 'b 1) (entry 'c 1)) #:key-comparator symbol<=>))
> (sorted-map-update! map 'b add1) > map (mutable-sorted-map 'a 1 'b 2 'c 1)
> (sorted-map-update! map 'd add1) sorted-map-update!: no value for key;
key: 'd
map: (mutable-sorted-map 'a 1 'b 2 'c 1)
> (sorted-map-update! map 'd add1 0) > map (mutable-sorted-map 'a 1 'b 2 'c 1 'd 1)
procedure
(sorted-map-remove map key) → immutable-sorted-map?
map : immutable-sorted-map? key : any/c
(define map (sorted-map 'a 1 'b 1 'c 1 #:key-comparator symbol<=>))
> (sorted-map-remove map 'b) (immutable-sorted-map 'a 1 'c 1)
procedure
(sorted-map-remove! map key) → void?
map : mutable-sorted-map? key : any/c
(define map (make-mutable-sorted-map (list (entry 'a 1) (entry 'b 1) (entry 'c 1)) #:key-comparator symbol<=>))
> (sorted-map-remove! map 'b) > map (mutable-sorted-map 'a 1 'c 1)
procedure
(sorted-map-remove-all map keys) → immutable-sorted-map?
map : immutable-sorted-map? keys : (sequence/c any/c)
(define map (sorted-map 'a 1 'b 1 'c 1 #:key-comparator symbol<=>))
> (sorted-map-remove-all map (list 'b 'c 'd)) (immutable-sorted-map 'a 1)
procedure
(sorted-map-remove-all! map keys) → void?
map : mutable-sorted-map? keys : (sequence/c any/c)
(define map (make-mutable-sorted-map (list (entry 'a 1) (entry 'b 1) (entry 'c 1)) #:key-comparator symbol<=>))
> (sorted-map-remove-all! map (list 'b 'c 'd)) > map (mutable-sorted-map 'a 1)
procedure
(sorted-map-clear! map) → void?
map : mutable-sorted-map?
(define map (make-mutable-sorted-map (list (entry 'a 1) (entry 'b 1) (entry 'c 1)) #:key-comparator symbol<=>))
> (sorted-map-clear! (sorted-submap map (less-than-range 'c #:comparator symbol<=>))) > map (mutable-sorted-map 'c 1)
4.13.6 Sorted Map Builders
A sorted map builder is a mutable object that can create sorted maps. Entries can be added
to a builder incrementally with sorted-map-builder-put, and immutable sorted maps can be
built from a builder with build-sorted-map. Builders can be reused —
procedure
(sorted-map-builder? v) → boolean?
v : any/c
procedure
(make-sorted-map-builder key-comparator) → sorted-map-builder?
key-comparator : comparator?
procedure
(sorted-map-builder-put builder key value) → sorted-map-builder?
builder : sorted-map-builder? key : any/c value : any/c
(define builder (make-sorted-map-builder natural<=>))
> (sorted-map-builder-put builder 7 'c) #<sorted-map-builder>
> (sorted-map-builder-put builder 2 'a) #<sorted-map-builder>
> (sorted-map-builder-put builder 5 'b) #<sorted-map-builder>
> (build-sorted-map builder) (immutable-sorted-map 2 'a 5 'b 7 'c)
procedure
(sorted-map-builder-put-all builder entries) → sorted-map-builder? builder : sorted-map-builder? entries : (sequence/c entry?)
(define builder (make-sorted-map-builder natural<=>))
> (sorted-map-builder-put-all builder (list (entry 1 'a) (entry 3 'c) (entry 2 'b))) #<sorted-map-builder>
> (build-sorted-map builder) (immutable-sorted-map 1 'a 2 'b 3 'c)
procedure
(build-sorted-map builder) → immutable-sorted-map?
builder : sorted-map-builder?
(define builder (make-sorted-map-builder natural<=>))
> (for/fold ([builder (make-sorted-map-builder natural<=>)] #:result (build-sorted-map builder)) ([char (in-string "hello")] [i (in-naturals)]) (sorted-map-builder-put builder i char)) (immutable-sorted-map 0 #\h 1 #\e 2 #\l 3 #\l 4 #\o)