(item: T | T[])
| 19 | } |
| 20 | |
| 21 | insert(item: T | T[]): number { |
| 22 | if (Array.isArray(item)) { |
| 23 | let ind = -1 |
| 24 | item.forEach((it) => { |
| 25 | ind = this.insert(it) |
| 26 | }) |
| 27 | |
| 28 | return ind |
| 29 | } |
| 30 | |
| 31 | if (this.raw.length === 0) { |
| 32 | this.raw.push(item) |
| 33 | |
| 34 | return 0 |
| 35 | } |
| 36 | |
| 37 | // find insert position |
| 38 | let lo = 0 |
| 39 | let hi = this.raw.length |
| 40 | |
| 41 | while (lo < hi) { |
| 42 | const mid = Math.floor((lo + hi) / 2) |
| 43 | |
| 44 | if (this.comparator(this.raw[mid], item) > 0) { |
| 45 | hi = mid |
| 46 | } else { |
| 47 | lo = mid + 1 |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | this.raw.splice(lo, 0, item) |
| 52 | |
| 53 | return lo |
| 54 | } |
| 55 | |
| 56 | // closest: return the closest value (right-hand side) |
| 57 | // meaning that raw[idx - 1] <= item <= raw[idx] |
no test coverage detected