data-structure-typed 1.49.1 → 1.49.2
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 +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +126 -129
- 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 +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -14
- package/dist/cjs/data-structures/queue/queue.js +15 -18
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -128
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -14
- package/dist/mjs/data-structures/queue/queue.js +15 -18
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +338 -370
- 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 +24 -0
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +138 -142
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +19 -23
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- 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 +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -6,7 +6,7 @@ const suite = new Benchmark.Suite();
|
|
|
6
6
|
const { HUNDRED_THOUSAND, TEN_THOUSAND } = magnitude;
|
|
7
7
|
|
|
8
8
|
suite
|
|
9
|
-
.add(`${HUNDRED_THOUSAND.toLocaleString()} add &
|
|
9
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
10
10
|
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
11
11
|
|
|
12
12
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
@@ -14,7 +14,7 @@ suite
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
17
|
-
heap.
|
|
17
|
+
heap.poll();
|
|
18
18
|
}
|
|
19
19
|
})
|
|
20
20
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & dfs`, () => {
|
|
@@ -55,7 +55,7 @@ suite
|
|
|
55
55
|
list.shift();
|
|
56
56
|
}
|
|
57
57
|
})
|
|
58
|
-
.add(`${LINEAR.toLocaleString()}
|
|
58
|
+
.add(`${LINEAR.toLocaleString()} addBefore`, () => {
|
|
59
59
|
const doublyList = new DoublyLinkedList<number>();
|
|
60
60
|
let midNode: DoublyLinkedListNode | undefined;
|
|
61
61
|
const midIndex = Math.floor(LINEAR / 2);
|
|
@@ -64,7 +64,7 @@ suite
|
|
|
64
64
|
if (i === midIndex) {
|
|
65
65
|
midNode = doublyList.getNode(i);
|
|
66
66
|
} else if (i > midIndex && midNode) {
|
|
67
|
-
doublyList.
|
|
67
|
+
doublyList.addBefore(midNode, i);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
});
|
|
@@ -28,7 +28,7 @@ suite
|
|
|
28
28
|
list.pop();
|
|
29
29
|
}
|
|
30
30
|
})
|
|
31
|
-
.add(`${TEN_THOUSAND.toLocaleString()}
|
|
31
|
+
.add(`${TEN_THOUSAND.toLocaleString()} addBefore`, () => {
|
|
32
32
|
const singlyList = new SinglyLinkedList<number>();
|
|
33
33
|
let midSinglyNode: SinglyLinkedListNode | undefined;
|
|
34
34
|
const midIndex = Math.floor(TEN_THOUSAND / 2);
|
|
@@ -37,7 +37,7 @@ suite
|
|
|
37
37
|
if (i === midIndex) {
|
|
38
38
|
midSinglyNode = singlyList.getNode(i);
|
|
39
39
|
} else if (i > midIndex && midSinglyNode) {
|
|
40
|
-
singlyList.
|
|
40
|
+
singlyList.addBefore(midSinglyNode.value, i);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
});
|
|
@@ -7,7 +7,7 @@ import { isCompetitor } from '../../../config';
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
8
|
const { HUNDRED_THOUSAND } = magnitude;
|
|
9
9
|
|
|
10
|
-
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add &
|
|
10
|
+
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
11
11
|
const pq = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
12
12
|
|
|
13
13
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
@@ -15,7 +15,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
18
|
-
pq.
|
|
18
|
+
pq.poll();
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (isCompetitor) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode, FamilyPosition, IterationType } from '../../../../src';
|
|
2
2
|
import { getRandomIntArray } from '../../../utils';
|
|
3
|
-
import { isDebugTest } from '../../../config';
|
|
3
|
+
// import { isDebugTest } from '../../../config';
|
|
4
4
|
|
|
5
|
-
const isDebug = isDebugTest;
|
|
5
|
+
// const isDebug = isDebugTest;
|
|
6
6
|
|
|
7
7
|
describe('BinaryTreeNode', () => {
|
|
8
8
|
it('should create an instance of BinaryTreeNode', () => {
|
|
@@ -453,7 +453,7 @@ describe('LinkedHashMap Test2', () => {
|
|
|
453
453
|
expect(hashMap.last).toEqual([key, value]);
|
|
454
454
|
expect(hashMap.reverseBegin().next().value).toEqual([key, value]);
|
|
455
455
|
} else if (index <= 1000) {
|
|
456
|
-
expect(hashMap.getAt(index)).
|
|
456
|
+
expect(hashMap.getAt(index)).toBe(value);
|
|
457
457
|
}
|
|
458
458
|
expect(hashMap.get(key)).toEqual(value);
|
|
459
459
|
index++;
|
|
@@ -502,7 +502,7 @@ describe('LinkedHashMap Test2', () => {
|
|
|
502
502
|
test('should get element at specific index', () => {
|
|
503
503
|
hashMap.set('key1', 'value1');
|
|
504
504
|
hashMap.set('key2', 'value2');
|
|
505
|
-
expect(hashMap.getAt(1)).
|
|
505
|
+
expect(hashMap.getAt(1)).toBe('value2');
|
|
506
506
|
});
|
|
507
507
|
});
|
|
508
508
|
|
|
@@ -5,7 +5,7 @@ import { logBigOMetricsWrap } from '../../../utils';
|
|
|
5
5
|
describe('Heap Operation Test', () => {
|
|
6
6
|
it('should numeric heap work well', function () {
|
|
7
7
|
const minNumHeap = new MinHeap<number>();
|
|
8
|
-
minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
|
|
8
|
+
minNumHeap.add(1);minNumHeap.add(6);minNumHeap.add(2);minNumHeap.add(0);minNumHeap.add(5);minNumHeap.add(9);
|
|
9
9
|
expect(minNumHeap.has(1)).toBe(true);
|
|
10
10
|
expect(minNumHeap.has(2)).toBe(true);
|
|
11
11
|
expect(minNumHeap.poll()).toBe(0);
|
|
@@ -19,11 +19,11 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
19
19
|
it('should out of bound index', () => {
|
|
20
20
|
expect(list.getNodeAt(-1)).toBe(undefined);
|
|
21
21
|
expect(list.getNodeAt(5)).toBe(undefined);
|
|
22
|
-
expect(list.
|
|
22
|
+
expect(list.addAt(5, 6)).toBe(true);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
it('should
|
|
26
|
-
expect(list.
|
|
25
|
+
it('should addBefore', () => {
|
|
26
|
+
expect(list.addBefore(1, 0)).toBe(true);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it('should deleteAt', () => {
|
|
@@ -49,11 +49,11 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
49
49
|
expect(list.findBackward(value => value === 0)).toBe(undefined);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
it('should
|
|
53
|
-
expect(list.
|
|
52
|
+
it('should addAfter tail', () => {
|
|
53
|
+
expect(list.addAfter(list.tail!, 6)).toBe(true);
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
it('should
|
|
56
|
+
it('should addAfter tail', () => {
|
|
57
57
|
expect([...list]).toEqual([1, 2, 3, 4, 5]);
|
|
58
58
|
});
|
|
59
59
|
});
|
|
@@ -68,7 +68,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
it('should initialize an empty list', () => {
|
|
71
|
-
expect(list.
|
|
71
|
+
expect(list.size).toBe(0);
|
|
72
72
|
expect(list.head).toBe(undefined);
|
|
73
73
|
expect(list.tail).toBe(undefined);
|
|
74
74
|
});
|
|
@@ -77,7 +77,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
77
77
|
list.push(1);
|
|
78
78
|
list.push(2);
|
|
79
79
|
list.push(3);
|
|
80
|
-
expect(list.
|
|
80
|
+
expect(list.size).toBe(3);
|
|
81
81
|
expect(list.head!.value).toBe(1);
|
|
82
82
|
expect(list.tail!.value).toBe(3);
|
|
83
83
|
});
|
|
@@ -87,7 +87,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
87
87
|
list.push(2);
|
|
88
88
|
const poppedValue = list.pop();
|
|
89
89
|
expect(poppedValue).toBe(2);
|
|
90
|
-
expect(list.
|
|
90
|
+
expect(list.size).toBe(1);
|
|
91
91
|
expect(list.head!.value).toBe(1);
|
|
92
92
|
expect(list.tail!.value).toBe(1);
|
|
93
93
|
});
|
|
@@ -97,20 +97,20 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
97
97
|
list.push(3);
|
|
98
98
|
|
|
99
99
|
// Inserting at the beginning
|
|
100
|
-
list.
|
|
101
|
-
expect(list.
|
|
100
|
+
list.addAt(0, 0);
|
|
101
|
+
expect(list.size).toBe(4);
|
|
102
102
|
expect(list.getAt(0)).toBe(0);
|
|
103
103
|
expect(list.getAt(1)).toBe(1);
|
|
104
104
|
|
|
105
105
|
// Inserting in the middle
|
|
106
|
-
list.
|
|
107
|
-
expect(list.
|
|
106
|
+
list.addAt(2, 1.5);
|
|
107
|
+
expect(list.size).toBe(5);
|
|
108
108
|
expect(list.getAt(2)).toBe(1.5);
|
|
109
109
|
expect(list.getAt(3)).toBe(2);
|
|
110
110
|
|
|
111
111
|
// Inserting at the end
|
|
112
|
-
list.
|
|
113
|
-
expect(list.
|
|
112
|
+
list.addAt(5, 4);
|
|
113
|
+
expect(list.size).toBe(6);
|
|
114
114
|
expect(list.getAt(5)).toBe(4);
|
|
115
115
|
expect(list.tail!.value).toBe(4);
|
|
116
116
|
});
|
|
@@ -122,18 +122,18 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
122
122
|
|
|
123
123
|
// Deleting from the beginning
|
|
124
124
|
const deletedValue = list.deleteAt(0);
|
|
125
|
-
expect(deletedValue).toBe(
|
|
126
|
-
expect(list.
|
|
125
|
+
expect(deletedValue).toBe(true);
|
|
126
|
+
expect(list.size).toBe(2);
|
|
127
127
|
expect(list.head!.value).toBe(2);
|
|
128
128
|
|
|
129
129
|
// Deleting from the middle
|
|
130
130
|
list.deleteAt(0); // Deleting the second element
|
|
131
|
-
expect(list.
|
|
131
|
+
expect(list.size).toBe(1);
|
|
132
132
|
expect(list.head!.value).toBe(3);
|
|
133
133
|
|
|
134
134
|
// Deleting from the end
|
|
135
135
|
list.deleteAt(0);
|
|
136
|
-
expect(list.
|
|
136
|
+
expect(list.size).toBe(0);
|
|
137
137
|
expect(list.head).toBe(undefined);
|
|
138
138
|
expect(list.tail).toBe(undefined);
|
|
139
139
|
});
|
|
@@ -144,16 +144,16 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
144
144
|
list.push(3);
|
|
145
145
|
|
|
146
146
|
list.delete(2);
|
|
147
|
-
expect(list.
|
|
147
|
+
expect(list.size).toBe(2);
|
|
148
148
|
expect(list.head!.value).toBe(1);
|
|
149
149
|
expect(list.tail!.value).toBe(3);
|
|
150
150
|
|
|
151
151
|
list.delete(1);
|
|
152
|
-
expect(list.
|
|
152
|
+
expect(list.size).toBe(1);
|
|
153
153
|
expect(list.head!.value).toBe(3);
|
|
154
154
|
|
|
155
155
|
list.delete(3);
|
|
156
|
-
expect(list.
|
|
156
|
+
expect(list.size).toBe(0);
|
|
157
157
|
expect(list.head).toBe(undefined);
|
|
158
158
|
expect(list.tail).toBe(undefined);
|
|
159
159
|
});
|
|
@@ -206,7 +206,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
206
206
|
list.push(2);
|
|
207
207
|
list.push(3);
|
|
208
208
|
|
|
209
|
-
list.
|
|
209
|
+
list.addAfter(2, 2.5);
|
|
210
210
|
|
|
211
211
|
expect(list.toArray()).toEqual([1, 2, 2.5, 3]);
|
|
212
212
|
});
|
|
@@ -216,7 +216,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
216
216
|
list.push(2);
|
|
217
217
|
list.push(3);
|
|
218
218
|
|
|
219
|
-
list.
|
|
219
|
+
list.addBefore(2, 1.5);
|
|
220
220
|
|
|
221
221
|
expect(list.toArray()).toEqual([1, 1.5, 2, 3]);
|
|
222
222
|
});
|
|
@@ -258,7 +258,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
258
258
|
|
|
259
259
|
list.clear();
|
|
260
260
|
|
|
261
|
-
expect(list.
|
|
261
|
+
expect(list.size).toBe(0);
|
|
262
262
|
expect(list.head).toBe(undefined);
|
|
263
263
|
expect(list.tail).toBe(undefined);
|
|
264
264
|
});
|
|
@@ -334,7 +334,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
334
334
|
list.push(2);
|
|
335
335
|
list.push(3);
|
|
336
336
|
|
|
337
|
-
const success = list.
|
|
337
|
+
const success = list.addAfter(2, 4);
|
|
338
338
|
expect(success).toBe(true);
|
|
339
339
|
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
340
340
|
});
|
|
@@ -344,7 +344,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
344
344
|
list.push(2);
|
|
345
345
|
list.push(3);
|
|
346
346
|
|
|
347
|
-
const success = list.
|
|
347
|
+
const success = list.addBefore(2, 0);
|
|
348
348
|
expect(success).toBe(true);
|
|
349
349
|
expect(list.toArray()).toEqual([1, 0, 2, 3]);
|
|
350
350
|
});
|
|
@@ -354,7 +354,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
354
354
|
list.push(2);
|
|
355
355
|
list.push(3);
|
|
356
356
|
|
|
357
|
-
const success = list.
|
|
357
|
+
const success = list.addAfter(4, 5);
|
|
358
358
|
expect(success).toBe(false);
|
|
359
359
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
360
360
|
});
|
|
@@ -364,7 +364,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
364
364
|
list.push(2);
|
|
365
365
|
list.push(3);
|
|
366
366
|
|
|
367
|
-
const success = list.
|
|
367
|
+
const success = list.addBefore(4, 0);
|
|
368
368
|
expect(success).toBe(false);
|
|
369
369
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
370
370
|
});
|
|
@@ -381,7 +381,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
381
381
|
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
382
382
|
|
|
383
383
|
const newObj = { keyA: 25 }; // Corrected newObj value
|
|
384
|
-
const insertSuccess = objectList.
|
|
384
|
+
const insertSuccess = objectList.addBefore(obj2, newObj);
|
|
385
385
|
expect(insertSuccess).toBe(true);
|
|
386
386
|
|
|
387
387
|
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('LinkedList Performance Test', () => {
|
|
5
|
-
it('should DoublyLinkedList
|
|
5
|
+
it('should DoublyLinkedList addBefore faster than SinglyLinkedList', () => {
|
|
6
6
|
expect(1).toBe(1);
|
|
7
7
|
});
|
|
8
8
|
});
|
|
@@ -84,12 +84,12 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
-
describe('
|
|
87
|
+
describe('addAfter', () => {
|
|
88
88
|
it('should insert an element after an existing value', () => {
|
|
89
89
|
list.push(1);
|
|
90
90
|
list.push(2);
|
|
91
91
|
list.push(3);
|
|
92
|
-
list.
|
|
92
|
+
list.addAfter(2, 4);
|
|
93
93
|
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
94
94
|
});
|
|
95
95
|
|
|
@@ -97,7 +97,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
97
97
|
list.push(1);
|
|
98
98
|
list.push(2);
|
|
99
99
|
list.push(3);
|
|
100
|
-
const result = list.
|
|
100
|
+
const result = list.addAfter(5, 4);
|
|
101
101
|
expect(result).toBe(false);
|
|
102
102
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
103
103
|
});
|
|
@@ -164,7 +164,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
164
164
|
list.push(3);
|
|
165
165
|
list.clear();
|
|
166
166
|
expect(list.toArray()).toEqual([]);
|
|
167
|
-
expect(list.
|
|
167
|
+
expect(list.size).toBe(0);
|
|
168
168
|
expect(list.isEmpty()).toBe(true);
|
|
169
169
|
});
|
|
170
170
|
});
|
|
@@ -223,19 +223,19 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
223
223
|
});
|
|
224
224
|
});
|
|
225
225
|
|
|
226
|
-
describe('
|
|
226
|
+
describe('addBefore', () => {
|
|
227
227
|
it('should insert an element before an existing value', () => {
|
|
228
228
|
list.push(1);
|
|
229
229
|
list.push(2);
|
|
230
230
|
list.push(3);
|
|
231
|
-
list.
|
|
231
|
+
list.addBefore(2, 4);
|
|
232
232
|
expect(list.toArray()).toEqual([1, 4, 2, 3]);
|
|
233
233
|
});
|
|
234
234
|
|
|
235
235
|
it('should insert an element at the beginning', () => {
|
|
236
236
|
list.push(1);
|
|
237
237
|
list.push(2);
|
|
238
|
-
list.
|
|
238
|
+
list.addBefore(1, 3);
|
|
239
239
|
expect(list.toArray()).toEqual([3, 1, 2]);
|
|
240
240
|
});
|
|
241
241
|
|
|
@@ -243,7 +243,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
243
243
|
list.push(1);
|
|
244
244
|
list.push(2);
|
|
245
245
|
list.push(3);
|
|
246
|
-
const result = list.
|
|
246
|
+
const result = list.addBefore(5, 4);
|
|
247
247
|
expect(result).toBe(false);
|
|
248
248
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
249
249
|
});
|
|
@@ -251,10 +251,10 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
251
251
|
|
|
252
252
|
describe('getLength', () => {
|
|
253
253
|
it('should return the correct length of the list', () => {
|
|
254
|
-
expect(list.
|
|
254
|
+
expect(list.size).toBe(0);
|
|
255
255
|
list.push(1);
|
|
256
256
|
list.push(2);
|
|
257
|
-
expect(list.
|
|
257
|
+
expect(list.size).toBe(2);
|
|
258
258
|
});
|
|
259
259
|
});
|
|
260
260
|
|
|
@@ -264,21 +264,21 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
264
264
|
list.push(2);
|
|
265
265
|
list.push(3);
|
|
266
266
|
const removed = list.deleteAt(1);
|
|
267
|
-
expect(removed).toBe(
|
|
267
|
+
expect(removed).toBe(true);
|
|
268
268
|
expect(list.toArray()).toEqual([1, 3]);
|
|
269
269
|
});
|
|
270
270
|
|
|
271
271
|
it('should return undefined for an out-of-bounds index', () => {
|
|
272
272
|
list.push(1);
|
|
273
273
|
const removed = list.deleteAt(1);
|
|
274
|
-
expect(removed).
|
|
274
|
+
expect(removed).toBe(false);
|
|
275
275
|
});
|
|
276
276
|
|
|
277
277
|
it('should delete and return the first element', () => {
|
|
278
278
|
list.push(1);
|
|
279
279
|
list.push(2);
|
|
280
280
|
const removed = list.deleteAt(0);
|
|
281
|
-
expect(removed).toBe(
|
|
281
|
+
expect(removed).toBe(true);
|
|
282
282
|
expect(list.toArray()).toEqual([2]);
|
|
283
283
|
});
|
|
284
284
|
|
|
@@ -286,7 +286,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
286
286
|
list.push(1);
|
|
287
287
|
list.push(2);
|
|
288
288
|
const removed = list.deleteAt(1);
|
|
289
|
-
expect(removed).toBe(
|
|
289
|
+
expect(removed).toBe(true);
|
|
290
290
|
expect(list.toArray()).toEqual([1]);
|
|
291
291
|
});
|
|
292
292
|
});
|
|
@@ -313,9 +313,9 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
313
313
|
|
|
314
314
|
describe('insert and toArray', () => {
|
|
315
315
|
it('should insert elements and return array correctly', () => {
|
|
316
|
-
list.
|
|
317
|
-
list.
|
|
318
|
-
list.
|
|
316
|
+
list.addAt(0, 1);
|
|
317
|
+
list.addAt(1, 3);
|
|
318
|
+
list.addAt(1, 2);
|
|
319
319
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
320
320
|
});
|
|
321
321
|
});
|
|
@@ -377,7 +377,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
377
377
|
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
378
378
|
|
|
379
379
|
const newObj = { keyA: 2.5 }; // Corrected newObj value
|
|
380
|
-
const insertSuccess = objectList.
|
|
380
|
+
const insertSuccess = objectList.addBefore(obj2, newObj);
|
|
381
381
|
expect(insertSuccess).toBe(true);
|
|
382
382
|
|
|
383
383
|
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
@@ -404,7 +404,7 @@ describe('SinglyLinkedList', () => {
|
|
|
404
404
|
it('should initialize an empty list', () => {
|
|
405
405
|
expect(list.head).toBe(undefined);
|
|
406
406
|
expect(list.tail).toBe(undefined);
|
|
407
|
-
expect(list.
|
|
407
|
+
expect(list.size).toBe(0);
|
|
408
408
|
});
|
|
409
409
|
|
|
410
410
|
it('should push elements to the end of the list', () => {
|
|
@@ -412,7 +412,7 @@ describe('SinglyLinkedList', () => {
|
|
|
412
412
|
list.push(2);
|
|
413
413
|
expect(list.head!.value).toBe(1);
|
|
414
414
|
expect(list.tail!.value).toBe(2);
|
|
415
|
-
expect(list.
|
|
415
|
+
expect(list.size).toBe(2);
|
|
416
416
|
});
|
|
417
417
|
|
|
418
418
|
it('should pop elements from the end of the list', () => {
|
|
@@ -422,7 +422,7 @@ describe('SinglyLinkedList', () => {
|
|
|
422
422
|
expect(popped).toBe(2);
|
|
423
423
|
expect(list.head!.value).toBe(1);
|
|
424
424
|
expect(list.tail!.value).toBe(1);
|
|
425
|
-
expect(list.
|
|
425
|
+
expect(list.size).toBe(1);
|
|
426
426
|
});
|
|
427
427
|
|
|
428
428
|
it('should reverse the list', () => {
|
|
@@ -66,11 +66,11 @@ describe('SkipList', () => {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
it('getFirst() should return the getFirst element', () => {
|
|
69
|
-
expect(skipList.
|
|
69
|
+
expect(skipList.first).toBe('One');
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
it('getLast() should return the getLast element', () => {
|
|
73
|
-
expect(skipList.
|
|
73
|
+
expect(skipList.last).toBe('Four');
|
|
74
74
|
});
|
|
75
75
|
|
|
76
76
|
it('higher(key) should return the getFirst element greater than the given key', () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Deque } from '../../../../src';
|
|
2
|
-
import { isDebugTest } from '../../../config';
|
|
2
|
+
// import { isDebugTest } from '../../../config';
|
|
3
3
|
|
|
4
|
-
const isDebug = isDebugTest;
|
|
4
|
+
// const isDebug = isDebugTest;
|
|
5
5
|
|
|
6
6
|
describe('Deque - Basic Operations', () => {
|
|
7
7
|
let deque: Deque<number>;
|
|
@@ -57,10 +57,10 @@ describe('Deque - Complex Operations', () => {
|
|
|
57
57
|
deque = new Deque<number>();
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
-
test('
|
|
60
|
+
test('addAt should insert elements at the specified position', () => {
|
|
61
61
|
deque.push(1);
|
|
62
62
|
deque.push(3);
|
|
63
|
-
deque.
|
|
63
|
+
deque.addAt(1, 2);
|
|
64
64
|
expect(deque.toArray()).toEqual([1, 2, 3]);
|
|
65
65
|
});
|
|
66
66
|
|
|
@@ -111,7 +111,7 @@ describe('Deque - Complex Operations', () => {
|
|
|
111
111
|
deque.push(1);
|
|
112
112
|
deque.push(2);
|
|
113
113
|
deque.sort((a, b) => a - b);
|
|
114
|
-
expect(deque
|
|
114
|
+
expect([...deque]).toEqual([1, 2, 3]);
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
test('shrinkToFit should reduce the memory footprint', () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LinkedListQueue, Queue } from '../../../../src';
|
|
2
|
-
import { isDebugTest } from '../../../config';
|
|
2
|
+
// import { isDebugTest } from '../../../config';
|
|
3
3
|
|
|
4
|
-
const isDebug = isDebugTest;
|
|
4
|
+
// const isDebug = isDebugTest;
|
|
5
5
|
|
|
6
6
|
describe('Queue', () => {
|
|
7
7
|
let queue: Queue<number>;
|
|
@@ -224,7 +224,7 @@ describe('LinkedListQueue', () => {
|
|
|
224
224
|
queue.enqueue('A');
|
|
225
225
|
queue.enqueue('B');
|
|
226
226
|
expect(queue.peek()).toBe('A');
|
|
227
|
-
expect(queue.
|
|
227
|
+
expect(queue.size).toBe(2);
|
|
228
228
|
});
|
|
229
229
|
|
|
230
230
|
it('should dequeue elements from the front of the queue', () => {
|
|
@@ -233,7 +233,7 @@ describe('LinkedListQueue', () => {
|
|
|
233
233
|
const dequeued = queue.dequeue();
|
|
234
234
|
expect(dequeued).toBe('A');
|
|
235
235
|
expect(queue.peek()).toBe('B');
|
|
236
|
-
expect(queue.
|
|
236
|
+
expect(queue.size).toBe(1);
|
|
237
237
|
});
|
|
238
238
|
|
|
239
239
|
it('should peek at the front of the queue', () => {
|
|
@@ -843,7 +843,7 @@ describe('Trie class', () => {
|
|
|
843
843
|
|
|
844
844
|
test('filter should return words that satisfy the predicate', () => {
|
|
845
845
|
const filteredWords = trie.filter(word => word.startsWith('ba'));
|
|
846
|
-
expect(filteredWords).toEqual(['banana', 'band', 'bandana']);
|
|
846
|
+
expect([...filteredWords]).toEqual(['banana', 'band', 'bandana']);
|
|
847
847
|
});
|
|
848
848
|
|
|
849
849
|
test('map should apply a function to each word', () => {
|