list-toolkit 1.0.2 → 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 +88 -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} +47 -31
  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 +6 -6
  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} +53 -34
  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 -71
  76. package/cjs/List.js +0 -294
  77. package/cjs/ListHead.js +0 -309
  78. package/cjs/SList.js +0 -342
  79. package/cjs/SListHead.js +0 -367
  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 -330
  85. package/src/SListHead.js +0 -354
  86. package/src/utils.js +0 -35
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ import List, {Ptr} from './core.js';
4
+ import {ValueNode} from './nodes.js';
5
+ import {addAliases, mapIterator, normalizeIterator} from '../meta-utils.js';
6
+
7
+ export class ValueSList extends List {
8
+ popFront() {
9
+ return this.popFrontNode()?.value;
10
+ }
11
+
12
+ adoptValue(value) {
13
+ if (value instanceof Ptr) {
14
+ if (!this.isCompatiblePtr(value)) throw new Error('Incompatible pointer');
15
+ if (value.node instanceof ValueNode) {
16
+ value.list = this;
17
+ return super.adoptNode(value);
18
+ }
19
+ return new ValueNode(value.node, this);
20
+ }
21
+ if (value instanceof ValueNode) {
22
+ if (!this.isNodeLike(value)) throw new Error('Incompatible node');
23
+ return super.adoptNode(value);
24
+ }
25
+ return new ValueNode(value, this);
26
+ }
27
+
28
+ // iterators
29
+
30
+ [Symbol.iterator]() {
31
+ let current = this[this.nextName],
32
+ readyToStop = this.isEmpty;
33
+ return normalizeIterator({
34
+ next: () => {
35
+ if (readyToStop && current === this) return {done: true};
36
+ readyToStop = true;
37
+ const value = current.value;
38
+ current = current[this.nextName];
39
+ return {value};
40
+ }
41
+ });
42
+ }
43
+
44
+ getValueIterator(range) {
45
+ return mapIterator(this.getNodeIterator(range), node => node.value);
46
+ }
47
+
48
+ // meta helpers
49
+
50
+ clone() {
51
+ return ValueSList.from(this, this);
52
+ }
53
+
54
+ make() {
55
+ return new ValueSList(this);
56
+ }
57
+
58
+ makeFrom(values) {
59
+ return ValueSList.from(values, this);
60
+ }
61
+
62
+ static from(values, options) {
63
+ const list = new ValueSList(options);
64
+ for (const value of values) list.pushBack(value);
65
+ return list;
66
+ }
67
+ }
68
+
69
+ ValueSList.Ptr = Ptr;
70
+ ValueSList.ValueNode = ValueNode;
71
+
72
+ addAliases(ValueSList.prototype, {popFront: 'pop', getValueIterator: 'getIterator'}, true);
73
+
74
+ export {ValueNode, Ptr};
75
+ export default ValueSList;
package/src/slist.js ADDED
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ export * from './slist/core.js';
4
+ import SList from './slist/core.js';
5
+
6
+ export default SList;
package/src/stack.js ADDED
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ import ValueList from './value-list.js';
4
+ import {addAlias} from './meta-utils.js';
5
+ import {pushValuesFront} from './list-utils.js';
6
+
7
+ export class Stack {
8
+ constructor(UnderlyingList = ValueList) {
9
+ this.size = 0;
10
+ this.list = new UnderlyingList();
11
+ }
12
+ get isEmpty() {
13
+ return this.list.isEmpty;
14
+ }
15
+ get top() {
16
+ return this.list.isEmpty ? undefined : this.list.front.value;
17
+ }
18
+ peek() {
19
+ return this.list.isEmpty ? undefined : this.list.front.value;
20
+ }
21
+ push(value) {
22
+ this.list.pushFront(value);
23
+ ++this.size;
24
+ return this;
25
+ }
26
+ pop() {
27
+ if (!this.list.isEmpty) {
28
+ --this.size;
29
+ return this.list.popFront().value;
30
+ }
31
+ // return undefined;
32
+ }
33
+ pushValues(values) {
34
+ pushValuesFront(this, values);
35
+ return this;
36
+ }
37
+ clear() {
38
+ this.list.clear();
39
+ this.size = 0;
40
+ return this;
41
+ }
42
+ [Symbol.iterator]() {
43
+ return this.list[Symbol.iterator]();
44
+ }
45
+ getReverseIterator() {
46
+ return this.list.getReverseIterator?.();
47
+ }
48
+ }
49
+
50
+ addAlias(Stack.prototype, 'push', 'pushFront');
51
+
52
+ export default Stack;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ export * from './list/value.js';
4
+ import ValueList from './list/value.js';
5
+
6
+ export default ValueList;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ export * from './slist/value.js';
4
+ import ValueSList from './slist/value.js';
5
+
6
+ export default ValueSList;
package/cjs/Cache.js DELETED
@@ -1,71 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.Cache = void 0;
7
- var _List = _interopRequireDefault(require("./List.js"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- class Cache {
10
- constructor(capacity = 10) {
11
- this.capacity = capacity;
12
- this.size = 0;
13
- this.list = new _List.default();
14
- this.dict = {};
15
- }
16
- find(key) {
17
- const node = this.dict[key];
18
- if (typeof node == 'object') {
19
- return node.value.value;
20
- }
21
- }
22
- remove(key) {
23
- const node = this.dict[key];
24
- if (typeof node == 'object') {
25
- delete this.dict[key];
26
- node.pop();
27
- --this.size;
28
- }
29
- return this;
30
- }
31
- register(key, value) {
32
- const node = this.dict[key];
33
- if (typeof node == 'object') {
34
- this.list.moveToFront(node);
35
- node.value.value = value;
36
- return this;
37
- }
38
- if (this.size >= this.capacity) {
39
- const node = this.list.back;
40
- this.list.moveToFront(node);
41
- delete this.dict[node.value.key];
42
- this.dict[key] = node;
43
- node.value = {
44
- key,
45
- value
46
- };
47
- return this;
48
- }
49
- this.list.pushFront({
50
- key,
51
- value
52
- });
53
- ++this.size;
54
- this.dict[key] = this.list.front;
55
- return this;
56
- }
57
- clear() {
58
- this.dict = {};
59
- this.list.clear();
60
- this.size = 0;
61
- return this;
62
- }
63
- [Symbol.iterator]() {
64
- return this.list[Symbol.iterator]();
65
- }
66
- getReverseIterable() {
67
- return this.list.getReverseIterable();
68
- }
69
- }
70
- exports.Cache = Cache;
71
- var _default = exports.default = Cache;
package/cjs/List.js DELETED
@@ -1,294 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.ListValueNode = exports.ListNode = exports.List = void 0;
7
- class ListNode {
8
- constructor() {
9
- this.prev = this.next = this;
10
- }
11
- }
12
- exports.ListNode = ListNode;
13
- const pop = head => {
14
- const rest = head.next;
15
- head.prev.next = head.next;
16
- head.next.prev = head.prev;
17
- head.prev = head.next = head;
18
- return {
19
- node: head,
20
- list: rest
21
- };
22
- };
23
- const extract = (from, to) => {
24
- const prev = from.prev,
25
- next = to.next;
26
- prev.next = next;
27
- next.prev = prev;
28
- from.prev = to;
29
- to.next = from;
30
- return from;
31
- };
32
- const splice = (head1, head2) => {
33
- const tail1 = head1.prev,
34
- tail2 = head2.prev;
35
- tail1.next = head2;
36
- head2.prev = tail1;
37
- tail2.next = head1;
38
- head1.prev = tail2;
39
- return head1;
40
- };
41
- class ListValueNode extends ListNode {
42
- constructor(value) {
43
- super();
44
- this.value = value;
45
- }
46
- pop() {
47
- return pop(this).node.value;
48
- }
49
- addBefore(value) {
50
- splice(this, new ListValueNode(value));
51
- return this;
52
- }
53
- addAfter(value) {
54
- splice(this.next, new ListValueNode(value));
55
- return this;
56
- }
57
- insertBefore(list) {
58
- splice(this, pop(list).list);
59
- return this;
60
- }
61
- insertAfter(list) {
62
- splice(this.next, pop(list).list);
63
- return this;
64
- }
65
- }
66
- exports.ListValueNode = ListValueNode;
67
- class List extends ListNode {
68
- get isEmpty() {
69
- return this.next === this;
70
- }
71
- get front() {
72
- return this.next;
73
- }
74
- get back() {
75
- return this.prev;
76
- }
77
- getLength() {
78
- let n = 0;
79
- for (let p = this.next; p !== this; ++n, p = p.next);
80
- return n;
81
- }
82
- popFront() {
83
- if (this.next !== this) {
84
- return this.next.pop();
85
- }
86
- }
87
- popBack() {
88
- if (this.next !== this) {
89
- return this.prev.pop();
90
- }
91
- }
92
- pushFront(value) {
93
- splice(this.next, new ListValueNode(value));
94
- return this;
95
- }
96
- pushBack(value) {
97
- splice(this, new ListValueNode(value));
98
- return this;
99
- }
100
- appendFront(list) {
101
- if (list.next !== list) {
102
- splice(this.next, extract(list.next, list.prev));
103
- }
104
- return this;
105
- }
106
- appendBack(list) {
107
- if (list.next !== list) {
108
- splice(this, extract(list.next, list.prev));
109
- }
110
- return this;
111
- }
112
- moveToFront(node) {
113
- if (this.next !== node) {
114
- splice(this.next, extract(node, node));
115
- }
116
- return this;
117
- }
118
- moveToBack(node) {
119
- if (this.prev !== node) {
120
- splice(this, extract(node, node));
121
- }
122
- return this;
123
- }
124
- clear() {
125
- this.prev = this.next = this;
126
- return this;
127
- }
128
- remove(from, to = from) {
129
- extract(from, to);
130
- return this;
131
- }
132
- extract(from, to) {
133
- return splice(new List(), extract(from, to));
134
- }
135
- reverse() {
136
- let next = this.next;
137
- this.next = this.prev;
138
- this.prev = next;
139
- while (next !== this) {
140
- const node = next;
141
- next = node.next;
142
- node.next = node.prev;
143
- node.prev = next;
144
- }
145
- return this;
146
- }
147
- sort(compareFn) {
148
- let current = this.next;
149
- for (const value of Array.from(this).sort(compareFn)) {
150
- current.value = value;
151
- current = current.next;
152
- }
153
- return this;
154
- }
155
-
156
- // iterators
157
-
158
- [Symbol.iterator]() {
159
- let current = this.next;
160
- return {
161
- next: () => {
162
- if (current === this) return {
163
- done: true
164
- };
165
- const value = current.value;
166
- current = current.next;
167
- return {
168
- value
169
- };
170
- }
171
- };
172
- }
173
- getIterable(from, to) {
174
- return {
175
- [Symbol.iterator]: () => {
176
- let current = from || this.next;
177
- const stop = to ? to.next : this;
178
- return {
179
- next: () => {
180
- if (current === stop) return {
181
- done: true
182
- };
183
- const value = current.value;
184
- current = current.next;
185
- return {
186
- value
187
- };
188
- }
189
- };
190
- }
191
- };
192
- }
193
- getNodeIterable(from, to) {
194
- return {
195
- [Symbol.iterator]: () => {
196
- let current = from || this.next;
197
- const stop = to ? to.next : this;
198
- return {
199
- next: () => {
200
- if (current === stop) return {
201
- done: true
202
- };
203
- const value = current;
204
- current = current.next;
205
- return {
206
- value
207
- };
208
- }
209
- };
210
- }
211
- };
212
- }
213
- getReverseIterable(from, to) {
214
- return {
215
- [Symbol.iterator]: () => {
216
- let current = to || this.prev;
217
- const stop = from ? from.prev : this;
218
- return {
219
- next: () => {
220
- if (current === stop) return {
221
- done: true
222
- };
223
- const value = current.value;
224
- current = current.prev;
225
- return {
226
- value
227
- };
228
- }
229
- };
230
- }
231
- };
232
- }
233
- getReverseNodeIterable(from, to) {
234
- return {
235
- [Symbol.iterator]: () => {
236
- let current = to || this.prev;
237
- const stop = from ? from.prev : this;
238
- return {
239
- next: () => {
240
- if (current === stop) return {
241
- done: true
242
- };
243
- const value = current;
244
- current = current.prev;
245
- return {
246
- value
247
- };
248
- }
249
- };
250
- }
251
- };
252
- }
253
-
254
- // helpers
255
-
256
- makeFrom(values) {
257
- return List.from(values);
258
- }
259
- pushValuesFront(values) {
260
- for (const value of values) {
261
- this.pushFront(value);
262
- }
263
- return this;
264
- }
265
- pushValuesBack(values) {
266
- for (const value of values) {
267
- this.pushBack(value);
268
- }
269
- return this;
270
- }
271
- appendValuesFront(values) {
272
- return this.appendFront(List.from(values));
273
- }
274
- appendValuesBack(values) {
275
- return this.appendBack(List.from(values));
276
- }
277
- static from(values) {
278
- const list = new List();
279
- for (const value of values) {
280
- list.pushBack(value);
281
- }
282
- return list;
283
- }
284
- }
285
- exports.List = List;
286
- List.pop = pop;
287
- List.extract = extract;
288
- List.splice = splice;
289
- List.Node = ListNode;
290
- List.ValueNode = ListValueNode;
291
- List.prototype.pop = List.prototype.popFront;
292
- List.prototype.push = List.prototype.pushFront;
293
- List.prototype.append = List.prototype.appendBack;
294
- var _default = exports.default = List;