| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = bisector;
|
|---|
| 7 | var _ascending = _interopRequireDefault(require("./ascending.js"));
|
|---|
| 8 | var _descending = _interopRequireDefault(require("./descending.js"));
|
|---|
| 9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 10 | function bisector(f) {
|
|---|
| 11 | let compare1, compare2, delta;
|
|---|
| 12 |
|
|---|
| 13 | // If an accessor is specified, promote it to a comparator. In this case we
|
|---|
| 14 | // can test whether the search value is (self-) comparable. We can’t do this
|
|---|
| 15 | // for a comparator (except for specific, known comparators) because we can’t
|
|---|
| 16 | // tell if the comparator is symmetric, and an asymmetric comparator can’t be
|
|---|
| 17 | // used to test whether a single value is comparable.
|
|---|
| 18 | if (f.length !== 2) {
|
|---|
| 19 | compare1 = _ascending.default;
|
|---|
| 20 | compare2 = (d, x) => (0, _ascending.default)(f(d), x);
|
|---|
| 21 | delta = (d, x) => f(d) - x;
|
|---|
| 22 | } else {
|
|---|
| 23 | compare1 = f === _ascending.default || f === _descending.default ? f : zero;
|
|---|
| 24 | compare2 = f;
|
|---|
| 25 | delta = f;
|
|---|
| 26 | }
|
|---|
| 27 | function left(a, x, lo = 0, hi = a.length) {
|
|---|
| 28 | if (lo < hi) {
|
|---|
| 29 | if (compare1(x, x) !== 0) return hi;
|
|---|
| 30 | do {
|
|---|
| 31 | const mid = lo + hi >>> 1;
|
|---|
| 32 | if (compare2(a[mid], x) < 0) lo = mid + 1;else hi = mid;
|
|---|
| 33 | } while (lo < hi);
|
|---|
| 34 | }
|
|---|
| 35 | return lo;
|
|---|
| 36 | }
|
|---|
| 37 | function right(a, x, lo = 0, hi = a.length) {
|
|---|
| 38 | if (lo < hi) {
|
|---|
| 39 | if (compare1(x, x) !== 0) return hi;
|
|---|
| 40 | do {
|
|---|
| 41 | const mid = lo + hi >>> 1;
|
|---|
| 42 | if (compare2(a[mid], x) <= 0) lo = mid + 1;else hi = mid;
|
|---|
| 43 | } while (lo < hi);
|
|---|
| 44 | }
|
|---|
| 45 | return lo;
|
|---|
| 46 | }
|
|---|
| 47 | function center(a, x, lo = 0, hi = a.length) {
|
|---|
| 48 | const i = left(a, x, lo, hi - 1);
|
|---|
| 49 | return i > lo && delta(a[i - 1], x) > -delta(a[i], x) ? i - 1 : i;
|
|---|
| 50 | }
|
|---|
| 51 | return {
|
|---|
| 52 | left,
|
|---|
| 53 | center,
|
|---|
| 54 | right
|
|---|
| 55 | };
|
|---|
| 56 | }
|
|---|
| 57 | function zero() {
|
|---|
| 58 | return 0;
|
|---|
| 59 | } |
|---|