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
package/README.md CHANGED
@@ -9,468 +9,136 @@ suitable to use in all environments including browsers.
9
9
 
10
10
  The toolkit provides the following data structures with a full set of efficiently implemented operations:
11
11
 
12
- * **List**: a doubly linked list implemented as a container.
13
- * **ListHead**: a doubly linked list that uses custom properties on external objects to link them around.
14
- * Using different custom properties the same object can be linked in different ways.
15
- * **SList**: a singly linked list implemented as a container.
16
- * **SListHead**: a singly linked list that uses custom properties on external objects to link them around.
17
- * Using different custom properties the same object can be linked in different ways.
18
- * **Cache**: a List-based container with a limited capacity and an LRU policy of evicting old entries.
19
- * **MinHeap**: a classic heap data structure (a priority queue).
12
+ * Converters for `null`-terminated (NT) lists, both singly and doubly linked. They convert in place from NT lists to circular lists and back.
13
+ * Various lists:
14
+ * Doubly linked circular lists (DLL) and singly linked circular lists (SLL).
15
+ * Value-based lists, where a list serves as a container for external objects, and node-based lists, where a list uses custom properties on external objects to link them around.
16
+ * Hosted lists, which use a special head node to manage nodes, and headless lists, which point to an external list without including any headers.
17
+ * Heaps:
18
+ * Min-heap to support priority queues.
19
+ * Various list-based data structures:
20
+ * Caches with various eviction algorithms: least recently used (LRU), least frequently used (LFU), first in first out (FIFO), and random.
21
+ * A decorator is provided to decorate functions, methods, and getters with a cache of your choice.
22
+ * Queue: an adapter for lists.
23
+ * Numerous list utilities.
20
24
 
21
- ## Installation
22
-
23
- ```bash
24
- npm install list-toolkit
25
- ```
26
-
27
- ## Usage
28
-
29
- The full documentation is available in the project's [wiki](https://github.com/uhop/list-toolkit/wiki). Below is a cheat sheet of the API.
30
-
31
- ### List
32
-
33
- `List` implements a circular doubly linked list. Its head is a node with two properties: `next` and `prev`.
34
- Other nodes of the list are value nodes (`List.ValueNode`) that have a `value` property.
35
-
36
- ```js
37
- import List from 'list-toolkit/List.js';
38
- // or
39
- // const List = require('list-toolkit/List.js').default; // CJS
40
-
41
- const list1 = new List();
42
- const list2 = List.from([1, 2, 3]);
43
- ```
44
-
45
- Main operations are:
46
-
47
- | Method | Description | Complexity |
48
- |------|-----------|-----|
49
- | `new List()` | create a new List | *O(1)* |
50
- | `List.from(values)` | create a new List from an iterable or an array | *O(k)* |
51
- | `isEmpty` | check if the list is empty | *O(1)* |
52
- | `front` | get the first element | *O(1)* |
53
- | `back` | get the last element | *O(1)* |
54
- | `getLength()` | get the length of the list | *O(n)* |
55
- | `popFront()` | remove and return the first element | *O(1)* |
56
- | `popBack()` | remove and return the last element | *O(1)* |
57
- | `pushFront(value)` | add a new element at the beginning | *O(1)* |
58
- | `pushBack(value)` | add a new element at the end | *O(1)* |
59
- | `appendFront(list)` | add a list at the beginning | *O(1)* |
60
- | `appendBack(list)` | add a list at the end | *O(1)* |
61
- | `moveToFront(node)` | move a node to the front | *O(1)* |
62
- | `moveToBack(node)` | move a node to the back | *O(1)* |
63
- | `clear()` | remove all elements | *O(1)* |
64
- | `remove(from, to = from)` | remove a range of elements | *O(1)* |
65
- | `extract(from, to)` | remove and return a range of elements as a new List | *O(1)* |
66
- | `reverse()` | reverse the list inline | *O(n)* |
67
- | `sort(compareFn)` | sort the list inline | *O(n * log(n))* |
68
-
69
- Here and everywhere `n` is a number of elements in the list, while `k` is the size of an argument.
70
-
71
- Useful aliases are:
72
-
73
- | Method | Alias of |
74
- |------|-----------|
75
- | `pop()` | `popFront()` |
76
- | `push()` | `pushFront()` |
77
- | `append()` | `appendBack()` |
78
-
79
- List can be used as an iterable:
80
-
81
- ```js
82
- const list = List.from([1, 2, 3]);
83
-
84
- for (const value of list) {
85
- console.log(value);
86
- }
87
- ```
88
-
89
- Additional iterator-related methods are:
25
+ All lists can be used without the toolkit. Your existing lists, either doubly or singly linked,
26
+ can be used. The toolkit provides a few utilities that you would write yourself if you wanted to use them.
90
27
 
