data-structure-typed 1.50.4 → 1.50.5
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 +116 -55
- package/SPECIFICATION.md +2 -2
- package/SPECIFICATION_zh-CN.md +81 -0
- package/{SPONSOR-zh-CN.md → SPONSOR_zh-CN.md} +1 -1
- package/benchmark/report.html +24 -24
- package/benchmark/report.json +242 -242
- package/dist/cjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/cjs/data-structures/base/iterable-base.js +8 -12
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- 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 +27 -69
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/cjs/data-structures/queue/deque.js +0 -61
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/cjs/data-structures/queue/queue.js +0 -87
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/mjs/data-structures/base/iterable-base.js +8 -12
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +33 -79
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/mjs/data-structures/queue/deque.js +0 -61
- package/dist/mjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/mjs/data-structures/queue/queue.js +0 -86
- package/dist/umd/data-structure-typed.js +62 -325
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +14 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -3
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
- package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -14
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +11 -6
- package/test/performance/data-structures/queue/deque.test.ts +8 -8
- package/test/performance/data-structures/queue/queue.test.ts +5 -12
- package/test/performance/reportor.ts +43 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +10 -10
- package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
- package/test/unit/data-structures/queue/deque.test.ts +26 -26
- package/test/unit/data-structures/queue/queue.test.ts +20 -20
|
@@ -21,13 +21,13 @@ if (isCompetitor) {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
suite.add(`Native Map ${MILLION.toLocaleString()} set`, () => {
|
|
24
|
+
suite.add(`Native JS Map ${MILLION.toLocaleString()} set`, () => {
|
|
25
25
|
const hm = new Map<number, number>();
|
|
26
26
|
|
|
27
27
|
for (let i = 0; i < MILLION; i++) hm.set(i, i);
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
suite.add(`Native Set ${MILLION.toLocaleString()} add`, () => {
|
|
30
|
+
suite.add(`Native JS Set ${MILLION.toLocaleString()} add`, () => {
|
|
31
31
|
const hs = new Set<number>();
|
|
32
32
|
|
|
33
33
|
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
@@ -49,14 +49,14 @@ if (isCompetitor) {
|
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
suite.add(`Native Map ${MILLION.toLocaleString()} set & get`, () => {
|
|
52
|
+
suite.add(`Native JS Map ${MILLION.toLocaleString()} set & get`, () => {
|
|
53
53
|
const hm = new Map<number, number>();
|
|
54
54
|
|
|
55
55
|
for (let i = 0; i < MILLION; i++) hm.set(i, i);
|
|
56
56
|
for (let i = 0; i < MILLION; i++) hm.get(i);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
suite.add(`Native Set ${MILLION.toLocaleString()} add & has`, () => {
|
|
59
|
+
suite.add(`Native JS Set ${MILLION.toLocaleString()} add & has`, () => {
|
|
60
60
|
const hs = new Set<number>();
|
|
61
61
|
|
|
62
62
|
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
@@ -74,7 +74,7 @@ suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
|
74
74
|
for (let i = 0; i < MILLION; i++) hm.get(objKeys[i]);
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
suite.add(`Native Map ${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
77
|
+
suite.add(`Native JS Map ${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
78
78
|
const hm = new Map<[number, number], number>();
|
|
79
79
|
const objs: [number, number][] = [];
|
|
80
80
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -85,7 +85,7 @@ suite.add(`Native Map ${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
|
85
85
|
for (let i = 0; i < MILLION; i++) hm.get(objs[i]);
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
suite.add(`Native Set ${MILLION.toLocaleString()} ObjKey add & has`, () => {
|
|
88
|
+
suite.add(`Native JS Set ${MILLION.toLocaleString()} ObjKey add & has`, () => {
|
|
89
89
|
const hs = new Set<[number, number]>();
|
|
90
90
|
const objs: [number, number][] = [];
|
|
91
91
|
for (let i = 0; i < MILLION; i++) {
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Heap } from '../../../../src';
|
|
2
2
|
import * as Benchmark from 'benchmark';
|
|
3
|
-
import { magnitude } from '../../../utils';
|
|
3
|
+
import { getRandomInt, magnitude } from '../../../utils';
|
|
4
4
|
|
|
5
5
|
const suite = new Benchmark.Suite();
|
|
6
|
-
const { HUNDRED_THOUSAND
|
|
6
|
+
const { HUNDRED_THOUSAND } = magnitude;
|
|
7
|
+
const indicesHT = new Array(HUNDRED_THOUSAND).fill(0).map(() => getRandomInt(0, HUNDRED_THOUSAND - 1));
|
|
7
8
|
|
|
8
9
|
suite
|
|
10
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
|
|
11
|
+
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
12
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(indicesHT[i]);
|
|
13
|
+
})
|
|
9
14
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
10
15
|
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
11
16
|
|
|
12
|
-
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
17
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(indicesHT[i]);
|
|
13
18
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.poll();
|
|
14
|
-
})
|
|
15
|
-
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & dfs`, () => {
|
|
16
|
-
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
17
|
-
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
18
|
-
heap.dfs();
|
|
19
|
-
})
|
|
20
|
-
.add(`${TEN_THOUSAND.toLocaleString()} fib add & pop`, () => {
|
|
21
|
-
const fbHeap = new FibonacciHeap<number>();
|
|
22
|
-
for (let i = 1; i <= TEN_THOUSAND; i++) fbHeap.push(i);
|
|
23
|
-
for (let i = 1; i <= TEN_THOUSAND; i++) fbHeap.pop();
|
|
24
19
|
});
|
|
20
|
+
// .add(`${TEN_THOUSAND.toLocaleString()} fib add & pop`, () => {
|
|
21
|
+
// const fbHeap = new FibonacciHeap<number>();
|
|
22
|
+
// for (let i = 1; i <= TEN_THOUSAND; i++) fbHeap.push(i);
|
|
23
|
+
// for (let i = 1; i <= TEN_THOUSAND; i++) fbHeap.pop();
|
|
24
|
+
// });
|
|
25
25
|
|
|
26
26
|
export { suite };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PriorityQueue as CPriorityQueue } from 'js-sdsl';
|
|
2
1
|
import { PriorityQueue } from '../../../../src';
|
|
2
|
+
import { PriorityQueue as CPriorityQueue } from 'js-sdsl';
|
|
3
3
|
import * as Benchmark from 'benchmark';
|
|
4
4
|
import { magnitude } from '../../../utils';
|
|
5
5
|
import { isCompetitor } from '../../../config';
|
|
@@ -7,12 +7,17 @@ import { isCompetitor } from '../../../config';
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
8
|
const { HUNDRED_THOUSAND } = magnitude;
|
|
9
9
|
|
|
10
|
-
suite
|
|
11
|
-
|
|
10
|
+
suite
|
|
11
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
|
|
12
|
+
const heap = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
13
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
14
|
+
})
|
|
15
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
16
|
+
const heap = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
18
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
19
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.poll();
|
|
20
|
+
});
|
|
16
21
|
if (isCompetitor) {
|
|
17
22
|
suite.add(`CPT ${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
18
23
|
const pq = new CPriorityQueue<number>();
|
|
@@ -21,25 +21,25 @@ if (isCompetitor) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
suite
|
|
24
|
-
// .add(`${TEN_THOUSAND.toLocaleString()} push & delete`, () => {
|
|
25
|
-
// const deque = new Deque<number>();
|
|
26
|
-
//
|
|
27
|
-
// for (let i = 0; i < TEN_THOUSAND; i++) deque.push(i);
|
|
28
|
-
// for (let i = 0; i < TEN_THOUSAND; i++) deque.delete(randomIndicesTenThousand[i]);
|
|
29
|
-
// })
|
|
30
24
|
.add(`${MILLION.toLocaleString()} push & pop`, () => {
|
|
31
25
|
const deque = new Deque<number>();
|
|
32
26
|
|
|
33
27
|
for (let i = 0; i < MILLION; i++) deque.push(i);
|
|
34
28
|
for (let i = 0; i < MILLION; i++) deque.pop();
|
|
35
29
|
})
|
|
30
|
+
.add(`${MILLION.toLocaleString()} push & shift`, () => {
|
|
31
|
+
const deque = new Deque<number>();
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < MILLION; i++) deque.push(i);
|
|
34
|
+
for (let i = 0; i < MILLION; i++) deque.shift();
|
|
35
|
+
})
|
|
36
36
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
|
|
37
37
|
const deque = new Deque<number>();
|
|
38
38
|
|
|
39
39
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) deque.push(i);
|
|
40
40
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) deque.shift();
|
|
41
41
|
})
|
|
42
|
-
.add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
|
|
42
|
+
.add(`Native JS Array ${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
|
|
43
43
|
const array = new Array<number>();
|
|
44
44
|
|
|
45
45
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) array.push(i);
|
|
@@ -51,7 +51,7 @@ suite
|
|
|
51
51
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) deque.unshift(i);
|
|
52
52
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) deque.shift();
|
|
53
53
|
})
|
|
54
|
-
.add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} unshift & shift`, () => {
|
|
54
|
+
.add(`Native JS Array ${HUNDRED_THOUSAND.toLocaleString()} unshift & shift`, () => {
|
|
55
55
|
const array = new Array<number>();
|
|
56
56
|
|
|
57
57
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) array.unshift(i);
|
|
@@ -25,18 +25,11 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
|
|
|
25
25
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) queue.push(i);
|
|
26
26
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) queue.shift();
|
|
27
27
|
});
|
|
28
|
-
suite
|
|
29
|
-
|
|
30
|
-
const arr = new Array<number>();
|
|
28
|
+
suite.add(`Native JS Array ${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
|
|
29
|
+
const arr = new Array<number>();
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
.add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} push & pop`, () => {
|
|
36
|
-
const arr = new Array<number>();
|
|
37
|
-
|
|
38
|
-
for (let i = 0; i < HUNDRED_THOUSAND; i++) arr.push(i);
|
|
39
|
-
for (let i = 0; i < HUNDRED_THOUSAND; i++) arr.pop();
|
|
40
|
-
});
|
|
31
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) arr.push(i);
|
|
32
|
+
for (let i = 0; i < HUNDRED_THOUSAND; i++) arr.shift();
|
|
33
|
+
});
|
|
41
34
|
|
|
42
35
|
export { suite };
|
|
@@ -4,6 +4,7 @@ import * as fs from 'fs';
|
|
|
4
4
|
import * as fastGlob from 'fast-glob';
|
|
5
5
|
import { Color, numberFix, render } from '../utils';
|
|
6
6
|
import { PerformanceTest } from './types';
|
|
7
|
+
import * as console from 'console';
|
|
7
8
|
|
|
8
9
|
const args = process.argv.slice(2);
|
|
9
10
|
|
|
@@ -171,7 +172,48 @@ function replaceMarkdownContent(startMarker: string, endMarker: string, newText:
|
|
|
171
172
|
});
|
|
172
173
|
}
|
|
173
174
|
|
|
174
|
-
|
|
175
|
+
const order = [
|
|
176
|
+
'heap',
|
|
177
|
+
'rb-tree',
|
|
178
|
+
'queue',
|
|
179
|
+
'deque',
|
|
180
|
+
'hash-map',
|
|
181
|
+
'trie',
|
|
182
|
+
'avl-tree',
|
|
183
|
+
'binary-tree-overall',
|
|
184
|
+
'directed-graph',
|
|
185
|
+
'doubly-linked-list',
|
|
186
|
+
'singly-linked-list',
|
|
187
|
+
'priority-queue',
|
|
188
|
+
'stack'
|
|
189
|
+
];
|
|
190
|
+
|
|
191
|
+
const sortedPerformanceTests = [...performanceTests].sort((a, b) => {
|
|
192
|
+
const indexA = order.indexOf(a.testName);
|
|
193
|
+
const indexB = order.indexOf(b.testName);
|
|
194
|
+
|
|
195
|
+
// If both a and b are in the order, sort them according to their indices in the order.
|
|
196
|
+
if (indexA !== -1 && indexB !== -1) {
|
|
197
|
+
return indexA - indexB;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// If there is only 'a' in the order, then place 'b' in front.
|
|
201
|
+
if (indexA !== -1) {
|
|
202
|
+
return 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// If only b is in the order, then a should be placed before it.
|
|
206
|
+
if (indexB !== -1) {
|
|
207
|
+
return -1;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// If neither a nor b are in order, keep their original order
|
|
211
|
+
return 0;
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
console.log(`${GREEN} Found tests${END}: ${sortedPerformanceTests.map(test => test.testName)}`);
|
|
215
|
+
|
|
216
|
+
sortedPerformanceTests.forEach(item => {
|
|
175
217
|
const { suite, testName, file } = item;
|
|
176
218
|
|
|
177
219
|
console.log(coloredLabeled('Running', file));
|
|
@@ -39,12 +39,12 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
39
39
|
|
|
40
40
|
it('should clone', function () {
|
|
41
41
|
const dList = new DoublyLinkedList<string>();
|
|
42
|
-
dList.
|
|
43
|
-
dList.
|
|
44
|
-
dList.
|
|
45
|
-
dList.
|
|
46
|
-
dList.
|
|
47
|
-
dList.
|
|
42
|
+
dList.push('1');
|
|
43
|
+
dList.push('6');
|
|
44
|
+
dList.push('2');
|
|
45
|
+
dList.push('0');
|
|
46
|
+
dList.push('5');
|
|
47
|
+
dList.push('9');
|
|
48
48
|
dList.delete('2');
|
|
49
49
|
expect([...dList]).toEqual(['1', '6', '0', '5', '9']);
|
|
50
50
|
const cloned = dList.clone();
|
|
@@ -32,7 +32,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
32
32
|
list.push(3);
|
|
33
33
|
const popped = list.pop();
|
|
34
34
|
expect(popped).toBe(3);
|
|
35
|
-
expect(list.
|
|
35
|
+
expect(list.pop()).toBe(2);
|
|
36
36
|
expect(list.toArray()).toEqual([1]);
|
|
37
37
|
});
|
|
38
38
|
|
|
@@ -49,7 +49,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
49
49
|
list.push(3);
|
|
50
50
|
const shifted = list.shift();
|
|
51
51
|
expect(shifted).toBe(1);
|
|
52
|
-
expect(list.
|
|
52
|
+
expect(list.shift()).toBe(2);
|
|
53
53
|
expect(list.toArray()).toEqual([3]);
|
|
54
54
|
});
|
|
55
55
|
|
|
@@ -62,7 +62,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
62
62
|
describe('unshift', () => {
|
|
63
63
|
it('should add elements to the beginning of the list', () => {
|
|
64
64
|
list.unshift(1);
|
|
65
|
-
list.
|
|
65
|
+
list.unshift(2);
|
|
66
66
|
expect(list.toArray()).toEqual([2, 1]);
|
|
67
67
|
});
|
|
68
68
|
});
|
|
@@ -350,12 +350,12 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
350
350
|
|
|
351
351
|
it('should clone', function () {
|
|
352
352
|
const sList = new SinglyLinkedList<string>();
|
|
353
|
-
sList.
|
|
354
|
-
sList.
|
|
355
|
-
sList.
|
|
356
|
-
sList.
|
|
357
|
-
sList.
|
|
358
|
-
sList.
|
|
353
|
+
sList.push('1');
|
|
354
|
+
sList.push('6');
|
|
355
|
+
sList.push('2');
|
|
356
|
+
sList.push('0');
|
|
357
|
+
sList.push('5');
|
|
358
|
+
sList.push('9');
|
|
359
359
|
sList.delete('2');
|
|
360
360
|
expect([...sList]).toEqual(['1', '6', '0', '5', '9']);
|
|
361
361
|
const cloned = sList.clone();
|
|
@@ -478,7 +478,7 @@ describe('SinglyLinkedList', () => {
|
|
|
478
478
|
});
|
|
479
479
|
|
|
480
480
|
it('should map the list', () => {
|
|
481
|
-
list.
|
|
481
|
+
list.push(1);
|
|
482
482
|
list.push(2);
|
|
483
483
|
list.push(3);
|
|
484
484
|
expect(list.map(value => value * 2).toArray()).toEqual([2, 4, 6]);
|
|
@@ -65,21 +65,21 @@ describe('SkipList Test2', () => {
|
|
|
65
65
|
skipList.add(4, 'Four');
|
|
66
66
|
});
|
|
67
67
|
|
|
68
|
-
it('
|
|
68
|
+
it('first() should return the first element', () => {
|
|
69
69
|
expect(skipList.first).toBe('One');
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
-
it('
|
|
72
|
+
it('last() should return the last element', () => {
|
|
73
73
|
expect(skipList.last).toBe('Four');
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
it('higher(key) should return the
|
|
76
|
+
it('higher(key) should return the first element greater than the given key', () => {
|
|
77
77
|
expect(skipList.higher(2)).toBe('Three');
|
|
78
78
|
expect(skipList.higher(3)).toBe('Four');
|
|
79
79
|
expect(skipList.higher(4)).toBeUndefined();
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
-
it('lower(key) should return the
|
|
82
|
+
it('lower(key) should return the last element less than the given key', () => {
|
|
83
83
|
expect(skipList.lower(2)).toBe('One');
|
|
84
84
|
expect(skipList.lower(1)).toBe(undefined);
|
|
85
85
|
});
|
|
@@ -52,12 +52,12 @@ describe('Deque - Basic Operations', () => {
|
|
|
52
52
|
|
|
53
53
|
it('should clone', function () {
|
|
54
54
|
const deque = new Deque<string>();
|
|
55
|
-
deque.
|
|
56
|
-
deque.
|
|
57
|
-
deque.
|
|
58
|
-
deque.
|
|
59
|
-
deque.
|
|
60
|
-
deque.
|
|
55
|
+
deque.push('1');
|
|
56
|
+
deque.push('6');
|
|
57
|
+
deque.push('2');
|
|
58
|
+
deque.push('0');
|
|
59
|
+
deque.push('5');
|
|
60
|
+
deque.push('9');
|
|
61
61
|
expect(deque.size).toBe(6);
|
|
62
62
|
deque.delete('2');
|
|
63
63
|
expect(deque.size).toBe(5);
|
|
@@ -70,7 +70,7 @@ describe('Deque - Basic Operations', () => {
|
|
|
70
70
|
expect([...deque]).toEqual(['1', '6', '0', '9']);
|
|
71
71
|
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
72
72
|
expect(cloned.size).toBe(5);
|
|
73
|
-
cloned.
|
|
73
|
+
cloned.push('8');
|
|
74
74
|
expect(cloned.size).toBe(6);
|
|
75
75
|
cloned.delete('6');
|
|
76
76
|
expect(cloned.size).toBe(5);
|
|
@@ -239,52 +239,52 @@ describe('Deque - Additional Operations', () => {
|
|
|
239
239
|
deque = new Deque<number>();
|
|
240
240
|
});
|
|
241
241
|
|
|
242
|
-
test('
|
|
243
|
-
deque.
|
|
244
|
-
deque.
|
|
242
|
+
test('push should add an element to the end', () => {
|
|
243
|
+
deque.push(1);
|
|
244
|
+
deque.push(2);
|
|
245
245
|
expect(deque.last).toBe(2);
|
|
246
246
|
expect(deque.size).toBe(2);
|
|
247
247
|
});
|
|
248
248
|
|
|
249
|
-
test('
|
|
250
|
-
deque.
|
|
251
|
-
deque.
|
|
252
|
-
expect(deque.
|
|
249
|
+
test('pop should remove and return the last element', () => {
|
|
250
|
+
deque.push(1);
|
|
251
|
+
deque.push(2);
|
|
252
|
+
expect(deque.pop()).toBe(2);
|
|
253
253
|
expect(deque.size).toBe(1);
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
-
test('
|
|
257
|
-
deque.
|
|
258
|
-
deque.
|
|
256
|
+
test('unshift should add an element to the beginning', () => {
|
|
257
|
+
deque.unshift(1);
|
|
258
|
+
deque.unshift(2);
|
|
259
259
|
expect(deque.first).toBe(2);
|
|
260
260
|
expect(deque.size).toBe(2);
|
|
261
261
|
});
|
|
262
262
|
|
|
263
|
-
test('
|
|
264
|
-
deque.
|
|
265
|
-
deque.
|
|
266
|
-
expect(deque.
|
|
263
|
+
test('shift should remove and return the first element', () => {
|
|
264
|
+
deque.unshift(1);
|
|
265
|
+
deque.unshift(2);
|
|
266
|
+
expect(deque.shift()).toBe(2);
|
|
267
267
|
expect(deque.size).toBe(1);
|
|
268
268
|
});
|
|
269
269
|
|
|
270
270
|
test('clear should reset the deque', () => {
|
|
271
|
-
deque.
|
|
271
|
+
deque.unshift(1);
|
|
272
272
|
deque.clear();
|
|
273
273
|
expect(deque.size).toBe(0);
|
|
274
274
|
expect(deque.isEmpty()).toBeTruthy();
|
|
275
275
|
});
|
|
276
276
|
|
|
277
277
|
test('begin should yield elements from the beginning', () => {
|
|
278
|
-
deque.
|
|
279
|
-
deque.
|
|
278
|
+
deque.push(1);
|
|
279
|
+
deque.push(2);
|
|
280
280
|
const iterator = deque.begin();
|
|
281
281
|
expect(iterator.next().value).toBe(1);
|
|
282
282
|
expect(iterator.next().value).toBe(2);
|
|
283
283
|
});
|
|
284
284
|
|
|
285
285
|
test('reverseBegin should yield elements in reverse order', () => {
|
|
286
|
-
deque.
|
|
287
|
-
deque.
|
|
286
|
+
deque.push(1);
|
|
287
|
+
deque.push(2);
|
|
288
288
|
const iterator = deque.reverseBegin();
|
|
289
289
|
expect(iterator.next().value).toBe(2);
|
|
290
290
|
expect(iterator.next().value).toBe(1);
|
|
@@ -23,24 +23,24 @@ describe('Queue', () => {
|
|
|
23
23
|
|
|
24
24
|
test('shift should remove the first element', () => {
|
|
25
25
|
queue.push(1);
|
|
26
|
-
queue.
|
|
26
|
+
queue.push(2);
|
|
27
27
|
expect(queue.shift()).toBe(1);
|
|
28
28
|
expect(queue.size).toBe(1);
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
test('shift should return undefined if queue is empty', () => {
|
|
32
|
-
expect(queue.
|
|
32
|
+
expect(queue.shift()).toBeUndefined();
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
test('
|
|
35
|
+
test('first should return the first element without removing it', () => {
|
|
36
36
|
queue.push(1);
|
|
37
37
|
queue.push(2);
|
|
38
|
-
expect(queue.
|
|
38
|
+
expect(queue.first).toBe(1);
|
|
39
39
|
expect(queue.size).toBe(2);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
test('
|
|
43
|
-
expect(queue.
|
|
42
|
+
test('first should return undefined if queue is empty', () => {
|
|
43
|
+
expect(queue.first).toBeUndefined();
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
test('size should return the number of elements', () => {
|
|
@@ -167,12 +167,12 @@ describe('Queue - Additional Methods', () => {
|
|
|
167
167
|
test('peekLast should return the last element without removing it', () => {
|
|
168
168
|
queue.push(1);
|
|
169
169
|
queue.push(2);
|
|
170
|
-
expect(queue.
|
|
170
|
+
expect(queue.last).toBe(2);
|
|
171
171
|
expect(queue.size).toBe(2);
|
|
172
172
|
});
|
|
173
173
|
|
|
174
174
|
test('peekLast should return undefined if queue is empty', () => {
|
|
175
|
-
expect(queue.
|
|
175
|
+
expect(queue.last).toBeUndefined();
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
test('at should return the element at the specified index', () => {
|
|
@@ -239,25 +239,25 @@ describe('LinkedListQueue', () => {
|
|
|
239
239
|
queue = new LinkedListQueue<string>();
|
|
240
240
|
});
|
|
241
241
|
|
|
242
|
-
it('should
|
|
243
|
-
queue.
|
|
244
|
-
queue.
|
|
245
|
-
expect(queue.
|
|
242
|
+
it('should push elements to the end of the queue', () => {
|
|
243
|
+
queue.push('A');
|
|
244
|
+
queue.push('B');
|
|
245
|
+
expect(queue.first).toBe('A');
|
|
246
246
|
expect(queue.size).toBe(2);
|
|
247
247
|
});
|
|
248
248
|
|
|
249
|
-
it('should
|
|
250
|
-
queue.
|
|
251
|
-
queue.
|
|
252
|
-
const dequeued = queue.
|
|
249
|
+
it('should shift elements from the front of the queue', () => {
|
|
250
|
+
queue.push('A');
|
|
251
|
+
queue.push('B');
|
|
252
|
+
const dequeued = queue.shift();
|
|
253
253
|
expect(dequeued).toBe('A');
|
|
254
|
-
expect(queue.
|
|
254
|
+
expect(queue.first).toBe('B');
|
|
255
255
|
expect(queue.size).toBe(1);
|
|
256
256
|
});
|
|
257
257
|
|
|
258
258
|
it('should peek at the front of the queue', () => {
|
|
259
|
-
queue.
|
|
260
|
-
queue.
|
|
261
|
-
expect(queue.
|
|
259
|
+
queue.push('A');
|
|
260
|
+
queue.push('B');
|
|
261
|
+
expect(queue.first).toBe('A');
|
|
262
262
|
});
|
|
263
263
|
});
|