| 29 | } |
| 30 | |
| 31 | addInterval(toAdd) { |
| 32 | if (this.intervals === null) { |
| 33 | this.intervals = []; |
| 34 | this.intervals.push(toAdd.clone()); |
| 35 | } else { |
| 36 | // find insert pos |
| 37 | for (let pos = 0; pos < this.intervals.length; pos++) { |
| 38 | const existing = this.intervals[pos]; |
| 39 | // distinct range -> insert |
| 40 | if (toAdd.stop < existing.start) { |
| 41 | this.intervals.splice(pos, 0, toAdd); |
| 42 | return; |
| 43 | } |
| 44 | // contiguous range -> adjust |
| 45 | else if (toAdd.stop === existing.start) { |
| 46 | this.intervals[pos] = new Interval(toAdd.start, existing.stop) |
| 47 | return; |
| 48 | } |
| 49 | // overlapping range -> adjust and reduce |
| 50 | else if (toAdd.start <= existing.stop) { |
| 51 | this.intervals[pos] = new Interval(Math.min(existing.start, toAdd.start), Math.max(existing.stop, toAdd.stop)); |
| 52 | this.reduce(pos); |
| 53 | return; |
| 54 | } |
| 55 | } |
| 56 | // greater than any existing |
| 57 | this.intervals.push(toAdd.clone()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | addSet(other) { |
| 62 | if (other.intervals !== null) { |