IntervalSet
Defined in: interval-set.ts:120
An immutable set of values of T, stored as the minimal sorted list of
disjoint, non-connected intervals (Guava’s RangeSet). Adding an interval
coalesces it with every interval it overlaps or touches, so on a discrete
domain [1,3] ∪ [4,6] is stored as [1,6], and on a continuous one
[1,2) ∪ [2,3] as [1,3]. Every operation returns a new set.
Build one with IntervalSet.of (any bag of intervals, overlapping or
not, in any order), IntervalSet.empty, IntervalSet.all or
IntervalSet.parse. Pass the domain the intervals use — a bound class
exposes it as NumberInterval.domain, InstantInterval.domain, …
Lookups (contains, intervalContaining, encloses, intersects, subSet)
binary-search the sorted list; the set algebra is linear in the two sizes.
Examples
Section titled “Examples”import { IntegerInterval, IntervalSet } from "@xtandard/lib/interval";
const set = IntervalSet.of(IntegerInterval.domain, [ IntegerInterval.closed(10, 12), IntegerInterval.closed(1, 3), IntegerInterval.closed(4, 6), // adjacent to [1,3] on the integers]);set.toString(); // "[1,6] ∪ [10,12]"set.contains(5); // trueset.contains(8); // falseset.gaps().toString(); // "[7,9]"set.remove(IntegerInterval.closed(2, 4)).toString(); // "[1,1] ∪ [5,6] ∪ [10,12]"set.complement().toString(); // "(-Infinity,0] ∪ [7,9] ∪ [13,+Infinity)"Continuous domains keep brackets exact
import { IntervalSet, NumberInterval } from "@xtandard/lib/interval";
const set = IntervalSet.of(NumberInterval.domain, [ NumberInterval.closedOpen(0, 1), NumberInterval.closed(1, 2), // touches [0,1) at 1 → coalesced NumberInterval.open(5, 6),]);set.toString(); // "[0,2] ∪ (5,6)"set.difference(NumberInterval.closed(1, 5.5)).toString(); // "[0,1) ∪ (5.5,6)"set.length(); // 3 (total measure: 2 + 1)Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Implements
Section titled “Implements”Iterable<Interval<T>>
Properties
Section titled “Properties”| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
domain |
readonly |
IntervalDomain<T> |
The domain that orders/steps/serializes T (shared with every interval in the set). |
interval-set.ts:122 |
Accessors
Section titled “Accessors”isEmpty
Section titled “isEmpty”Get Signature
Section titled “Get Signature”get isEmpty(): boolean;Defined in: interval-set.ts:190
true when the set contains no values.
Returns
Section titled “Returns”boolean
Get Signature
Section titled “Get Signature”get size(): number;Defined in: interval-set.ts:195
The number of disjoint intervals (what iteration yields), not the number of values.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”[iterator](): IterableIterator<Interval<T>>;Defined in: interval-set.ts:443
The intervals in ascending order (sorted, disjoint, non-connected).
Returns
Section titled “Returns”IterableIterator<Interval<T>>
Implementation of
Section titled “Implementation of”Iterable.[iterator]
add(interval): IntervalSet<T>;Defined in: interval-set.ts:397
Add one interval (shorthand for union(interval); Guava’s add).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
interval |
Interval<T> |
Returns
Section titled “Returns”IntervalSet<T>
complement()
Section titled “complement()”complement(): IntervalSet<T>;Defined in: interval-set.ts:327
Every value not in this set, relative to the domain’s whole line (IntervalSet.all).
Returns
Section titled “Returns”IntervalSet<T>
contains()
Section titled “contains()”contains(value): boolean;Defined in: interval-set.ts:200
Whether value is a member (respects open/closed and domain.isValid).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”boolean
difference()
Section titled “difference()”difference(other): IntervalSet<T>;Defined in: interval-set.ts:382
Values in this set but not in other (this ∩ complement(other)).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”IntervalSet<T>
encloses()
Section titled “encloses()”encloses(other): boolean;Defined in: interval-set.ts:226
Whether every value of other (an interval, a set, or a bag of intervals) is
in this set. An interval is enclosed only if ONE interval of the set encloses
it — never across a gap.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”boolean
equals()
Section titled “equals()”equals(other): boolean;Defined in: interval-set.ts:433
Same domain and same values (compares the normalized intervals).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
IntervalSet<T> |
Returns
Section titled “Returns”boolean
format()
Section titled “format()”format(options?): string;Defined in: interval-set.ts:456
Render as [1,3] ∪ [5,7) (each piece via Interval.format), or ∅ when
empty. Pass custom ±∞ tokens via options. Read it back with IntervalSet.parse.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options? |
{ negativeInfinity?: string; positiveInfinity?: string; } |
options.negativeInfinity? |
string |
options.positiveInfinity? |
string |
Returns
Section titled “Returns”string
gaps()
Section titled “gaps()”gaps(): IntervalSet<T>;Defined in: interval-set.ts:291
The holes between consecutive intervals — the complement within span. Empty when the set has fewer than two intervals.
Returns
Section titled “Returns”IntervalSet<T>
intersection()
Section titled “intersection()”intersection(other): IntervalSet<T>;Defined in: interval-set.ts:354
Values in both. Accepts a set, one interval, or a bag of intervals.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”IntervalSet<T>
intersects()
Section titled “intersects()”intersects(other): boolean;Defined in: interval-set.ts:244
Whether this set and other (an interval, a set, or a bag of intervals) share any value.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”boolean
intervalContaining()
Section titled “intervalContaining()”intervalContaining(value): Interval<T> | null;Defined in: interval-set.ts:208
The interval of the set that contains value, or null if it is not a
member (Guava’s rangeContaining).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”Interval<T> | null
isDisjointFrom()
Section titled “isDisjointFrom()”isDisjointFrom(other): boolean;Defined in: interval-set.ts:272
Whether this set and other share no value (!intersects).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”boolean
isSubsetOf()
Section titled “isSubsetOf()”isSubsetOf(other): boolean;Defined in: interval-set.ts:267
Whether every value of this set is in other.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”boolean
isSupersetOf()
Section titled “isSupersetOf()”isSupersetOf(other): boolean;Defined in: interval-set.ts:262
Whether this set contains every value of other (alias of encloses).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”boolean
length()
Section titled “length()”length(): number | null;Defined in: interval-set.ts:312
Total size: the sum of each interval’s length() (a measure for continuous
domains, an element count for discrete ones). 0 for the empty set; null
if any interval is unbounded or the domain has no measure.
Returns
Section titled “Returns”number | null
remove()
Section titled “remove()”remove(interval): IntervalSet<T>;Defined in: interval-set.ts:402
Remove one interval (shorthand for difference(interval); Guava’s remove).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
interval |
Interval<T> |
Returns
Section titled “Returns”IntervalSet<T>
span()
Section titled “span()”span(): Interval<T>;Defined in: interval-set.ts:280
The smallest single interval enclosing the whole set (across its gaps); the empty interval for an empty set.
Returns
Section titled “Returns”Interval<T>
subSet()
Section titled “subSet()”subSet(window): IntervalSet<T>;Defined in: interval-set.ts:407
The part of this set inside window (Guava’s subRangeSet).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
window |
Interval<T> |
Returns
Section titled “Returns”IntervalSet<T>
symmetricDifference()
Section titled “symmetricDifference()”symmetricDifference(other): IntervalSet<T>;Defined in: interval-set.ts:390
Values in exactly one of the two.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”IntervalSet<T>
toArray()
Section titled “toArray()”toArray(): Interval<T>[];Defined in: interval-set.ts:448
A fresh array of the intervals, in ascending order.
Returns
Section titled “Returns”Interval<T>[]
toString()
Section titled “toString()”toString(): string;Defined in: interval-set.ts:463
Default format() (e.g. "[1,3] ∪ [5,7)").
Returns
Section titled “Returns”string
union()
Section titled “union()”union(other): IntervalSet<T>;Defined in: interval-set.ts:345
Values in either (coalesced). Accepts a set, one interval, or a bag of intervals.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
other |
| Interval<T> | Iterable<Interval<T>, any, any> |
Returns
Section titled “Returns”IntervalSet<T>
static all<T>(domain): IntervalSet<T>;Defined in: interval-set.ts:148
The whole line of the domain: (-Infinity,+Infinity), or [-Infinity,+Infinity]
when the domain declares ±∞ values (numberDomain). The complement of ∅.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
domain |
IntervalDomain<T> |
Returns
Section titled “Returns”IntervalSet<T>
empty()
Section titled “empty()”static empty<T>(domain): IntervalSet<T>;Defined in: interval-set.ts:140
The empty set ∅.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
domain |
IntervalDomain<T> |
Returns
Section titled “Returns”IntervalSet<T>
static of<T>(domain, intervals?): IntervalSet<T>;Defined in: interval-set.ts:135
The set covering every value in intervals (any order; overlapping, touching
and empty intervals are fine — they are coalesced/dropped).
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type | Default value |
|---|---|---|
domain |
IntervalDomain<T> |
undefined |
intervals? |
Iterable<Interval<T>> |
[] |
Returns
Section titled “Returns”IntervalSet<T>
parse()
Section titled “parse()”static parse<T>(domain, text): IntervalSet<T>;Defined in: interval-set.ts:168
Parse the IntervalSet.format form — intervals joined by ∪
("[1,3] ∪ [5,7)"), or ∅ / empty text for the empty set. Each piece goes
through parseInterval, so the same tokens and domain rules apply; the
pieces may be in any order and may overlap.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
domain |
IntervalDomain<T> |
text |
string |
Returns
Section titled “Returns”IntervalSet<T>
Throws
Section titled “Throws”If a piece is not a valid interval for the domain.
Example
Section titled “Example”import { IntervalSet, NumberInterval } from "@xtandard/lib/interval";
IntervalSet.parse(NumberInterval.domain, "[5,6] ∪ [0,2) ∪ [1,3]").toString(); // "[0,3] ∪ [5,6]"IntervalSet.parse(NumberInterval.domain, "∅").isEmpty; // true