data-structure-typed 1.45.3 → 1.46.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.
- package/CHANGELOG.md +1 -1
- package/README.md +91 -40
- package/benchmark/report.html +17 -17
- package/benchmark/report.json +160 -142
- package/dist/cjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/cjs/data-structures/hash/hash-map.js +9 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/cjs/data-structures/heap/heap.js +133 -123
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/cjs/data-structures/queue/deque.js +543 -211
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/cjs/types/helpers.d.ts +11 -0
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +3 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/mjs/data-structures/hash/hash-map.js +9 -6
- package/dist/mjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/mjs/data-structures/heap/heap.js +133 -123
- package/dist/mjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/mjs/data-structures/queue/deque.js +544 -201
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/mjs/types/helpers.d.ts +11 -0
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +1 -0
- package/dist/umd/data-structure-typed.js +684 -328
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +605 -226
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +21 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
- package/test/performance/data-structures/queue/deque.test.ts +24 -13
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -4
- package/test/unit/data-structures/heap/heap.test.ts +2 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
- package/test/unit/data-structures/queue/deque.test.ts +252 -240
- /package/test/performance/data-structures/{comparation.test.ts → comparison.test.ts} +0 -0
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
export const enum IterateDirection {
|
|
2
|
-
DEFAULT = 0,
|
|
3
|
-
REVERSE = 1
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export type HashMapOptions<T> = {
|
|
7
|
-
sizeFunction?: number | (() => number);
|
|
8
|
-
fixedLength?: number;
|
|
9
|
-
forEach: (callback: (el: T) => void) => void;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
1
|
export type HashMapLinkedNode<K, V> = {
|
|
13
2
|
key: K;
|
|
14
3
|
value: V;
|
package/src/types/helpers.ts
CHANGED
|
@@ -9,3 +9,18 @@ export enum CP {
|
|
|
9
9
|
eq = 'eq',
|
|
10
10
|
gt = 'gt'
|
|
11
11
|
}
|
|
12
|
+
|
|
13
|
+
export const enum IterateDirection {
|
|
14
|
+
DEFAULT = 0,
|
|
15
|
+
REVERSE = 1
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IterableWithSize<T> extends Iterable<T> {
|
|
19
|
+
size: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface IterableWithLength<T> extends Iterable<T> {
|
|
23
|
+
length: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>
|
package/src/utils/utils.ts
CHANGED
|
@@ -97,3 +97,5 @@ export const isObjOrFunc = (input: unknown): input is Record<string, unknown> |
|
|
|
97
97
|
const inputType = typeof input;
|
|
98
98
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
99
99
|
};
|
|
100
|
+
|
|
101
|
+
export const calcMinUnitsRequired = (totalQuantity: number, unitSize: number) => Math.floor((totalQuantity + unitSize - 1) / unitSize)
|
|
@@ -7,6 +7,25 @@ import { isCompetitor } from '../../../config';
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
8
|
const { LINEAR } = magnitude;
|
|
9
9
|
|
|
10
|
+
suite.add(`${LINEAR.toLocaleString()} push`, () => {
|
|
11
|
+
const list = new DoublyLinkedList<number>();
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
14
|
+
list.push(i);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if (isCompetitor) {
|
|
20
|
+
suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
|
|
21
|
+
const list = new CLinkedList<number>();
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
24
|
+
list.pushBack(i);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
10
29
|
suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
|
|
11
30
|
const list = new DoublyLinkedList<number>();
|
|
12
31
|
|
|
@@ -14,6 +33,7 @@ suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
|
|
|
14
33
|
list.unshift(i);
|
|
15
34
|
}
|
|
16
35
|
});
|
|
36
|
+
|
|
17
37
|
if (isCompetitor) {
|
|
18
38
|
suite.add(`${LINEAR.toLocaleString()} CPT unshift`, () => {
|
|
19
39
|
const list = new CLinkedList<number>();
|
|
@@ -23,6 +43,7 @@ if (isCompetitor) {
|
|
|
23
43
|
}
|
|
24
44
|
});
|
|
25
45
|
}
|
|
46
|
+
|
|
26
47
|
suite
|
|
27
48
|
.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
|
|
28
49
|
const list = new DoublyLinkedList<number>();
|
|
@@ -5,28 +5,28 @@ import { magnitude } from '../../../utils';
|
|
|
5
5
|
import { isCompetitor } from '../../../config';
|
|
6
6
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
|
-
const {
|
|
8
|
+
const { HUNDRED_THOUSAND } = magnitude;
|
|
9
9
|
|
|
10
|
-
suite.add(`${
|
|
10
|
+
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
11
11
|
const pq = new PriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
12
12
|
|
|
13
|
-
for (let i = 0; i <
|
|
13
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
14
14
|
pq.add(i);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
for (let i = 0; i <
|
|
17
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
18
18
|
pq.pop();
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (isCompetitor) {
|
|
22
|
-
suite.add(`${
|
|
22
|
+
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} CPT add & pop`, () => {
|
|
23
23
|
const pq = new CPriorityQueue<number>();
|
|
24
24
|
|
|
25
|
-
for (let i = 0; i <
|
|
25
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
26
26
|
pq.push(i);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
for (let i = 0; i <
|
|
29
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
30
30
|
pq.pop();
|
|
31
31
|
}
|
|
32
32
|
});
|
|
@@ -9,22 +9,33 @@ const { LINEAR } = magnitude;
|
|
|
9
9
|
|
|
10
10
|
suite.add(`${LINEAR.toLocaleString()} push`, () => {
|
|
11
11
|
const deque = new Deque<number>();
|
|
12
|
-
for (let i = 0; i < LINEAR; i++)
|
|
13
|
-
deque.push(i);
|
|
14
|
-
}
|
|
12
|
+
for (let i = 0; i < LINEAR; i++) deque.push(i);
|
|
15
13
|
});
|
|
14
|
+
|
|
16
15
|
if (isCompetitor) {
|
|
17
16
|
suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
|
|
18
|
-
const
|
|
19
|
-
for (let i = 0; i < LINEAR; i++)
|
|
20
|
-
deque.pushBack(i);
|
|
21
|
-
}
|
|
17
|
+
const _deque = new CDeque<number>();
|
|
18
|
+
for (let i = 0; i < LINEAR; i++) _deque.pushBack(i);
|
|
22
19
|
});
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
|
|
24
|
+
const _deque = new Deque<number>();
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < LINEAR; i++) _deque.push(i);
|
|
27
|
+
for (let i = 0; i < LINEAR; i++) _deque.pop();
|
|
28
|
+
|
|
29
|
+
});
|
|
30
|
+
suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
|
|
31
|
+
const _deque = new Deque<number>();
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < LINEAR; i++) _deque.push(i);
|
|
34
|
+
for (let i = 0; i < LINEAR; i++) _deque.shift();
|
|
35
|
+
});
|
|
36
|
+
suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
|
|
37
|
+
const _deque = new Deque<number>();
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < LINEAR; i++) _deque.unshift(i);
|
|
40
|
+
for (let i = 0; i < LINEAR; i++) _deque.shift();
|
|
30
41
|
});
|
|
@@ -171,10 +171,10 @@ describe('HashMap', () => {
|
|
|
171
171
|
let index = 0;
|
|
172
172
|
stdMap.forEach((value, key) => {
|
|
173
173
|
if (index === 0) {
|
|
174
|
-
expect(hashMap.
|
|
174
|
+
expect(hashMap.first).toEqual([key, value]);
|
|
175
175
|
expect(hashMap.begin.current[0]).toEqual(key);
|
|
176
176
|
} else if (index === hashMap.size - 1) {
|
|
177
|
-
expect(hashMap.
|
|
177
|
+
expect(hashMap.last).toEqual([key, value]);
|
|
178
178
|
expect(hashMap.reverseBegin.current[0]).toEqual(key);
|
|
179
179
|
} else if (index <= 1000) {
|
|
180
180
|
expect(hashMap.getAt(index)).toEqual([key, value]);
|
|
@@ -217,11 +217,11 @@ describe('HashMap', () => {
|
|
|
217
217
|
test('should return the last element', () => {
|
|
218
218
|
hashMap.set('key1', 'value1');
|
|
219
219
|
hashMap.set('key2', 'value2');
|
|
220
|
-
expect(hashMap.
|
|
220
|
+
expect(hashMap.last).toEqual(['key2', 'value2']);
|
|
221
221
|
});
|
|
222
222
|
|
|
223
223
|
test('should return undefined for empty map', () => {
|
|
224
|
-
expect(hashMap.
|
|
224
|
+
expect(hashMap.last).toBeUndefined();
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
test('should get element at specific index', () => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FibonacciHeap, MaxHeap, MinHeap } from '../../../../src';
|
|
2
2
|
import { logBigOMetricsWrap } from '../../../utils';
|
|
3
3
|
|
|
4
|
+
|
|
4
5
|
describe('Heap Operation Test', () => {
|
|
5
6
|
it('should numeric heap work well', function () {
|
|
6
7
|
const minNumHeap = new MinHeap<number>();
|
|
@@ -262,7 +263,7 @@ describe('FibonacciHeap Stress Test', () => {
|
|
|
262
263
|
// const minHeap = new MinHeap<number>();
|
|
263
264
|
// const cHeap = new CHeap<number>();
|
|
264
265
|
// const cPQ = new PriorityQueue<number>(undefined, (a, b) => a - b);
|
|
265
|
-
// const n =
|
|
266
|
+
// const n = 100000;
|
|
266
267
|
//
|
|
267
268
|
// it('should add performance well', () => {
|
|
268
269
|
// const heapCost = calcRunTime(() => {
|
|
@@ -52,7 +52,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
52
52
|
|
|
53
53
|
it('should correctly heapify an array', () => {
|
|
54
54
|
const array = [5, 3, 7, 1];
|
|
55
|
-
const heap = MaxPriorityQueue.heapify<number>({
|
|
55
|
+
const heap = MaxPriorityQueue.heapify<number>({ elements: array, comparator: (a, b) => b - a });
|
|
56
56
|
heap.refill(array);
|
|
57
57
|
|
|
58
58
|
expect(heap.poll()).toBe(7);
|
|
@@ -62,8 +62,8 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
it('should correctly heapify an object array', () => {
|
|
65
|
-
const
|
|
66
|
-
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({
|
|
65
|
+
const elements = [{ keyA: 5 }, { keyA: 3 }, { keyA: 7 }, { keyA: 1 }];
|
|
66
|
+
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({ elements, comparator: (a, b) => b.keyA - a.keyA });
|
|
67
67
|
|
|
68
68
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
@@ -15,7 +15,7 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
15
15
|
expect(minPQ.peek()).toBe(4);
|
|
16
16
|
expect(
|
|
17
17
|
PriorityQueue.heapify({
|
|
18
|
-
|
|
18
|
+
elements: [3, 2, 1, 5, 6, 7, 8, 9, 10],
|
|
19
19
|
comparator: (a, b) => a - b
|
|
20
20
|
}).toArray()
|
|
21
21
|
).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]);
|
|
@@ -32,7 +32,7 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
32
32
|
expect(maxPriorityQueue.peek()).toBe(3);
|
|
33
33
|
expect(
|
|
34
34
|
PriorityQueue.heapify({
|
|
35
|
-
|
|
35
|
+
elements: [3, 2, 1, 5, 6, 7, 8, 9, 10],
|
|
36
36
|
comparator: (a, b) => a - b
|
|
37
37
|
}).toArray()
|
|
38
38
|
).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]);
|
|
@@ -42,7 +42,7 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
42
42
|
const minPQ1 = new PriorityQueue<number>({ comparator: (a, b) => a - b });
|
|
43
43
|
minPQ1.refill([2, 5, 8, 3, 1, 6, 7, 4]);
|
|
44
44
|
const clonedPriorityQueue = minPQ1.clone();
|
|
45
|
-
expect(clonedPriorityQueue.
|
|
45
|
+
expect(clonedPriorityQueue.elements).toEqual(minPQ1.elements);
|
|
46
46
|
expect(clonedPriorityQueue.sort()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
47
47
|
expect(minPQ1.dfs('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]);
|
|
48
48
|
expect(minPQ1.dfs('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]);
|