list-toolkit 1.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2005-2024 Eugene Lazutkin
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,465 @@
1
+ # List toolkit
2
+
3
+ List-based **efficient** data structures to organize your objects.
4
+ This is a pure JavaScript module with no dependencies
5
+ suitable to use in all environments including browsers.
6
+
7
+ The toolkit provides the following data structures with a full set of efficiently implemented operations:
8
+
9
+ * **List**: a doubly linked list implemented as a container.
10
+ * **ListHead**: a doubly linked list that uses custom properties on external objects to link them around.
11
+ * Using different custom properties the same object can be linked in different ways.
12
+ * **SList**: a singly linked list implemented as a container.
13
+ * Using different custom properties the same object can be linked in different ways.
14
+ * **Cache**: a List-based container with a limited capacity and an LRU policy of evicting old entries.
15
+ * **MinHeap**: a classic heap data structure (a priority queue).
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install list-toolkit
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ 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.
26
+
27
+ ### List
28
+
29
+ `List` implements a circular doubly linked list. Its head is a node with two properties: `next` and `prev`.
30
+ Other nodes of the list are value nodes (`List.ValueNode`) that have a `value` property.
31
+
32
+ ```js
33
+ import List from 'list-toolkit/List.js';
34
+ // or
35
+ // const List = require('list-toolkit/List.js').default; // CJS
36
+
37
+ const list1 = new List();
38
+ const list2 = List.from([1, 2, 3]);
39
+ ```
40
+
41
+ Main operations are:
42
+
43
+ | Method | Description | Complexity |
44
+ |------|-----------|-----|
45
+ | `new List()` | create a new List | *O(1)* |
46
+ | `List.from(values)` | create a new List from an iterable or an array | *O(k)* |
47
+ | `isEmpty` | check if the list is empty | *O(1)* |
48
+ | `front` | get the first element | *O(1)* |
49
+ | `back` | get the last element | *O(1)* |
50
+ | `getLength()` | get the length of the list | *O(n)* |
51
+ | `popFront()` | remove and return the first element | *O(1)* |
52
+ | `popBack()` | remove and return the last element | *O(1)* |
53
+ | `pushFront(value)` | add a new element at the beginning | *O(1)* |
54
+ | `pushBack(value)` | add a new element at the end | *O(1)* |
55
+ | `appendFront(list)` | add a list at the beginning | *O(1)* |
56
+ | `appendBack(list)` | add a list at the end | *O(1)* |
57
+ | `moveToFront(node)` | move a node to the front | *O(1)* |
58
+ | `moveToBack(node)` | move a node to the back | *O(1)* |
59
+ | `clear()` | remove all elements | *O(1)* |
60
+ | `remove(from, to = from)` | remove a range of elements | *O(1)* |
61
+ | `extract(from, to)` | remove and return a range of elements as a new List | *O(1)* |
62
+ | `reverse()` | reverse the list inline | *O(n)* |
63
+ | `sort(compareFn)` | sort the list inline | *O(n * log(n))* |
64
+
65
+ Here and everywhere `n` is a number of elements in the list, while `k` is the size of an argument.
66
+
67
+ Useful aliases are:
68
+
69
+ | Method | Alias of |
70
+ |------|-----------|
71
+ | `pop()` | `popFront()` |
72
+ | `push()` | `pushFront()` |
73
+ | `append()` | `appendBack()` |
74
+
75
+ List can be used as an iterable:
76
+
77
+ ```js
78
+ const list = List.from([1, 2, 3]);
79
+
80
+ for (const value of list) {
81
+ console.log(value);
82
+ }
83
+ ```
84
+
85
+ Additional iterator-related methods are:
86
+
87
+ | Method | Description | Complexity |
88
+ |------|-----------|-----|
89
+ | `getIterable(from, to)` | get an iterable of a range | *O(1)* |
90
+ | `getReverseIterable(from, to)` | get an iterable of a range in reverse order | *O(1)* |
91
+ | `getNodeIterable(from, to)` | get an iterable of a range by nodes | *O(1)* |
92
+ | `getNodeReverseIterable(from, to)` | get an iterable of a range by nodes in reverse order | *O(1)* |
93
+
94
+ Helper methods are:
95
+
96
+ | Method | Description | Complexity |
97
+ |------|-----------|-----|
98
+ | `makeFrom(values)` | (a meta helper) create a new List from an iterable or an array | *O(k)* |
99
+ | `pushValuesFront(values)` | add values at the beginning | *O(k)* |
100
+ | `pushValuesBack(values)` | add values at the end | *O(k)* |
101
+ | `appendValuesFront(values)` | add values as a list at the beginning | *O(k)* |
102
+ | `appendValuesBack(values)` | add values as a list at the end | *O(k)* |
103
+
104
+ Value node (`List.ValueNode`) methods are:
105
+
106
+ | Method | Description | Complexity |
107
+ |------|-----------|-----|
108
+ | `pop()` | remove and return the value | *O(1)* |
109
+ | `addBefore(value)` | add a new value before the current node | *O(1)* |
110
+ | `addAfter(value)` | add a new value after the current node | *O(1)* |
111
+ | `insertBefore(list)` | insert a list before the current node | *O(1)* |
112
+ | `insertAfter(list)` | insert a list after the current node | *O(1)* |
113
+
114
+ Stand-alone methods for nodes (`List.Node`) are:
115
+
116
+ | Method | Description | Complexity |
117
+ |------|-----------|-----|
118
+ | `List.pop(node)` | remove the node from its list and return `{node, list}` | *O(1)* |
119
+ | `List.extract(from, to)` | remove nodes from their list and return the first node of the extracted list | *O(1)* |
120
+ | `List.splice(head1, head2)` | combine two lists and return the first node of the combined list | *O(1)* |
121
+
122
+ ### ListHead
123
+
124
+ `ListHead` implements a circular doubly linked list. Its head is an object with the following properties:
125
+
126
+ * `nextName`: the property name of the next node. It can be a string or a symbol. Default: `"next"`.
127
+ * `prevName`: the property name of the previous node. It can be a string or a symbol. Default: `"prev"`.
128
+ * `head`: the head node of a circular doubly linked list. Default: `{}`.
129
+
130
+ All values of the list are objects that can be used as nodes. When adopted by a list,
131
+ objects are modified in-place by adding the properties defined by `nextName` and `prevName`.
132
+
133
+ While a value can belong to multiple `List` instances, we never know which one it belongs to.
134
+ If we know `nextName` and `prevName`, we can always find its `ListHead` instances from the value,
135
+ and we can manipulate the node without accessing its lists directly.
136
+
137
+ Because lists are circular structures, we can have multiple `ListHead` instances pointing to
138
+ different nodes of the same list. In fact, creating a new `ListHead` instance is cheap and can be done
139
+ when needed using any node/value.
140
+
141
+ An empty `ListHead` instance is created with an empty node that plays the role of a head of the list.
142
+ Alternatively a `ListHead` can be created by pointing to an existing node. In this case,
143
+ any valid node can be used as a head.
144
+
145
+ Some methods use nodes to manipulate the list. In `List`, they are usually found by iterations.
146
+ `ListHead` methods can use named objects for that.
147
+
148
+ ```js
149
+ import ListHead from 'list-toolkit/ListHead.js';
150
+ // or
151
+ // const ListHead = require('list-toolkit/ListHead.js').default; // CJS
152
+
153
+ const list1 = new ListHead();
154
+ const list2 = new ListHead(null, {nextName: Symbol('next'), prevName:Symbol('prev')});
155
+
156
+ const value = {a: 1};
157
+
158
+ list1.push(value);
159
+ list2.push(value);
160
+
161
+ ListHead.pop({nextName: 'next', prevName: 'prev'}, value);
162
+ ListHead.pop(list2, value);
163
+ ```
164
+
165
+ Almost all APIs are the same as for `List` and have the same semantics and complexity.
166
+ The differences are:
167
+
168
+ | Method | Description | Complexity |
169
+ |------|-----------|-----|
170
+ | `new ListHead(head = null, {nextName = 'next', prevName = 'prev'})` | create a new List optionally adopting a list by `head` | *O(1)* |
171
+ | `ListHead.from(values, next, prev)` | create a new List from an iterable | *O(k)* |
172
+ | `ListHead.pop({nextName, prevName}, node)` | remove the node from its list and return `{node, list}` | *O(1)* |
173
+ | `ListHead.extract({nextName, prevName}, from, to)` | remove nodes from their list and return the first node of the extracted list | *O(1)* |
174
+ | `ListHead.splice({nextName, prevName}, head1, head2)` | combine two lists and return the first node of the combined list | *O(1)* |
175
+ | `makeNode()` | return an otherwise empty object with proper `next` and `prev` properties as circular doubly linked list node | *O(1)* |
176
+ | `adopt(node)` | make sure that a node is already a circular doubly linked list or make it so | *O(1)* |
177
+ | `makeList(head)` | return a new list pointing to `head` with the same `nextName` and `prevName` properties as this list | *O(1)* |
178
+ | `clear(true)` | remove all elements and breaks circular references | *O(n)* |
179
+ | `clear()` | remove all elements | *O(1)* |
180
+
181
+ `ListHead` defines a helper class `ListHead.Unsafe` that can be used to create lists without checking
182
+ their `nextName` and `prevName` properties:
183
+
184
+ ```js
185
+ const item1 = {a: 1}, item2 = {b: 2};
186
+ item1.next = item2;
187
+ item2.prev = item1;
188
+ item1.prev = item2;
189
+ item2.next = item1;
190
+
191
+ const list = new ListHead(new ListHead.Unsafe(item1));
192
+ ```
193
+
194
+ ### SList
195
+
196
+ `SList` implements a circular singly linked list. Its head is a node with one property: `next`.
197
+ Other nodes of the list are value nodes (`SList.ValueNode`) that have a `value` property.
198
+
199
+ ```js
200
+ import SList from 'list-toolkit/SList.js';
201
+ // or
202
+ // const SList = require('list-toolkit/SList.js').default; // CJS
203
+
204
+ const list1 = new SList();
205
+ const list2 = SList.from([1, 2, 3]);
206
+ ```
207
+
208
+ `SList` API is modelled on `List`. The main difference is that `SList` is cheaper in general
209
+ but some operations have higher complexity, e.g., any operations that need to access the back of the list.
210
+ `SList` does not implement some operations, e.g., a reverse iterator, due to their complexity.
211
+ If you need these operations frequently, use `List` instead.
212
+
213
+ Note that for efficiency reasons some methods accept a previous node as a pointer to a required node.
214
+
215
+ Main operations are:
216
+
217
+ | Method | Description | Complexity |
218
+ |------|-----------|-----|
219
+ | `new SList()` | create a new List | *O(1)* |
220
+ | `SList.from(values)` | create a new List from an iterable or an array | *O(k)* |
221
+ | `isEmpty` | check if the list is empty | *O(1)* |
222
+ | `front` | get the first element | *O(1)* |
223
+ | `getBack()` | get the last element | *O(n)* |
224
+ | `getLength()` | get the length of the list | *O(n)* |
225
+ | `getPtr()` | get a special pointer value (see below) | *O(1)* |
226
+ | `popFront()` | remove and return the first element | *O(1)* |
227
+ | `popBack()` | remove and return the last element | *O(n)* |
228
+ | `pushFront(value)` | add a new element at the beginning | *O(1)* |
229
+ | `pushBack(value)` | add a new element at the end | *O(n)* |
230
+ | `appendFront(list)` | add a list at the beginning | *O(nk* |
231
+ | `appendBack(list)` | add a list at the end | *O(n + k)* |
232
+ | `moveToFront(node)` | move a node to the front | *O(n)* |
233
+ | `moveToFront(ptr)` | move a node by pointer (see below) to the front | *O(1)* |
234
+ | `moveToBack(node)` | move a node to the back | *O(n)* |
235
+ | `moveToBack(ptr)` | move a node by pointer (see below) to the back | *O(1)* |
236
+ | `clear()` | remove all elements | *O(1)* |
237
+ | `remove(from, to = from)` | remove a range of elements | *O(n)* |
238
+ | `remove(fromPtr, to = from)` | remove a range of elements using a pointer (see below) | *O(1)* |
239
+ | `extract(from, to)` | remove and return a range of elements as a new list | *O(n)* |
240
+ | `extract(fromPtr, to)` | remove and return a range of elements as a new list using a pointer (see below) | *O(1)* |
241
+ | `reverse()` | reverse the list inline | *O(n)* |
242
+ | `sort(compareFn)` | sort the list inline | *O(n * log(n))* |
243
+
244
+ Useful aliases are:
245
+
246
+ | Method | Alias of |
247
+ |------|-----------|
248
+ | `pop()` | `popFront()` |
249
+ | `push()` | `pushFront()` |
250
+ | `append()` | `appendBack()` |
251
+
252
+ List can be used as an iterable:
253
+
254
+ ```js
255
+ const list = SList.from([1, 2, 3]);
256
+
257
+ for (const value of list) {
258
+ console.log(value);
259
+ }
260
+ ```
261
+
262
+ Additional iterator-related methods are:
263
+
264
+ | Method | Description | Complexity |
265
+ |------|-----------|-----|
266
+ | `getIterable(from, to)` | get an iterable of a range | *O(1)* |
267
+ | `getPtrIterable(from, to)` | get an iterable of a range using a pointer (see below) | *O(n)* |
268
+ | `getPtrIterable(fromPtr, to)` | get an iterable of a range using a pointer (see below) | *O(1)* |
269
+
270
+ Helper methods are:
271
+
272
+ | Method | Description | Complexity |
273
+ |------|-----------|-----|
274
+ | `makeFrom(values)` | (a meta helper) create a new list from an iterable or an array | *O(k)* |
275
+ | `pushValuesFront(values)` | add values at the beginning | *O(k)* |
276
+ | `appendValuesFront(values)` | add values as a list at the beginning | *O(k)* |
277
+
278
+ Value node (`SList.ValueNode`) methods are:
279
+
280
+ | Method | Description | Complexity |
281
+ |------|-----------|-----|
282
+ | `addAfter(value)` | add a new value after the current node | *O(1)* |
283
+ | `insertAfter(list)` | insert a list after the current node | *O(k)* |
284
+
285
+ Stand-alone methods for nodes (`SList.Node`) are:
286
+
287
+ | Method | Description | Complexity |
288
+ |------|-----------|-----|
289
+ | `SList.pop(prev)` | remove the node from its list and return `{node, list}` | *O(1)* |
290
+ | `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)* |
291
+ | `SList.splice(prev1, {prev, node})` | combine two lists and return the first node of the combined list | *O(1)* |
292
+ | `SList.getPrev(list, node)` | get a previous node of the given node | *O(n)* |
293
+
294
+ `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.
295
+
296
+ Its methods are:
297
+
298
+ | Method | Description | Complexity |
299
+ |------|-----------|-----|
300
+ | `new SList.SListPtr(list, prev = list)` | create a new pointer | *O(1)* |
301
+ | `list` | the head node | *O(1)* |
302
+ | `prev` | the previous node | *O(1)* |
303
+ | `isHead` | check if the pointer is at the head of the list | *O(1)* |
304
+ | `next()` | move the pointer to the next node | *O(1)* |
305
+ | `clone()` | make a copy of the pointer | *O(1)* |
306
+
307
+ Every method of `SList` that accepts a node can accept a pointer too.
308
+ Using pointers is frequently more efficient than using nodes directly.
309
+
310
+ ### SListHead
311
+
312
+ `SListHead` is modelled after `ListHead` but it is specialized for `SList` instances.
313
+ Just like `ListHead`, it works with naked objects.
314
+
315
+ `SListHead` implements a circular singly linked list. Its head is an object with the following properties:
316
+
317
+ * `nextName`: the property name of the next node. It can be a string or a symbol. Default: `"next"`.
318
+ * `head`: the head node of a circular singly linked list. Default: `{}`.
319
+
320
+ All notes related to `ListHead` apply here too.
321
+
322
+ ```js
323
+ import SListHead from 'list-toolkit/SListHead.js';
324
+ // or
325
+ // const SListHead = require('list-toolkit/SListHead.js').default; // CJS
326
+
327
+ const list1 = new SListHead();
328
+ const list2 = new SListHead(null, {nextName: Symbol('next')});
329
+
330
+ const value = {a: 1};
331
+
332
+ list1.push(value);
333
+ list2.push(value);
334
+
335
+ SListHead.pop({nextName: 'next'}, value);
336
+ SListHead.pop(list2, value);
337
+ ```
338
+
339
+ Almost all APIs are the same as for `SList` and have the same semantics and complexity.
340
+ The differences are:
341
+
342
+ | Method | Description | Complexity |
343
+ |------|-----------|-----|
344
+ | `new SListHead(head = null, next = 'next')` | create a new `SListHead` optionally adopting a list by `head` | *O(1)* |
345
+ | `SListHead.from(values, next)` | create a new `SListHead` from an iterable | *O(k)* |
346
+ | `SListHead.pop({nextName}, prev)` | remove the node by its previous node from its list and return `{node, list}` | *O(1)* |
347
+ | `SListHead.extract({nextName}, prevFrom, nodeTo)` | remove nodes from its list and return `{prev, node}` | *O(1)* |
348
+ | `SListHead.splice({nextName}, prev1, {prev, node})` | combine two lists and return the first node of the combined list | *O(1)* |
349
+ | `SListHead.getPrev({nextName}, list, node)` | get a previous node of the given node | *O(n)* |
350
+ | `make(newHead = null)` | return a new `SListHead` pointing to `newHead` with the same `nextName` property as this list | *O(1)* |
351
+ | `makeFrom(values)` | (a meta helper) create a new `SListHead` from an iterable with the same `nextName` property as this list | *O(k)* |
352
+ | `clear(true)` | remove all elements and breaks circular references | *O(n)* |
353
+ | `clear()` | remove all elements | *O(1)* |
354
+
355
+ `SListHead.SListPtr` is a pointer-like class similar to `SList.SListPtr`
356
+ but specialized for `SListHead` instances. It has the same API and the same semantics.
357
+
358
+ ### Cache
359
+
360
+ This is the class that is used for caching values using simple unique keys (numbers, strings or symbols).
361
+ It defines a capacity and when it is full, it removes the least recently used value.
362
+
363
+ Internally it is based on `List`.
364
+
365
+ ```js
366
+ import Cache from 'list-toolkit/Cache.js';
367
+ // or
368
+ // const Cache = require('list-toolkit/Cache.js').default; // CJS
369
+
370
+ const cache = new Cache(1000);
371
+
372
+ const processRequest = key => {
373
+ let data = cache.get(key);
374
+ if (!data) {
375
+ data = new DataObject();
376
+ cache.register(key, data);
377
+ }
378
+ // continue processing
379
+ };
380
+ ```
381
+
382
+ The main operations are:
383
+
384
+ | Method | Description | Complexity |
385
+ |------|-----------|-----|
386
+ | `new Cache(capacity = 10)` | create a new cache | *O(1)* |
387
+ | `size` | get the number of values in the cache | *O(1)* |
388
+ | `capacity` | get the capacity of the cache | *O(1)* |
389
+ | `find(key)` | find a value by its key and return it or `undefined` | *O(1)* |
390
+ | `remove(key)` | remove a value by its key | *O(1)* |
391
+ | `register(key, value)` | register a value by its key | *O(1)* |
392
+ | `clear()` | clear the cache | *O(1)* |
393
+ | `getReverseIterable()` | get an iterable of the cache in reverse order | *O(1)* |
394
+
395
+ A `Cache` instance is iterable, so it can be used in loops:
396
+
397
+ ```js
398
+ for (const value of cache) {
399
+ console.log(value);
400
+ }
401
+ ```
402
+
403
+ It iterates from the most recently used to the least recently used.
404
+
405
+ ### MinHeap
406
+
407
+ `MinHeap` is a classic data structure: a priority queue. From the [Wikipedia article](https://en.wikipedia.org/wiki/Heap_(data_structure)):
408
+
409
+ > 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.
410
+
411
+ ```js
412
+ import MinHeap from 'list-toolkit/MinHeap.js';
413
+ // or
414
+ // const MinHeap = require('list-toolkit/MinHeap.js').default; // CJS
415
+
416
+ const heap = new MinHeap({less: (a, b) => a.priority > b.priority});
417
+
418
+ const enqueueTask = (priority, task) => {
419
+ heap.push({priority, task});
420
+ };
421
+
422
+ const processTasks = () => {
423
+ while (!heap.isEmpty) {
424
+ const {priority, task} = heap.pop();
425
+ // process the highest priority task
426
+ }
427
+ }
428
+ ```
429
+
430
+ The main operations are:
431
+
432
+ | Method | Description | Complexity |
433
+ |------|-----------|-----|
434
+ | `new MinHeap({less = (a, b) => a < b, equal: (a, b) => a === b}, ...args)` | create a new heap optionally from iterables or other heaps | *O(k)* |
435
+ | `length` | get the number of elements in the heap | *O(1)* |
436
+ | `isEmpty` | check if the heap is empty | *O(1)* |
437
+ | `top` | get the top element | *O(1)* |
438
+ | `clear()` | clear the heap | *O(1)* |
439
+ | `pop()` | remove and return the top element | *O(log(n))* |
440
+ | `push(value)` | add new element | *O(log(n))* |
441
+ | `pushPop(value)` | add new element and then return the top element | *O(log(n))* |
442
+ | `replaceTop(value)` | return the top element and then add new element | *O(log(n))* |
443
+ | `releaseSorted()` | remove all elements and return them as an array in the reverse sorted order (the heap will be cleared) | *O(n)* |
444
+ | `merge(...args)` | add elements from iterables or other heaps | *O(k)* |
445
+ | `make(...args)` | return a new heap with the same options | *O(1)* |
446
+ | `clone()` | return a copy of the heap | *O(n)* |
447
+
448
+ `MinHeap` provides a number of static methods to create heaps on arrays (used internally):
449
+
450
+ | Method | Description | Complexity |
451
+ |------|-----------|-----|
452
+ | `MinHeap.build(array, less = (a, b) => a < b)` | create a new heap from an array | *O(n)* |
453
+ | `MinHeap.pop(heapArray, less = (a, b) => a < b)` | remove and return the top element | *O(log(n))* |
454
+ | `MinHeap.push(heapArray, value, less = (a, b) => a < b)` | add new element | *O(log(n))* |
455
+ | `MinHeap.pushPop(heapArray, value, less = (a, b) => a < b)` | add new element and then return the top element | *O(log(n))* |
456
+ | `MinHeap.replaceTop(heapArray, value, less = (a, b) => a < b)` | return the top element and then add new element | *O(log(n))* |
457
+ | `MinHeap.sort(heapArray, less = (a, b) => a < b)` | sort an array in place | *O(n * log(n))* |
458
+
459
+ ## License
460
+
461
+ BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.
462
+
463
+ ## Release History
464
+
465
+ * 1.0.1 *Initial release.*
package/cjs/Cache.js ADDED
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = 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
+ var _default = exports.default = Cache;