6 Interval Maps
(require data/interval-map) | package: data-lib |
An interval-map is a mutable data structure that maps half-open intervals of exact integers to values. An interval-map is queried at a discrete point, and the result of the query is the value mapped to the interval containing the point.
Internally, interval-maps use a skip-list (data/skip-list) of intervals for efficient query and update, including efficient contraction and expansion of intervals.
Interval-maps implement the dictionary (racket/dict) interface to a limited extent. Only dict-ref and the iteration-based methods (dict-iterate-first, dict-map, etc) are supported. For the iteration-based methods, the mapping’s keys are considered the pairs of the start and end positions of the mapping’s (half-open) intervals.
> (define r (make-interval-map)) > (interval-map-set! r 1 5 'apple) > (interval-map-set! r 6 10 'pear) > (interval-map-set! r 3 7 'banana) > r (make-interval-map '(((1 . 3) . apple) ((3 . 7) . banana) ((7 . 10) . pear)))
> (interval-map-ref r 1 #f) 'apple
> (interval-map-ref r 3 #f) 'banana
> (interval-map-ref r 10 #f) #f
Operations on interval-maps are not thread-safe.
procedure
(make-interval-map [ contents #:key-contract key-contract #:value-contract value-contract]) → interval-map?
contents : (listof (cons/c (cons/c exact-integer? exact-integer?) any/c)) = null key-contract : contract? = any/c value-contract : contract? = any/c
> (define r (make-interval-map '(((0 . 5) . apple) ((5 . 10) . banana)))) > (interval-map-ref r 2) 'apple
> (interval-map-ref r 5) 'banana
procedure
(interval-map? v) → boolean?
v : any/c
procedure
(interval-map-ref interval-map position [ default]) → any/c interval-map : interval-map? position : exact-integer? default : any/c = (lambda () (error ....))
Added in version 1.1 of package data-lib.
procedure
(interval-map-ref/bounds interval-map position [ default])
→
(or/c #f exact-integer?) (or/c #f exact-integer?) any/c interval-map : interval-map? position : exact-integer? default : any/c = (lambda () (error ....))
procedure
(interval-map-set! interval-map start end value) → void? interval-map : interval-map? start : exact-integer? end : exact-integer? value : any/c
Existing interval mappings contained in [start, end) are destroyed, and partly overlapping intervals are truncated. See interval-map-update*! for an updating procedure that preserves distinctions within [start, end).
procedure
(interval-map-update*! interval-map start end updater [ default]) → void? interval-map : interval-map? start : exact-integer? end : exact-integer? updater : (-> any/c any/c) default : any/c = (lambda () (error ....))
Unlike interval-map-set!, interval-map-update*! preserves existing distinctions within [start, end).
procedure
(interval-map-remove! interval-map start end) → void? interval-map : interval-map? start : (or/c exact-integer? -inf.0) end : (or/c exact-integer? +inf.0)
procedure
(interval-map-contract! interval-map start end) → void? interval-map : interval-map? start : exact-integer? end : exact-integer?
If start is not less than end, an exception is raised.
procedure
(interval-map-expand! interval-map start end) → void? interval-map : interval-map? start : exact-integer? end : exact-integer?
If start is not less than end, an exception is raised.
procedure
(interval-map-cons*! interval-map start end v [ default]) → void? interval-map : interval-map? start : any/c end : any/c v : any/c default : any/c = null
(interval-map-update*! interval-map start end (lambda (old) (cons v old)) default)
procedure
(interval-map-iterate-first interval-map)
→ (or/c interval-map-iter? #f) interval-map : interval-map?
procedure
(interval-map-iterate-next interval-map iter) → (or/c interval-map-iter? #f) interval-map : interval-map? iter : interval-map-iter?
procedure
(interval-map-iterate-key interval-map iter) → pair? interval-map : interval-map? iter : interval-map-iter?
procedure
(interval-map-iterate-value interval-map iter) → any interval-map : interval-map? iter : interval-map-iter?
procedure
(interval-map-iterate-least interval-map)
→ (or/c interval-map-iter? #f) interval-map : interval-map?
procedure
(interval-map-iterate-greatest interval-map)
→ (or/c interval-map-iter? #f) interval-map : interval-map?
Added in version 1.2 of package data-lib.
Note that interval maps do not implement the gen:ordered-dict interface, as these operations accept individual bounds rather than the pairs returned by interval-map-iterate-key. Therefore, these operations must be used directly.
Added in version 1.2 of package data-lib.
procedure
(interval-map-iter? v) → boolean?
v : any/c