On this page:
make-interval-map
make-numeric-interval-map
interval-map?
interval-map-with-translate?
interval-map-ref
interval-map-set!
interval-map-update*!
interval-map-remove!
interval-map-expand!
interval-map-contract!
interval-map-cons*!
interval-map-iter?
Version: 5.0

22 Interval Maps

Ryan Culpepper <ryanc@racket-lang.org>

 (require unstable/interval-map)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

An interval-map is a mutable dictionary-like data structure where mappings are added by half-open intervals and queried by discrete points. Interval-maps can be used with any total order. Internally, an interval-map uses a skip-list (unstable/skip-list) of intervals for efficient query and update.

Interval-maps implement the dictionary (racket/dict) interface to a limited extent. Only dict-ref and the iteraction-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 intervals.

Examples:

  > (define r (make-numeric-interval-map))
  > (interval-map-set! r 1 5 'apple)
  > (interval-map-set! r 6 10 'pear)
  > (interval-map-set! r 3 6 'banana)
  > (dict-map r list)

  '(((1 . 3) apple) ((3 . 6) banana) ((6 . 10) pear))

(make-interval-map =? <? [translate])  interval-map?
  =? : (any/c any/c . -> . any/c)
  <? : (any/c any/c . -> . any/c)
  translate : (or/c (any/c any/c . -> . (any/c . -> . any/c)) #f)
   = #f
Makes a new empty interval-map. The interval-map uses =? and <? to order the endpoints of intervals.

If translate is a procedure, the interval-map supports contraction and expansion of regions of its domain via interval-map-contract! and interval-map-expand!. See also make-numeric-interval-map.

Makes a new empty interval-map suitable for representing numeric ranges.

Equivalent to
  (make-interval-map = < (lambda (x y) (lambda (z) (+ z (- y x)))))

(interval-map? v)  boolean?
  v : any/c
Returns #t if v is an interval-map, #f otherwise.

Returns #t if v is an interval-map constructed with support for translation of keys, #f otherwise.

(interval-map-ref interval-map    
  position    
  [default])  any/c
  interval-map : interval-map?
  position : any/c
  default : any/c = (lambda () (error ....))
Return the value associated with position in interval-map. If no mapping is found, default is applied if it is a procedure, or returned otherwise.

(interval-map-set! interval-map    
  start    
  end    
  value)  void?
  interval-map : interval-map?
  start : any/c
  end : any/c
  value : any/c
Updates interval-map, associating every position in [start, end) with value.

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).

(interval-map-update*! interval-map    
  start    
  end    
  updater    
  [default])  void?
  interval-map : interval-map?
  start : any/c
  end : any/c
  updater : (any/c . -> . any/c)
  default : any/c = (lambda () (error ....))
Updates interval-map, associating every position in [start, end) with the result of applying updater to the position’s previously associated value, or to the default value produced by default if no mapping exists.

Unlike interval-map-set!, interval-map-update*! preserves existing distinctions within [start, end).

(interval-map-remove! interval-map    
  start    
  end)  void?
  interval-map : interval-map?
  start : any/c
  end : any/c
Removes the value associated with every position in [start, end).

(interval-map-expand! interval-map    
  start    
  end)  void?
  interval-map : interval-map-with-translate?
  start : any/c
  end : any/c
Expands interval-map’s domain by introducing a gap [start, end) and adjusting intervals after start using (translate start end).

If interval-map was not constructed with a translate argument, an exception is raised. If start is not less than end, an exception is raised.

(interval-map-contract! interval-map    
  start    
  end)  void?
  interval-map : interval-map-with-translate?
  start : any/c
  end : any/c
Contracts interval-map’s domain by removing all mappings on the interval [start, end) and adjusting intervals after end using (translate end start).

If interval-map was not constructed with a translate argument, an exception is raised. If start is not less than end, an exception is raised.

(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
Same as the following:
  (interval-map-update*! interval-map start end
                         (lambda (old) (cons v old))
                         default)

(interval-map-iter? v)  boolean?
  v : any/c
Returns #t if v represents a position in an interval-map, #f otherwise.