data-structure-typed 1.48.5 → 1.48.7
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/CHANGELOG.md +1 -1
- package/README.md +109 -59
- package/README_zh-CN.md +1028 -0
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +204 -174
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -16
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +24 -19
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +35 -11
- package/dist/cjs/data-structures/binary-tree/bst.js +58 -39
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +9 -9
- package/dist/cjs/data-structures/queue/deque.js +9 -9
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +24 -19
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +35 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +58 -39
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +4 -4
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/deque.d.ts +9 -9
- package/dist/mjs/data-structures/queue/deque.js +9 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +99 -78
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +31 -20
- package/src/data-structures/binary-tree/bst.ts +73 -46
- package/src/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +4 -4
- package/src/data-structures/queue/deque.ts +10 -10
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/performance/data-structures/hash/hash-map.test.ts +8 -8
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +12 -1
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/performance/data-structures/queue/deque.test.ts +27 -15
- package/test/performance/data-structures/queue/queue.test.ts +27 -4
- package/test/performance/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +29 -29
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +15 -15
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.48.
|
|
4
|
-
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree,
|
|
3
|
+
"version": "1.48.7",
|
|
4
|
+
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
7
7
|
"types": "dist/mjs/index.d.ts",
|
|
@@ -99,7 +99,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
99
99
|
/**
|
|
100
100
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
101
101
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
102
|
-
*
|
|
102
|
+
*
|
|
103
103
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
104
104
|
* a new node.
|
|
105
105
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -235,7 +235,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
235
235
|
/**
|
|
236
236
|
* Time Complexity O(log n) - O(n)
|
|
237
237
|
* Space Complexity O(1)
|
|
238
|
-
*
|
|
238
|
+
*
|
|
239
239
|
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
240
240
|
* existing node with the same key.
|
|
241
241
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
@@ -248,6 +248,9 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
248
248
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
249
249
|
if (newNode === undefined) return;
|
|
250
250
|
|
|
251
|
+
// TODO There are still some problems with the way duplicate nodes are handled
|
|
252
|
+
if (newNode !== null && this.has(newNode.key)) return undefined;
|
|
253
|
+
|
|
251
254
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
252
255
|
const queue = new Queue<N>([root]);
|
|
253
256
|
while (queue.size > 0) {
|
|
@@ -288,38 +291,46 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
288
291
|
* Time Complexity: O(k log n) - O(k * n)
|
|
289
292
|
* Space Complexity: O(1)
|
|
290
293
|
*
|
|
291
|
-
* The
|
|
292
|
-
*
|
|
293
|
-
* @param nodes -
|
|
294
|
-
*
|
|
295
|
-
* @returns The function `addMany` returns an array of
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[] {
|
|
294
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
295
|
+
* adds each node with its corresponding value to the data structure.
|
|
296
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
297
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
298
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
299
|
+
*/
|
|
300
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
|
299
301
|
// TODO not sure addMany not be run multi times
|
|
300
302
|
const inserted: (N | null | undefined)[] = [];
|
|
303
|
+
|
|
304
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
305
|
+
if (values) {
|
|
306
|
+
valuesIterator = values[Symbol.iterator]();
|
|
307
|
+
}
|
|
308
|
+
|
|
301
309
|
for (const kne of nodes) {
|
|
302
|
-
|
|
310
|
+
let value: V | undefined | null = undefined;
|
|
311
|
+
|
|
312
|
+
if (valuesIterator) {
|
|
313
|
+
const valueResult = valuesIterator.next();
|
|
314
|
+
if (!valueResult.done) {
|
|
315
|
+
value = valueResult.value;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
inserted.push(this.add(kne, value));
|
|
303
320
|
}
|
|
321
|
+
|
|
304
322
|
return inserted;
|
|
305
323
|
}
|
|
306
324
|
|
|
307
|
-
/**
|
|
308
|
-
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
309
|
-
* Space Complexity: O(1)
|
|
310
|
-
*/
|
|
311
325
|
|
|
312
326
|
/**
|
|
313
327
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
314
328
|
* Space Complexity: O(1)
|
|
315
|
-
*
|
|
316
|
-
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
317
|
-
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
318
|
-
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
319
329
|
*/
|
|
320
|
-
|
|
330
|
+
|
|
331
|
+
refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
|
|
321
332
|
this.clear();
|
|
322
|
-
this.addMany(nodesOrKeysOrEntries);
|
|
333
|
+
this.addMany(nodesOrKeysOrEntries, values);
|
|
323
334
|
}
|
|
324
335
|
|
|
325
336
|
/**
|
|
@@ -192,7 +192,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
192
192
|
/**
|
|
193
193
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
194
194
|
* Space Complexity: O(1) - Constant space is used.
|
|
195
|
-
*
|
|
195
|
+
*
|
|
196
196
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
197
197
|
* or inserting a new node if the key is unique.
|
|
198
198
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
@@ -256,31 +256,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
256
256
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
257
257
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
258
258
|
*
|
|
259
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
260
|
-
*
|
|
261
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
262
|
-
* binary tree.
|
|
263
|
-
* @param [
|
|
264
|
-
*
|
|
259
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
260
|
+
* balancing the tree after each addition.
|
|
261
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
262
|
+
* the binary tree.
|
|
263
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
264
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
265
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
266
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
267
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
268
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
269
|
+
* in the order they appear in the input.
|
|
265
270
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
266
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
271
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
272
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
273
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
270
274
|
*/
|
|
271
275
|
override addMany(
|
|
272
276
|
keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>,
|
|
277
|
+
values?: Iterable<V | undefined>,
|
|
273
278
|
isBalanceAdd = true,
|
|
274
279
|
iterationType = this.iterationType
|
|
275
280
|
): (N | undefined)[] {
|
|
276
|
-
const inserted: (N | undefined)[] = []
|
|
281
|
+
const inserted: (N | undefined)[] = [];
|
|
282
|
+
|
|
283
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
284
|
+
|
|
285
|
+
if (values) {
|
|
286
|
+
valuesIterator = values[Symbol.iterator]();
|
|
287
|
+
}
|
|
288
|
+
|
|
277
289
|
if (!isBalanceAdd) {
|
|
278
290
|
for (const kve of keysOrNodesOrEntries) {
|
|
279
|
-
const
|
|
291
|
+
const value = valuesIterator?.next().value;
|
|
292
|
+
const nn = this.add(kve, value);
|
|
280
293
|
inserted.push(nn);
|
|
281
294
|
}
|
|
282
295
|
return inserted;
|
|
283
296
|
}
|
|
297
|
+
|
|
284
298
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
285
299
|
|
|
286
300
|
const isRealBTNExemplar = (kve: BTNodeExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
@@ -292,22 +306,20 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
292
306
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
293
307
|
}
|
|
294
308
|
|
|
295
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
296
309
|
let sorted: BTNodePureExemplar<K, V, N>[] = [];
|
|
297
310
|
|
|
298
311
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
299
312
|
let aR: number, bR: number;
|
|
300
|
-
if (this.isEntry(a)) aR = this.extractor(a[0])
|
|
301
|
-
else if (this.isRealNode(a)) aR = this.extractor(a.key)
|
|
313
|
+
if (this.isEntry(a)) aR = this.extractor(a[0]);
|
|
314
|
+
else if (this.isRealNode(a)) aR = this.extractor(a.key);
|
|
302
315
|
else aR = this.extractor(a);
|
|
303
316
|
|
|
304
|
-
if (this.isEntry(b)) bR = this.extractor(b[0])
|
|
305
|
-
else if (this.isRealNode(b)) bR = this.extractor(b.key)
|
|
317
|
+
if (this.isEntry(b)) bR = this.extractor(b[0]);
|
|
318
|
+
else if (this.isRealNode(b)) bR = this.extractor(b.key);
|
|
306
319
|
else bR = this.extractor(b);
|
|
307
320
|
|
|
308
321
|
return aR - bR;
|
|
309
|
-
})
|
|
310
|
-
|
|
322
|
+
});
|
|
311
323
|
|
|
312
324
|
const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
|
|
313
325
|
if (arr.length === 0) return;
|
|
@@ -318,6 +330,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
318
330
|
_dfs(arr.slice(0, mid));
|
|
319
331
|
_dfs(arr.slice(mid + 1));
|
|
320
332
|
};
|
|
333
|
+
|
|
321
334
|
const _iterate = () => {
|
|
322
335
|
const n = sorted.length;
|
|
323
336
|
const stack: [[number, number]] = [[0, n - 1]];
|
|
@@ -335,6 +348,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
335
348
|
}
|
|
336
349
|
}
|
|
337
350
|
};
|
|
351
|
+
|
|
338
352
|
if (iterationType === IterationType.RECURSIVE) {
|
|
339
353
|
_dfs(sorted);
|
|
340
354
|
} else {
|
|
@@ -344,31 +358,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
344
358
|
return inserted;
|
|
345
359
|
}
|
|
346
360
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
364
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
365
|
+
*/
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
369
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
370
|
+
*
|
|
371
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
372
|
+
* leftmost node if the comparison result is greater than.
|
|
373
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
374
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
375
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
376
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
377
|
+
* be performed. It can have one of the following values:
|
|
378
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
379
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
380
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
381
|
+
*/
|
|
382
|
+
lastKey(beginRoot: BSTNodeKeyOrNode<K, N> = this.root): K | undefined {
|
|
383
|
+
let current = this.ensureNode(beginRoot);
|
|
384
|
+
if (!current) return undefined;
|
|
385
|
+
|
|
386
|
+
if (this._variant === BSTVariant.MIN) {
|
|
387
|
+
// For BSTVariant.MIN, find the rightmost node
|
|
388
|
+
while (current.right !== undefined) {
|
|
389
|
+
current = current.right;
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
// For BSTVariant.MAX, find the leftmost node
|
|
393
|
+
while (current.left !== undefined) {
|
|
394
|
+
current = current.left;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return current.key;
|
|
398
|
+
}
|
|
399
|
+
|
|
372
400
|
|
|
373
401
|
/**
|
|
374
402
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -621,7 +649,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
621
649
|
if (l <= r) {
|
|
622
650
|
const m = l + Math.floor((r - l) / 2);
|
|
623
651
|
const midNode = sorted[m];
|
|
624
|
-
debugger;
|
|
625
652
|
this.add([midNode.key, midNode.value]);
|
|
626
653
|
stack.push([m + 1, r]);
|
|
627
654
|
stack.push([l, m - 1]);
|
|
@@ -153,7 +153,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
153
153
|
/**
|
|
154
154
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
155
155
|
* Space Complexity: O(1)
|
|
156
|
-
*
|
|
156
|
+
*
|
|
157
157
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
158
158
|
* color changes to maintain the red-black tree properties.
|
|
159
159
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -126,7 +126,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
126
126
|
/**
|
|
127
127
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
128
128
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
129
|
-
*
|
|
129
|
+
*
|
|
130
130
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
131
131
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
132
132
|
* entry. It represents the key, node, or entry that you want to add to the binary tree.
|
|
@@ -625,7 +625,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
625
625
|
protected* _getIterator() {
|
|
626
626
|
let node = this._head;
|
|
627
627
|
while (node !== this._sentinel) {
|
|
628
|
-
yield
|
|
628
|
+
yield [node.key, node.value] as [K, V];
|
|
629
629
|
node = node.next;
|
|
630
630
|
}
|
|
631
631
|
}
|
|
@@ -162,11 +162,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
162
162
|
* Time Complexity: O(1)
|
|
163
163
|
* Space Complexity: O(1)
|
|
164
164
|
*
|
|
165
|
-
* The `
|
|
165
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
166
166
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
167
167
|
* list is empty, it returns undefined.
|
|
168
168
|
*/
|
|
169
|
-
|
|
169
|
+
pollLast(): E | undefined {
|
|
170
170
|
return this.pop();
|
|
171
171
|
}
|
|
172
172
|
|
|
@@ -206,11 +206,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
206
206
|
* Time Complexity: O(1)
|
|
207
207
|
* Space Complexity: O(1)
|
|
208
208
|
*
|
|
209
|
-
* The `
|
|
209
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
210
210
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
211
211
|
* list.
|
|
212
212
|
*/
|
|
213
|
-
|
|
213
|
+
pollFirst(): E | undefined {
|
|
214
214
|
return this.shift();
|
|
215
215
|
}
|
|
216
216
|
|
|
@@ -164,12 +164,12 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
164
164
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
165
165
|
* Space Complexity: O(1) - Constant space.
|
|
166
166
|
*
|
|
167
|
-
* The `
|
|
167
|
+
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
168
168
|
* pointers accordingly.
|
|
169
169
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
170
170
|
* the linked list is empty, it returns `undefined`.
|
|
171
171
|
*/
|
|
172
|
-
|
|
172
|
+
pollLast(): E | undefined {
|
|
173
173
|
return this.pop();
|
|
174
174
|
}
|
|
175
175
|
|
|
@@ -202,10 +202,10 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
202
202
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
203
203
|
* Space Complexity: O(1) - Constant space.
|
|
204
204
|
*
|
|
205
|
-
* The `
|
|
205
|
+
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
|
|
206
206
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
207
207
|
*/
|
|
208
|
-
|
|
208
|
+
pollFirst(): E | undefined {
|
|
209
209
|
return this.shift();
|
|
210
210
|
}
|
|
211
211
|
|
|
@@ -120,10 +120,10 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
120
120
|
* Time Complexity: O(1) - Removes the last element.
|
|
121
121
|
* Space Complexity: O(1) - Operates in-place.
|
|
122
122
|
*
|
|
123
|
-
* The function "
|
|
123
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
124
124
|
* @returns The last element of the array is being returned.
|
|
125
125
|
*/
|
|
126
|
-
|
|
126
|
+
pollLast(): E | undefined {
|
|
127
127
|
return this.pop();
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -143,11 +143,11 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
143
143
|
* Time Complexity: O(1) - Removes the first element.
|
|
144
144
|
* Space Complexity: O(1) - In-place operation.
|
|
145
145
|
*
|
|
146
|
-
* The function "
|
|
147
|
-
* @returns The method `
|
|
146
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
147
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
148
148
|
* from the beginning. If the array is empty, it will return `undefined`.
|
|
149
149
|
*/
|
|
150
|
-
|
|
150
|
+
pollFirst(): E | undefined {
|
|
151
151
|
return this.shift();
|
|
152
152
|
}
|
|
153
153
|
|
|
@@ -947,10 +947,10 @@ export class ObjectDeque<E = number> {
|
|
|
947
947
|
* Time Complexity: O(1)
|
|
948
948
|
* Space Complexity: O(1)
|
|
949
949
|
*
|
|
950
|
-
* The function `
|
|
950
|
+
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
951
951
|
* @returns The element of the first element in the data structure.
|
|
952
952
|
*/
|
|
953
|
-
|
|
953
|
+
pollFirst() {
|
|
954
954
|
if (!this.size) return;
|
|
955
955
|
const element = this.getFirst();
|
|
956
956
|
delete this.nodes[this.first];
|
|
@@ -984,10 +984,10 @@ export class ObjectDeque<E = number> {
|
|
|
984
984
|
* Time Complexity: O(1)
|
|
985
985
|
* Space Complexity: O(1)
|
|
986
986
|
*
|
|
987
|
-
* The `
|
|
987
|
+
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
988
988
|
* @returns The element that was removed from the data structure.
|
|
989
989
|
*/
|
|
990
|
-
|
|
990
|
+
pollLast() {
|
|
991
991
|
if (!this.size) return;
|
|
992
992
|
const element = this.getLast();
|
|
993
993
|
delete this.nodes[this.last];
|
|
@@ -1039,4 +1039,4 @@ export class ObjectDeque<E = number> {
|
|
|
1039
1039
|
isEmpty() {
|
|
1040
1040
|
return this.size <= 0;
|
|
1041
1041
|
}
|
|
1042
|
-
}
|
|
1042
|
+
}
|
|
@@ -15,7 +15,7 @@ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V,
|
|
|
15
15
|
|
|
16
16
|
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
17
17
|
|
|
18
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N
|
|
18
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
19
19
|
|
|
20
20
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
21
21
|
}
|
|
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
|
|
|
7
7
|
|
|
8
8
|
export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
9
9
|
|
|
10
|
-
export type RBTreeOptions<K> = BSTOptions<K> & {};
|
|
10
|
+
export type RBTreeOptions<K> = BSTOptions<K> & {};
|
|
@@ -18,7 +18,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
|
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
if (isCompetitor) {
|
|
21
|
-
suite.add(
|
|
21
|
+
suite.add(`CPT ${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
|
|
22
22
|
for (let i = 0; i < arr.length; i++) {
|
|
23
23
|
cOrderedMap.setElement(arr[i], arr[i]);
|
|
24
24
|
}
|
|
@@ -16,7 +16,7 @@ suite.add(`${MILLION.toLocaleString()} set`, () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
if (isCompetitor) {
|
|
19
|
-
suite.add(
|
|
19
|
+
suite.add(`CPT ${MILLION.toLocaleString()} set`, () => {
|
|
20
20
|
const hm = new CHashMap<number, number>();
|
|
21
21
|
|
|
22
22
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -25,13 +25,13 @@ if (isCompetitor) {
|
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
suite.add(
|
|
28
|
+
suite.add(`Native Map ${MILLION.toLocaleString()} set`, () => {
|
|
29
29
|
const hm = new Map<number, number>();
|
|
30
30
|
|
|
31
31
|
for (let i = 0; i < MILLION; i++) hm.set(i, i);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
suite.add(
|
|
34
|
+
suite.add(`Native Set ${MILLION.toLocaleString()} add`, () => {
|
|
35
35
|
const hs = new Set<number>();
|
|
36
36
|
|
|
37
37
|
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
@@ -49,7 +49,7 @@ suite.add(`${MILLION.toLocaleString()} set & get`, () => {
|
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
if (isCompetitor) {
|
|
52
|
-
suite.add(
|
|
52
|
+
suite.add(`CPT ${MILLION.toLocaleString()} set & get`, () => {
|
|
53
53
|
const hm = new CHashMap<number, number>();
|
|
54
54
|
|
|
55
55
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -61,7 +61,7 @@ if (isCompetitor) {
|
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
suite.add(
|
|
64
|
+
suite.add(`Native Map ${MILLION.toLocaleString()} set & get`, () => {
|
|
65
65
|
const hm = new Map<number, number>();
|
|
66
66
|
|
|
67
67
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -72,7 +72,7 @@ suite.add(`${MILLION.toLocaleString()} Map set & get`, () => {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
suite.add(
|
|
75
|
+
suite.add(`Native Set ${MILLION.toLocaleString()} add & has`, () => {
|
|
76
76
|
const hs = new Set<number>();
|
|
77
77
|
|
|
78
78
|
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
@@ -94,7 +94,7 @@ suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
|
94
94
|
}
|
|
95
95
|
});
|
|
96
96
|
|
|
97
|
-
suite.add(
|
|
97
|
+
suite.add(`Native Map ${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
98
98
|
const hm = new Map<[number, number], number>();
|
|
99
99
|
const objs: [number, number][] = [];
|
|
100
100
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -107,7 +107,7 @@ suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
|
|
|
107
107
|
}
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
-
suite.add(
|
|
110
|
+
suite.add(`Native Set ${MILLION.toLocaleString()} ObjKey add & has`, () => {
|
|
111
111
|
const hs = new Set<[number, number]>();
|
|
112
112
|
const objs: [number, number][] = [];
|
|
113
113
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -17,7 +17,7 @@ suite.add(`${LINEAR.toLocaleString()} push`, () => {
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
if (isCompetitor) {
|
|
20
|
-
suite.add(
|
|
20
|
+
suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
|
|
21
21
|
const list = new CLinkedList<number>();
|
|
22
22
|
|
|
23
23
|
for (let i = 0; i < LINEAR; i++) {
|
|
@@ -35,7 +35,7 @@ suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
|
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
if (isCompetitor) {
|
|
38
|
-
suite.add(
|
|
38
|
+
suite.add(`CPT ${LINEAR.toLocaleString()} unshift`, () => {
|
|
39
39
|
const list = new CLinkedList<number>();
|
|
40
40
|
|
|
41
41
|
for (let i = 0; i < LINEAR; i++) {
|
|
@@ -3,9 +3,20 @@ import * as Benchmark from 'benchmark';
|
|
|
3
3
|
import { magnitude } from '../../../utils';
|
|
4
4
|
|
|
5
5
|
const suite = new Benchmark.Suite();
|
|
6
|
-
const { TEN_THOUSAND } = magnitude;
|
|
6
|
+
const { MILLION, TEN_THOUSAND } = magnitude;
|
|
7
7
|
|
|
8
8
|
suite
|
|
9
|
+
.add(`${MILLION.toLocaleString()} push & shift`, () => {
|
|
10
|
+
const list = new SinglyLinkedList<number>();
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < MILLION; i++) {
|
|
13
|
+
list.push(i);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < MILLION; i++) {
|
|
17
|
+
list.shift();
|
|
18
|
+
}
|
|
19
|
+
})
|
|
9
20
|
.add(`${TEN_THOUSAND.toLocaleString()} push & pop`, () => {
|
|
10
21
|
const list = new SinglyLinkedList<number>();
|
|
11
22
|
|
|
@@ -19,7 +19,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (isCompetitor) {
|
|
22
|
-
suite.add(
|
|
22
|
+
suite.add(`CPT ${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
23
23
|
const pq = new CPriorityQueue<number>();
|
|
24
24
|
|
|
25
25
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|