list-toolkit 1.0.1 → 2.0.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 (86) hide show
  1. package/README.md +93 -407
  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} +77 -10
  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/value-list.js +22 -0
  37. package/cjs/value-slist.js +22 -0
  38. package/package.json +10 -8
  39. package/src/cache/cache-fifo.js +27 -0
  40. package/src/cache/cache-lfu.js +63 -0
  41. package/src/cache/cache-lru.js +87 -0
  42. package/src/cache/cache-random.js +73 -0
  43. package/src/cache/decorator.js +45 -0
  44. package/src/cache.js +9 -0
  45. package/src/ext-list.js +6 -0
  46. package/src/ext-slist.js +6 -0
  47. package/src/ext-value-list.js +6 -0
  48. package/src/ext-value-slist.js +6 -0
  49. package/src/{MinHeap.js → heap/min-heap.js} +91 -9
  50. package/src/heap.js +6 -0
  51. package/src/list/basics.js +64 -0
  52. package/src/list/core.js +314 -0
  53. package/src/list/ext-value.js +81 -0
  54. package/src/list/ext.js +370 -0
  55. package/src/list/nodes.js +262 -0
  56. package/src/list/ptr.js +58 -0
  57. package/src/list/value.js +88 -0
  58. package/src/list-helpers.js +80 -0
  59. package/src/list-utils.js +140 -0
  60. package/src/list.js +6 -0
  61. package/src/meta-utils.js +147 -0
  62. package/src/nt-utils.js +85 -0
  63. package/src/queue.js +52 -0
  64. package/src/slist/basics.js +47 -0
  65. package/src/slist/core.js +364 -0
  66. package/src/slist/ext-value.js +74 -0
  67. package/src/slist/ext.js +331 -0
  68. package/src/slist/nodes.js +290 -0
  69. package/src/slist/ptr.js +77 -0
  70. package/src/slist/value.js +75 -0
  71. package/src/slist.js +6 -0
  72. package/src/stack.js +52 -0
  73. package/src/value-list.js +6 -0
  74. package/src/value-slist.js +6 -0
  75. package/cjs/Cache.js +0 -70
  76. package/cjs/List.js +0 -291
  77. package/cjs/ListHead.js +0 -308
  78. package/cjs/SList.js +0 -336
  79. package/cjs/SListHead.js +0 -363
  80. package/cjs/utils.js +0 -43
  81. package/src/Cache.js +0 -61
  82. package/src/List.js +0 -303
  83. package/src/ListHead.js +0 -304
  84. package/src/SList.js +0 -329
  85. package/src/SListHead.js +0 -353
  86. package/src/utils.js +0 -35
