Interval API

class Interval(low[, high[, lowInclude[, highInclude]]])
Arguments:
  • low (float) – leftmost endpoint of interval
  • high (float) – rightmost endpoint of interval
  • lowInclude (boolean) –
    low endpoint value included in interval
    true means left-closed
    false means left-open
    true by default
  • highInclude (boolean) –
    high endpoint value included in interval
    true means right-closed
    false means right-open
    false by default

If only low is given, or if low == high, the interval is singular. In this case lowInclude and highInclude are both true.

If low is -Infinity, lowInclude is always true If high is Infinity, highInclude is always true

Interval.low

float: left endpoint value

Interval.high

float: right endpoint value

Interval.lowInclude

boolean: true if interval is left-closed

Interval.highInclude

boolean: true if interval is right-closed

Interval.singular

boolean: true if interval is singular

Interval.finite

boolean: true if both low and high are finite values

Interval.length

float: interval length (high-low)

Interval.endpointLow

endpoint: low endpoint [value, false, lowInclude, singular]

Interval.endpointHigh

endpoint: low endpoint [value, true, highInclude, singular]

Interval.toString()
Returns string:

Human readable string

Interval.covers_endpoint(p)
Arguments:
  • p (number) – point
Returns boolean:
 

True if point p is inside interval

Test if point p is inside interval.

See Interval Comparison.

let a = new Interval(4, 5)  // [4,5)
a.covers_endpoint(4.0)  // true
a.covers_endpoint(4.3)  // true
a.covers_endpoint(5.0)  // false
Interval.equals(other)
Arguments:
  • other (Interval) – interval to compare with
Returns boolean:
 

true if intervals are equal

See Interval Comparison.

Interval.compare(other)
Arguments:
  • other (Interval) – interval to compare with
Returns int:

comparison relation

Compares interval to another interval, i.e. cmp(interval, other). See Interval Comparison.

let a = new Interval(4, 5)  // [4,5)
let b = new Interval(4, 5, true, true)  // [4,5]
a.compare(b) == Interval.Relation.COVERED  // true
b.compare(a) == Interval.Relation.COVERS   // true
Interval.match(other[, mask=62])
Arguments:
  • other (Interval) – interval to compare with
Returns boolean:
 

true if intervals match

Matches two intervals. Mask defines what consitutes a match. See Interval Match.

let a = new Interval(4, 5)  // [4,5)
let b = new Interval(4, 5, true, true)  // [4,5]
a.match(b) // true
b.match(a) // true
Interval.Relation
{
    OUTSIDE_LEFT: 64,   // 0b1000000
    OVERLAP_LEFT: 32,   // 0b0100000
    COVERED: 16,        // 0b0010000
    EQUALS: 8,          // 0b0001000
    COVERS: 4,          // 0b0000100
    OVERLAP_RIGHT: 2,   // 0b0000010
    OUTSIDE_RIGHT: 1    // 0b0000001
}
Interval.Interval.Relation.OUTSIDE_LEFT
Interval.Relation.OVERLAP_LEFT
Interval.Relation.COVERED
Interval.Relation.EQUAL
Interval.Relation.COVERS
Interval.Relation.OVERLAP_RIGHT
Interval.Relation.OUTSIDE_RIGHT
Interval.cmpLow(interval_a, interval_b)
Arguments:
Returns int:
a < b : -1
a == b : 0
a > b : 1

Use with Array.sort() to sort Intervals by their low endpoint.

a = [
    new Interval(4,5),
    new Interval(2,3),
    new Interval(1,6)
];
a.sort(Interval.cmpLow);
// [1,6), [2,3), [4,5)
Interval.cmpHigh(interval_a, interval_b)
Arguments:
Returns int:
a < b : -1
a == b : 0
a > b : 1

Use with Array.sort() to sort Intervals by their high endpoint.

a = [
    new Interval(4,5),
    new Interval(2,3),
    new Interval(1,6)
];
a.sort(Interval.cmpHigh);
// [2,3), [4,5), [1,6)