@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.
Files changed (94) hide show
  1. package/dist/cli.js +761 -594
  2. package/dist/cli.js.map +1 -1
  3. package/package.json +2 -1
  4. package/.prettierrc +0 -5
  5. package/jestconfig.json +0 -14
  6. package/lib/HelloWorld.d.ts +0 -3
  7. package/lib/HelloWorld.js +0 -11
  8. package/lib/_api.js +0 -91
  9. package/lib/_api.js.map +0 -1
  10. package/lib/_cli.js +0 -434
  11. package/lib/_cli.js.map +0 -1
  12. package/lib/_data.js +0 -425
  13. package/lib/_data.js.map +0 -1
  14. package/lib/analyze.d.ts +0 -3
  15. package/lib/analyze.js +0 -69
  16. package/lib/analyze.js.map +0 -1
  17. package/lib/api.d.ts +0 -34
  18. package/lib/api.js +0 -117
  19. package/lib/api.js.map +0 -1
  20. package/lib/cli.d.ts +0 -1
  21. package/lib/cli.js +0 -386
  22. package/lib/cli.js.map +0 -1
  23. package/lib/cohesive.d.ts +0 -4
  24. package/lib/cohesive.js +0 -132
  25. package/lib/cohesive.js.map +0 -1
  26. package/lib/compact.d.ts +0 -4
  27. package/lib/compact.js +0 -183
  28. package/lib/compact.js.map +0 -1
  29. package/lib/constants.js +0 -367
  30. package/lib/constants.js.map +0 -1
  31. package/lib/data.js +0 -188
  32. package/lib/data.js.map +0 -1
  33. package/lib/equal.d.ts +0 -4
  34. package/lib/equal.js +0 -59
  35. package/lib/equal.js.map +0 -1
  36. package/lib/features.js +0 -19
  37. package/lib/features.js.map +0 -1
  38. package/lib/geofeature.js +0 -112
  39. package/lib/geofeature.js.map +0 -1
  40. package/lib/index.d.ts +0 -5
  41. package/lib/index.js +0 -11
  42. package/lib/index.js.map +0 -1
  43. package/lib/minority.d.ts +0 -3
  44. package/lib/minority.js +0 -61
  45. package/lib/minority.js.map +0 -1
  46. package/lib/political.d.ts +0 -7
  47. package/lib/political.js +0 -88
  48. package/lib/political.js.map +0 -1
  49. package/lib/preprocess.d.ts +0 -4
  50. package/lib/preprocess.js +0 -101
  51. package/lib/preprocess.js.map +0 -1
  52. package/lib/report.d.ts +0 -14
  53. package/lib/report.js +0 -817
  54. package/lib/report.js.map +0 -1
  55. package/lib/sample.d.ts +0 -1
  56. package/lib/sample.js +0 -32
  57. package/lib/sample.js.map +0 -1
  58. package/lib/scorecard.d.ts +0 -4
  59. package/lib/scorecard.js +0 -237
  60. package/lib/scorecard.js.map +0 -1
  61. package/lib/settings.d.ts +0 -5
  62. package/lib/settings.js +0 -18
  63. package/lib/settings.js.map +0 -1
  64. package/lib/types.d.ts +0 -125
  65. package/lib/types.js +0 -7
  66. package/lib/types.js.map +0 -1
  67. package/lib/utils.d.ts +0 -20
  68. package/lib/utils.js +0 -223
  69. package/lib/utils.js.map +0 -1
  70. package/lib/valid.d.ts +0 -5
  71. package/lib/valid.js +0 -230
  72. package/lib/valid.js.map +0 -1
  73. package/main.js +0 -4
  74. package/src/_api.ts +0 -121
  75. package/src/_data.ts +0 -595
  76. package/src/analyze.ts +0 -92
  77. package/src/cohesive.ts +0 -156
  78. package/src/compact.ts +0 -208
  79. package/src/constants.ts +0 -371
  80. package/src/equal.ts +0 -75
  81. package/src/geofeature.ts +0 -138
  82. package/src/index.ts +0 -6
  83. package/src/minority.ts +0 -70
  84. package/src/political.ts +0 -114
  85. package/src/preprocess.ts +0 -132
  86. package/src/report.ts +0 -1022
  87. package/src/settings.ts +0 -20
  88. package/src/types.ts +0 -185
  89. package/src/utils.ts +0 -245
  90. package/src/valid.ts +0 -275
  91. package/tsconfig.json +0 -25
  92. package/tslint.json +0 -3
  93. package/types/polygon-clipping/index.d.ts +0 -1
  94. 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/dist/splay.esm.js");
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/dist/splay.esm.js":
8175
- /*!**************************************************!*\
8176
- !*** ./node_modules/splaytree/dist/splay.esm.js ***!
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
- * splaytree v3.0.0
8185
- * Fast Splay tree for Node and browser
8186
- *
8187
- * @author Alexander Milevski <info@w8r.name>
8188
- * @license MIT
8189
- * @preserve
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
- class Node {
8193
- constructor(key, data) {
8194
- this.next = null;
8195
- this.key = key;
8196
- this.data = data;
8197
- this.left = null;
8198
- this.right = null;
8199
- }
8200
- }
8201
-
8202
- /* follows "An implementation of top-down splaying"
8203
- * by D. Sleator <sleator@cs.cmu.edu> March 1992
8204
- */
8205
- function DEFAULT_COMPARE(a, b) {
8206
- return a > b ? 1 : a < b ? -1 : 0;
8207
- }
8208
- /**
8209
- * Simple top down splay, not requiring i to be in the tree t.
8210
- */
8211
- function splay(i, t, comparator) {
8212
- const N = new Node(null, null);
8213
- let l = N;
8214
- let r = N;
8215
- while (true) {
8216
- const cmp = comparator(i, t.key);
8217
- //if (i < t.key) {
8218
- if (cmp < 0) {
8219
- if (t.left === null)
8220
- break;
8221
- //if (i < t.left.key) {
8222
- if (comparator(i, t.left.key) < 0) {
8223
- const y = t.left; /* rotate right */
8224
- t.left = y.right;
8225
- y.right = t;
8226
- t = y;
8227
- if (t.left === null)
8228
- break;
8229
- }
8230
- r.left = t; /* link right */
8231
- r = t;
8232
- t = t.left;
8233
- //} else if (i > t.key) {
8234
- }
8235
- else if (cmp > 0) {
8236
- if (t.right === null)
8237
- break;
8238
- //if (i > t.right.key) {
8239
- if (comparator(i, t.right.key) > 0) {
8240
- const y = t.right; /* rotate left */
8241
- t.right = y.left;
8242
- y.left = t;
8243
- t = y;
8244
- if (t.right === null)
8245
- break;
8246
- }
8247
- l.right = t; /* link left */
8248
- l = t;
8249
- t = t.right;
8250
- }
8251
- else
8252
- break;
8253
- }
8254
- /* assemble */
8255
- l.right = t.left;
8256
- r.left = t.right;
8257
- t.left = N.right;
8258
- t.right = N.left;
8259
- return t;
8260
- }
8261
- function insert(i, data, t, comparator) {
8262
- const node = new Node(i, data);
8263
- if (t === null) {
8264
- node.left = node.right = null;
8265
- return node;
8266
- }
8267
- t = splay(i, t, comparator);
8268
- const cmp = comparator(i, t.key);
8269
- if (cmp < 0) {
8270
- node.left = t.left;
8271
- node.right = t;
8272
- t.left = null;
8273
- }
8274
- else if (cmp >= 0) {
8275
- node.right = t.right;
8276
- node.left = t;
8277
- t.right = null;
8278
- }
8279
- return node;
8280
- }
8281
- function split(key, v, comparator) {
8282
- let left = null;
8283
- let right = null;
8284
- if (v) {
8285
- v = splay(key, v, comparator);
8286
- const cmp = comparator(v.key, key);
8287
- if (cmp === 0) {
8288
- left = v.left;
8289
- right = v.right;
8290
- }
8291
- else if (cmp < 0) {
8292
- right = v.right;
8293
- v.right = null;
8294
- left = v;
8295
- }
8296
- else {
8297
- left = v.left;
8298
- v.left = null;
8299
- right = v;
8300
- }
8301
- }
8302
- return { left, right };
8303
- }
8304
- function merge(left, right, comparator) {
8305
- if (right === null)
8306
- return left;
8307
- if (left === null)
8308
- return right;
8309
- right = splay(left.key, right, comparator);
8310
- right.left = left;
8311
- return right;
8312
- }
8313
- /**
8314
- * Prints level of the tree
8315
- */
8316
- function printRow(root, prefix, isTail, out, printNode) {
8317
- if (root) {
8318
- out(`${prefix}${isTail ? '└── ' : '├── '}${printNode(root)}\n`);
8319
- const indent = prefix + (isTail ? ' ' : '│ ');
8320
- if (root.left)
8321
- printRow(root.left, indent, false, out, printNode);
8322
- if (root.right)
8323
- printRow(root.right, indent, true, out, printNode);
8324
- }
8325
- }
8326
- class Tree {
8327
- constructor(comparator = DEFAULT_COMPARE) {
8328
- this._root = null;
8329
- this._size = 0;
8330
- this._comparator = comparator;
8331
- }
8332
- /**
8333
- * Inserts a key, allows duplicates
8334
- */
8335
- insert(key, data) {
8336
- this._size++;
8337
- return this._root = insert(key, data, this._root, this._comparator);
8338
- }
8339
- /**
8340
- * Adds a key, if it is not present in the tree
8341
- */
8342
- add(key, data) {
8343
- const node = new Node(key, data);
8344
- if (this._root === null) {
8345
- node.left = node.right = null;
8346
- this._size++;
8347
- this._root = node;
8348
- }
8349
- const comparator = this._comparator;
8350
- const t = splay(key, this._root, comparator);
8351
- const cmp = comparator(key, t.key);
8352
- if (cmp === 0)
8353
- this._root = t;
8354
- else {
8355
- if (cmp < 0) {
8356
- node.left = t.left;
8357
- node.right = t;
8358
- t.left = null;
8359
- }
8360
- else if (cmp > 0) {
8361
- node.right = t.right;
8362
- node.left = t;
8363
- t.right = null;
8364
- }
8365
- this._size++;
8366
- this._root = node;
8367
- }
8368
- return this._root;
8369
- }
8370
- /**
8371
- * @param {Key} key
8372
- * @return {Node|null}
8373
- */
8374
- remove(key) {
8375
- this._root = this._remove(key, this._root, this._comparator);
8376
- }
8377
- /**
8378
- * Deletes i from the tree if it's there
8379
- */
8380
- _remove(i, t, comparator) {
8381
- let x;
8382
- if (t === null)
8383
- return null;
8384
- t = splay(i, t, comparator);
8385
- const cmp = comparator(i, t.key);
8386
- if (cmp === 0) { /* found it */
8387
- if (t.left === null) {
8388
- x = t.right;
8389
- }
8390
- else {
8391
- x = splay(i, t.left, comparator);
8392
- x.right = t.right;
8393
- }
8394
- this._size--;
8395
- return x;
8396
- }
8397
- return t; /* It wasn't there */
8398
- }
8399
- /**
8400
- * Removes and returns the node with smallest key
8401
- */
8402
- pop() {
8403
- let node = this._root;
8404
- if (node) {
8405
- while (node.left)
8406
- node = node.left;
8407
- this._root = splay(node.key, this._root, this._comparator);
8408
- this._root = this._remove(node.key, this._root, this._comparator);
8409
- return { key: node.key, data: node.data };
8410
- }
8411
- return null;
8412
- }
8413
- /**
8414
- * Find without splaying
8415
- */
8416
- findStatic(key) {
8417
- let current = this._root;
8418
- const compare = this._comparator;
8419
- while (current) {
8420
- const cmp = compare(key, current.key);
8421
- if (cmp === 0)
8422
- return current;
8423
- else if (cmp < 0)
8424
- current = current.left;
8425
- else
8426
- current = current.right;
8427
- }
8428
- return null;
8429
- }
8430
- find(key) {
8431
- if (this._root) {
8432
- this._root = splay(key, this._root, this._comparator);
8433
- if (this._comparator(key, this._root.key) !== 0)
8434
- return null;
8435
- }
8436
- return this._root;
8437
- }
8438
- contains(key) {
8439
- let current = this._root;
8440
- const compare = this._comparator;
8441
- while (current) {
8442
- const cmp = compare(key, current.key);
8443
- if (cmp === 0)
8444
- return true;
8445
- else if (cmp < 0)
8446
- current = current.left;
8447
- else
8448
- current = current.right;
8449
- }
8450
- return false;
8451
- }
8452
- forEach(visitor, ctx) {
8453
- let current = this._root;
8454
- const Q = []; /* Initialize stack s */
8455
- let done = false;
8456
- while (!done) {
8457
- if (current !== null) {
8458
- Q.push(current);
8459
- current = current.left;
8460
- }
8461
- else {
8462
- if (Q.length !== 0) {
8463
- current = Q.pop();
8464
- visitor.call(ctx, current);
8465
- current = current.right;
8466
- }
8467
- else
8468
- done = true;
8469
- }
8470
- }
8471
- return this;
8472
- }
8473
- /**
8474
- * Walk key range from `low` to `high`. Stops if `fn` returns a value.
8475
- */
8476
- range(low, high, fn, ctx) {
8477
- const Q = [];
8478
- const compare = this._comparator;
8479
- let node = this._root;
8480
- let cmp;
8481
- while (Q.length !== 0 || node) {
8482
- if (node) {
8483
- Q.push(node);
8484
- node = node.left;
8485
- }
8486
- else {
8487
- node = Q.pop();
8488
- cmp = compare(node.key, high);
8489
- if (cmp > 0) {
8490
- break;
8491
- }
8492
- else if (compare(node.key, low) >= 0) {
8493
- if (fn.call(ctx, node))
8494
- return this; // stop if smth is returned
8495
- }
8496
- node = node.right;
8497
- }
8498
- }
8499
- return this;
8500
- }
8501
- /**
8502
- * Returns array of keys
8503
- */
8504
- keys() {
8505
- const keys = [];
8506
- this.forEach(({ key }) => keys.push(key));
8507
- return keys;
8508
- }
8509
- /**
8510
- * Returns array of all the data in the nodes
8511
- */
8512
- values() {
8513
- const values = [];
8514
- this.forEach(({ data }) => values.push(data));
8515
- return values;
8516
- }
8517
- min() {
8518
- if (this._root)
8519
- return this.minNode(this._root).key;
8520
- return null;
8521
- }
8522
- max() {
8523
- if (this._root)
8524
- return this.maxNode(this._root).key;
8525
- return null;
8526
- }
8527
- minNode(t = this._root) {
8528
- if (t)
8529
- while (t.left)
8530
- t = t.left;
8531
- return t;
8532
- }
8533
- maxNode(t = this._root) {
8534
- if (t)
8535
- while (t.right)
8536
- t = t.right;
8537
- return t;
8538
- }
8539
- /**
8540
- * Returns node at given index
8541
- */
8542
- at(index) {
8543
- let current = this._root;
8544
- let done = false;
8545
- let i = 0;
8546
- const Q = [];
8547
- while (!done) {
8548
- if (current) {
8549
- Q.push(current);
8550
- current = current.left;
8551
- }
8552
- else {
8553
- if (Q.length > 0) {
8554
- current = Q.pop();
8555
- if (i === index)
8556
- return current;
8557
- i++;
8558
- current = current.right;
8559
- }
8560
- else
8561
- done = true;
8562
- }
8563
- }
8564
- return null;
8565
- }
8566
- next(d) {
8567
- let root = this._root;
8568
- let successor = null;
8569
- if (d.right) {
8570
- successor = d.right;
8571
- while (successor.left)
8572
- successor = successor.left;
8573
- return successor;
8574
- }
8575
- const comparator = this._comparator;
8576
- while (root) {
8577
- const cmp = comparator(d.key, root.key);
8578
- if (cmp === 0)
8579
- break;
8580
- else if (cmp < 0) {
8581
- successor = root;
8582
- root = root.left;
8583
- }
8584
- else
8585
- root = root.right;
8586
- }
8587
- return successor;
8588
- }
8589
- prev(d) {
8590
- let root = this._root;
8591
- let predecessor = null;
8592
- if (d.left !== null) {
8593
- predecessor = d.left;
8594
- while (predecessor.right)
8595
- predecessor = predecessor.right;
8596
- return predecessor;
8597
- }
8598
- const comparator = this._comparator;
8599
- while (root) {
8600
- const cmp = comparator(d.key, root.key);
8601
- if (cmp === 0)
8602
- break;
8603
- else if (cmp < 0)
8604
- root = root.left;
8605
- else {
8606
- predecessor = root;
8607
- root = root.right;
8608
- }
8609
- }
8610
- return predecessor;
8611
- }
8612
- clear() {
8613
- this._root = null;
8614
- this._size = 0;
8615
- return this;
8616
- }
8617
- toList() {
8618
- return toList(this._root);
8619
- }
8620
- /**
8621
- * Bulk-load items. Both array have to be same size
8622
- */
8623
- load(keys, values = [], presort = false) {
8624
- let size = keys.length;
8625
- const comparator = this._comparator;
8626
- // sort if needed
8627
- if (presort)
8628
- sort(keys, values, 0, size - 1, comparator);
8629
- if (this._root === null) { // empty tree
8630
- this._root = loadRecursive(keys, values, 0, size);
8631
- this._size = size;
8632
- }
8633
- else { // that re-builds the whole tree from two in-order traversals
8634
- const mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
8635
- size = this._size + size;
8636
- this._root = sortedListToBST({ head: mergedList }, 0, size);
8637
- }
8638
- return this;
8639
- }
8640
- isEmpty() { return this._root === null; }
8641
- get size() { return this._size; }
8642
- get root() { return this._root; }
8643
- toString(printNode = (n) => String(n.key)) {
8644
- const out = [];
8645
- printRow(this._root, '', true, (v) => out.push(v), printNode);
8646
- return out.join('');
8647
- }
8648
- update(key, newKey, newData) {
8649
- const comparator = this._comparator;
8650
- let { left, right } = split(key, this._root, comparator);
8651
- if (comparator(key, newKey) < 0) {
8652
- right = insert(newKey, newData, right, comparator);
8653
- }
8654
- else {
8655
- left = insert(newKey, newData, left, comparator);
8656
- }
8657
- this._root = merge(left, right, comparator);
8658
- }
8659
- split(key) {
8660
- return split(key, this._root, this._comparator);
8661
- }
8662
- }
8663
- function loadRecursive(keys, values, start, end) {
8664
- const size = end - start;
8665
- if (size > 0) {
8666
- const middle = start + Math.floor(size / 2);
8667
- const key = keys[middle];
8668
- const data = values[middle];
8669
- const node = new Node(key, data);
8670
- node.left = loadRecursive(keys, values, start, middle);
8671
- node.right = loadRecursive(keys, values, middle + 1, end);
8672
- return node;
8673
- }
8674
- return null;
8675
- }
8676
- function createList(keys, values) {
8677
- const head = new Node(null, null);
8678
- let p = head;
8679
- for (let i = 0; i < keys.length; i++) {
8680
- p = p.next = new Node(keys[i], values[i]);
8681
- }
8682
- p.next = null;
8683
- return head.next;
8684
- }
8685
- function toList(root) {
8686
- let current = root;
8687
- const Q = [];
8688
- let done = false;
8689
- const head = new Node(null, null);
8690
- let p = head;
8691
- while (!done) {
8692
- if (current) {
8693
- Q.push(current);
8694
- current = current.left;
8695
- }
8696
- else {
8697
- if (Q.length > 0) {
8698
- current = p = p.next = Q.pop();
8699
- current = current.right;
8700
- }
8701
- else
8702
- done = true;
8703
- }
8704
- }
8705
- p.next = null; // that'll work even if the tree was empty
8706
- return head.next;
8707
- }
8708
- function sortedListToBST(list, start, end) {
8709
- const size = end - start;
8710
- if (size > 0) {
8711
- const middle = start + Math.floor(size / 2);
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
  /***/ }),