list-toolkit 1.0.2 → 2.1.0

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 (88) hide show
  1. package/README.md +89 -419
  2. package/cjs/cache/cache-fifo.js +37 -0
  3. package/cjs/cache/cache-lfu.js +76 -0
  4. package/cjs/cache/cache-lru.js +100 -0
  5. package/cjs/cache/cache-random.js +77 -0
  6. package/cjs/cache/decorator.js +47 -0
  7. package/cjs/cache.js +28 -0
  8. package/cjs/ext-list.js +22 -0
  9. package/cjs/ext-slist.js +22 -0
  10. package/cjs/ext-value-list.js +22 -0
  11. package/cjs/ext-value-slist.js +22 -0
  12. package/cjs/{MinHeap.js → heap/min-heap.js} +68 -82
  13. package/cjs/heap.js +22 -0
  14. package/cjs/list/basics.js +88 -0
  15. package/cjs/list/core.js +305 -0
  16. package/cjs/list/ext-value.js +89 -0
  17. package/cjs/list/ext.js +356 -0
  18. package/cjs/list/nodes.js +240 -0
  19. package/cjs/list/ptr.js +61 -0
  20. package/cjs/list/value.js +100 -0
  21. package/cjs/list-helpers.js +91 -0
  22. package/cjs/list-utils.js +141 -0
  23. package/cjs/list.js +22 -0
  24. package/cjs/meta-utils.js +167 -0
  25. package/cjs/nt-utils.js +132 -0
  26. package/cjs/queue.js +58 -0
  27. package/cjs/slist/basics.js +71 -0
  28. package/cjs/slist/core.js +362 -0
  29. package/cjs/slist/ext-value.js +83 -0
  30. package/cjs/slist/ext.js +336 -0
  31. package/cjs/slist/nodes.js +276 -0
  32. package/cjs/slist/ptr.js +87 -0
  33. package/cjs/slist/value.js +91 -0
  34. package/cjs/slist.js +22 -0
  35. package/cjs/stack.js +55 -0
  36. package/cjs/tree/splay-tree.js +362 -0
  37. package/cjs/value-list.js +22 -0
  38. package/cjs/value-slist.js +22 -0
  39. package/package.json +7 -7
  40. package/src/cache/cache-fifo.js +27 -0
  41. package/src/cache/cache-lfu.js +63 -0
  42. package/src/cache/cache-lru.js +87 -0
  43. package/src/cache/cache-random.js +73 -0
  44. package/src/cache/decorator.js +45 -0
  45. package/src/cache.js +9 -0
  46. package/src/ext-list.js +6 -0
  47. package/src/ext-slist.js +6 -0
  48. package/src/ext-value-list.js +6 -0
  49. package/src/ext-value-slist.js +6 -0
  50. package/src/{MinHeap.js → heap/min-heap.js} +73 -85
  51. package/src/heap.js +6 -0
  52. package/src/list/basics.js +64 -0
  53. package/src/list/core.js +314 -0
  54. package/src/list/ext-value.js +81 -0
  55. package/src/list/ext.js +370 -0
  56. package/src/list/nodes.js +262 -0
  57. package/src/list/ptr.js +58 -0
  58. package/src/list/value.js +88 -0
  59. package/src/list-helpers.js +80 -0
  60. package/src/list-utils.js +140 -0
  61. package/src/list.js +6 -0
  62. package/src/meta-utils.js +147 -0
  63. package/src/nt-utils.js +85 -0
  64. package/src/queue.js +52 -0
  65. package/src/slist/basics.js +47 -0
  66. package/src/slist/core.js +364 -0
  67. package/src/slist/ext-value.js +74 -0
  68. package/src/slist/ext.js +331 -0
  69. package/src/slist/nodes.js +290 -0
  70. package/src/slist/ptr.js +77 -0
  71. package/src/slist/value.js +75 -0
  72. package/src/slist.js +6 -0
  73. package/src/stack.js +52 -0
  74. package/src/tree/splay-tree.js +378 -0
  75. package/src/value-list.js +6 -0
  76. package/src/value-slist.js +6 -0
  77. package/cjs/Cache.js +0 -71
  78. package/cjs/List.js +0 -294
  79. package/cjs/ListHead.js +0 -309
  80. package/cjs/SList.js +0 -342
  81. package/cjs/SListHead.js +0 -367
  82. package/cjs/utils.js +0 -43
  83. package/src/Cache.js +0 -61
  84. package/src/List.js +0 -303
  85. package/src/ListHead.js +0 -304
  86. package/src/SList.js +0 -330
  87. package/src/SListHead.js +0 -354
  88. package/src/utils.js +0 -35
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.CacheLFU = void 0;
7
+ var _metaUtils = require("../meta-utils.js");
8
+ var _minHeap = _interopRequireDefault(require("../heap/min-heap.js"));
9
+ var _cacheLru = _interopRequireDefault(require("./cache-lru.js"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ // Evicts the least frequently used items.
12
+
13
+ class CacheLFU extends _cacheLru.default {
14
+ constructor(capacity = 10) {
15
+ super(capacity);
16
+ this.heap = new _minHeap.default({
17
+ less: (a, b) => a.value.counter < b.value.counter
18
+ });
19
+ }
20
+ use(key) {
21
+ const node = this.dict.get(key);
22
+ if (node) ++node.value.counter;
23
+ return node;
24
+ }
25
+ update(node, value) {
26
+ node.value.counter = 1;
27
+ node.value.value = value;
28
+ return this;
29
+ }
30
+ addNew(key, value) {
31
+ this.list.pushFront({
32
+ key,
33
+ value,
34
+ counter: 1
35
+ });
36
+ const node = this.list.front;
37
+ this.dict.set(key, node);
38
+ this.heap.push(node);
39
+ return node;
40
+ }
41
+ evictAndReplace(key, value) {
42
+ const node = this.heap.top;
43
+ this.dict.delete(node.value.key);
44
+ node.value = {
45
+ key,
46
+ value,
47
+ counter: 1
48
+ };
49
+ this.dict.set(key, node);
50
+ this.heap.updateTop();
51
+ return node;
52
+ }
53
+ remove(key) {
54
+ const node = this.dict.get(key);
55
+ if (node) {
56
+ this.dict.delete(key);
57
+ this.list.removeNode(node);
58
+ this.heap.remove(node);
59
+ }
60
+ return this;
61
+ }
62
+ clear() {
63
+ super.clear();
64
+ this.heap.clear();
65
+ return this;
66
+ }
67
+ resetCounters(initialValue = 1) {
68
+ for (const item of this.heap) {
69
+ item.counter = initialValue;
70
+ }
71
+ return this;
72
+ }
73
+ }
74
+ exports.CacheLFU = CacheLFU;
75
+ (0, _metaUtils.addAlias)(CacheLFU.prototype, 'remove', 'delete');
76
+ var _default = exports.default = CacheLFU;
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.CacheLRU = void 0;
7
+ var _valueList = _interopRequireDefault(require("../value-list.js"));
8
+ var _metaUtils = require("../meta-utils.js");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ // The base cache class. Evicts the least recently used items.
11
+ // Based on doubly linked value lists.
12
+
13
+ class CacheLRU {
14
+ constructor(capacity = 10) {
15
+ this.capacity = capacity;
16
+ this.list = new _valueList.default();
17
+ this.dict = new Map();
18
+ }
19
+ get isEmpty() {
20
+ return !this.dict.size;
21
+ }
22
+ get size() {
23
+ return this.dict.size;
24
+ }
25
+ has(key) {
26
+ return this.dict.has(key);
27
+ }
28
+ find(key) {
29
+ const node = this.use(key);
30
+ return node ? node.value.value : undefined;
31
+ }
32
+ remove(key) {
33
+ const node = this.dict.get(key);
34
+ if (node) {
35
+ this.dict.delete(key);
36
+ this.list.removeNode(node);
37
+ }
38
+ return this;
39
+ }
40
+ register(key, value) {
41
+ const node = this.use(key);
42
+ if (node) {
43
+ this.update(node, value);
44
+ return this;
45
+ }
46
+ if (this.dict.size >= this.capacity) {
47
+ this.evictAndReplace(key, value);
48
+ return this;
49
+ }
50
+ this.addNew(key, value);
51
+ return this;
52
+ }
53
+ use(key) {
54
+ const node = this.dict.get(key);
55
+ if (node) this.list.moveToFront(node);
56
+ return node;
57
+ }
58
+ update(node, value) {
59
+ node.value.value = value;
60
+ return this;
61
+ }
62
+ addNew(key, value) {
63
+ this.list.pushFront({
64
+ key,
65
+ value
66
+ });
67
+ const node = this.list.front;
68
+ this.dict.set(key, node);
69
+ return node;
70
+ }
71
+ evictAndReplace(key, value) {
72
+ const node = this.list.back;
73
+ this.list.moveToFront(node);
74
+ this.dict.delete(node.value.key);
75
+ this.dict.set(key, node);
76
+ node.value = {
77
+ key,
78
+ value
79
+ };
80
+ return node;
81
+ }
82
+ clear() {
83
+ this.dict.clear();
84
+ this.list.clear();
85
+ return this;
86
+ }
87
+ [Symbol.iterator]() {
88
+ return this.list[Symbol.iterator]();
89
+ }
90
+ getReverseIterator() {
91
+ return this.list.getReverseIterator();
92
+ }
93
+ }
94
+ exports.CacheLRU = CacheLRU;
95
+ (0, _metaUtils.addAliases)(CacheLRU.prototype, {
96
+ register: 'add, set',
97
+ remove: 'delete',
98
+ find: 'get'
99
+ });
100
+ var _default = exports.default = CacheLRU;
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.CacheRandom = void 0;
7
+ var _metaUtils = require("../meta-utils.js");
8
+ var _minHeap = _interopRequireDefault(require("../heap/min-heap.js"));
9
+ var _cacheLru = _interopRequireDefault(require("./cache-lru.js"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ // Evicts items randomly.
12
+
13
+ class CacheRandom extends _cacheLru.default {
14
+ constructor(capacity = 10) {
15
+ super(capacity);
16
+ this.heap = new _minHeap.default({
17
+ less: (a, b) => a.value.id > b.value.id
18
+ });
19
+ this.nextId = 0;
20
+ }
21
+ use(key) {
22
+ return this.dict.get(key);
23
+ }
24
+ update(node, value) {
25
+ node.value.value = value;
26
+ return this;
27
+ }
28
+ addNew(key, value) {
29
+ this.list.pushFront({
30
+ key,
31
+ value,
32
+ id: this.nextId++
33
+ });
34
+ const node = this.list.front;
35
+ this.dict.set(key, node);
36
+ this.heap.push(node);
37
+ return node;
38
+ }
39
+ evictAndReplace(key, value) {
40
+ const index = Math.floor(this.heap.length * Math.random());
41
+ const node = this.heap.array[index],
42
+ isDecreased = value > node.value.value;
43
+ this.dict.delete(node.value.key);
44
+ this.dict.set(key, node);
45
+ node.value.key = key;
46
+ node.value.value = value;
47
+ this.heap.updateByIndex(index, isDecreased);
48
+ return node;
49
+ }
50
+ remove(key) {
51
+ const node = this.dict.get(key);
52
+ if (node) {
53
+ this.dict.delete(key);
54
+ this.list.removeNode(node);
55
+ this.heap.remove(node);
56
+ }
57
+ return this;
58
+ }
59
+ clear() {
60
+ super.clear();
61
+ this.heap.clear();
62
+ this.nextId = 0;
63
+ return this;
64
+ }
65
+ resetIds() {
66
+ this.nextId = 0;
67
+ for (const item of this.heap) {
68
+ item.id = this.nextId++;
69
+ }
70
+ const array = this.heap.array;
71
+ this.heap.clear().merge(array);
72
+ return this;
73
+ }
74
+ }
75
+ exports.CacheRandom = CacheRandom;
76
+ (0, _metaUtils.addAlias)(CacheRandom.prototype, 'remove', 'delete');
77
+ var _default = exports.default = CacheRandom;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getCache = exports.default = exports.decorateMethod = exports.decorateFn = exports.decorate = void 0;
7
+ const decorateFn = (fn, cache) => {
8
+ if (typeof fn !== 'function') throw new TypeError('Not a function');
9
+ const wrapped = function (...args) {
10
+ const key = args[0],
11
+ cache = wrapped.cache;
12
+ if (cache.has(key)) return cache.get(key);
13
+ const result = wrapped.fn.apply(this, args);
14
+ cache.set(key, result);
15
+ return result;
16
+ };
17
+ wrapped.fn = fn;
18
+ wrapped.cache = cache;
19
+ return wrapped;
20
+ };
21
+ exports.decorateFn = decorateFn;
22
+ const decorate = (object, key, cache) => {
23
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
24
+ if (!descriptor) throw new Error('Missing property: ' + key);
25
+ const newDescriptor = {
26
+ ...descriptor
27
+ },
28
+ wrapped = decorateFn(descriptor.value, cache);
29
+ newDescriptor.value = wrapped;
30
+ Object.defineProperty(object, key, newDescriptor);
31
+ return wrapped;
32
+ };
33
+ exports.decorate = decorate;
34
+ const decorateMethod = (object, key, cache) => {
35
+ const fn = object[key],
36
+ wrapped = decorateFn(fn, cache);
37
+ object[key] = wrapped;
38
+ return wrapped;
39
+ };
40
+ exports.decorateMethod = decorateMethod;
41
+ const getCache = (object, key) => {
42
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
43
+ if (!descriptor) throw new Error('Missing property: ' + key);
44
+ return descriptor.value.cache;
45
+ };
46
+ exports.getCache = getCache;
47
+ var _default = exports.default = decorate;
package/cjs/cache.js ADDED
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ cacheDecorator: true
8
+ };
9
+ exports.default = exports.cacheDecorator = void 0;
10
+ var _cacheLru = _interopRequireWildcard(require("./cache/cache-lru.js"));
11
+ Object.keys(_cacheLru).forEach(function (key) {
12
+ if (key === "default" || key === "__esModule") return;
13
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
14
+ if (key in exports && exports[key] === _cacheLru[key]) return;
15
+ Object.defineProperty(exports, key, {
16
+ enumerable: true,
17
+ get: function () {
18
+ return _cacheLru[key];
19
+ }
20
+ });
21
+ });
22
+ var _decorator = _interopRequireDefault(require("./cache/decorator.js"));
23
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
24
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
25
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
26
+ const cacheDecorator = (object, key, cache = new _cacheLru.default()) => (0, _decorator.default)(object, key, cache);
27
+ exports.cacheDecorator = cacheDecorator;
28
+ var _default = exports.default = _cacheLru.default;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {};
7
+ exports.default = void 0;
8
+ var _ext = _interopRequireWildcard(require("./list/ext.js"));
9
+ Object.keys(_ext).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
+ if (key in exports && exports[key] === _ext[key]) return;
13
+ Object.defineProperty(exports, key, {
14
+ enumerable: true,
15
+ get: function () {
16
+ return _ext[key];
17
+ }
18
+ });
19
+ });
20
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
21
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
+ var _default = exports.default = _ext.default;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {};
7
+ exports.default = void 0;
8
+ var _ext = _interopRequireWildcard(require("./slist/ext.js"));
9
+ Object.keys(_ext).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
+ if (key in exports && exports[key] === _ext[key]) return;
13
+ Object.defineProperty(exports, key, {
14
+ enumerable: true,
15
+ get: function () {
16
+ return _ext[key];
17
+ }
18
+ });
19
+ });
20
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
21
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
+ var _default = exports.default = _ext.default;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {};
7
+ exports.default = void 0;
8
+ var _extValue = _interopRequireWildcard(require("./list/ext-value.js"));
9
+ Object.keys(_extValue).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
+ if (key in exports && exports[key] === _extValue[key]) return;
13
+ Object.defineProperty(exports, key, {
14
+ enumerable: true,
15
+ get: function () {
16
+ return _extValue[key];
17
+ }
18
+ });
19
+ });
20
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
21
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
+ var _default = exports.default = _extValue.default;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {};
7
+ exports.default = void 0;
8
+ var _extValue = _interopRequireWildcard(require("./slist/ext-value.js"));
9
+ Object.keys(_extValue).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
+ if (key in exports && exports[key] === _extValue[key]) return;
13
+ Object.defineProperty(exports, key, {
14
+ enumerable: true,
15
+ get: function () {
16
+ return _extValue[key];
17
+ }
18
+ });
19
+ });
20
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
21
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
+ var _default = exports.default = _extValue.default;
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = exports.MinHeap = void 0;
7
- var _utils = require("./utils.js");
7
+ var _metaUtils = require("../meta-utils.js");
8
8
  // the following functions are inlined:
