IntervalMap
Defined in: interval-map.ts:73
An immutable map from disjoint intervals of T to values V (Guava’s
RangeMap). Each value of T maps to at most one V; IntervalMap.put
overwrites whatever it overlaps, splitting the entries it cuts through, and
IntervalMap.putCoalescing also merges the new entry with connected
neighbours that hold an equal value. Every operation returns a new map.
Entries are kept sorted; get / getEntry binary-search them. Unlike an
IntervalSet, put does not merge touching entries even when their
values are equal (Guava’s behaviour) — use putCoalescing for that.
Examples
Section titled “Examples”import { IntervalMap, NumberInterval } from "@xtandard/lib/interval";
const tax = IntervalMap.of(NumberInterval.domain, [ [NumberInterval.closedOpen(0, 10_000), 0], [NumberInterval.closedOpen(10_000, 40_000), 0.1], [NumberInterval.atLeast(40_000), 0.2],]);tax.get(25_000); // 0.1tax.get(-5); // undefined
// put overwrites the overlapped part and splits what it cuts through:const promo = tax.put(NumberInterval.closedOpen(5_000, 15_000), 0.05);promo.toString();// "{[0,5000) => 0, [5000,15000) => 0.05, [15000,40000) => 0.1, [40000,+Infinity) => 0.2}"promo.getEntry(12_000)?.[0].toString(); // "[5000,15000)"putCoalescing merges connected entries with an equal value
import { IntegerInterval, IntervalMap } from "@xtandard/lib/interval";
const shifts = IntervalMap.empty<number, string>(IntegerInterval.domain) .putCoalescing(IntegerInterval.closed(1, 5), "ana") .putCoalescing(IntegerInterval.closed(6, 9), "ana") // adjacent on the integers .putCoalescing(IntegerInterval.closed(10, 12), "bo");shifts.toString(); // "{[1,9] => ana, [10,12] => bo}"shifts.remove(IntegerInterval.closed(4, 6)).toString();// "{[1,3] => ana, [7,9] => ana, [10,12] => bo}"Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
V |
Implements
Section titled “Implements”Iterable<IntervalMapEntry<T,V>>
Properties
Section titled “Properties”| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
domain |
readonly |
IntervalDomain<T> |
The domain that orders/steps/serializes the keys. | interval-map.ts:75 |
valueEquals |
readonly |
(a, b) => boolean |
The value equality used by putCoalescing and equals (default Object.is). |
interval-map.ts:77 |
Accessors
Section titled “Accessors”isEmpty
Section titled “isEmpty”Get Signature
Section titled “Get Signature”get isEmpty(): boolean;Defined in: interval-map.ts:129
true when the map has no entries.
Returns
Section titled “Returns”boolean
Get Signature
Section titled “Get Signature”get size(): number;Defined in: interval-map.ts:134
The number of entries (what iteration yields).
Returns
Section titled “Returns”number
Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”[iterator](): IterableIterator<IntervalMapEntry<T, V>>;Defined in: interval-map.ts:295
The [interval, value] entries in ascending interval order.
Returns
Section titled “Returns”IterableIterator<IntervalMapEntry<T, V>>
Implementation of
Section titled “Implementation of”Iterable.[iterator]
entries()
Section titled “entries()”entries(): IterableIterator<IntervalMapEntry<T, V>>;Defined in: interval-map.ts:300
The [interval, value] entries in ascending interval order (like Map#entries).
Returns
Section titled “Returns”IterableIterator<IntervalMapEntry<T, V>>
equals()
Section titled “equals()”equals(other): boolean;Defined in: interval-map.ts:283
Same domain, same intervals, and equal values (per this map’s valueEquals).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
IntervalMap<T, V> |
Returns
Section titled “Returns”boolean
format()
Section titled “format()”format(options?): string;Defined in: interval-map.ts:324
Render as {[0,10) => a, [10,20) => b} ({} when empty). Intervals use
Interval.format (custom ±∞ tokens via options); values use formatValue
(default String).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options? |
{ formatValue?: (value) => string; negativeInfinity?: string; positiveInfinity?: string; } |
options.formatValue? |
(value) => string |
options.negativeInfinity? |
string |
options.positiveInfinity? |
string |
Returns
Section titled “Returns”string
get(value): V | undefined;Defined in: interval-map.ts:153
The value mapped at value, or undefined if no entry covers it.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”V | undefined
getEntry()
Section titled “getEntry()”getEntry(value): | IntervalMapEntry<T, V> | undefined;Defined in: interval-map.ts:139
The [interval, value] entry whose interval contains value, or undefined.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”| IntervalMapEntry<T, V>
| undefined
has(value): boolean;Defined in: interval-map.ts:158
Whether some entry covers value (use it when V itself may be undefined).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”boolean
keys()
Section titled “keys()”keys(): IterableIterator<Interval<T>>;Defined in: interval-map.ts:305
The intervals in ascending order (like Map#keys).
Returns
Section titled “Returns”IterableIterator<Interval<T>>
put(interval, value): IntervalMap<T, V>;Defined in: interval-map.ts:182
Map every value of interval to value, overwriting the overlapped parts of
existing entries (an entry that straddles an end is split; the part outside
keeps its old value). An empty interval is a no-op.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
interval |
Interval<T> |
value |
V |
Returns
Section titled “Returns”IntervalMap<T, V>
putCoalescing()
Section titled “putCoalescing()”putCoalescing(interval, value): IntervalMap<T, V>;Defined in: interval-map.ts:200
Like put, but first widens interval to absorb the neighbouring
entries it overlaps or touches whose value is equal (per valueEquals) —
Guava’s putCoalescing. Keeps the map minimal when equal values meet.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
interval |
Interval<T> |
value |
V |
Returns
Section titled “Returns”IntervalMap<T, V>
remove()
Section titled “remove()”remove(interval): IntervalMap<T, V>;Defined in: interval-map.ts:238
Unmap every value of interval, splitting entries that straddle its ends.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
interval |
Interval<T> |
Returns
Section titled “Returns”IntervalMap<T, V>
span()
Section titled “span()”span(): Interval<T>;Defined in: interval-map.ts:163
The smallest interval enclosing every entry; the empty interval for an empty map.
Returns
Section titled “Returns”Interval<T>
subMap()
Section titled “subMap()”subMap(window): IntervalMap<T, V>;Defined in: interval-map.ts:259
The entries restricted to window (each clipped to it); Guava’s subRangeMap.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
window |
Interval<T> |
Returns
Section titled “Returns”IntervalMap<T, V>
toArray()
Section titled “toArray()”toArray(): IntervalMapEntry<T, V>[];Defined in: interval-map.ts:315
A fresh array of the entries, in ascending interval order.
Returns
Section titled “Returns”IntervalMapEntry<T, V>[]
toIntervalSet()
Section titled “toIntervalSet()”toIntervalSet(): IntervalSet<T>;Defined in: interval-map.ts:171
Every value of T that has an entry, as an IntervalSet (touching entries merge).
Returns
Section titled “Returns”IntervalSet<T>
toString()
Section titled “toString()”toString(): string;Defined in: interval-map.ts:338
Default format() (e.g. "{[0,10) => a, [10,20) => b}").
Returns
Section titled “Returns”string
values()
Section titled “values()”values(): IterableIterator<V>;Defined in: interval-map.ts:310
The values in ascending interval order (like Map#values).
Returns
Section titled “Returns”IterableIterator<V>
empty()
Section titled “empty()”static empty<T, V>(domain, options?): IntervalMap<T, V>;Defined in: interval-map.ts:107
The empty map. Give V explicitly: IntervalMap.empty<number, string>(domain).
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
V |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
domain |
IntervalDomain<T> |
options? |
IntervalMapOptions<V> |
Returns
Section titled “Returns”IntervalMap<T, V>
static of<T, V>( domain, entries?, options?): IntervalMap<T, V>;Defined in: interval-map.ts:95
A map built by put-ing each entry in order — so where entries overlap, the
later one wins on the overlapped part.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
V |
Parameters
Section titled “Parameters”| Parameter | Type | Default value |
|---|---|---|
domain |
IntervalDomain<T> |
undefined |
entries? |
Iterable<readonly [Interval<T>, V]> |
[] |
options? |
IntervalMapOptions<V> |
{} |
Returns
Section titled “Returns”IntervalMap<T, V>