data-structure-typed 1.46.4 → 1.46.6
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 +65 -79
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +428 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +29 -46
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +10 -5
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js +1 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/types/{helpers.js → common.js} +1 -1
- package/dist/cjs/types/common.js.map +1 -0
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/index.js +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +29 -46
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +10 -5
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.js +1 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/types/index.d.ts +1 -1
- package/dist/mjs/types/index.js +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +33 -51
- package/dist/umd/data-structure-typed.min.js +10 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +33 -46
- package/src/data-structures/binary-tree/rb-tree.ts +9 -4
- package/src/data-structures/hash/hash-map.ts +4 -5
- package/src/types/data-structures/hash/hash-map.ts +1 -1
- package/src/types/index.ts +1 -1
- package/src/utils/utils.ts +1 -1
- package/test/integration/index.html +32 -23
- package/test/performance/data-structures/{comparison.test.ts → comparison/comparison.test.ts} +34 -28
- package/test/performance/data-structures/hash/hash-map.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -0
- package/tsup.config.js +1 -1
- package/dist/cjs/types/helpers.js.map +0 -1
- /package/dist/cjs/types/{helpers.d.ts → common.d.ts} +0 -0
- /package/dist/mjs/types/{helpers.d.ts → common.d.ts} +0 -0
- /package/dist/mjs/types/{helpers.js → common.js} +0 -0
- /package/src/types/{helpers.ts → common.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.46.
|
|
3
|
+
"version": "1.46.6",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -1727,7 +1727,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1727
1727
|
|
|
1728
1728
|
/**
|
|
1729
1729
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1730
|
-
* @param {N | null | undefined}
|
|
1730
|
+
* @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
|
|
1731
1731
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1732
1732
|
* following types:
|
|
1733
1733
|
*/
|
|
@@ -1736,61 +1736,48 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1736
1736
|
if (!beginRoot) return;
|
|
1737
1737
|
|
|
1738
1738
|
const display = (root: N | null | undefined): void => {
|
|
1739
|
-
const [lines, , ,] = _displayAux(root);
|
|
1739
|
+
const [lines, , ,] = this._displayAux(root);
|
|
1740
1740
|
for (const line of lines) {
|
|
1741
1741
|
console.log(line);
|
|
1742
1742
|
}
|
|
1743
1743
|
};
|
|
1744
1744
|
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
return [[], 0, 0, 0];
|
|
1748
|
-
}
|
|
1745
|
+
display(beginRoot);
|
|
1746
|
+
}
|
|
1749
1747
|
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
const middle = Math.floor(width / 2);
|
|
1755
|
-
return [[line], width, height, middle];
|
|
1756
|
-
}
|
|
1748
|
+
protected _displayAux(node: N | null | undefined): [string[], number, number, number] {
|
|
1749
|
+
if (!node) {
|
|
1750
|
+
return [['─'], 1, 0, 0];
|
|
1751
|
+
}
|
|
1757
1752
|
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
const s = `${node.key}`;
|
|
1761
|
-
const u = s.length;
|
|
1762
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1763
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1764
|
-
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1765
|
-
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1766
|
-
}
|
|
1753
|
+
const line = node.key.toString();
|
|
1754
|
+
const width = line.length;
|
|
1767
1755
|
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
const x = s.length;
|
|
1772
|
-
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1773
|
-
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1774
|
-
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1775
|
-
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1776
|
-
}
|
|
1756
|
+
if (!node.left && !node.right) {
|
|
1757
|
+
return [[line], width, 1, Math.floor(width / 2)];
|
|
1758
|
+
}
|
|
1777
1759
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
const s = `${node.key}`;
|
|
1781
|
-
const u = s.length;
|
|
1782
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1783
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1784
|
-
if (p < q) {
|
|
1785
|
-
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1786
|
-
} else if (q < p) {
|
|
1787
|
-
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1788
|
-
}
|
|
1789
|
-
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1790
|
-
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1791
|
-
};
|
|
1760
|
+
const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[''], 0, 0, 0];
|
|
1761
|
+
const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[''], 0, 0, 0];
|
|
1792
1762
|
|
|
1793
|
-
|
|
1763
|
+
const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
|
|
1764
|
+
+ '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
|
|
1765
|
+
+ line
|
|
1766
|
+
+ '_'.repeat(Math.max(0, rightMiddle))
|
|
1767
|
+
+ ' '.repeat(Math.max(0, rightWidth - rightMiddle));
|
|
1768
|
+
|
|
1769
|
+
const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
|
|
1770
|
+
+ ' '.repeat(width)
|
|
1771
|
+
+ (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
|
|
1772
|
+
|
|
1773
|
+
const mergedLines = [firstLine, secondLine];
|
|
1774
|
+
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
1775
|
+
const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
|
|
1776
|
+
const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
|
|
1777
|
+
mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
1794
1781
|
}
|
|
1795
1782
|
|
|
1796
1783
|
protected _defaultOneParamCallback = (node: N) => node.key;
|
|
@@ -103,11 +103,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
103
103
|
|
|
104
104
|
while (x !== this.NIL) {
|
|
105
105
|
y = x;
|
|
106
|
-
if (x
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
if (x) {
|
|
107
|
+
if (node.key < x.key) {
|
|
108
|
+
x = x.left;
|
|
109
|
+
} else if (node.key > x.key) {
|
|
110
|
+
x = x?.right;
|
|
111
|
+
} else {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
110
114
|
}
|
|
115
|
+
|
|
111
116
|
}
|
|
112
117
|
|
|
113
118
|
node.parent = y;
|
|
@@ -12,12 +12,12 @@ import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
|
12
12
|
export class HashMap<K = any, V = any> {
|
|
13
13
|
|
|
14
14
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
15
|
-
protected _objMap = new WeakMap<
|
|
15
|
+
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
|
|
16
16
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
17
17
|
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
18
18
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
19
19
|
protected _hashFn: (key: K) => string;
|
|
20
|
-
protected _objHashFn: (key: K) =>
|
|
20
|
+
protected _objHashFn: (key: K) => object;
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
@@ -27,7 +27,7 @@ export class HashMap<K = any, V = any> {
|
|
|
27
27
|
constructor(options: HashMapOptions<K, V> = {
|
|
28
28
|
elements: [],
|
|
29
29
|
hashFn: (key: K) => String(key),
|
|
30
|
-
objHashFn: (key: K) => (<
|
|
30
|
+
objHashFn: (key: K) => (<object>key)
|
|
31
31
|
}) {
|
|
32
32
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
33
33
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
@@ -114,8 +114,7 @@ export class HashMap<K = any, V = any> {
|
|
|
114
114
|
let node;
|
|
115
115
|
|
|
116
116
|
if (isWeakKey(key)) {
|
|
117
|
-
|
|
118
|
-
const hash = key;
|
|
117
|
+
const hash = this._objHashFn(key);
|
|
119
118
|
node = this._objMap.get(hash);
|
|
120
119
|
|
|
121
120
|
if (node) {
|
package/src/types/index.ts
CHANGED
package/src/utils/utils.ts
CHANGED
|
@@ -93,7 +93,7 @@ export const throwRangeError = (message = 'The value is off-limits.'): void => {
|
|
|
93
93
|
throw new RangeError(message);
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
export const isWeakKey = (input: unknown): input is
|
|
96
|
+
export const isWeakKey = (input: unknown): input is object => {
|
|
97
97
|
const inputType = typeof input;
|
|
98
98
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
99
99
|
};
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset='UTF-8'>
|
|
5
5
|
<title>CDN Test</title>
|
|
6
|
-
|
|
7
|
-
<script src=
|
|
6
|
+
<script src="../../dist/umd/data-structure-typed.min.js"></script>
|
|
7
|
+
<!-- <script src="../../dist/umd/data-structure-typed.js"></script>-->
|
|
8
|
+
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>-->
|
|
9
|
+
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.42.2/dist/umd/data-structure-typed.min.js'></script>-->
|
|
10
|
+
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.43.3/dist/umd/data-structure-typed.min.js'></script>-->
|
|
11
|
+
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
|
|
12
|
+
|
|
8
13
|
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
|
|
9
14
|
</head>
|
|
10
15
|
<body>
|
|
@@ -19,17 +24,20 @@
|
|
|
19
24
|
|
|
20
25
|
try {
|
|
21
26
|
const queue = new Queue();
|
|
22
|
-
|
|
27
|
+
const n = 100000;
|
|
28
|
+
const startEn = performance.now();
|
|
29
|
+
for (let i = 0; i < n; i++) {
|
|
23
30
|
queue.enqueue(i);
|
|
24
31
|
}
|
|
32
|
+
console.log(`Queue ${n} enqueue `, performance.now() - startEn);
|
|
25
33
|
let last = 0;
|
|
26
34
|
const startTime = performance.now();
|
|
27
35
|
|
|
28
|
-
for (let i = 0; i <
|
|
36
|
+
for (let i = 0; i < n; i++) {
|
|
29
37
|
last = queue.dequeue();
|
|
30
38
|
}
|
|
31
39
|
|
|
32
|
-
console.log(performance.now() - startTime);
|
|
40
|
+
console.log(`Queue ${n} dequeue `, performance.now() - startTime);
|
|
33
41
|
|
|
34
42
|
} catch (e) {
|
|
35
43
|
console.error(e);
|
|
@@ -77,21 +85,22 @@
|
|
|
77
85
|
|
|
78
86
|
try {
|
|
79
87
|
const { OrderedMap } = sdsl;
|
|
80
|
-
const { RedBlackTree } = dataStructureTyped;
|
|
81
|
-
const
|
|
82
|
-
const
|
|
88
|
+
const { RedBlackTree, AVLTree } = dataStructureTyped;
|
|
89
|
+
const cRBTree = new OrderedMap();
|
|
90
|
+
const rbTree = new RedBlackTree();
|
|
83
91
|
const tS = performance.now();
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
const n = 100000;
|
|
93
|
+
for (let i = 1; i < n; i++) {
|
|
94
|
+
rbTree.add(i, i);
|
|
86
95
|
}
|
|
87
|
-
console.log(performance.now() - tS);
|
|
88
|
-
console.log(
|
|
96
|
+
console.log(`RedBlackTree ${n} add`, performance.now() - tS);
|
|
97
|
+
console.log(rbTree.size);
|
|
89
98
|
const cS = performance.now();
|
|
90
|
-
for (let i = 1; i <
|
|
91
|
-
|
|
99
|
+
for (let i = 1; i < 100000; i++) {
|
|
100
|
+
cRBTree.setElement(i, i);
|
|
92
101
|
}
|
|
93
|
-
console.log(performance.now() - cS);
|
|
94
|
-
console.log(
|
|
102
|
+
console.log(`CRedBlackTree ${n} add`, performance.now() - cS);
|
|
103
|
+
console.log(cRBTree.size());
|
|
95
104
|
|
|
96
105
|
// console.log(tree.isPerfectlyBalanced());
|
|
97
106
|
// tree.print();
|
|
@@ -106,27 +115,27 @@
|
|
|
106
115
|
const pq = new PriorityQueue({ comparator: (a, b) => b - a });
|
|
107
116
|
|
|
108
117
|
const tS = performance.now();
|
|
109
|
-
|
|
110
|
-
for (let i = 0; i <
|
|
118
|
+
const n = 1000000;
|
|
119
|
+
for (let i = 0; i < n; i++) {
|
|
111
120
|
pq.add(i);
|
|
112
121
|
}
|
|
113
122
|
|
|
114
|
-
for (let i = 0; i <
|
|
123
|
+
for (let i = 0; i < n; i++) {
|
|
115
124
|
pq.pop();
|
|
116
125
|
}
|
|
117
|
-
console.log(performance.now() - tS);
|
|
126
|
+
console.log(`PriorityQueue ${n} add`, performance.now() - tS);
|
|
118
127
|
console.log(pq.size);
|
|
119
128
|
const cS = performance.now();
|
|
120
129
|
const cpq = new CPriorityQueue();
|
|
121
130
|
|
|
122
|
-
for (let i = 0; i <
|
|
131
|
+
for (let i = 0; i < n; i++) {
|
|
123
132
|
cpq.push(i);
|
|
124
133
|
}
|
|
125
134
|
|
|
126
|
-
for (let i = 0; i <
|
|
135
|
+
for (let i = 0; i < n; i++) {
|
|
127
136
|
cpq.pop();
|
|
128
137
|
}
|
|
129
|
-
console.log(performance.now() - cS);
|
|
138
|
+
console.log(`CPriorityQueue ${n} add`, performance.now() - cS);
|
|
130
139
|
console.log(cpq.size());
|
|
131
140
|
} catch (e) {
|
|
132
141
|
console.error(e);
|
package/test/performance/data-structures/{comparison.test.ts → comparison/comparison.test.ts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { PriorityQueue as MJSPriorityQueue } from '
|
|
2
|
-
import { PriorityQueue as SRCPriorityQueue } from '
|
|
3
|
-
import { PriorityQueue as CJSPriorityQueue } from '
|
|
1
|
+
import { PriorityQueue as MJSPriorityQueue } from '../../../../dist/cjs';
|
|
2
|
+
import { PriorityQueue as SRCPriorityQueue } from '../../../../src';
|
|
3
|
+
import { PriorityQueue as CJSPriorityQueue } from '../../../../dist/mjs';
|
|
4
4
|
import {
|
|
5
5
|
Deque as CDeque,
|
|
6
6
|
HashMap as CHashMap,
|
|
@@ -12,27 +12,30 @@ import {
|
|
|
12
12
|
} from 'js-sdsl';
|
|
13
13
|
|
|
14
14
|
import * as Benchmark from 'benchmark';
|
|
15
|
-
import { getRandomIntArray, magnitude } from '
|
|
16
|
-
import { isCompetitor } from '
|
|
15
|
+
import { getRandomIntArray, magnitude } from '../../../utils';
|
|
16
|
+
import { isCompetitor } from '../../../config';
|
|
17
17
|
|
|
18
18
|
const suite = new Benchmark.Suite();
|
|
19
19
|
const { TEN_THOUSAND, HUNDRED_THOUSAND, LINEAR } = magnitude;
|
|
20
20
|
const cOrderedMap = new OrderedMap<number, number>();
|
|
21
21
|
const arrHundredThousand = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND, true);
|
|
22
22
|
|
|
23
|
-
suite
|
|
23
|
+
suite
|
|
24
|
+
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
})
|
|
26
|
+
const pq = new SRCPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
27
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
28
|
+
})
|
|
29
|
+
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
31
|
+
const pq = new CJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
32
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
33
|
+
})
|
|
34
|
+
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
+
const pq = new MJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
37
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
38
|
+
});
|
|
36
39
|
|
|
37
40
|
if (isCompetitor) {
|
|
38
41
|
suite.add(`CPT PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
@@ -42,22 +45,25 @@ if (isCompetitor) {
|
|
|
42
45
|
});
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
suite
|
|
46
|
-
|
|
48
|
+
suite
|
|
49
|
+
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
50
|
+
const pq = new SRCPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
53
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
54
|
+
})
|
|
55
|
+
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
56
|
+
const pq = new CJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
52
57
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
})
|
|
56
|
-
|
|
58
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
59
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
60
|
+
})
|
|
61
|
+
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
62
|
+
const pq = new MJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
});
|
|
64
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
65
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
66
|
+
});
|
|
61
67
|
|
|
62
68
|
|
|
63
69
|
if (isCompetitor) {
|
|
@@ -83,7 +83,7 @@ suite.add(`${MILLION.toLocaleString()} Set add & has`, () => {
|
|
|
83
83
|
|
|
84
84
|
suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
85
85
|
const hm = new HashMap<[number, number], number>();
|
|
86
|
-
const objKeys:[number, number][] = [];
|
|
86
|
+
const objKeys: [number, number][] = [];
|
|
87
87
|
for (let i = 0; i < MILLION; i++) {
|
|
88
88
|
const obj: [number, number] = [i, i];
|
|
89
89
|
objKeys.push(obj)
|
|
@@ -96,7 +96,7 @@ suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
|
96
96
|
|
|
97
97
|
suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
|
|
98
98
|
const hm = new Map<[number, number], number>();
|
|
99
|
-
const objs:[number, number][] = [];
|
|
99
|
+
const objs: [number, number][] = [];
|
|
100
100
|
for (let i = 0; i < MILLION; i++) {
|
|
101
101
|
const obj: [number, number] = [i, i];
|
|
102
102
|
objs.push(obj)
|
|
@@ -109,7 +109,7 @@ suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
|
|
|
109
109
|
|
|
110
110
|
suite.add(`${MILLION.toLocaleString()} Set ObjKey add & has`, () => {
|
|
111
111
|
const hs = new Set<[number, number]>();
|
|
112
|
-
const objs:[number, number][] = [];
|
|
112
|
+
const objs: [number, number][] = [];
|
|
113
113
|
for (let i = 0; i < MILLION; i++) {
|
|
114
114
|
const obj: [number, number] = [i, i];
|
|
115
115
|
objs.push(obj)
|
|
@@ -487,4 +487,21 @@ describe('RedBlackTree', () => {
|
|
|
487
487
|
}
|
|
488
488
|
isDebug && console.log(performance.now() - cS);
|
|
489
489
|
});
|
|
490
|
+
|
|
491
|
+
it('duplicates', () => {
|
|
492
|
+
tree.addMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
|
|
493
|
+
tree.print();
|
|
494
|
+
|
|
495
|
+
expect(tree.size).toBe(8);
|
|
496
|
+
expect(tree.isBST()).toBe(true);
|
|
497
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
498
|
+
tree.addMany([10, 5, 2, 11]);
|
|
499
|
+
expect(tree.size).toBe(10);
|
|
500
|
+
expect(tree.isBST()).toBe(true);
|
|
501
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
502
|
+
|
|
503
|
+
tree.clear();
|
|
504
|
+
tree.addMany([10, 20, 30, 40, 50, 60])
|
|
505
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
506
|
+
})
|
|
490
507
|
});
|
package/tsup.config.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/types/helpers.ts"],"names":[],"mappings":";;;AAMA,IAAY,EAIX;AAJD,WAAY,EAAE;IACZ,eAAS,CAAA;IACT,eAAS,CAAA;IACT,eAAS,CAAA;AACX,CAAC,EAJW,EAAE,kBAAF,EAAE,QAIb"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|