Skip to content

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.

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.1
tax.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 Parameter
T
V
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
get isEmpty(): boolean;

Defined in: interval-map.ts:129

true when the map has no entries.

boolean


get size(): number;

Defined in: interval-map.ts:134

The number of entries (what iteration yields).

number

[iterator](): IterableIterator<IntervalMapEntry<T, V>>;

Defined in: interval-map.ts:295

The [interval, value] entries in ascending interval order.

IterableIterator<IntervalMapEntry<T, V>>

Iterable.[iterator]


entries(): IterableIterator<IntervalMapEntry<T, V>>;

Defined in: interval-map.ts:300

The [interval, value] entries in ascending interval order (like Map#entries).

IterableIterator<IntervalMapEntry<T, V>>


equals(other): boolean;

Defined in: interval-map.ts:283

Same domain, same intervals, and equal values (per this map’s valueEquals).

Parameter Type
other IntervalMap<T, V>

boolean


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

Parameter Type
options? { formatValue?: (value) => string; negativeInfinity?: string; positiveInfinity?: string; }
options.formatValue? (value) => string
options.negativeInfinity? string
options.positiveInfinity? string

string


get(value): V | undefined;

Defined in: interval-map.ts:153

The value mapped at value, or undefined if no entry covers it.

Parameter Type
value T

V | undefined


getEntry(value):
| IntervalMapEntry<T, V>
| undefined;

Defined in: interval-map.ts:139

The [interval, value] entry whose interval contains value, or undefined.

Parameter Type
value T

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

Parameter Type
value T

boolean


keys(): IterableIterator<Interval<T>>;

Defined in: interval-map.ts:305

The intervals in ascending order (like Map#keys).

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.

Parameter Type
interval Interval<T>
value V

IntervalMap<T, V>


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.

Parameter Type
interval Interval<T>
value V

IntervalMap<T, V>


remove(interval): IntervalMap<T, V>;

Defined in: interval-map.ts:238

Unmap every value of interval, splitting entries that straddle its ends.

Parameter Type
interval Interval<T>

IntervalMap<T, V>


span(): Interval<T>;

Defined in: interval-map.ts:163

The smallest interval enclosing every entry; the empty interval for an empty map.

Interval<T>


subMap(window): IntervalMap<T, V>;

Defined in: interval-map.ts:259

The entries restricted to window (each clipped to it); Guava’s subRangeMap.

Parameter Type
window Interval<T>

IntervalMap<T, V>


toArray(): IntervalMapEntry<T, V>[];

Defined in: interval-map.ts:315

A fresh array of the entries, in ascending interval order.

IntervalMapEntry<T, V>[]


toIntervalSet(): IntervalSet<T>;

Defined in: interval-map.ts:171

Every value of T that has an entry, as an IntervalSet (touching entries merge).

IntervalSet<T>


toString(): string;

Defined in: interval-map.ts:338

Default format() (e.g. "{[0,10) => a, [10,20) => b}").

string


values(): IterableIterator<V>;

Defined in: interval-map.ts:310

The values in ascending interval order (like Map#values).

IterableIterator<V>


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 Parameter
T
V
Parameter Type
domain IntervalDomain<T>
options? IntervalMapOptions<V>

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 Parameter
T
V
Parameter Type Default value
domain IntervalDomain<T> undefined
entries? Iterable<readonly [Interval<T>, V]> []
options? IntervalMapOptions<V> {}

IntervalMap<T, V>