@dra2020/district-analytics 1.0.7 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +761 -594
- package/dist/cli.js.map +1 -1
- package/package.json +2 -1
- package/.prettierrc +0 -5
- package/jestconfig.json +0 -14
- package/lib/HelloWorld.d.ts +0 -3
- package/lib/HelloWorld.js +0 -11
- package/lib/_api.js +0 -91
- package/lib/_api.js.map +0 -1
- package/lib/_cli.js +0 -434
- package/lib/_cli.js.map +0 -1
- package/lib/_data.js +0 -425
- package/lib/_data.js.map +0 -1
- package/lib/analyze.d.ts +0 -3
- package/lib/analyze.js +0 -69
- package/lib/analyze.js.map +0 -1
- package/lib/api.d.ts +0 -34
- package/lib/api.js +0 -117
- package/lib/api.js.map +0 -1
- package/lib/cli.d.ts +0 -1
- package/lib/cli.js +0 -386
- package/lib/cli.js.map +0 -1
- package/lib/cohesive.d.ts +0 -4
- package/lib/cohesive.js +0 -132
- package/lib/cohesive.js.map +0 -1
- package/lib/compact.d.ts +0 -4
- package/lib/compact.js +0 -183
- package/lib/compact.js.map +0 -1
- package/lib/constants.js +0 -367
- package/lib/constants.js.map +0 -1
- package/lib/data.js +0 -188
- package/lib/data.js.map +0 -1
- package/lib/equal.d.ts +0 -4
- package/lib/equal.js +0 -59
- package/lib/equal.js.map +0 -1
- package/lib/features.js +0 -19
- package/lib/features.js.map +0 -1
- package/lib/geofeature.js +0 -112
- package/lib/geofeature.js.map +0 -1
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -11
- package/lib/index.js.map +0 -1
- package/lib/minority.d.ts +0 -3
- package/lib/minority.js +0 -61
- package/lib/minority.js.map +0 -1
- package/lib/political.d.ts +0 -7
- package/lib/political.js +0 -88
- package/lib/political.js.map +0 -1
- package/lib/preprocess.d.ts +0 -4
- package/lib/preprocess.js +0 -101
- package/lib/preprocess.js.map +0 -1
- package/lib/report.d.ts +0 -14
- package/lib/report.js +0 -817
- package/lib/report.js.map +0 -1
- package/lib/sample.d.ts +0 -1
- package/lib/sample.js +0 -32
- package/lib/sample.js.map +0 -1
- package/lib/scorecard.d.ts +0 -4
- package/lib/scorecard.js +0 -237
- package/lib/scorecard.js.map +0 -1
- package/lib/settings.d.ts +0 -5
- package/lib/settings.js +0 -18
- package/lib/settings.js.map +0 -1
- package/lib/types.d.ts +0 -125
- package/lib/types.js +0 -7
- package/lib/types.js.map +0 -1
- package/lib/utils.d.ts +0 -20
- package/lib/utils.js +0 -223
- package/lib/utils.js.map +0 -1
- package/lib/valid.d.ts +0 -5
- package/lib/valid.js +0 -230
- package/lib/valid.js.map +0 -1
- package/main.js +0 -4
- package/src/_api.ts +0 -121
- package/src/_data.ts +0 -595
- package/src/analyze.ts +0 -92
- package/src/cohesive.ts +0 -156
- package/src/compact.ts +0 -208
- package/src/constants.ts +0 -371
- package/src/equal.ts +0 -75
- package/src/geofeature.ts +0 -138
- package/src/index.ts +0 -6
- package/src/minority.ts +0 -70
- package/src/political.ts +0 -114
- package/src/preprocess.ts +0 -132
- package/src/report.ts +0 -1022
- package/src/settings.ts +0 -20
- package/src/types.ts +0 -185
- package/src/utils.ts +0 -245
- package/src/valid.ts +0 -275
- package/tsconfig.json +0 -25
- package/tslint.json +0 -3
- package/types/polygon-clipping/index.d.ts +0 -1
- package/webpack.config.js +0 -73
package/dist/cli.js
CHANGED
|
@@ -6139,7 +6139,7 @@ module.exports = function(data, options={}){
|
|
|
6139
6139
|
|
|
6140
6140
|
"use strict";
|
|
6141
6141
|
__webpack_require__.r(__webpack_exports__);
|
|
6142
|
-
/* harmony import */ var splaytree__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! splaytree */ "./node_modules/splaytree/
|
|
6142
|
+
/* harmony import */ var splaytree__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! splaytree */ "./node_modules/splaytree/index.js");
|
|
6143
6143
|
|
|
6144
6144
|
|
|
6145
6145
|
function _classCallCheck(instance, Constructor) {
|
|
@@ -7178,12 +7178,12 @@ function () {
|
|
|
7178
7178
|
consumee = consumee.consumedBy;
|
|
7179
7179
|
}
|
|
7180
7180
|
|
|
7181
|
-
var cmp = Segment.compare(consumer, consumee);
|
|
7182
|
-
if (cmp === 0) return; // already consumed
|
|
7181
|
+
var cmp$$1 = Segment.compare(consumer, consumee);
|
|
7182
|
+
if (cmp$$1 === 0) return; // already consumed
|
|
7183
7183
|
// the winner of the consumption is the earlier segment
|
|
7184
7184
|
// according to sweep line ordering
|
|
7185
7185
|
|
|
7186
|
-
if (cmp > 0) {
|
|
7186
|
+
if (cmp$$1 > 0) {
|
|
7187
7187
|
var tmp = consumer;
|
|
7188
7188
|
consumer = consumee;
|
|
7189
7189
|
consumee = tmp;
|
|
@@ -8171,604 +8171,771 @@ var index = {
|
|
|
8171
8171
|
|
|
8172
8172
|
/***/ }),
|
|
8173
8173
|
|
|
8174
|
-
/***/ "./node_modules/splaytree/
|
|
8175
|
-
|
|
8176
|
-
!*** ./node_modules/splaytree/
|
|
8177
|
-
|
|
8174
|
+
/***/ "./node_modules/splaytree/index.js":
|
|
8175
|
+
/*!*****************************************!*\
|
|
8176
|
+
!*** ./node_modules/splaytree/index.js ***!
|
|
8177
|
+
\*****************************************/
|
|
8178
8178
|
/*! exports provided: default */
|
|
8179
8179
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
8180
8180
|
|
|
8181
8181
|
"use strict";
|
|
8182
8182
|
__webpack_require__.r(__webpack_exports__);
|
|
8183
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return Tree; });
|
|
8184
|
+
/* follows "An implementation of top-down splaying"
|
|
8185
|
+
* by D. Sleator <sleator@cs.cmu.edu> March 1992
|
|
8186
|
+
*/
|
|
8187
|
+
|
|
8183
8188
|
/**
|
|
8184
|
-
*
|
|
8185
|
-
|
|
8186
|
-
|
|
8187
|
-
|
|
8188
|
-
|
|
8189
|
-
* @
|
|
8189
|
+
* @typedef {*} Key
|
|
8190
|
+
*/
|
|
8191
|
+
|
|
8192
|
+
|
|
8193
|
+
/**
|
|
8194
|
+
* @typedef {*} Value
|
|
8195
|
+
*/
|
|
8196
|
+
|
|
8197
|
+
|
|
8198
|
+
/**
|
|
8199
|
+
* @typedef {function(node:Node):void} Visitor
|
|
8200
|
+
*/
|
|
8201
|
+
|
|
8202
|
+
|
|
8203
|
+
/**
|
|
8204
|
+
* @typedef {function(a:Key, b:Key):number} Comparator
|
|
8205
|
+
*/
|
|
8206
|
+
|
|
8207
|
+
|
|
8208
|
+
/**
|
|
8209
|
+
* @param {function(node:Node):string} NodePrinter
|
|
8210
|
+
*/
|
|
8211
|
+
|
|
8212
|
+
|
|
8213
|
+
/**
|
|
8214
|
+
* @typedef {Object} Node
|
|
8215
|
+
* @property {Key} Key
|
|
8216
|
+
* @property {Value=} data
|
|
8217
|
+
* @property {Node} left
|
|
8218
|
+
* @property {Node} right
|
|
8219
|
+
*/
|
|
8220
|
+
|
|
8221
|
+
class Node {
|
|
8222
|
+
|
|
8223
|
+
constructor (key, data) {
|
|
8224
|
+
this.key = key;
|
|
8225
|
+
this.data = data;
|
|
8226
|
+
this.left = null;
|
|
8227
|
+
this.right = null;
|
|
8228
|
+
}
|
|
8229
|
+
}
|
|
8230
|
+
|
|
8231
|
+
function DEFAULT_COMPARE (a, b) { return a > b ? 1 : a < b ? -1 : 0; }
|
|
8232
|
+
|
|
8233
|
+
|
|
8234
|
+
/**
|
|
8235
|
+
* Simple top down splay, not requiring i to be in the tree t.
|
|
8236
|
+
* @param {Key} i
|
|
8237
|
+
* @param {Node?} t
|
|
8238
|
+
* @param {Comparator} comparator
|
|
8239
|
+
*/
|
|
8240
|
+
function splay (i, t, comparator) {
|
|
8241
|
+
if (t === null) return t;
|
|
8242
|
+
let l, r, y;
|
|
8243
|
+
const N = new Node();
|
|
8244
|
+
l = r = N;
|
|
8245
|
+
|
|
8246
|
+
while (true) {
|
|
8247
|
+
const cmp = comparator(i, t.key);
|
|
8248
|
+
//if (i < t.key) {
|
|
8249
|
+
if (cmp < 0) {
|
|
8250
|
+
if (t.left === null) break;
|
|
8251
|
+
//if (i < t.left.key) {
|
|
8252
|
+
if (comparator(i, t.left.key) < 0) {
|
|
8253
|
+
y = t.left; /* rotate right */
|
|
8254
|
+
t.left = y.right;
|
|
8255
|
+
y.right = t;
|
|
8256
|
+
t = y;
|
|
8257
|
+
if (t.left === null) break;
|
|
8258
|
+
}
|
|
8259
|
+
r.left = t; /* link right */
|
|
8260
|
+
r = t;
|
|
8261
|
+
t = t.left;
|
|
8262
|
+
//} else if (i > t.key) {
|
|
8263
|
+
} else if (cmp > 0) {
|
|
8264
|
+
if (t.right === null) break;
|
|
8265
|
+
//if (i > t.right.key) {
|
|
8266
|
+
if (comparator(i, t.right.key) > 0) {
|
|
8267
|
+
y = t.right; /* rotate left */
|
|
8268
|
+
t.right = y.left;
|
|
8269
|
+
y.left = t;
|
|
8270
|
+
t = y;
|
|
8271
|
+
if (t.right === null) break;
|
|
8272
|
+
}
|
|
8273
|
+
l.right = t; /* link left */
|
|
8274
|
+
l = t;
|
|
8275
|
+
t = t.right;
|
|
8276
|
+
} else {
|
|
8277
|
+
break;
|
|
8278
|
+
}
|
|
8279
|
+
}
|
|
8280
|
+
/* assemble */
|
|
8281
|
+
l.right = t.left;
|
|
8282
|
+
r.left = t.right;
|
|
8283
|
+
t.left = N.right;
|
|
8284
|
+
t.right = N.left;
|
|
8285
|
+
return t;
|
|
8286
|
+
}
|
|
8287
|
+
|
|
8288
|
+
|
|
8289
|
+
/**
|
|
8290
|
+
* @param {Key} i
|
|
8291
|
+
* @param {Value} data
|
|
8292
|
+
* @param {Comparator} comparator
|
|
8293
|
+
* @param {Tree} tree
|
|
8294
|
+
* @return {Node} root
|
|
8295
|
+
*/
|
|
8296
|
+
function insert (i, data, t, comparator, tree) {
|
|
8297
|
+
const node = new Node(i, data);
|
|
8298
|
+
|
|
8299
|
+
tree._size++;
|
|
8300
|
+
|
|
8301
|
+
if (t === null) {
|
|
8302
|
+
node.left = node.right = null;
|
|
8303
|
+
return node;
|
|
8304
|
+
}
|
|
8305
|
+
|
|
8306
|
+
t = splay(i, t, comparator);
|
|
8307
|
+
const cmp = comparator(i, t.key);
|
|
8308
|
+
if (cmp < 0) {
|
|
8309
|
+
node.left = t.left;
|
|
8310
|
+
node.right = t;
|
|
8311
|
+
t.left = null;
|
|
8312
|
+
} else if (cmp >= 0) {
|
|
8313
|
+
node.right = t.right;
|
|
8314
|
+
node.left = t;
|
|
8315
|
+
t.right = null;
|
|
8316
|
+
}
|
|
8317
|
+
return node;
|
|
8318
|
+
}
|
|
8319
|
+
|
|
8320
|
+
|
|
8321
|
+
/**
|
|
8322
|
+
* Insert i into the tree t, unless it's already there.
|
|
8323
|
+
* @param {Key} i
|
|
8324
|
+
* @param {Value} data
|
|
8325
|
+
* @param {Comparator} comparator
|
|
8326
|
+
* @param {Tree} tree
|
|
8327
|
+
* @return {Node} root
|
|
8328
|
+
*/
|
|
8329
|
+
function add (i, data, t, comparator, tree) {
|
|
8330
|
+
const node = new Node(i, data);
|
|
8331
|
+
|
|
8332
|
+
if (t === null) {
|
|
8333
|
+
node.left = node.right = null;
|
|
8334
|
+
tree._size++;
|
|
8335
|
+
return node;
|
|
8336
|
+
}
|
|
8337
|
+
|
|
8338
|
+
t = splay(i, t, comparator);
|
|
8339
|
+
const cmp = comparator(i, t.key);
|
|
8340
|
+
if (cmp === 0) return t;
|
|
8341
|
+
else {
|
|
8342
|
+
if (cmp < 0) {
|
|
8343
|
+
node.left = t.left;
|
|
8344
|
+
node.right = t;
|
|
8345
|
+
t.left = null;
|
|
8346
|
+
} else if (cmp > 0) {
|
|
8347
|
+
node.right = t.right;
|
|
8348
|
+
node.left = t;
|
|
8349
|
+
t.right = null;
|
|
8350
|
+
}
|
|
8351
|
+
tree._size++;
|
|
8352
|
+
return node;
|
|
8353
|
+
}
|
|
8354
|
+
}
|
|
8355
|
+
|
|
8356
|
+
|
|
8357
|
+
/**
|
|
8358
|
+
* Deletes i from the tree if it's there
|
|
8359
|
+
* @param {Key} i
|
|
8360
|
+
* @param {Tree} tree
|
|
8361
|
+
* @param {Comparator} comparator
|
|
8362
|
+
* @param {Tree} tree
|
|
8363
|
+
* @return {Node} new root
|
|
8190
8364
|
*/
|
|
8365
|
+
function remove (i, t, comparator, tree) {
|
|
8366
|
+
let x;
|
|
8367
|
+
if (t === null) return null;
|
|
8368
|
+
t = splay(i, t, comparator);
|
|
8369
|
+
var cmp = comparator(i, t.key);
|
|
8370
|
+
if (cmp === 0) { /* found it */
|
|
8371
|
+
if (t.left === null) {
|
|
8372
|
+
x = t.right;
|
|
8373
|
+
} else {
|
|
8374
|
+
x = splay(i, t.left, comparator);
|
|
8375
|
+
x.right = t.right;
|
|
8376
|
+
}
|
|
8377
|
+
tree._size--;
|
|
8378
|
+
return x;
|
|
8379
|
+
}
|
|
8380
|
+
return t; /* It wasn't there */
|
|
8381
|
+
}
|
|
8382
|
+
|
|
8383
|
+
|
|
8384
|
+
function split (key, v, comparator) {
|
|
8385
|
+
let left, right;
|
|
8386
|
+
if (v === null) {
|
|
8387
|
+
left = right = null;
|
|
8388
|
+
} else {
|
|
8389
|
+
v = splay(key, v, comparator);
|
|
8390
|
+
|
|
8391
|
+
const cmp = comparator(v.key, key);
|
|
8392
|
+
if (cmp === 0) {
|
|
8393
|
+
left = v.left;
|
|
8394
|
+
right = v.right;
|
|
8395
|
+
} else if (cmp < 0) {
|
|
8396
|
+
right = v.right;
|
|
8397
|
+
v.right = null;
|
|
8398
|
+
left = v;
|
|
8399
|
+
} else {
|
|
8400
|
+
left = v.left;
|
|
8401
|
+
v.left = null;
|
|
8402
|
+
right = v;
|
|
8403
|
+
}
|
|
8404
|
+
}
|
|
8405
|
+
return { left, right };
|
|
8406
|
+
}
|
|
8407
|
+
|
|
8408
|
+
|
|
8409
|
+
function merge (left, right, comparator) {
|
|
8410
|
+
if (right === null) return left;
|
|
8411
|
+
if (left === null) return right;
|
|
8412
|
+
|
|
8413
|
+
right = splay(left.key, right, comparator);
|
|
8414
|
+
right.left = left;
|
|
8415
|
+
return right;
|
|
8416
|
+
}
|
|
8417
|
+
|
|
8191
8418
|
|
|
8192
|
-
|
|
8193
|
-
|
|
8194
|
-
|
|
8195
|
-
|
|
8196
|
-
|
|
8197
|
-
|
|
8198
|
-
|
|
8199
|
-
|
|
8200
|
-
|
|
8201
|
-
|
|
8202
|
-
|
|
8203
|
-
|
|
8204
|
-
|
|
8205
|
-
|
|
8206
|
-
|
|
8207
|
-
}
|
|
8208
|
-
|
|
8209
|
-
|
|
8210
|
-
|
|
8211
|
-
|
|
8212
|
-
|
|
8213
|
-
|
|
8214
|
-
|
|
8215
|
-
|
|
8216
|
-
|
|
8217
|
-
|
|
8218
|
-
|
|
8219
|
-
|
|
8220
|
-
|
|
8221
|
-
|
|
8222
|
-
|
|
8223
|
-
|
|
8224
|
-
|
|
8225
|
-
|
|
8226
|
-
|
|
8227
|
-
|
|
8228
|
-
|
|
8229
|
-
|
|
8230
|
-
|
|
8231
|
-
|
|
8232
|
-
|
|
8233
|
-
|
|
8234
|
-
|
|
8235
|
-
|
|
8236
|
-
|
|
8237
|
-
|
|
8238
|
-
|
|
8239
|
-
|
|
8240
|
-
|
|
8241
|
-
|
|
8242
|
-
|
|
8243
|
-
|
|
8244
|
-
|
|
8245
|
-
|
|
8246
|
-
|
|
8247
|
-
|
|
8248
|
-
|
|
8249
|
-
|
|
8250
|
-
|
|
8251
|
-
|
|
8252
|
-
|
|
8253
|
-
|
|
8254
|
-
|
|
8255
|
-
|
|
8256
|
-
|
|
8257
|
-
|
|
8258
|
-
|
|
8259
|
-
|
|
8260
|
-
}
|
|
8261
|
-
|
|
8262
|
-
|
|
8263
|
-
|
|
8264
|
-
|
|
8265
|
-
|
|
8266
|
-
|
|
8267
|
-
|
|
8268
|
-
|
|
8269
|
-
|
|
8270
|
-
|
|
8271
|
-
|
|
8272
|
-
|
|
8273
|
-
|
|
8274
|
-
|
|
8275
|
-
|
|
8276
|
-
|
|
8277
|
-
|
|
8278
|
-
}
|
|
8279
|
-
return
|
|
8280
|
-
}
|
|
8281
|
-
|
|
8282
|
-
|
|
8283
|
-
|
|
8284
|
-
|
|
8285
|
-
|
|
8286
|
-
|
|
8287
|
-
|
|
8288
|
-
|
|
8289
|
-
|
|
8290
|
-
|
|
8291
|
-
|
|
8292
|
-
|
|
8293
|
-
|
|
8294
|
-
|
|
8295
|
-
|
|
8296
|
-
|
|
8297
|
-
|
|
8298
|
-
|
|
8299
|
-
|
|
8300
|
-
|
|
8301
|
-
|
|
8302
|
-
|
|
8303
|
-
|
|
8304
|
-
|
|
8305
|
-
|
|
8306
|
-
|
|
8307
|
-
|
|
8308
|
-
|
|
8309
|
-
|
|
8310
|
-
|
|
8311
|
-
|
|
8312
|
-
|
|
8313
|
-
/**
|
|
8314
|
-
|
|
8315
|
-
|
|
8316
|
-
|
|
8317
|
-
|
|
8318
|
-
|
|
8319
|
-
|
|
8320
|
-
|
|
8321
|
-
|
|
8322
|
-
|
|
8323
|
-
|
|
8324
|
-
|
|
8325
|
-
|
|
8326
|
-
|
|
8327
|
-
|
|
8328
|
-
|
|
8329
|
-
|
|
8330
|
-
|
|
8331
|
-
|
|
8332
|
-
|
|
8333
|
-
|
|
8334
|
-
|
|
8335
|
-
|
|
8336
|
-
|
|
8337
|
-
|
|
8338
|
-
|
|
8339
|
-
|
|
8340
|
-
|
|
8341
|
-
|
|
8342
|
-
|
|
8343
|
-
|
|
8344
|
-
|
|
8345
|
-
|
|
8346
|
-
|
|
8347
|
-
|
|
8348
|
-
|
|
8349
|
-
|
|
8350
|
-
|
|
8351
|
-
|
|
8352
|
-
|
|
8353
|
-
|
|
8354
|
-
|
|
8355
|
-
|
|
8356
|
-
|
|
8357
|
-
|
|
8358
|
-
|
|
8359
|
-
|
|
8360
|
-
|
|
8361
|
-
|
|
8362
|
-
|
|
8363
|
-
|
|
8364
|
-
|
|
8365
|
-
|
|
8366
|
-
|
|
8367
|
-
|
|
8368
|
-
|
|
8369
|
-
|
|
8370
|
-
|
|
8371
|
-
|
|
8372
|
-
|
|
8373
|
-
|
|
8374
|
-
|
|
8375
|
-
|
|
8376
|
-
|
|
8377
|
-
|
|
8378
|
-
|
|
8379
|
-
|
|
8380
|
-
|
|
8381
|
-
|
|
8382
|
-
|
|
8383
|
-
|
|
8384
|
-
|
|
8385
|
-
|
|
8386
|
-
|
|
8387
|
-
|
|
8388
|
-
|
|
8389
|
-
|
|
8390
|
-
|
|
8391
|
-
|
|
8392
|
-
|
|
8393
|
-
|
|
8394
|
-
|
|
8395
|
-
|
|
8396
|
-
|
|
8397
|
-
|
|
8398
|
-
|
|
8399
|
-
|
|
8400
|
-
|
|
8401
|
-
|
|
8402
|
-
|
|
8403
|
-
|
|
8404
|
-
|
|
8405
|
-
|
|
8406
|
-
|
|
8407
|
-
|
|
8408
|
-
|
|
8409
|
-
|
|
8410
|
-
|
|
8411
|
-
|
|
8412
|
-
|
|
8413
|
-
|
|
8414
|
-
|
|
8415
|
-
|
|
8416
|
-
|
|
8417
|
-
|
|
8418
|
-
|
|
8419
|
-
|
|
8420
|
-
|
|
8421
|
-
|
|
8422
|
-
|
|
8423
|
-
|
|
8424
|
-
|
|
8425
|
-
|
|
8426
|
-
|
|
8427
|
-
|
|
8428
|
-
|
|
8429
|
-
|
|
8430
|
-
|
|
8431
|
-
|
|
8432
|
-
|
|
8433
|
-
|
|
8434
|
-
|
|
8435
|
-
|
|
8436
|
-
|
|
8437
|
-
|
|
8438
|
-
|
|
8439
|
-
|
|
8440
|
-
|
|
8441
|
-
|
|
8442
|
-
|
|
8443
|
-
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8447
|
-
|
|
8448
|
-
|
|
8449
|
-
}
|
|
8450
|
-
|
|
8451
|
-
}
|
|
8452
|
-
|
|
8453
|
-
|
|
8454
|
-
|
|
8455
|
-
|
|
8456
|
-
|
|
8457
|
-
|
|
8458
|
-
|
|
8459
|
-
|
|
8460
|
-
|
|
8461
|
-
|
|
8462
|
-
|
|
8463
|
-
|
|
8464
|
-
|
|
8465
|
-
|
|
8466
|
-
|
|
8467
|
-
|
|
8468
|
-
|
|
8469
|
-
|
|
8470
|
-
|
|
8471
|
-
|
|
8472
|
-
|
|
8473
|
-
|
|
8474
|
-
|
|
8475
|
-
|
|
8476
|
-
|
|
8477
|
-
|
|
8478
|
-
|
|
8479
|
-
|
|
8480
|
-
|
|
8481
|
-
|
|
8482
|
-
|
|
8483
|
-
|
|
8484
|
-
|
|
8485
|
-
|
|
8486
|
-
|
|
8487
|
-
|
|
8488
|
-
|
|
8489
|
-
|
|
8490
|
-
|
|
8491
|
-
|
|
8492
|
-
|
|
8493
|
-
|
|
8494
|
-
|
|
8495
|
-
|
|
8496
|
-
|
|
8497
|
-
|
|
8498
|
-
|
|
8499
|
-
|
|
8500
|
-
|
|
8501
|
-
|
|
8502
|
-
|
|
8503
|
-
|
|
8504
|
-
|
|
8505
|
-
|
|
8506
|
-
|
|
8507
|
-
|
|
8508
|
-
|
|
8509
|
-
|
|
8510
|
-
|
|
8511
|
-
|
|
8512
|
-
|
|
8513
|
-
|
|
8514
|
-
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8519
|
-
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
|
|
8523
|
-
|
|
8524
|
-
|
|
8525
|
-
|
|
8526
|
-
|
|
8527
|
-
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
|
|
8531
|
-
|
|
8532
|
-
|
|
8533
|
-
|
|
8534
|
-
|
|
8535
|
-
|
|
8536
|
-
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
|
|
8553
|
-
|
|
8554
|
-
|
|
8555
|
-
|
|
8556
|
-
|
|
8557
|
-
|
|
8558
|
-
|
|
8559
|
-
|
|
8560
|
-
|
|
8561
|
-
|
|
8562
|
-
|
|
8563
|
-
|
|
8564
|
-
|
|
8565
|
-
|
|
8566
|
-
|
|
8567
|
-
|
|
8568
|
-
|
|
8569
|
-
|
|
8570
|
-
|
|
8571
|
-
|
|
8572
|
-
|
|
8573
|
-
|
|
8574
|
-
|
|
8575
|
-
|
|
8576
|
-
|
|
8577
|
-
|
|
8578
|
-
|
|
8579
|
-
|
|
8580
|
-
|
|
8581
|
-
|
|
8582
|
-
|
|
8583
|
-
|
|
8584
|
-
|
|
8585
|
-
|
|
8586
|
-
|
|
8587
|
-
|
|
8588
|
-
|
|
8589
|
-
|
|
8590
|
-
|
|
8591
|
-
|
|
8592
|
-
|
|
8593
|
-
|
|
8594
|
-
|
|
8595
|
-
|
|
8596
|
-
|
|
8597
|
-
|
|
8598
|
-
|
|
8599
|
-
|
|
8600
|
-
|
|
8601
|
-
|
|
8602
|
-
|
|
8603
|
-
|
|
8604
|
-
|
|
8605
|
-
|
|
8606
|
-
|
|
8607
|
-
|
|
8608
|
-
|
|
8609
|
-
|
|
8610
|
-
|
|
8611
|
-
|
|
8612
|
-
|
|
8613
|
-
|
|
8614
|
-
|
|
8615
|
-
|
|
8616
|
-
|
|
8617
|
-
|
|
8618
|
-
|
|
8619
|
-
|
|
8620
|
-
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
|
|
8624
|
-
|
|
8625
|
-
|
|
8626
|
-
|
|
8627
|
-
|
|
8628
|
-
|
|
8629
|
-
|
|
8630
|
-
|
|
8631
|
-
|
|
8632
|
-
|
|
8633
|
-
|
|
8634
|
-
|
|
8635
|
-
|
|
8636
|
-
|
|
8637
|
-
|
|
8638
|
-
|
|
8639
|
-
|
|
8640
|
-
|
|
8641
|
-
|
|
8642
|
-
|
|
8643
|
-
|
|
8644
|
-
|
|
8645
|
-
|
|
8646
|
-
|
|
8647
|
-
|
|
8648
|
-
|
|
8649
|
-
|
|
8650
|
-
|
|
8651
|
-
|
|
8652
|
-
|
|
8653
|
-
|
|
8654
|
-
|
|
8655
|
-
|
|
8656
|
-
|
|
8657
|
-
|
|
8658
|
-
|
|
8659
|
-
|
|
8660
|
-
|
|
8661
|
-
|
|
8662
|
-
|
|
8663
|
-
function
|
|
8664
|
-
|
|
8665
|
-
|
|
8666
|
-
|
|
8667
|
-
|
|
8668
|
-
|
|
8669
|
-
|
|
8670
|
-
|
|
8671
|
-
|
|
8672
|
-
|
|
8673
|
-
|
|
8674
|
-
|
|
8675
|
-
|
|
8676
|
-
|
|
8677
|
-
|
|
8678
|
-
|
|
8679
|
-
|
|
8680
|
-
|
|
8681
|
-
|
|
8682
|
-
|
|
8683
|
-
|
|
8684
|
-
|
|
8685
|
-
|
|
8686
|
-
|
|
8687
|
-
|
|
8688
|
-
|
|
8689
|
-
|
|
8690
|
-
|
|
8691
|
-
|
|
8692
|
-
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
|
|
8696
|
-
|
|
8697
|
-
|
|
8698
|
-
|
|
8699
|
-
|
|
8700
|
-
|
|
8701
|
-
|
|
8702
|
-
|
|
8703
|
-
|
|
8704
|
-
|
|
8705
|
-
|
|
8706
|
-
|
|
8707
|
-
}
|
|
8708
|
-
|
|
8709
|
-
|
|
8710
|
-
|
|
8711
|
-
|
|
8712
|
-
const left = sortedListToBST(list, start, middle);
|
|
8713
|
-
const root = list.head;
|
|
8714
|
-
root.left = left;
|
|
8715
|
-
list.head = list.head.next;
|
|
8716
|
-
root.right = sortedListToBST(list, middle + 1, end);
|
|
8717
|
-
return root;
|
|
8718
|
-
}
|
|
8719
|
-
return null;
|
|
8720
|
-
}
|
|
8721
|
-
function mergeLists(l1, l2, compare) {
|
|
8722
|
-
const head = new Node(null, null); // dummy
|
|
8723
|
-
let p = head;
|
|
8724
|
-
let p1 = l1;
|
|
8725
|
-
let p2 = l2;
|
|
8726
|
-
while (p1 !== null && p2 !== null) {
|
|
8727
|
-
if (compare(p1.key, p2.key) < 0) {
|
|
8728
|
-
p.next = p1;
|
|
8729
|
-
p1 = p1.next;
|
|
8730
|
-
}
|
|
8731
|
-
else {
|
|
8732
|
-
p.next = p2;
|
|
8733
|
-
p2 = p2.next;
|
|
8734
|
-
}
|
|
8735
|
-
p = p.next;
|
|
8736
|
-
}
|
|
8737
|
-
if (p1 !== null) {
|
|
8738
|
-
p.next = p1;
|
|
8739
|
-
}
|
|
8740
|
-
else if (p2 !== null) {
|
|
8741
|
-
p.next = p2;
|
|
8742
|
-
}
|
|
8743
|
-
return head.next;
|
|
8744
|
-
}
|
|
8745
|
-
function sort(keys, values, left, right, compare) {
|
|
8746
|
-
if (left >= right)
|
|
8747
|
-
return;
|
|
8748
|
-
const pivot = keys[(left + right) >> 1];
|
|
8749
|
-
let i = left - 1;
|
|
8750
|
-
let j = right + 1;
|
|
8751
|
-
while (true) {
|
|
8752
|
-
do
|
|
8753
|
-
i++;
|
|
8754
|
-
while (compare(keys[i], pivot) < 0);
|
|
8755
|
-
do
|
|
8756
|
-
j--;
|
|
8757
|
-
while (compare(keys[j], pivot) > 0);
|
|
8758
|
-
if (i >= j)
|
|
8759
|
-
break;
|
|
8760
|
-
let tmp = keys[i];
|
|
8761
|
-
keys[i] = keys[j];
|
|
8762
|
-
keys[j] = tmp;
|
|
8763
|
-
tmp = values[i];
|
|
8764
|
-
values[i] = values[j];
|
|
8765
|
-
values[j] = tmp;
|
|
8766
|
-
}
|
|
8767
|
-
sort(keys, values, left, j, compare);
|
|
8768
|
-
sort(keys, values, j + 1, right, compare);
|
|
8769
|
-
}
|
|
8770
|
-
|
|
8771
|
-
/* harmony default export */ __webpack_exports__["default"] = (Tree);
|
|
8419
|
+
/**
|
|
8420
|
+
* Prints level of the tree
|
|
8421
|
+
* @param {Node} root
|
|
8422
|
+
* @param {String} prefix
|
|
8423
|
+
* @param {Boolean} isTail
|
|
8424
|
+
* @param {Array<string>} out
|
|
8425
|
+
* @param {Function(node:Node):String} printNode
|
|
8426
|
+
*/
|
|
8427
|
+
function printRow (root, prefix, isTail, out, printNode) {
|
|
8428
|
+
if (root) {
|
|
8429
|
+
out(`${ prefix }${ isTail ? '└── ' : '├── ' }${ printNode(root) }\n`);
|
|
8430
|
+
const indent = prefix + (isTail ? ' ' : '│ ');
|
|
8431
|
+
if (root.left) printRow(root.left, indent, false, out, printNode);
|
|
8432
|
+
if (root.right) printRow(root.right, indent, true, out, printNode);
|
|
8433
|
+
}
|
|
8434
|
+
}
|
|
8435
|
+
|
|
8436
|
+
|
|
8437
|
+
class Tree {
|
|
8438
|
+
|
|
8439
|
+
constructor (comparator = DEFAULT_COMPARE) {
|
|
8440
|
+
this._comparator = comparator;
|
|
8441
|
+
this._root = null;
|
|
8442
|
+
this._size = 0;
|
|
8443
|
+
}
|
|
8444
|
+
|
|
8445
|
+
|
|
8446
|
+
/**
|
|
8447
|
+
* Inserts a key, allows duplicates
|
|
8448
|
+
* @param {Key} key
|
|
8449
|
+
* @param {Value=} data
|
|
8450
|
+
* @return {Node|null}
|
|
8451
|
+
*/
|
|
8452
|
+
insert (key, data) {
|
|
8453
|
+
return this._root = insert(key, data, this._root, this._comparator, this);
|
|
8454
|
+
}
|
|
8455
|
+
|
|
8456
|
+
|
|
8457
|
+
/**
|
|
8458
|
+
* Adds a key, if it is not present in the tree
|
|
8459
|
+
* @param {Key} key
|
|
8460
|
+
* @param {Value=} data
|
|
8461
|
+
* @return {Node|null}
|
|
8462
|
+
*/
|
|
8463
|
+
add (key, data) {
|
|
8464
|
+
return this._root = add(key, data, this._root, this._comparator, this);
|
|
8465
|
+
}
|
|
8466
|
+
|
|
8467
|
+
|
|
8468
|
+
/**
|
|
8469
|
+
* @param {Key} key
|
|
8470
|
+
* @return {Node|null}
|
|
8471
|
+
*/
|
|
8472
|
+
remove (key) {
|
|
8473
|
+
this._root = remove(key, this._root, this._comparator, this);
|
|
8474
|
+
}
|
|
8475
|
+
|
|
8476
|
+
|
|
8477
|
+
/**
|
|
8478
|
+
* Removes and returns the node with smallest key
|
|
8479
|
+
* @return {?Node}
|
|
8480
|
+
*/
|
|
8481
|
+
pop () {
|
|
8482
|
+
let node = this._root;
|
|
8483
|
+
if (node) {
|
|
8484
|
+
while (node.left) node = node.left;
|
|
8485
|
+
this._root = splay(node.key, this._root, this._comparator);
|
|
8486
|
+
this._root = remove(node.key, this._root, this._comparator, this);
|
|
8487
|
+
return { key: node.key, data: node.data };
|
|
8488
|
+
}
|
|
8489
|
+
return null;
|
|
8490
|
+
}
|
|
8491
|
+
|
|
8492
|
+
|
|
8493
|
+
/**
|
|
8494
|
+
* @param {Key} key
|
|
8495
|
+
* @return {Node|null}
|
|
8496
|
+
*/
|
|
8497
|
+
findStatic (key) {
|
|
8498
|
+
let current = this._root;
|
|
8499
|
+
const compare = this._comparator;
|
|
8500
|
+
while (current) {
|
|
8501
|
+
const cmp = compare(key, current.key);
|
|
8502
|
+
if (cmp === 0) return current;
|
|
8503
|
+
else if (cmp < 0) current = current.left;
|
|
8504
|
+
else current = current.right;
|
|
8505
|
+
}
|
|
8506
|
+
return null;
|
|
8507
|
+
}
|
|
8508
|
+
|
|
8509
|
+
|
|
8510
|
+
/**
|
|
8511
|
+
* @param {Key} key
|
|
8512
|
+
* @return {Node|null}
|
|
8513
|
+
*/
|
|
8514
|
+
find (key) {
|
|
8515
|
+
if (this._root) {
|
|
8516
|
+
this._root = splay(key, this._root, this._comparator);
|
|
8517
|
+
if (this._comparator(key, this._root.key) !== 0) return null;
|
|
8518
|
+
}
|
|
8519
|
+
return this._root;
|
|
8520
|
+
}
|
|
8521
|
+
|
|
8522
|
+
|
|
8523
|
+
/**
|
|
8524
|
+
* @param {Key} key
|
|
8525
|
+
* @return {Boolean}
|
|
8526
|
+
*/
|
|
8527
|
+
contains (key) {
|
|
8528
|
+
let current = this._root;
|
|
8529
|
+
const compare = this._comparator;
|
|
8530
|
+
while (current) {
|
|
8531
|
+
const cmp = compare(key, current.key);
|
|
8532
|
+
if (cmp === 0) return true;
|
|
8533
|
+
else if (cmp < 0) current = current.left;
|
|
8534
|
+
else current = current.right;
|
|
8535
|
+
}
|
|
8536
|
+
return false;
|
|
8537
|
+
}
|
|
8538
|
+
|
|
8539
|
+
|
|
8540
|
+
/**
|
|
8541
|
+
* @param {Visitor} visitor
|
|
8542
|
+
* @param {*=} ctx
|
|
8543
|
+
* @return {SplayTree}
|
|
8544
|
+
*/
|
|
8545
|
+
forEach (visitor, ctx) {
|
|
8546
|
+
let current = this._root;
|
|
8547
|
+
const Q = []; /* Initialize stack s */
|
|
8548
|
+
let done = false;
|
|
8549
|
+
|
|
8550
|
+
while (!done) {
|
|
8551
|
+
if (current !== null) {
|
|
8552
|
+
Q.push(current);
|
|
8553
|
+
current = current.left;
|
|
8554
|
+
} else {
|
|
8555
|
+
if (Q.length !== 0) {
|
|
8556
|
+
current = Q.pop();
|
|
8557
|
+
visitor.call(ctx, current);
|
|
8558
|
+
|
|
8559
|
+
current = current.right;
|
|
8560
|
+
} else done = true;
|
|
8561
|
+
}
|
|
8562
|
+
}
|
|
8563
|
+
return this;
|
|
8564
|
+
}
|
|
8565
|
+
|
|
8566
|
+
|
|
8567
|
+
/**
|
|
8568
|
+
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
|
|
8569
|
+
* @param {Key} low
|
|
8570
|
+
* @param {Key} high
|
|
8571
|
+
* @param {Function} fn
|
|
8572
|
+
* @param {*?} ctx
|
|
8573
|
+
* @return {SplayTree}
|
|
8574
|
+
*/
|
|
8575
|
+
range (low, high, fn, ctx) {
|
|
8576
|
+
const Q = [];
|
|
8577
|
+
const compare = this._comparator;
|
|
8578
|
+
let node = this._root, cmp;
|
|
8579
|
+
|
|
8580
|
+
while (Q.length !== 0 || node) {
|
|
8581
|
+
if (node) {
|
|
8582
|
+
Q.push(node);
|
|
8583
|
+
node = node.left;
|
|
8584
|
+
} else {
|
|
8585
|
+
node = Q.pop();
|
|
8586
|
+
cmp = compare(node.key, high);
|
|
8587
|
+
if (cmp > 0) {
|
|
8588
|
+
break;
|
|
8589
|
+
} else if (compare(node.key, low) >= 0) {
|
|
8590
|
+
if (fn.call(ctx, node)) return this; // stop if smth is returned
|
|
8591
|
+
}
|
|
8592
|
+
node = node.right;
|
|
8593
|
+
}
|
|
8594
|
+
}
|
|
8595
|
+
return this;
|
|
8596
|
+
}
|
|
8597
|
+
|
|
8598
|
+
|
|
8599
|
+
/**
|
|
8600
|
+
* Returns array of keys
|
|
8601
|
+
* @return {Array<Key>}
|
|
8602
|
+
*/
|
|
8603
|
+
keys () {
|
|
8604
|
+
const keys = [];
|
|
8605
|
+
this.forEach(({ key }) => keys.push(key));
|
|
8606
|
+
return keys;
|
|
8607
|
+
}
|
|
8608
|
+
|
|
8609
|
+
|
|
8610
|
+
/**
|
|
8611
|
+
* Returns array of all the data in the nodes
|
|
8612
|
+
* @return {Array<Value>}
|
|
8613
|
+
*/
|
|
8614
|
+
values () {
|
|
8615
|
+
const values = [];
|
|
8616
|
+
this.forEach(({ data }) => values.push(data));
|
|
8617
|
+
return values;
|
|
8618
|
+
}
|
|
8619
|
+
|
|
8620
|
+
|
|
8621
|
+
/**
|
|
8622
|
+
* @return {Key|null}
|
|
8623
|
+
*/
|
|
8624
|
+
min() {
|
|
8625
|
+
if (this._root) return this.minNode(this._root).key;
|
|
8626
|
+
return null;
|
|
8627
|
+
}
|
|
8628
|
+
|
|
8629
|
+
|
|
8630
|
+
/**
|
|
8631
|
+
* @return {Key|null}
|
|
8632
|
+
*/
|
|
8633
|
+
max() {
|
|
8634
|
+
if (this._root) return this.maxNode(this._root).key;
|
|
8635
|
+
return null;
|
|
8636
|
+
}
|
|
8637
|
+
|
|
8638
|
+
|
|
8639
|
+
/**
|
|
8640
|
+
* @return {Node|null}
|
|
8641
|
+
*/
|
|
8642
|
+
minNode(t = this._root) {
|
|
8643
|
+
if (t) while (t.left) t = t.left;
|
|
8644
|
+
return t;
|
|
8645
|
+
}
|
|
8646
|
+
|
|
8647
|
+
|
|
8648
|
+
/**
|
|
8649
|
+
* @return {Node|null}
|
|
8650
|
+
*/
|
|
8651
|
+
maxNode(t = this._root) {
|
|
8652
|
+
if (t) while (t.right) t = t.right;
|
|
8653
|
+
return t;
|
|
8654
|
+
}
|
|
8655
|
+
|
|
8656
|
+
|
|
8657
|
+
/**
|
|
8658
|
+
* Returns node at given index
|
|
8659
|
+
* @param {number} index
|
|
8660
|
+
* @return {?Node}
|
|
8661
|
+
*/
|
|
8662
|
+
at (index) {
|
|
8663
|
+
let current = this._root, done = false, i = 0;
|
|
8664
|
+
const Q = [];
|
|
8665
|
+
|
|
8666
|
+
while (!done) {
|
|
8667
|
+
if (current) {
|
|
8668
|
+
Q.push(current);
|
|
8669
|
+
current = current.left;
|
|
8670
|
+
} else {
|
|
8671
|
+
if (Q.length > 0) {
|
|
8672
|
+
current = Q.pop();
|
|
8673
|
+
if (i === index) return current;
|
|
8674
|
+
i++;
|
|
8675
|
+
current = current.right;
|
|
8676
|
+
} else done = true;
|
|
8677
|
+
}
|
|
8678
|
+
}
|
|
8679
|
+
return null;
|
|
8680
|
+
}
|
|
8681
|
+
|
|
8682
|
+
|
|
8683
|
+
/**
|
|
8684
|
+
* @param {Node} d
|
|
8685
|
+
* @return {Node|null}
|
|
8686
|
+
*/
|
|
8687
|
+
next (d) {
|
|
8688
|
+
let root = this._root;
|
|
8689
|
+
let successor = null;
|
|
8690
|
+
|
|
8691
|
+
if (d.right) {
|
|
8692
|
+
successor = d.right;
|
|
8693
|
+
while (successor.left) successor = successor.left;
|
|
8694
|
+
return successor;
|
|
8695
|
+
}
|
|
8696
|
+
|
|
8697
|
+
const comparator = this._comparator;
|
|
8698
|
+
while (root) {
|
|
8699
|
+
const cmp = comparator(d.key, root.key);
|
|
8700
|
+
if (cmp === 0) break;
|
|
8701
|
+
else if (cmp < 0) {
|
|
8702
|
+
successor = root;
|
|
8703
|
+
root = root.left;
|
|
8704
|
+
} else root = root.right;
|
|
8705
|
+
}
|
|
8706
|
+
|
|
8707
|
+
return successor;
|
|
8708
|
+
}
|
|
8709
|
+
|
|
8710
|
+
|
|
8711
|
+
/**
|
|
8712
|
+
* @param {Node} d
|
|
8713
|
+
* @return {Node|null}
|
|
8714
|
+
*/
|
|
8715
|
+
prev (d) {
|
|
8716
|
+
let root = this._root;
|
|
8717
|
+
let predecessor = null;
|
|
8718
|
+
|
|
8719
|
+
if (d.left !== null) {
|
|
8720
|
+
predecessor = d.left;
|
|
8721
|
+
while (predecessor.right) predecessor = predecessor.right;
|
|
8722
|
+
return predecessor;
|
|
8723
|
+
}
|
|
8724
|
+
|
|
8725
|
+
const comparator = this._comparator;
|
|
8726
|
+
while (root) {
|
|
8727
|
+
const cmp = comparator(d.key, root.key);
|
|
8728
|
+
if (cmp === 0) break;
|
|
8729
|
+
else if (cmp < 0) root = root.left;
|
|
8730
|
+
else {
|
|
8731
|
+
predecessor = root;
|
|
8732
|
+
root = root.right;
|
|
8733
|
+
}
|
|
8734
|
+
}
|
|
8735
|
+
return predecessor;
|
|
8736
|
+
}
|
|
8737
|
+
|
|
8738
|
+
|
|
8739
|
+
/**
|
|
8740
|
+
* @return {SplayTree}
|
|
8741
|
+
*/
|
|
8742
|
+
clear() {
|
|
8743
|
+
this._root = null;
|
|
8744
|
+
this._size = 0;
|
|
8745
|
+
return this;
|
|
8746
|
+
}
|
|
8747
|
+
|
|
8748
|
+
|
|
8749
|
+
/**
|
|
8750
|
+
* @return {NodeList}
|
|
8751
|
+
*/
|
|
8752
|
+
toList() {
|
|
8753
|
+
return toList(this._root);
|
|
8754
|
+
}
|
|
8755
|
+
|
|
8756
|
+
|
|
8757
|
+
/**
|
|
8758
|
+
* Bulk-load items. Both array have to be same size
|
|
8759
|
+
* @param {Array<Key>} keys
|
|
8760
|
+
* @param {Array<Value>} [values]
|
|
8761
|
+
* @param {Boolean} [presort=false] Pre-sort keys and values, using
|
|
8762
|
+
* tree's comparator. Sorting is done
|
|
8763
|
+
* in-place
|
|
8764
|
+
* @return {AVLTree}
|
|
8765
|
+
*/
|
|
8766
|
+
load (keys = [], values = [], presort = false) {
|
|
8767
|
+
let size = keys.length;
|
|
8768
|
+
const comparator = this._comparator;
|
|
8769
|
+
|
|
8770
|
+
// sort if needed
|
|
8771
|
+
if (presort) sort(keys, values, 0, size - 1, comparator);
|
|
8772
|
+
|
|
8773
|
+
if (this._root === null) { // empty tree
|
|
8774
|
+
this._root = loadRecursive(this._root, keys, values, 0, size);
|
|
8775
|
+
this._size = size;
|
|
8776
|
+
} else { // that re-builds the whole tree from two in-order traversals
|
|
8777
|
+
const mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
|
|
8778
|
+
size = this._size + size;
|
|
8779
|
+
this._root = sortedListToBST({ head: mergedList }, 0, size);
|
|
8780
|
+
}
|
|
8781
|
+
return this;
|
|
8782
|
+
}
|
|
8783
|
+
|
|
8784
|
+
|
|
8785
|
+
/**
|
|
8786
|
+
* @return {Boolean}
|
|
8787
|
+
*/
|
|
8788
|
+
isEmpty() { return this._root === null; }
|
|
8789
|
+
|
|
8790
|
+
get size () { return this._size; }
|
|
8791
|
+
|
|
8792
|
+
|
|
8793
|
+
/**
|
|
8794
|
+
* @param {NodePrinter=} printNode
|
|
8795
|
+
* @return {String}
|
|
8796
|
+
*/
|
|
8797
|
+
toString (printNode = (n) => n.key) {
|
|
8798
|
+
const out = [];
|
|
8799
|
+
printRow(this._root, '', true, (v) => out.push(v), printNode);
|
|
8800
|
+
return out.join('');
|
|
8801
|
+
}
|
|
8802
|
+
|
|
8803
|
+
|
|
8804
|
+
update (key, newKey, newData) {
|
|
8805
|
+
const comparator = this._comparator;
|
|
8806
|
+
let { left, right } = split(key, this._root, comparator);
|
|
8807
|
+
this._size--;
|
|
8808
|
+
if (comparator(key, newKey) < 0) {
|
|
8809
|
+
right = insert(newKey, newData, right, comparator, this);
|
|
8810
|
+
} else {
|
|
8811
|
+
left = insert(newKey, newData, left, comparator, this);
|
|
8812
|
+
}
|
|
8813
|
+
this._root = merge(left, right, comparator);
|
|
8814
|
+
}
|
|
8815
|
+
|
|
8816
|
+
|
|
8817
|
+
split(key) {
|
|
8818
|
+
return split(key, this._root, this._comparator);
|
|
8819
|
+
}
|
|
8820
|
+
}
|
|
8821
|
+
|
|
8822
|
+
|
|
8823
|
+
function loadRecursive (parent, keys, values, start, end) {
|
|
8824
|
+
const size = end - start;
|
|
8825
|
+
if (size > 0) {
|
|
8826
|
+
const middle = start + Math.floor(size / 2);
|
|
8827
|
+
const key = keys[middle];
|
|
8828
|
+
const data = values[middle];
|
|
8829
|
+
const node = { key, data, parent };
|
|
8830
|
+
node.left = loadRecursive(node, keys, values, start, middle);
|
|
8831
|
+
node.right = loadRecursive(node, keys, values, middle + 1, end);
|
|
8832
|
+
return node;
|
|
8833
|
+
}
|
|
8834
|
+
return null;
|
|
8835
|
+
}
|
|
8836
|
+
|
|
8837
|
+
|
|
8838
|
+
function createList(keys, values) {
|
|
8839
|
+
const head = { next: null };
|
|
8840
|
+
let p = head;
|
|
8841
|
+
for (let i = 0; i < keys.length; i++) {
|
|
8842
|
+
p = p.next = { key: keys[i], data: values[i] };
|
|
8843
|
+
}
|
|
8844
|
+
p.next = null;
|
|
8845
|
+
return head.next;
|
|
8846
|
+
}
|
|
8847
|
+
|
|
8848
|
+
|
|
8849
|
+
function toList (root) {
|
|
8850
|
+
var current = root;
|
|
8851
|
+
var Q = [], done = false;
|
|
8852
|
+
|
|
8853
|
+
const head = { next: null };
|
|
8854
|
+
let p = head;
|
|
8855
|
+
|
|
8856
|
+
while (!done) {
|
|
8857
|
+
if (current) {
|
|
8858
|
+
Q.push(current);
|
|
8859
|
+
current = current.left;
|
|
8860
|
+
} else {
|
|
8861
|
+
if (Q.length > 0) {
|
|
8862
|
+
current = p = p.next = Q.pop();
|
|
8863
|
+
current = current.right;
|
|
8864
|
+
} else done = true;
|
|
8865
|
+
}
|
|
8866
|
+
}
|
|
8867
|
+
p.next = null; // that'll work even if the tree was empty
|
|
8868
|
+
return head.next;
|
|
8869
|
+
}
|
|
8870
|
+
|
|
8871
|
+
|
|
8872
|
+
function sortedListToBST(list, start, end) {
|
|
8873
|
+
const size = end - start;
|
|
8874
|
+
if (size > 0) {
|
|
8875
|
+
const middle = start + Math.floor(size / 2);
|
|
8876
|
+
const left = sortedListToBST(list, start, middle);
|
|
8877
|
+
|
|
8878
|
+
const root = list.head;
|
|
8879
|
+
root.left = left;
|
|
8880
|
+
|
|
8881
|
+
list.head = list.head.next;
|
|
8882
|
+
|
|
8883
|
+
root.right = sortedListToBST(list, middle + 1, end);
|
|
8884
|
+
return root;
|
|
8885
|
+
}
|
|
8886
|
+
return null;
|
|
8887
|
+
}
|
|
8888
|
+
|
|
8889
|
+
|
|
8890
|
+
function mergeLists (l1, l2, compare = (a, b) => a - b) {
|
|
8891
|
+
const head = {}; // dummy
|
|
8892
|
+
let p = head;
|
|
8893
|
+
|
|
8894
|
+
let p1 = l1;
|
|
8895
|
+
let p2 = l2;
|
|
8896
|
+
|
|
8897
|
+
while (p1 !== null && p2 !== null) {
|
|
8898
|
+
if (compare(p1.key, p2.key) < 0) {
|
|
8899
|
+
p.next = p1;
|
|
8900
|
+
p1 = p1.next;
|
|
8901
|
+
} else {
|
|
8902
|
+
p.next = p2;
|
|
8903
|
+
p2 = p2.next;
|
|
8904
|
+
}
|
|
8905
|
+
p = p.next;
|
|
8906
|
+
}
|
|
8907
|
+
|
|
8908
|
+
if (p1 !== null) p.next = p1;
|
|
8909
|
+
else if (p2 !== null) p.next = p2;
|
|
8910
|
+
|
|
8911
|
+
return head.next;
|
|
8912
|
+
}
|
|
8913
|
+
|
|
8914
|
+
|
|
8915
|
+
function sort(keys, values, left, right, compare) {
|
|
8916
|
+
if (left >= right) return;
|
|
8917
|
+
|
|
8918
|
+
const pivot = keys[(left + right) >> 1];
|
|
8919
|
+
let i = left - 1;
|
|
8920
|
+
let j = right + 1;
|
|
8921
|
+
|
|
8922
|
+
while (true) {
|
|
8923
|
+
do i++; while (compare(keys[i], pivot) < 0);
|
|
8924
|
+
do j--; while (compare(keys[j], pivot) > 0);
|
|
8925
|
+
if (i >= j) break;
|
|
8926
|
+
|
|
8927
|
+
let tmp = keys[i];
|
|
8928
|
+
keys[i] = keys[j];
|
|
8929
|
+
keys[j] = tmp;
|
|
8930
|
+
|
|
8931
|
+
tmp = values[i];
|
|
8932
|
+
values[i] = values[j];
|
|
8933
|
+
values[j] = tmp;
|
|
8934
|
+
}
|
|
8935
|
+
|
|
8936
|
+
sort(keys, values, left, j, compare);
|
|
8937
|
+
sort(keys, values, j + 1, right, compare);
|
|
8938
|
+
}
|
|
8772
8939
|
|
|
8773
8940
|
|
|
8774
8941
|
/***/ }),
|