9
9
 
10
10
  // const left = i => (i << 1) + 1;
@@ -27,17 +27,10 @@ const down = (array, i, less = defaultLess, n = array.length) => {
27
27
  for (;;) {
28
28
  const l = (i << 1) + 1;
29
29
  if (l >= n) break;
30
- let c = l,
30
+ const r = l + 1,
31
+ c = r < n && less(array[r], array[l]) ? r : l,
32
+ iValue = array[i],
31
33
  cValue = array[c];
32
- const r = c + 1;
33
- if (r < n) {
34
- const rValue = array[r];
35
- if (less(rValue, cValue)) {
36
- c = r;
37
- cValue = rValue;
38
- }
39
- }
40
- const iValue = array[i];
41
34
  if (!less(cValue, iValue)) break;
42
35
  array[i] = cValue;
43
36
  array[c] = iValue;
@@ -47,7 +40,11 @@ const down = (array, i, less = defaultLess, n = array.length) => {
47
40
  };
48
41
  class MinHeap {
49
42
  constructor(options, ...args) {
50
- (0, _utils.copyOptions)(this, MinHeap.defaults, options);
43
+ (0, _metaUtils.copyOptions)(this, MinHeap.defaults, options);
44
+ if (typeof this.compare == 'function') {
45
+ this.less = (a, b) => this.compare(a, b) < 0;
46
+ this.equal = (a, b) => !this.compare(a, b);
47
+ }
51
48
  this.array = [];
52
49
  this.merge(...args);
53
50
  }
@@ -60,9 +57,8 @@ class MinHeap {
60
57
  get top() {
61
58
  return this.array[0];
62
59
  }
63
- clear() {
64
- this.array = [];
65
- return this;
60
+ peek() {
61
+ return this.array[0];
66
62
  }
67
63
  pop() {
68
64
  // return MinHeap.pop(this.array, this.less); // inlined
@@ -79,17 +75,10 @@ class MinHeap {
79
75
  for (let i = 0;;) {
80
76
  const l = (i << 1) + 1;
81
77
  if (l >= n) break;
82
- let c = l,
78
+ const r = l + 1,
79
+ c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
80
+ iValue = this.array[i],
83
81
  cValue = this.array[c];
84
- const r = c + 1;
85
- if (r < n) {
86
- const rValue = this.array[r];
87
- if (this.less(rValue, cValue)) {
88
- c = r;
89
- cValue = rValue;
90
- }
91
- }
92
- const iValue = this.array[i];
93
82
  if (!this.less(cValue, iValue)) break;
94
83
  this.array[i] = cValue;
95
84
  this.array[c] = iValue;
@@ -121,17 +110,10 @@ class MinHeap {
121
110
  for (let i = 0;;) {
122
111
  const l = (i << 1) + 1;
123
112
  if (l >= n) break;
124
- let c = l,
113
+ const r = l + 1,
114
+ c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
115
+ iValue = this.array[i],
125
116
  cValue = this.array[c];
126
- const r = c + 1;
127
- if (r < n) {
128
- const rValue = this.array[r];
129
- if (this.less(rValue, cValue)) {
130
- c = r;
131
- cValue = rValue;
132
- }
133
- }
134
- const iValue = this.array[i];
135
117
  if (!this.less(cValue, iValue)) break;
136
118
  this.array[i] = cValue;
137
119
  this.array[c] = iValue;
@@ -148,17 +130,10 @@ class MinHeap {
148
130
  for (let i = 0;;) {
149
131
  const l = (i << 1) + 1;
150
132
  if (l >= n) break;
151
- let c = l,
133
+ const r = l + 1,
134
+ c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
135
+ iValue = this.array[i],
152
136
  cValue = this.array[c];
153
- const r = c + 1;
154
- if (r < n) {
155
- const rValue = this.array[r];
156
- if (this.less(rValue, cValue)) {
157
- c = r;
158
- cValue = rValue;
159
- }
160
- }
161
- const iValue = this.array[i];
162
137
  if (!this.less(cValue, iValue)) break;
163
138
  this.array[i] = cValue;
164
139
  this.array[c] = iValue;
@@ -170,11 +145,36 @@ class MinHeap {
170
145
  // return MinHeap.has(this.array, value, this.equal); // inlined
171
146
  return this.array.findIndex(element => this.equal(element, value)) >= 0;
172
147
  }
148
+ findIndex(value) {
149
+ return this.array.findIndex(element => this.equal(element, value));
150
+ }
173
151
  remove(value) {
174
- return MinHeap.remove(this.array, value, this.less, this.equal);
152
+ MinHeap.remove(this.array, value, this.less, this.equal);
153
+ return this;
154
+ }
155
+ removeByIndex(index) {
156
+ MinHeap.removeByIndex(this.array, index, this.less);
157
+ return this;
175
158
  }
176
159
  replace(value, newValue) {
177
- return MinHeap.replace(this.array, value, newValue, this.less, this.equal);
160
+ MinHeap.replace(this.array, value, newValue, this.less, this.equal);
161
+ return this;
162
+ }
163
+ replaceByIndex(index, newValue) {
164
+ MinHeap.replaceByIndex(this.array, index, newValue, this.less);
165
+ return this;
166
+ }
167
+ updateTop() {
168
+ down(this.array, 0, this.less);
169
+ return this;
170
+ }
171
+ updateByIndex(index, isDecreased) {
172
+ MinHeap.updateByIndex(this.array, index, isDecreased, this.less);
173
+ return this;
174
+ }
175
+ clear() {
176
+ this.array = [];
177
+ return this;
178
178
  }
179
179
  releaseSorted() {
180
180
  MinHeap.sort(this.array, this.less);
@@ -206,17 +206,10 @@ class MinHeap {
206
206
  for (let i = j;;) {
207
207
  const l = (i << 1) + 1;
208
208
  if (l >= n) break;
209
- let c = l,
209
+ const r = l + 1,
210
+ c = r < n && less(array[r], array[l]) ? r : l,
211
+ iValue = array[i],
210
212
  cValue = array[c];
211
- const r = c + 1;
212
- if (r < n) {
213
- const rValue = array[r];
214
- if (less(rValue, cValue)) {
215
- c = r;
216
- cValue = rValue;
217
- }
218
- }
219
- const iValue = array[i];
220
213
  if (!less(cValue, iValue)) break;
221
214
  array[i] = cValue;
222
215
  array[c] = iValue;
@@ -244,10 +237,7 @@ class MinHeap {
244
237
  }
245
238
  static pushPop(heapArray, item, less = MinHeap.defaults.less) {
246
239
  if (!heapArray.length || less(item, heapArray[0])) return item;
247
- const top = heapArray[0];
248
- heapArray[0] = item;
249
- down(heapArray, 0, less);
250
- return top;
240
+ return MinHeap.replaceTop(heapArray, item, less);
251
241
  }
252
242
  static replaceTop(heapArray, item, less = MinHeap.defaults.less) {
253
243
  const top = heapArray[0];
@@ -261,39 +251,34 @@ class MinHeap {
261
251
  static findIndex(heapArray, item, equal = MinHeap.defaults.equal) {
262
252
  return heapArray.findIndex(element => equal(element, item));
263
253
  }
264
- static removeByIndex(heapArray, index, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
254
+ static removeByIndex(heapArray, index, less = MinHeap.defaults.less) {
265
255
  if (index < 0 || index >= heapArray.length) return this;
266
256
  const last = heapArray.length - 1;
267
257
  if (index !== last) {
268
- const item = heapArray[index];
269
- heapArray[index] = heapArray.pop();
270
- if (less(heapArray[index], item)) up(heapArray, index, less);else down(heapArray, index, less);
271
- } else heapArray.pop();
272
- return this;
258
+ const item = heapArray[index],
259
+ newItem = heapArray[index] = heapArray.pop();
260
+ return MinHeap.updateByIndex(heapArray, index, less(newItem, item), less);
261
+ }
262
+ heapArray.pop();
263
+ return heapArray;
273
264
  }
274
265
  static remove(heapArray, item, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
275
266
  const index = heapArray.findIndex(element => equal(element, item));
276
- if (index < 0) return this;
277
- const last = heapArray.length - 1;
278
- if (index !== last) {
279
- heapArray[index] = heapArray.pop();
280
- if (less(heapArray[index], item)) up(heapArray, index, less);else down(heapArray, index, less);
281
- } else heapArray.pop();
282
- return this;
267
+ return MinHeap.removeByIndex(heapArray, index, less);
283
268
  }
284
- static replaceByIndex(heapArray, index, newItem, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
269
+ static replaceByIndex(heapArray, index, newItem, less = MinHeap.defaults.less) {
285
270
  if (index < 0 || index >= heapArray.length) return this;
286
271
  const item = heapArray[index];
287
272
  heapArray[index] = newItem;
288
- if (less(newItem, item)) up(heapArray, index, less);else down(heapArray, index, less);
289
- return this;
273
+ return MinHeap.updateByIndex(heapArray, index, less(newItem, item), less);
290
274
  }
291
275
  static replace(heapArray, item, newItem, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
292
276
  const index = heapArray.findIndex(element => equal(element, item));
293
- if (index < 0) return this;
294
- heapArray[index] = newItem;
295
- if (less(newItem, item)) up(heapArray, index, less);else down(heapArray, index, less);
296
- return this;
277
+ return MinHeap.replaceByIndex(heapArray, index, newItem, less);
278
+ }
279
+ static updateByIndex(heapArray, index, isDecreased, less = MinHeap.defaults.less) {
280
+ if (index < 0 || index >= heapArray.length) return this;
281
+ return (isDecreased ? up : down)(heapArray, index, less);
297
282
  }
298
283
  static sort(heapArray, less = MinHeap.defaults.less) {
299
284
  if (heapArray.length <= 1) return heapArray;
@@ -307,6 +292,7 @@ class MinHeap {
307
292
  exports.MinHeap = MinHeap;
308
293
  MinHeap.defaults = {
309
294
  less: defaultLess,
310
- equal: defaultEqual
295
+ equal: defaultEqual,
296
+ compare: null
311
297
  };
312
298
  var _default = exports.default = MinHeap;