91
- | Method | Description | Complexity |
92
- |------|-----------|-----|
93
- | `getIterable(from, to)` | get an iterable of a range | *O(1)* |
94
- | `getReverseIterable(from, to)` | get an iterable of a range in reverse order | *O(1)* |
95
- | `getNodeIterable(from, to)` | get an iterable of a range by nodes | *O(1)* |
96
- | `getNodeReverseIterable(from, to)` | get an iterable of a range by nodes in reverse order | *O(1)* |
28
+ The implementation philosophy was very simple:
97
29
 
98
- Helper methods are:
30
+ * Flexibility, efficiency, and simplicity.
31
+ * No dependencies. No unexpected surprises.
32
+ * You never pay for what you don't use.
33
+ * Suitable for all environments.
34
+ * Should be usable with already existing lists.
35
+ * Could be used as a foundation for other list-based data structures.
99
36
 
100
- | Method | Description | Complexity |
101
- |------|-----------|-----|
102
- | `makeFrom(values)` | (a meta helper) create a new List from an iterable or an array | *O(k)* |
103
- | `pushValuesFront(values)` | add values at the beginning | *O(k)* |
104
- | `pushValuesBack(values)` | add values at the end | *O(k)* |
105
- | `appendValuesFront(values)` | add values as a list at the beginning | *O(k)* |
106
- | `appendValuesBack(values)` | add values as a list at the end | *O(k)* |
37
+ **Read all about the implemented ideas in the [Backgrounders](./Backgrounder).**
107
38
 
108
- Value node (`List.ValueNode`) methods are:
39
+ All lists support similar intuitive interfaces:
109
40
 
110
- | Method | Description | Complexity |
111
- |------|-----------|-----|
112
- | `pop()` | remove and return the value | *O(1)* |
113
- | `addBefore(value)` | add a new value before the current node | *O(1)* |
114
- | `addAfter(value)` | add a new value after the current node | *O(1)* |
115
- | `insertBefore(list)` | insert a list before the current node | *O(1)* |
116
- | `insertAfter(list)` | insert a list after the current node | *O(1)* |
41
+ * Creating from existing objects.
42
+ * Adding, inserting, extracting and removing nodes.
43
+ * Forward and reverse iterators.
44
+ * General manipulations like reversing and sorting.
45
+ * Link names for the next and previous links (for doubly linked lists) are customizable.
117
46
 
118
- Stand-alone methods for nodes (`List.Node`) are:
47
+ All facilities are efficient, well-debugged, and battle-tested.
119
48
 