@@ -0,0 +1,305 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.List = void 0;
7
+ Object.defineProperty(exports, "Ptr", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _ptr.default;
11
+ }
12
+ });
13
+ exports.default = void 0;
14
+ var _nodes = require("./nodes.js");
15
+ var _basics = require("./basics.js");
16
+ var _ptr = _interopRequireDefault(require("./ptr.js"));
17
+ var _metaUtils = require("../meta-utils.js");
18
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
19
+ class List extends _nodes.HeadNode {
20
+ get frontPtr() {
21
+ return new _ptr.default(this, this.front);
22
+ }
23
+ get backPtr() {
24
+ return new _ptr.default(this, this.back);
25
+ }
26
+ makePtr(node) {
27
+ if (node && !this.isNodeLike(node)) throw new Error('"node" is not a compatible node');
28
+ return new _ptr.default(this, node || this.front);
29
+ }
30
+ makePtrFromPrev(prev) {
31
+ if (prev && !this.isNodeLike(prev)) throw new Error('"prev" is not a compatible node');
32
+ return new _ptr.default(this, prev ? prev[this.nextName] : this.front);
33
+ }
34
+ pushFront(value) {
35
+ const node = this.adoptValue(value);
36
+ (0, _basics.splice)(this, this, node);
37
+ return this.makePtr(node);
38
+ }
39
+ pushBack(value) {
40
+ const node = this.adoptValue(value);
41
+ (0, _basics.splice)(this, this[this.prevName], node);
42
+ return this.makePtr(node);
43
+ }
44
+ popFrontNode() {
45
+ if (!this.isEmpty) return (0, _basics.pop)(this, this[this.nextName]).extracted;
46
+ }
47
+ popBackNode() {
48
+ if (!this.isEmpty) return (0, _basics.pop)(this, this[this.prevName]).extracted;
49
+ }
50
+ pushFrontNode(nodeOrPtr) {
51
+ const node = this.adoptNode(nodeOrPtr);
52
+ (0, _basics.splice)(this, this, node);
53
+ return this.makePtr(node);
54
+ }
55
+ pushBackNode(nodeOrPtr) {
56
+ const node = this.adoptNode(nodeOrPtr);
57
+ (0, _basics.splice)(this, this[this.prevName], node);
58
+ return this.makePtr(node);
59
+ }
60
+ appendFront(list) {
61
+ if (!this.isCompatible(list)) throw new Error('Incompatible lists');
62
+ if (list.isEmpty) return this;
63
+ const head = list[this.nextName];
64
+ (0, _basics.append)(this, this, {
65
+ from: head,
66
+ to: list[this.prevName]
67
+ });
68
+ return this.makePtr();
69
+ }
70
+ appendBack(list) {
71
+ if (!this.isCompatible(list)) throw new Error('Incompatible lists');
72
+ if (list.isEmpty) return this;
73
+ const head = list[this.nextName];
74
+ (0, _basics.append)(this, this[this.prevName], {
75
+ from: head,
76
+ to: list[this.prevName]
77
+ });
78
+ return this.makePtr(head);
79
+ }
80
+ moveToFront(nodeOrPtr) {
81
+ const node = this.normalizeNode(nodeOrPtr);
82
+ if (this[this.nextName] !== node) {
83
+ (0, _basics.splice)(this, this, (0, _basics.pop)(this, node).extracted);
84
+ }
85
+ return this.frontPtr;
86
+ }
87
+ moveToBack(nodeOrPtr) {
88
+ const node = this.normalizeNode(nodeOrPtr);
89
+ if (this[this.prevName] !== node) {
90
+ (0, _basics.splice)(this, this[this.prevName], (0, _basics.pop)(this, node).extracted);
91
+ }
92
+ return this.backPtr;
93
+ }
94
+ clear(drop) {
95
+ if (drop) {
96
+ while (!this.isEmpty) this.popFrontNode();
97
+ } else {
98
+ this[this.nextName] = this[this.prevName] = this;
99
+ }
100
+ return this;
101
+ }
102
+ removeNode(nodeOrPtr) {
103
+ return (0, _basics.pop)(this, this.normalizeNode(nodeOrPtr)).extracted;
104
+ }
105
+ removeRange(range, drop) {
106
+ return this.extractRange(range).clear(drop);
107
+ }
108
+ extractRange(range = {}) {
109
+ range = this.normalizeRange(range);
110
+ range.from ||= this.front;
111
+ range.to ||= this.back;
112
+ return (0, _basics.append)(this, this.make(), range);
113
+ }
114
+ extractBy(condition) {
115
+ const extracted = this.make();
116
+ if (this.isEmpty) return extracted;
117
+ while (this.isEmpty && condition(this.front)) extracted.pushBack(this.popFront());
118
+ if (this.isOneOrEmpty) return extracted;
119
+ for (const ptr of this.getPtrIterator({
120
+ from: this.front[this.nextName]
121
+ })) {
122
+ if (condition(ptr.node)) extracted.pushBack(ptr.removeCurrent());
123
+ }
124
+ return extracted;
125
+ }
126
+ reverse() {
127
+ let current = this;
128
+ do {
129
+ const next = current[this.nextName];
130
+ current[this.nextName] = current[this.prevName];
131
+ current[this.prevName] = next;
132
+ current = next;
133
+ } while (current !== this);
134
+ return this;
135
+ }
136
+ sort(lessFn) {
137
+ if (this.isOneOrEmpty) return this;
138
+ const left = this.make(),
139
+ right = this.make();
140
+
141
+ // split into two sublists
142
+ for (let isLeft = true; !this.isEmpty; isLeft = !isLeft) {
143
+ (isLeft ? left : right).pushBackNode(this.popFrontNode());
144
+ }
145
+ // the list is empty now
146
+
147
+ // sort sublists
148
+ left.sort(lessFn);
149
+ right.sort(lessFn);
150
+
151
+ // merge sublists
152
+ while (!left.isEmpty && !right.isEmpty) {
153
+ this.pushBackNode((lessFn(left.front, right.front) ? left : right).popFrontNode());
154
+ }
155
+ if (!left.isEmpty) this.appendBack(left);
156
+ if (!right.isEmpty) this.appendBack(right);
157
+ return this;
158
+ }
159
+ releaseRawList() {
160
+ return this.isEmpty ? null : (0, _basics.pop)(this, this).rest;
161
+ }
162
+ releaseNTList() {
163
+ if (this.isEmpty) return null;
164
+ const head = this[this.nextName],
165
+ tail = this[this.prevName];
166
+ this.clear();
167
+ head[this.prevName] = tail[this.nextName] = null;
168
+ return {
169
+ head,
170
+ tail
171
+ };
172
+ }
173
+ validateRange(range = {}) {
174
+ range = this.normalizeRange(range);
175
+ let current = range.from;
176
+ do {
177
+ if (current === this) return false;
178
+ current = current[this.nextName];
179
+ } while (current !== range.to);
180
+ return true;
181
+ }
182
+
183
+ // iterators
184
+
185
+ [Symbol.iterator]() {
186
+ let current = this[this.nextName],
187
+ readyToStop = this.isEmpty;
188
+ return (0, _metaUtils.normalizeIterator)({
189
+ next: () => {
190
+ if (readyToStop && current === this) return {
191
+ done: true
192
+ };
193
+ readyToStop = true;
194
+ const value = current;
195
+ current = current[this.nextName];
196
+ return {
197
+ value
198
+ };
199
+ }
200
+ });
201
+ }
202
+ getNodeIterator(range = {}) {
203
+ range = this.normalizeRange(range);
204
+ const {
205
+ from,
206
+ to
207
+ } = range;
208
+ return {
209
+ [Symbol.iterator]: () => {
210
+ let current = from || this[this.nextName],
211
+ readyToStop = this.isEmpty;
212
+ const stop = to ? to[this.nextName] : this;
213
+ return (0, _metaUtils.normalizeIterator)({
214
+ next: () => {
215
+ if (readyToStop && current === stop) return {
216
+ done: true
217
+ };
218
+ readyToStop = true;
219
+ const value = current;
220
+ current = current[this.nextName];
221
+ return {
222
+ value
223
+ };
224
+ }
225
+ });
226
+ }
227
+ };
228
+ }
229
+ getPtrIterator(range) {
230
+ return (0, _metaUtils.mapIterator)(this.getNodeIterator(range), node => new _ptr.default(this, node));
231
+ }
232
+ getReverseNodeIterator(range = {}) {
233
+ range = this.normalizeRange(range);
234
+ const {
235
+ from,
236
+ to
237
+ } = range;
238
+ return {
239
+ [Symbol.iterator]: () => {
240
+ let current = to || this[this.prevName],
241
+ readyToStop = this.isEmpty;
242
+ const stop = from ? from[this.prevName] : this;
243
+ return (0, _metaUtils.normalizeIterator)({
244
+ next: () => {
245
+ if (readyToStop && current === stop) return {
246
+ done: true
247
+ };
248
+ readyToStop = true;
249
+ const value = current;
250
+ current = current[this.prevName];
251
+ return {
252
+ value
253
+ };
254
+ }
255
+ });
256
+ }
257
+ };
258
+ }
259
+ getReversePtrIterator(range) {
260
+ return (0, _metaUtils.mapIterator)(this.getReverseNodeIterator(range), node => new _ptr.default(this, node));
261
+ }
262
+
263
+ // meta helpers
264
+
265
+ make() {
266
+ return new List(this);
267
+ }
268
+ makeFrom(values) {
269
+ return List.from(values, this);
270
+ }
271
+ makeFromRange(range) {
272
+ return List.fromRange(range, this);
273
+ }
274
+ static from(values, options) {
275
+ const list = new List(options);
276
+ for (const value of values) list.pushBack(value);
277
+ return list;
278
+ }
279
+ static fromRange(range, options) {
280
+ const list = new List(options);
281
+ if (!range) return list;
282
+ range = list.normalizeRange(range);
283
+ if (!range.from || !range.to) throw new Error('"range" should be fully specified');
284
+ return (0, _basics.append)(list, list, range);
285
+ }
286
+ static fromExtList(extList) {
287
+ if (!(extList instanceof _nodes.ExtListBase)) throw new Error('Not a circular list');
288
+ const list = new List(extList);
289
+ if (extList.isEmpty) return list;
290
+ (0, _basics.splice)(list, list, extList.head);
291
+ extList.clear();
292
+ return list;
293
+ }
294
+ }
295
+ exports.List = List;
296
+ List.Ptr = _ptr.default;
297
+ (0, _metaUtils.addAliases)(List.prototype, {
298
+ popFrontNode: 'popFront, pop',
299
+ popBackNode: 'popBack',
300
+ pushFront: 'push',
301
+ getNodeIterator: 'getIterator',
302
+ getReverseNodeIterator: 'getReverseIterator',
303
+ appendBack: 'append'
304
+ });
305
+ var _default = exports.default = List;
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ExtValueList = void 0;
7
+ Object.defineProperty(exports, "ValueNode", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _nodes.ValueNode;
11
+ }
12
+ });
13
+ exports.default = void 0;
14
+ var _ext = _interopRequireWildcard(require("./ext.js"));
15
+ var _nodes = require("./nodes.js");
16
+ var _metaUtils = require("../meta-utils.js");
17
+ 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); }
18
+ 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; }
19
+ class ExtValueList extends _ext.default {
20
+ adoptValue(value) {
21
+ if (value instanceof _ext.Ptr) {
22
+ if (!this.isCompatiblePtr(value)) throw new Error('Incompatible pointer');
23
+ if (value.node instanceof _nodes.ValueNode) {
24
+ value.list = this;
25
+ return super.adoptNode(value);
26
+ }
27
+ return new _nodes.ValueNode(value.node, this);
28
+ }
29
+ if (value instanceof _nodes.ValueNode) {
30
+ if (!this.isNodeLike(value)) throw new Error('Incompatible node');
31
+ return super.adoptNode(value);
32
+ }
33
+ return new _nodes.ValueNode(value, this);
34
+ }
35
+
36
+ // iterators
37
+
38
+ [Symbol.iterator]() {
39
+ let current = this.head,
40
+ readyToStop = this.isEmpty;
41
+ return (0, _metaUtils.normalizeIterator)({
42
+ next: () => {
43
+ if (readyToStop && current === this.head) return {
44
+ done: true
45
+ };
46
+ readyToStop = true;
47
+ const value = current.value;
48
+ current = current[this.nextName];
49
+ return {
50
+ value
51
+ };
52
+ }
53
+ });
54
+ }
55
+ getValueIterator(range) {
56
+ return (0, _metaUtils.mapIterator)(this.getNodeIterator(range), node => node.value);
57
+ }
58
+ getReverseValueIterator(range) {
59
+ return (0, _metaUtils.mapIterator)(this.getReverseNodeIterator(range), node => node.value);
60
+ }
61
+
62
+ // meta helpers
63
+
64
+ clone() {
65
+ return new ExtValueList(this);
66
+ }
67
+ make(head = null) {
68
+ return new ExtValueList(head, this);
69
+ }
70
+ makeFrom(values) {
71
+ return ExtValueList.from(values, this);
72
+ }
73
+ static from(values, options) {
74
+ const list = new ExtValueList(null, options);
75
+ for (const value of values) {
76
+ list.addAfter(value);
77
+ list.next();
78
+ }
79
+ return list.next();
80
+ }
81
+ }
82
+ exports.ExtValueList = ExtValueList;
83
+ ExtValueList.Ptr = _ext.Ptr;
84
+ ExtValueList.ValueNode = _nodes.ValueNode;
85
+ (0, _metaUtils.addAliases)(ExtValueList.prototype, {
86
+ getValueIterator: 'getIterator',
87
+ getReverseValueIterator: 'getReverseIterator'
88
+ });
89
+ var _default = exports.default = ExtValueList;