All files / src/datatypes interval.coffee

91.3% Statements 84/92
88.68% Branches 141/159
100% Functions 33/33
92.77% Lines 77/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 3271x 1x 1x 1x 1x     107615x       1x         260x 260x   141x   141x         2x 194x                             2x 445x 445x                           679x 679x                     137x 137x             137x 137x           1x     1x     1x 2x 2x 4x 4x 4x   1x 2x 2x 4x 4x 4x     1x     127x 127x             127x                             116x 116x             116x                         1x   151x   111x 111x                               2x             1x                 5x       1x             1x                 418x                 183x                 182x                                                                     6x   1x 109x         11x   1x 64x       3x   11x 14x       14x   3x   11x             8x         6x         6847x   6620x       6620x       75x 82x           245x 245x 245x  
{ DateTime } = require './datetime'
{ Uncertainty } = require './uncertainty'
{ ThreeValuedLogic } = require './logic'
{ successor, predecessor, maxValueForInstance, minValueForInstance } = require '../util/math'
cmp = require '../util/comparison'
 
module.exports.Interval = class Interval
  constructor: (@low, @high, @lowClosed = true, @highClosed = true) ->
 
  # Define a simple getter to allow type-checking of this class without instanceof
  # and in a way that survives minification (as opposed to checking constructor.name)
  Object.defineProperties @prototype,
    isInterval:
      get: -> true
 
  copy: ->
    newLow = @low
    newHigh = @high
    if @low? and typeof @low.copy == 'function'
      newLow = @low.copy()
    if @high? and typeof @high.copy == 'function'
      newHigh = @high.copy();
 
    new Interval(newLow, newHigh, @lowClosed, @highClosed)
 
  contains: (item, precision) ->
    if item instanceof Interval then throw new Error("Argument to contains must be a point")
    closed = @toClosed()
    ThreeValuedLogic.and(
      cmp.lessThanOrEquals(closed.low, item, precision),
      cmp.greaterThanOrEquals(closed.high, item, precision)
    )
 
  properlyIncludes: (other, precision) ->
    if not (other instanceof Interval) then throw new Error("Argument to properlyIncludes must be an interval")
    ThreeValuedLogic.and(
      @includes(other, precision),
      ThreeValuedLogic.not(other.includes(@, precision))
    )
 
  includes: (other, precision) ->
    if not (other instanceof Interval)
      return @.contains(other,precision)
    a = @toClosed()
    b = other.toClosed()
    ThreeValuedLogic.and(
      cmp.lessThanOrEquals(a.low, b.low, precision),
      cmp.greaterThanOrEquals(a.high, b.high, precision)
    )
 
  includedIn: (other, precision) ->
    # For the point overload, this operator is a synonym for the in operator
    if not (other instanceof Interval)
      @.contains(other, precision)
    else
      other.includes @
 
  overlaps: (item, precision) ->
    closed = @toClosed()
    [low, high] = if item instanceof Interval
      itemClosed = item.toClosed()
      [itemClosed.low, itemClosed.high]
    else
      [item, item]
    ThreeValuedLogic.and(
      cmp.lessThanOrEquals(closed.low, high, precision),
      cmp.greaterThanOrEquals(closed.high, low, precision)
    )
 
  overlapsAfter: (item, precision) ->
    closed = @toClosed()
    high = if item instanceof Interval then item.toClosed().high else item
    ThreeValuedLogic.and(
      cmp.lessThanOrEquals(closed.low, high, precision),
      cmp.greaterThan(closed.high, high, precision)
    )
 
  overlapsBefore: (item, precision) ->
    closed = @toClosed()
    low = if item instanceof Interval then item.toClosed().low else item
    ThreeValuedLogic.and(
      cmp.lessThan(closed.low, low, precision),
      cmp.greaterThanOrEquals(closed.high, low, precision)
    )
 
  areDateTimes = (x, y) ->
    [x, y].every (z) -> z instanceof DateTime
 
  areNumeric = (x, y) ->
    [x, y].every (z) -> typeof z is 'number' or (z instanceof Uncertainty and typeof z.low is 'number')
 
  lowestNumericUncertainty = (x, y) ->
    if not(x instanceof Uncertainty) then x = new Uncertainty(x)
    if not(y instanceof Uncertainty) then y = new Uncertainty(y)
    low = if x.low < y.low then x.low else y.low
    high = if x.high < y.high then x.high else y.high
    if low != high then return new Uncertainty(low, high) else return low
 
  highestNumericUncertainty = (x, y) ->
    if not(x instanceof Uncertainty) then x = new Uncertainty(x)
    if not(y instanceof Uncertainty) then y = new Uncertainty(y)
    low = if x.low > y.low then x.low else y.low
    high = if x.high > y.high then x.high else y.high
    if low != high then return new Uncertainty(low, high) else return low
 
  union: (other) ->
    if not (other instanceof Interval) then throw new Error("Argument to union must be an interval")
    # Note that interval union is only defined if the arguments overlap or meet.
    if @overlaps(other) or @meets(other)
      [a, b] = [@toClosed(), other.toClosed()]
      [l, lc] = switch
        when cmp.lessThanOrEquals(a.low, b.low) then [@low, @lowClosed]
        when cmp.greaterThanOrEquals(a.low, b.low) then [other.low, other.lowClosed]
        when areNumeric(a.low, b.low) then [lowestNumericUncertainty(a.low, b.low), true]
        # TODO: Do we need to support quantities here?
        when areDateTimes(a.low, b.low) and a.low.isMorePrecise(b.low) then [other.low, other.lowClosed]
        else [@low, @lowClosed]
      [h, hc] = switch
        when cmp.greaterThanOrEquals(a.high, b.high) then [@high, @highClosed]
        when cmp.lessThanOrEquals(a.high, b.high) then [other.high, other.highClosed]
        when areNumeric(a.high, b.high) then [highestNumericUncertainty(a.high, b.high), true]
        # TODO: Do we need to support quantities here?
        when areDateTimes(a.high, b.high) and a.high.isMorePrecise(b.high) then [other.high, other.highClosed]
        else [@high, @highClosed]
      new Interval(l, h, lc, hc)
    else
      null
 
  intersect: (other) ->
    if not (other instanceof Interval) then throw new Error("Argument to union must be an interval")
    # Note that interval union is only defined if the arguments overlap.
    if @overlaps(other)
      [a, b] = [@toClosed(), other.toClosed()]
      [l, lc] = switch
        when cmp.greaterThanOrEquals(a.low, b.low) then [@low, @lowClosed]
        when cmp.lessThanOrEquals(a.low, b.low) then [other.low, other.lowClosed]
        when areNumeric(a.low, b.low) then [highestNumericUncertainty(a.low, b.low), true]
        # TODO: Do we need to support quantities here?
        when areDateTimes(a.low, b.low) and b.low.isMorePrecise(a.low) then [other.low, other.lowClosed]
        else [@low, @lowClosed]
      [h, hc] = switch
        when cmp.lessThanOrEquals(a.high, b.high) then [@high, @highClosed]
        when cmp.greaterThanOrEquals(a.high, b.high) then [other.high, other.highClosed]
        when areNumeric(a.high, b.high) then [lowestNumericUncertainty(a.high, b.high), true]
        # TODO: Do we need to support quantities here?
        when areDateTimes(a.high, b.high) and b.high.isMorePrecise(a.high) then [other.high, other.highClosed]
        else [@high, @highClosed]
      new Interval(l, h, lc, hc)
    else
      null
 
  except: (other) ->
    if (other == null) then return null
    if not (other instanceof Interval) then throw new Error("Argument to except must be an interval")
 
    ol = @overlaps other
    if ol is true
      olb = @overlapsBefore other
      ola = @overlapsAfter other
      if olb is true and ola is false then new Interval(@low, other.low, @lowClosed, not other.lowClosed)
      else if ola is true and olb is false then new Interval(other.high, @high, not other.highClosed, @highClosed)
      else null
    else if ol is false
      @
    else # ol is null
      null
 
  sameAs: (other, precision) ->
    # This large if and else if block handles the scenarios where there is an open ended null
    # If both lows or highs exists, it can be determined that intervals are not Same As
    if (@low? and other.low? and !@high? and other.high? and !@highClosed) or
       (@low? and other.low? and @high? and !other.high? and !other.highClosed) or
       (@low? and other.low? and !@high? and !other.high? and !other.highClosed and !@highClosed)
      if typeof @low == 'number'
        if !(@.start() == other.start()) then return false
      else
        if !(@.start().sameAs(other.start(), precision)) then return false
    else if (@low? and !other.low? and @high? and other.high?) or
            (!@low? and other.low? and @high? and other.high?) or
            (!@low? and !other.low? and @high? and other.high?)
      if typeof @high == 'number'
        if !(@.end() == other.end()) then return false
      else
        if !(@.end().sameAs(other.end(), precision)) then return false
 
    # Checks to see if any of the Intervals have a open, null boundary
    if (!@low? and !@lowClosed) or
       (!@high? and !@highClosed) or
       (!other.low? and !other.lowClosed) or
       (!other.high? and !other.highClosed)
      return null
 
    # For the special cases where @ is Interval[null,null]
    if @lowClosed and !@low? and @highClosed and !@high?
      return other.lowClosed and !other.low? and other.highClosed and !other.high?
 
    # For the special case where Interval[...] same as Interval[null,null] should return false
    # This accounts for the inverse of the if statement above: where the second Interval is [null,null] and not the first Interval
    # The reason why this isn't caught below is due to how start() and end() work
    # There is no way to tell the datatype for MIN and MAX if both boundaries are null
    if other.lowClosed and !other.low? and other.highClosed and !other.high?
      return false
 
    if typeof @low == 'number'
      @.start() == other.start() and @.end() == other.end()
    else
      @.start().sameAs(other.start(), precision) and @.end().sameAs(other.end(), precision)
 
  equals: (other) ->
    if other instanceof Interval
      [a, b] = [@toClosed(), other.toClosed()]
      ThreeValuedLogic.and(
        cmp.equals(a.low, b.low),
        cmp.equals(a.high, b.high)
      )
    else
      false
 
  after: (other, precision) ->
    closed = @toClosed()
    # Meets spec, but not 100% correct (e.g., (null, 5] after [6, 10] --> null)
    # Simple way to fix it: and w/ not overlaps
    if !!other.toClosed
      cmp.greaterThan closed.low, other.toClosed().high, precision
    else
      cmp.greaterThan closed.low, other, precision
 
  before: (other, precision) ->
    closed = @toClosed()
    # Meets spec, but not 100% correct (e.g., (null, 5] after [6, 10] --> null)
    # Simple way to fix it: and w/ not overlaps
    if !!other.toClosed
      cmp.lessThan closed.high, other.toClosed().low, precision
    else
      cmp.lessThan closed.high, other, precision
 
  meets: (other, precision) ->
    ThreeValuedLogic.or(
      @meetsBefore(other, precision),
      @meetsAfter(other, precision)
    )
 
  meetsAfter: (other, precision) ->
    try
      if precision? and @low instanceof DateTime
        @toClosed().low.sameAs(other.toClosed().high?.add(1, precision), precision)
      else
        cmp.equals @toClosed().low, successor(other.toClosed().high)
    catch
      false
 
  meetsBefore: (other, precision) ->
    try
      if precision? and @high instanceof DateTime
        @toClosed().high.sameAs(other.toClosed().low?.add(-1, precision), precision)
      else
        cmp.equals @toClosed().high, predecessor(other.toClosed().low)
    catch
      false
 
  start: () ->
    if !@low?
      if @lowClosed
        return minValueForInstance(@high)
      else
        return @low
    return @toClosed().low
 
  end: () ->
    if !@high?
      if @highClosed
        return maxValueForInstance(@low)
      else
        return @high
    return @toClosed().high
 
  starts: (other, precision) ->
    if precision? and @low instanceof DateTime
      startEqual = @low.sameAs(other.low, precision)
    else
      startEqual = cmp.equals(@low, other.low)
    endLessThanOrEqual = cmp.lessThanOrEquals(@high, other.high, precision)
    startEqual and endLessThanOrEqual
 
  ends: (other, precision) ->
    startGreaterThanOrEqual = cmp.greaterThanOrEquals(@low, other.low, precision)
    if precision? and @low instanceof DateTime
      endEqual = @high.sameAs(other.high, precision)
    else
      endEqual = cmp.equals(@high, other.high)
    startGreaterThanOrEqual && endEqual
 
  width: () ->
    if @low instanceof DateTime or @high instanceof DateTime
      throw new Error("Width of DateTime intervals is not supported")
 
    closed = @toClosed()
    if closed.low instanceof Uncertainty or closed.high instanceof Uncertainty
      null
    else
      # TODO: Fix precision to 8 decimals in other places that return numbers
      diff = Math.abs(closed.high - closed.low)
      Math.round(diff * Math.pow(10, 8)) / Math.pow(10, 8)
 
 
  toClosed: () ->
    point = @low ? @high
    if typeof(point) is 'number' or point instanceof DateTime or point?.isQuantity or point?.isDate
      low = switch
        when @lowClosed and not @low? then minValueForInstance point
        when not @lowClosed and @low? then successor @low
        else @low
      high = switch
        when @highClosed and not @high? then maxValueForInstance point
        when not @highClosed and @high? then predecessor @high
        else @high
      if not low? then low = new Uncertainty(minValueForInstance(point), high)
      if not high? then high = new Uncertainty(low, maxValueForInstance(point))
      new Interval(low, high, true, true)
    else
      new Interval(@low, @high, true, true)
 
  toString:  () ->
    start = if @lowClosed then '[' else '('
    end = if @highClosed then ']' else ')'
    return start + @low.toString() + ', ' + @high.toString() + end