120
- | Method | Description | Complexity |
121
- |------|-----------|-----|
122
- | `List.pop(node)` | remove the node from its list and return `{node, list}` | *O(1)* |
123
- | `List.extract(from, to)` | remove nodes from their list and return the first node of the extracted list | *O(1)* |
124
- | `List.splice(head1, head2)` | combine two lists and return the first node of the combined list | *O(1)* |
49
+ **All documentation is in the [wiki](https://github.com/uhop/list-toolkit/wiki).**
125
50
 
126
- ### ListHead
127
-
128
- `ListHead` implements a circular doubly linked list. Its head is an object with the following properties:
129
-
130
- * `nextName`: the property name of the next node. It can be a string or a symbol. Default: `"next"`.
131
- * `prevName`: the property name of the previous node. It can be a string or a symbol. Default: `"prev"`.
132
- * `head`: the head node of a circular doubly linked list. Default: `{}`.
133
-
134
- All values of the list are objects that can be used as nodes. When adopted by a list,
135
- objects are modified in-place by adding the properties defined by `nextName` and `prevName`.
136
-
137
- While a value can belong to multiple `List` instances, we never know which one it belongs to.
138
- If we know `nextName` and `prevName`, we can always find its `ListHead` instances from the value,
139
- and we can manipulate the node without accessing its lists directly.
140
-
141
- Because lists are circular structures, we can have multiple `ListHead` instances pointing to
142
- different nodes of the same list. In fact, creating a new `ListHead` instance is cheap and can be done
143
- when needed using any node/value.
144
-
145
- An empty `ListHead` instance is created with an empty node that plays the role of a head of the list.
146
- Alternatively a `ListHead` can be created by pointing to an existing node. In this case,
147
- any valid node can be used as a head.
148
-
149
- Some methods use nodes to manipulate the list. In `List`, they are usually found by iterations.
150
- `ListHead` methods can use named objects for that.
151
-
152
- ```js
153
- import ListHead from 'list-toolkit/ListHead.js';
154
- // or
155
- // const ListHead = require('list-toolkit/ListHead.js').default; // CJS
156
-
157
- const list1 = new ListHead();
158
- const list2 = new ListHead(null, {nextName: Symbol('next'), prevName:Symbol('prev')});
159
-
160
- const value = {a: 1};
161
-
162
- list1.push(value);
163
- list2.push(value);
51
+ ## Installation
164
52
 
165
- ListHead.pop({nextName: 'next', prevName: 'prev'}, value);
166
- ListHead.pop(list2, value);
53
+ ```bash
54
+ npm install list-toolkit
167
55
  ```
168
56
 
169
- Almost all APIs are the same as for `List` and have the same semantics and complexity.
170
- The differences are:
171
-
172
- | Method | Description | Complexity |
173
- |------|-----------|-----|
174
- | `new ListHead(head = null, {nextName = 'next', prevName = 'prev'})` | create a new List optionally adopting a list by `head` | *O(1)* |
175
- | `ListHead.from(values, next, prev)` | create a new List from an iterable | *O(k)* |
176
- | `ListHead.pop({nextName, prevName}, node)` | remove the node from its list and return `{node, list}` | *O(1)* |
177
- | `ListHead.extract({nextName, prevName}, from, to)` | remove nodes from their list and return the first node of the extracted list | *O(1)* |
178
- | `ListHead.splice({nextName, prevName}, head1, head2)` | combine two lists and return the first node of the combined list | *O(1)* |
179
- | `makeNode()` | return an otherwise empty object with proper `next` and `prev` properties as circular doubly linked list node | *O(1)* |
180
- | `adopt(node)` | make sure that a node is already a circular doubly linked list or make it so | *O(1)* |
181
- | `makeList(head)` | return a new list pointing to `head` with the same `nextName` and `prevName` properties as this list | *O(1)* |
182
- | `clear(true)` | remove all elements and breaks circular references | *O(n)* |
183
- | `clear()` | remove all elements | *O(1)* |
184
-
185
- `ListHead` defines a helper class `ListHead.Unsafe` that can be used to create lists without checking
186
- their `nextName` and `prevName` properties:
187
-
188
- ```js
189
- const item1 = {a: 1}, item2 = {b: 2};
190
- item1.next = item2;
191
- item2.prev = item1;
192
- item1.prev = item2;
193
- item2.next = item1;
194
-
195
- const list = new ListHead(new ListHead.Unsafe(item1));
196
- ```
57
+ ## Introduction
197
58
 
198
- ### SList
59
+ The full documentation is available in the project's [wiki](https://github.com/uhop/list-toolkit/wiki). Below is a cheat sheet of the API.
199
60
 
200
- `SList` implements a circular singly linked list. Its head is a node with one property: `next`.
201
- Other nodes of the list are value nodes (`SList.ValueNode`) that have a `value` property.
61
+ Value lists are containers for arbitrary values:
202
62
 
203
63
  ```js
204
- import SList from 'list-toolkit/SList.js';
205
- // or
206
- // const SList = require('list-toolkit/SList.js').default; // CJS
207
-
208
- const list1 = new SList();
209
- const list2 = SList.from([1, 2, 3]);
210
- ```
64
+ import ValueList from 'list-toolkit/value-list.js';
211
65
 
212
- `SList` API is modelled on `List`. The main difference is that `SList` is cheaper in general
213
- but some operations have higher complexity, e.g., any operations that need to access the back of the list.
214
- `SList` does not implement some operations, e.g., a reverse iterator, due to their complexity.
215
- If you need these operations frequently, use `List` instead.
216
-
217
- Note that for efficiency reasons some methods accept a previous node as a pointer to a required node.
218
-
219
- Main operations are:
220
-
221
- | Method | Description | Complexity |
222
- |------|-----------|-----|
223
- | `new SList()` | create a new List | *O(1)* |
224
- | `SList.from(values)` | create a new List from an iterable or an array | *O(k)* |
225
- | `isEmpty` | check if the list is empty | *O(1)* |
226
- | `front` | get the first element | *O(1)* |
227
- | `getBack()` | get the last element | *O(n)* |
228
- | `getLength()` | get the length of the list | *O(n)* |
229
- | `getPtr()` | get a special pointer value (see below) | *O(1)* |
230
- | `popFront()` | remove and return the first element | *O(1)* |
231
- | `popBack()` | remove and return the last element | *O(n)* |
232
- | `pushFront(value)` | add a new element at the beginning | *O(1)* |
233
- | `pushBack(value)` | add a new element at the end | *O(n)* |
234
- | `appendFront(list)` | add a list at the beginning | *O(nk* |
235
- | `appendBack(list)` | add a list at the end | *O(n + k)* |
236
- | `moveToFront(node)` | move a node to the front | *O(n)* |
237
- | `moveToFront(ptr)` | move a node by pointer (see below) to the front | *O(1)* |
238
- | `moveToBack(node)` | move a node to the back | *O(n)* |
239
- | `moveToBack(ptr)` | move a node by pointer (see below) to the back | *O(1)* |
240
- | `clear()` | remove all elements | *O(1)* |
241
- | `remove(from, to = from)` | remove a range of elements | *O(n)* |
242
- | `remove(fromPtr, to = from)` | remove a range of elements using a pointer (see below) | *O(1)* |
243
- | `extract(from, to)` | remove and return a range of elements as a new list | *O(n)* |
244
- | `extract(fromPtr, to)` | remove and return a range of elements as a new list using a pointer (see below) | *O(1)* |
245
- | `reverse()` | reverse the list inline | *O(n)* |
246
- | `sort(compareFn)` | sort the list inline | *O(n * log(n))* |
247
-
248
- Useful aliases are:
249
-
250
- | Method | Alias of |
251
- |------|-----------|
252
- | `pop()` | `popFront()` |
253
- | `push()` | `pushFront()` |
254
- | `append()` | `appendBack()` |
255
-
256
- List can be used as an iterable:
66
+ const list = ValueList.from([1, 2, 3]);
257
67
 
258
- ```js
259
- const list = SList.from([1, 2, 3]);
68
+ // iterate over the list manually
69
+ for (let node = list.front; node !== list; node = node.next) {
70
+ console.log(node.value); // 1, 2, 3
71
+ }
260
72
 
73
+ // iterate over the list with an iterator
261
74
  for (const value of list) {
262
- console.log(value);
75
+ console.log(value); // 1, 2, 3
263
76
  }
264
- ```
265
-
266
- Additional iterator-related methods are:
267
-
268
- | Method | Description | Complexity |
269
- |------|-----------|-----|
270
- | `getIterable(from, to)` | get an iterable of a range | *O(1)* |
271
- | `getPtrIterable(from, to)` | get an iterable of a range using a pointer (see below) | *O(n)* |
272
- | `getPtrIterable(fromPtr, to)` | get an iterable of a range using a pointer (see below) | *O(1)* |
273
-
274
- Helper methods are:
275
-
276
- | Method | Description | Complexity |
277
- |------|-----------|-----|
278
- | `makeFrom(values)` | (a meta helper) create a new list from an iterable or an array | *O(k)* |
279
- | `pushValuesFront(values)` | add values at the beginning | *O(k)* |
280
- | `appendValuesFront(values)` | add values as a list at the beginning | *O(k)* |
281
-
282
- Value node (`SList.ValueNode`) methods are:
283
-
284
- | Method | Description | Complexity |
285
- |------|-----------|-----|
286
- | `addAfter(value)` | add a new value after the current node | *O(1)* |
287
- | `insertAfter(list)` | insert a list after the current node | *O(k)* |
288
-
289
- Stand-alone methods for nodes (`SList.Node`) are:
290
77
 
291
- | Method | Description | Complexity |
292
- |------|-----------|-----|
293
- | `SList.pop(prev)` | remove the node from its list and return `{node, list}` | *O(1)* |
294
- | `SList.extract(prevFrom, nodeTo)` | remove nodes from their list and return `{prev, node}`, where `node` is the first node of the extracted list, and `prev` is the previous node | *O(1)* |
295
- | `SList.splice(prev1, {prev, node})` | combine two lists and return the first node of the combined list | *O(1)* |
296
- | `SList.getPrev(list, node)` | get a previous node of the given node | *O(n)* |
297
-
298
- `SList` provides a special pointer: `SList.SListPtr`. It can be used to create a pointer to a node in a list. It is constructed using a previous node, which makes some operations more efficient.
299
-
300
- Its methods are:
301
-
302
- | Method | Description | Complexity |
303
- |------|-----------|-----|
304
- | `new SList.SListPtr(list, prev = list)` | create a new pointer | *O(1)* |
305
- | `list` | the head node | *O(1)* |
306
- | `prev` | the previous node | *O(1)* |
307
- | `isHead` | check if the pointer is at the head of the list | *O(1)* |
308
- | `next()` | move the pointer to the next node | *O(1)* |
309
- | `clone()` | make a copy of the pointer | *O(1)* |
310
-
311
- Every method of `SList` that accepts a node can accept a pointer too.
312
- Using pointers is frequently more efficient than using nodes directly.
313
-
314
- ### SListHead
315
-
316
- `SListHead` is modelled after `ListHead` but it is specialized for `SList` instances.
317
- Just like `ListHead`, it works with naked objects.
318
-
319
- `SListHead` implements a circular singly linked list. Its head is an object with the following properties:
320
-
321
- * `nextName`: the property name of the next node. It can be a string or a symbol. Default: `"next"`.
322
- * `head`: the head node of a circular singly linked list. Default: `{}`.
323
-
324
- All notes related to `ListHead` apply here too.
325
-
326
- ```js
327
- import SListHead from 'list-toolkit/SListHead.js';
328
- // or
329
- // const SListHead = require('list-toolkit/SListHead.js').default; // CJS
78
+ // add more values:
79
+ list.pushBack(4);
80
+ list.pushFront(0);
330
81
 
331
- const list1 = new SListHead();
332
- const list2 = new SListHead(null, {nextName: Symbol('next')});
333
-
334
- const value = {a: 1};
335
-
336
- list1.push(value);
337
- list2.push(value);
338
-
339
- SListHead.pop({nextName: 'next'}, value);
340
- SListHead.pop(list2, value);
82
+ console.log(list.popFront().value); // 0
83
+ console.log(list.front.value); // 1
341
84
  ```
342
85
 
343
- Almost all APIs are the same as for `SList` and have the same semantics and complexity.
344
- The differences are:
345
-
346
- | Method | Description | Complexity |
347
- |------|-----------|-----|
348
- | `new SListHead(head = null, next = 'next')` | create a new `SListHead` optionally adopting a list by `head` | *O(1)* |
349
- | `SListHead.from(values, next)` | create a new `SListHead` from an iterable | *O(k)* |
350
- | `SListHead.pop({nextName}, prev)` | remove the node by its previous node from its list and return `{node, list}` | *O(1)* |
351
- | `SListHead.extract({nextName}, prevFrom, nodeTo)` | remove nodes from its list and return `{prev, node}` | *O(1)* |
352
- | `SListHead.splice({nextName}, prev1, {prev, node})` | combine two lists and return the first node of the combined list | *O(1)* |
353
- | `SListHead.getPrev({nextName}, list, node)` | get a previous node of the given node | *O(n)* |
354
- | `make(newHead = null)` | return a new `SListHead` pointing to `newHead` with the same `nextName` property as this list | *O(1)* |
355
- | `makeFrom(values)` | (a meta helper) create a new `SListHead` from an iterable with the same `nextName` property as this list | *O(k)* |
356
- | `clear(true)` | remove all elements and breaks circular references | *O(n)* |
357
- | `clear()` | remove all elements | *O(1)* |
358
-
359
- `SListHead.SListPtr` is a pointer-like class similar to `SList.SListPtr`
360
- but specialized for `SListHead` instances. It has the same API and the same semantics.
361
-
362
- ### Cache
363
-
364
- This is the class that is used for caching values using simple unique keys (numbers, strings or symbols).
365
- It defines a capacity and when it is full, it removes the least recently used value.
366
-
367
- Internally it is based on `List`.
86
+ Lists can be made of arbitrary objects:
368
87
 
369
88
  ```js
370
- import Cache from 'list-toolkit/Cache.js';
371
- // or
372
- // const Cache = require('list-toolkit/Cache.js').default; // CJS
373
-
374
- const cache = new Cache(1000);
89
+ import List from 'list-toolkit/list.js';
375
90
 
376
- const processRequest = key => {
377
- let data = cache.get(key);
378
- if (!data) {
379
- data = new DataObject();
380
- cache.register(key, data);
91
+ class Person {
92
+ constructor(name) {
93
+ this.name = name;
381
94
  }
382
- // continue processing
383
95
  };
384
- ```
385
-
386
- The main operations are:
387
96
 
388
- | Method | Description | Complexity |
389
- |------|-----------|-----|
390
- | `new Cache(capacity = 10)` | create a new cache | *O(1)* |
391
- | `size` | get the number of values in the cache | *O(1)* |
392
- | `capacity` | get the capacity of the cache | *O(1)* |
393
- | `find(key)` | find a value by its key and return it or `undefined` | *O(1)* |
394
- | `remove(key)` | remove a value by its key | *O(1)* |
395
- | `register(key, value)` | register a value by its key | *O(1)* |
396
- | `clear()` | clear the cache | *O(1)* |
397
- | `getReverseIterable()` | get an iterable of the cache in reverse order | *O(1)* |
97
+ const john = new Person('John'),
98
+ jane = new Person('Jane'),
99
+ jim = new Person('Jim'),
100
+ jill = new Person('Jill');
398
101
 
399
- A `Cache` instance is iterable, so it can be used in loops:
102
+ const people = List.from([john, jane, jim, jill]);
400
103
 
401
- ```js
402
- for (const value of cache) {
403
- console.log(value);
104
+ // iterator over the list manually:
105
+ for (let node = people.front; node !== people; node = node[people.nextName]) {
106
+ console.log(node.name); // John, Jane, Jim, Jill
404
107
  }
405
- ```
406
-
407
- It iterates from the most recently used to the least recently used.
108
+ // yes, the link names are customizable, can be strings or symbols, for example:
408
109
 
409
- ### MinHeap
110
+ const ladies = List.from([jane, jill], {nextName: 'n', prevName: 'p'});
410
111
 
411
- `MinHeap` is a classic data structure: a priority queue. From the [Wikipedia article](https://en.wikipedia.org/wiki/Heap_(data_structure)):
112
+ // iterate over ladies
113
+ for (let node = ladies.front; node !== ladies; node = node.n) {
114
+ console.log(node.name); // Jane, Jill
115
+ }
412
116
 
413
- > The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact, priority queues are often referred to as "heaps", regardless of how they may be implemented. In a heap, the highest (or lowest) priority element is always stored at the root. However, a heap is not a sorted structure; it can be regarded as being partially ordered. A heap is a useful data structure when it is necessary to repeatedly remove the object with the highest (or lowest) priority, or when insertions need to be interspersed with removals of the root node.
117
+ // let's move Jim to the front and John to the back:
118
+ people.moveToFront(jim);
119
+ people.moveToBack(john);
414
120
 
415
- ```js
416
- import MinHeap from 'list-toolkit/MinHeap.js';
417
- // or
418
- // const MinHeap = require('list-toolkit/MinHeap.js').default; // CJS
121
+ // sort the list
122
+ people.sort((a, b) => a.name.localeCompare(b.name) < 0);
419
123
 
420
- const heap = new MinHeap({less: (a, b) => a.priority > b.priority});
124
+ for (const node of people) {
125
+ console.log(node.name); // Jane, Jill, Jim, John
126
+ }
421
127
 
422
- const enqueueTask = (priority, task) => {
423
- heap.push({priority, task});
424
- };
128
+ // let's extract all people from Jill to Jim
129
+ const ji = people.extract({from: jill, to: jim});
130
+ for (const node of people) console.log(node.name); // Jane, John
131
+ for (const node of ji) console.log(node.name); // Jim, Jill
425
132
 
426
- const processTasks = () => {
427
- while (!heap.isEmpty) {
428
- const {priority, task} = heap.pop();
429
- // process the highest priority task
430
- }
133
+ // add them back:
134
+ people.append(ji);
135
+ for (const node of people.getReverseIterator()) {
136
+ console.log(node.name); // Jill, Jim, John, Jane
431
137
  }
432
- ```
138
+ ji.isEmpty === true;
433
139
 
434
- The main operations are:
435
-
436
- | Method | Description | Complexity |
437
- |------|-----------|-----|
438
- | `new MinHeap({less = (a, b) => a < b, equal: (a, b) => a === b}, ...args)` | create a new heap and optionally populate it from iterables or other heaps | *O(k)* |
439
- | `array` | get the underlying array organized as min heap | *O(1)* |
440
- | `less` | get the comparison function | *O(1)* |
441
- | `equal` | get the equality function | *O(1)* |
442
- | `length` | get the number of elements in the heap | *O(1)* |
443
- | `isEmpty` | check if the heap is empty | *O(1)* |
444
- | `top` | get the top element | *O(1)* |
445
- | `clear()` | clear the heap | *O(1)* |
446
- | `pop()` | remove and return the top element | *O(log(n))* |
447
- | `push(value)` | add new element | *O(log(n))* |
448
- | `pushPop(value)` | add new element and then return the top element | *O(log(n))* |
449
- | `replaceTop(value)` | return the top element and then add new element | *O(log(n))* |
450
- | `has(value)` | check if the heap contains an element | *O(n)* |
451
- | `remove(value)` | remove an element | *O(n)* |
452
- | `replace(value, newValue)` | replace an element | *O(n)* |
453
- | `releaseSorted()` | remove all elements and return them as an array in the reverse sorted order (the heap will be cleared) | *O(n)* |
454
- | `merge(...args)` | add elements from iterables or other heaps | *O(k)* |
455
- | `make(...args)` | return a new heap with the same options | *O(1)* |
456
- | `clone()` | return a copy of the heap | *O(n)* |
457
-
458
- `MinHeap` provides a number of static methods to create heaps on arrays (used internally):
459
-
460
- | Method | Description | Complexity |
461
- |------|-----------|-----|
462
- | `MinHeap.build(array, less = (a, b) => a < b)` | create a new heap from an array | *O(n)* |
463
- | `MinHeap.pop(heapArray, less = (a, b) => a < b)` | remove and return the top element | *O(log(n))* |
464
- | `MinHeap.push(heapArray, value, less = (a, b) => a < b)` | add new element | *O(log(n))* |
465
- | `MinHeap.pushPop(heapArray, value, less = (a, b) => a < b)` | add new element and then return the top element | *O(log(n))* |
466
- | `MinHeap.replaceTop(heapArray, value, less = (a, b) => a < b)` | return the top element and then add new element | *O(log(n))* |
467
- | `MinHeap.findIndex(heapArray, item, equal = (a, b) => a === b)` | return the index of an element or -1| *O(n)* |
468
- | `MinHead.removeByIndex(heapArray, index, less = (a, b) => a < b, equal = (a, b) => a === b)` | remove an element by index | *O(log(n))* |
469
- | `MinHeap.replaceByIndex(heapArray, index, value, less = (a, b) => a < b, equal = (a, b) => a === b)` | replace an element by index | *O(log(n))* |
470
- | `MinHead.has(heapArray, item, equal = (a, b) => a === b)` | check if the heap contains an element | *O(n)* |
471
- | `MinHeap.remove(heapArray, item, less = (a, b) => a < b, equal = (a, b) => a === b)` | remove an element | *O(n)* |
472
- | `MinHeap.replace(heapArray, item, newItem, less = (a, b) => a < b, equal = (a, b) => a === b)` | replace an element | *O(n)* |
473
- | `MinHeap.sort(heapArray, less = (a, b) => a < b)` | sort an array in place | *O(n * log(n))* |
140
+ // BTW, the list `ladies` is unchanged
141
+ ```
474
142
 
475
143
  ## License
476
144
 
@@ -478,5 +146,7 @@ BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.
478
146
 
479
147
  ## Release History
480
148
 
149
+ * 2.1.0 *Added splay tree. Updated deps.*
150
+ * 2.0.0 *New major release.*
481
151
  * 1.0.1 *Fixed exports. Added more methods to `MinHeap`.*
482
152
  * 1.0.1 *Initial release.*
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.CacheFIFO = void 0;
7
+ var _cacheLru = _interopRequireDefault(require("./cache-lru.js"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ // Evicts on the first-in-first-out basis.
10
+
11
+ class CacheFIFO extends _cacheLru.default {
12
+ use(key) {
13
+ return this.dict.get(key);
14
+ }
15
+ addNew(key, value) {
16
+ this.list.pushBack({
17
+ key,
18
+ value
19
+ });
20
+ const node = this.list.back;
21
+ this.dict.set(key, node);
22
+ return node;
23
+ }
24
+ evictAndReplace(key, value) {
25
+ const node = this.list.front;
26
+ this.list.moveToBack(node);
27
+ this.dict.delete(node.value.key);
28
+ this.dict.set(key, node);
29
+ node.value = {
30
+ key,
31
+ value
32
+ };
33
+ return node;
34
+ }
35
+ }
36
+ exports.CacheFIFO = CacheFIFO;
37
+ var _default = exports.default = CacheFIFO;