data-structure-typed 1.52.4 → 1.52.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.
Files changed (183) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/README_zh-CN.md +216 -26
  4. package/benchmark/report.html +13 -13
  5. package/benchmark/report.json +158 -158
  6. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +1 -37
  7. package/dist/cjs/data-structures/base/iterable-element-base.js +1 -37
  8. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  9. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +2 -54
  10. package/dist/cjs/data-structures/base/iterable-entry-base.js +1 -49
  11. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -32
  13. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +9 -41
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -46
  16. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -46
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +82 -147
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +300 -332
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -40
  22. package/dist/cjs/data-structures/binary-tree/bst.js +12 -44
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -48
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +2 -50
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -32
  28. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +9 -41
  29. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  30. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -75
  31. package/dist/cjs/data-structures/graph/abstract-graph.js +0 -75
  32. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/directed-graph.d.ts +0 -98
  34. package/dist/cjs/data-structures/graph/directed-graph.js +0 -98
  35. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +0 -50
  37. package/dist/cjs/data-structures/graph/undirected-graph.js +0 -50
  38. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/hash/hash-map.d.ts +5 -92
  40. package/dist/cjs/data-structures/hash/hash-map.js +27 -111
  41. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/heap.d.ts +0 -32
  43. package/dist/cjs/data-structures/heap/heap.js +0 -32
  44. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  45. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +5 -88
  46. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +5 -88
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  48. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +1 -83
  49. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +2 -84
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +1 -35
  52. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +1 -35
  53. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/deque.d.ts +1 -98
  55. package/dist/cjs/data-structures/queue/deque.js +3 -99
  56. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  57. package/dist/cjs/data-structures/queue/queue.d.ts +1 -54
  58. package/dist/cjs/data-structures/queue/queue.js +0 -53
  59. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  60. package/dist/cjs/data-structures/stack/stack.d.ts +1 -34
  61. package/dist/cjs/data-structures/stack/stack.js +1 -34
  62. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  63. package/dist/cjs/data-structures/tree/tree.js +2 -1
  64. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +0 -64
  66. package/dist/cjs/data-structures/trie/trie.js +0 -64
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +8 -0
  69. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +6 -0
  70. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  71. package/dist/cjs/types/utils/utils.d.ts +13 -12
  72. package/dist/cjs/utils/number.d.ts +13 -0
  73. package/dist/cjs/utils/number.js +13 -0
  74. package/dist/cjs/utils/number.js.map +1 -1
  75. package/dist/cjs/utils/utils.d.ts +125 -3
  76. package/dist/cjs/utils/utils.js +177 -21
  77. package/dist/cjs/utils/utils.js.map +1 -1
  78. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +1 -37
  79. package/dist/mjs/data-structures/base/iterable-element-base.js +1 -37
  80. package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +2 -54
  81. package/dist/mjs/data-structures/base/iterable-entry-base.js +1 -49
  82. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -32
  83. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +9 -41
  84. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -46
  85. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -46
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +82 -147
  87. package/dist/mjs/data-structures/binary-tree/binary-tree.js +299 -333
  88. package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -40
  89. package/dist/mjs/data-structures/binary-tree/bst.js +12 -44
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -48
  91. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -50
  92. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -32
  93. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +9 -41
  94. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -75
  95. package/dist/mjs/data-structures/graph/abstract-graph.js +0 -75
  96. package/dist/mjs/data-structures/graph/directed-graph.d.ts +0 -98
  97. package/dist/mjs/data-structures/graph/directed-graph.js +0 -98
  98. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +0 -50
  99. package/dist/mjs/data-structures/graph/undirected-graph.js +0 -50
  100. package/dist/mjs/data-structures/hash/hash-map.d.ts +5 -92
  101. package/dist/mjs/data-structures/hash/hash-map.js +27 -111
  102. package/dist/mjs/data-structures/heap/heap.d.ts +0 -32
  103. package/dist/mjs/data-structures/heap/heap.js +0 -32
  104. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +5 -88
  105. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +5 -88
  106. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +1 -83
  107. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -84
  108. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +1 -35
  109. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +1 -35
  110. package/dist/mjs/data-structures/queue/deque.d.ts +1 -98
  111. package/dist/mjs/data-structures/queue/deque.js +3 -99
  112. package/dist/mjs/data-structures/queue/queue.d.ts +1 -54
  113. package/dist/mjs/data-structures/queue/queue.js +0 -53
  114. package/dist/mjs/data-structures/stack/stack.d.ts +1 -34
  115. package/dist/mjs/data-structures/stack/stack.js +1 -34
  116. package/dist/mjs/data-structures/tree/tree.js +2 -1
  117. package/dist/mjs/data-structures/trie/trie.d.ts +0 -64
  118. package/dist/mjs/data-structures/trie/trie.js +0 -64
  119. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +8 -0
  120. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +5 -1
  121. package/dist/mjs/types/utils/utils.d.ts +13 -12
  122. package/dist/mjs/utils/number.d.ts +13 -0
  123. package/dist/mjs/utils/number.js +13 -0
  124. package/dist/mjs/utils/utils.d.ts +125 -3
  125. package/dist/mjs/utils/utils.js +177 -21
  126. package/dist/umd/data-structure-typed.js +411 -1475
  127. package/dist/umd/data-structure-typed.min.js +5 -4
  128. package/dist/umd/data-structure-typed.min.js.map +1 -1
  129. package/package.json +6 -6
  130. package/src/data-structures/base/iterable-element-base.ts +2 -42
  131. package/src/data-structures/base/iterable-entry-base.ts +3 -62
  132. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +8 -48
  133. package/src/data-structures/binary-tree/avl-tree.ts +0 -57
  134. package/src/data-structures/binary-tree/binary-tree.ts +330 -359
  135. package/src/data-structures/binary-tree/bst.ts +11 -54
  136. package/src/data-structures/binary-tree/rb-tree.ts +2 -62
  137. package/src/data-structures/binary-tree/tree-multi-map.ts +8 -48
  138. package/src/data-structures/graph/abstract-graph.ts +0 -92
  139. package/src/data-structures/graph/directed-graph.ts +0 -122
  140. package/src/data-structures/graph/undirected-graph.ts +0 -62
  141. package/src/data-structures/hash/hash-map.ts +29 -133
  142. package/src/data-structures/heap/heap.ts +0 -40
  143. package/src/data-structures/linked-list/doubly-linked-list.ts +5 -112
  144. package/src/data-structures/linked-list/singly-linked-list.ts +2 -104
  145. package/src/data-structures/linked-list/skip-linked-list.ts +1 -44
  146. package/src/data-structures/queue/deque.ts +2 -125
  147. package/src/data-structures/queue/queue.ts +1 -68
  148. package/src/data-structures/stack/stack.ts +1 -43
  149. package/src/data-structures/tree/tree.ts +1 -1
  150. package/src/data-structures/trie/trie.ts +0 -80
  151. package/src/types/data-structures/binary-tree/binary-tree.ts +8 -1
  152. package/src/types/utils/utils.ts +17 -15
  153. package/src/utils/number.ts +13 -0
  154. package/src/utils/utils.ts +174 -18
  155. package/test/config.ts +8 -0
  156. package/test/integration/all-in-one.test.ts +1 -1
  157. package/test/integration/avl-tree.test.ts +1 -1
  158. package/test/integration/bst.test.ts +2 -2
  159. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +13 -13
  160. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +18 -13
  161. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +577 -167
  162. package/test/unit/data-structures/binary-tree/bst.test.ts +167 -23
  163. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  164. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +136 -13
  165. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +18 -13
  166. package/test/unit/data-structures/graph/directed-graph.test.ts +46 -32
  167. package/test/unit/data-structures/graph/map-graph.test.ts +24 -2
  168. package/test/unit/data-structures/graph/undirected-graph.test.ts +24 -24
  169. package/test/unit/data-structures/hash/hash-map.test.ts +225 -35
  170. package/test/unit/data-structures/heap/heap.test.ts +47 -39
  171. package/test/unit/data-structures/heap/min-heap.test.ts +5 -5
  172. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +34 -4
  173. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +32 -0
  174. package/test/unit/data-structures/matrix/matrix.test.ts +35 -5
  175. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +31 -0
  176. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +18 -0
  177. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +17 -0
  178. package/test/unit/data-structures/queue/deque.test.ts +288 -47
  179. package/test/unit/data-structures/queue/queue.test.ts +62 -37
  180. package/test/unit/data-structures/stack/stack.test.ts +30 -5
  181. package/test/unit/data-structures/tree/tree.test.ts +58 -0
  182. package/test/unit/data-structures/trie/trie.test.ts +46 -5
  183. package/test/unit/utils/utils.test.ts +169 -0
@@ -9,6 +9,16 @@ describe('SinglyLinkedListNode', () => {
9
9
  });
10
10
  });
11
11
 
12
+ describe('SinglyLinkedList Initiate Test', () => {
13
+ it('should initiate with toElementFn', () => {
14
+ const sl = new SinglyLinkedList([{ key: 1 }, { key: 2 }, { key: 3 }, { key: 4 }, { key: 5 }], {
15
+ toElementFn: ({ key }) => key
16
+ });
17
+
18
+ expect([...sl]).toEqual([1, 2, 3, 4, 5]);
19
+ });
20
+ });
21
+
12
22
  describe('SinglyLinkedList Operation Test', () => {
13
23
  let list: SinglyLinkedList<number>;
14
24
  let objectList: SinglyLinkedList<{
@@ -95,6 +105,10 @@ describe('SinglyLinkedList Operation Test', () => {
95
105
  list.push(3);
96
106
  list.addAfter(2, 4);
97
107
  expect(list.toArray()).toEqual([1, 2, 4, 3]);
108
+ list.addAfter(list.getNode(2)!, 4);
109
+ expect(list.toArray()).toEqual([1, 2, 4, 4, 3]);
110
+ list.addAfter(list.getNode(3)!, 4);
111
+ expect(list.toArray()).toEqual([1, 2, 4, 4, 3, 4]);
98
112
  });
99
113
 
100
114
  it('should return false if the existing value is not found', () => {
@@ -132,12 +146,17 @@ describe('SinglyLinkedList Operation Test', () => {
132
146
  list.push(3);
133
147
  list.push(4);
134
148
  list.push(5);
149
+ expect(list.delete(undefined)).toBe(false);
135
150
  expect(list.delete(2)).toBe(true);
136
151
  expect(list.toArray()).toEqual([1, 3, 4, 5]);
137
152
  expect(list.delete(1)).toBe(true);
138
153
  expect(list.toArray()).toEqual([3, 4, 5]);
139
154
  expect(list.delete(5)).toBe(true);
140
155
  expect(list.toArray()).toEqual([3, 4]);
156
+ expect(list.delete(4)).toBe(true);
157
+ expect(list.toArray()).toEqual([3]);
158
+ expect(list.delete(3)).toBe(true);
159
+ expect(list.toArray()).toEqual([]);
141
160
  });
142
161
 
143
162
  it('should return false if the value is not found', () => {
@@ -229,11 +248,14 @@ describe('SinglyLinkedList Operation Test', () => {
229
248
 
230
249
  describe('addBefore', () => {
231
250
  it('should insert an element before an existing value', () => {
251
+ expect(list.addBefore(2, 4)).toBe(false);
232
252
  list.push(1);
233
253
  list.push(2);
234
254
  list.push(3);
235
255
  list.addBefore(2, 4);
236
256
  expect(list.toArray()).toEqual([1, 4, 2, 3]);
257
+ expect(list.addBefore(list.getNode(2)!, 4)).toBe(true);
258
+ expect([...list]).toEqual([1, 4, 4, 2, 3]);
237
259
  });
238
260
 
239
261
  it('should insert an element at the beginning', () => {
@@ -281,7 +303,11 @@ describe('SinglyLinkedList Operation Test', () => {
281
303
  it('should delete and return the first element', () => {
282
304
  list.push(1);
283
305
  list.push(2);
306
+ expect(list.first).toBe(1);
307
+ expect(list.last).toBe(2);
284
308
  const removed = list.deleteAt(0);
309
+ expect(list.first).toBe(2);
310
+ expect(list.last).toBe(2);
285
311
  expect(removed).toBe(true);
286
312
  expect(list.toArray()).toEqual([2]);
287
313
  });
@@ -321,6 +347,9 @@ describe('SinglyLinkedList Operation Test', () => {
321
347
  list.addAt(1, 3);
322
348
  list.addAt(1, 2);
323
349
  expect(list.toArray()).toEqual([1, 2, 3]);
350
+ expect(list.addAt(5, 5)).toBe(false);
351
+ expect(list.addAt(-1, -1)).toBe(false);
352
+ expect(list.toArray()).toEqual([1, 2, 3]);
324
353
  });
325
354
  });
326
355
 
@@ -354,6 +383,7 @@ describe('SinglyLinkedList Operation Test', () => {
354
383
 
355
384
  it('should clone', function () {
356
385
  const sList = new SinglyLinkedList<string>();
386
+ sList.delete('1');
357
387
  sList.push('1');
358
388
  sList.push('6');
359
389
  sList.push('2');
@@ -367,6 +397,8 @@ describe('SinglyLinkedList Operation Test', () => {
367
397
  sList.delete('5');
368
398
  expect([...sList]).toEqual(['1', '6', '0', '9']);
369
399
  expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
400
+ sList.delete(sList.getNode('0'));
401
+ expect([...sList]).toEqual(['1', '6', '9']);
370
402
  });
371
403
 
372
404
  describe('countOccurrences', () => {
@@ -22,6 +22,14 @@ describe('Matrix', () => {
22
22
  ]);
23
23
  });
24
24
 
25
+ it('should initiate with empty', () => {
26
+ matrix = new Matrix([], { rows: 2, cols: 2 });
27
+ expect(matrix.data).toEqual([
28
+ [0, 0],
29
+ [0, 0]
30
+ ]);
31
+ });
32
+
25
33
  it('should get a value at a specific position', () => {
26
34
  expect(matrix.get(1, 1)).toBe(0);
27
35
  expect(matrix.get(2, 2)).toBe(0);
@@ -213,7 +221,7 @@ describe('Matrix', () => {
213
221
  });
214
222
 
215
223
  describe('transpose', () => {
216
- test('should transpose a matrix with numeric values correctly', () => {
224
+ it('should transpose a matrix with numeric values correctly', () => {
217
225
  const originalMatrix = new Matrix([
218
226
  [1, 2, 3],
219
227
  [4, 5, 6],
@@ -231,7 +239,7 @@ describe('Matrix', () => {
231
239
  ]);
232
240
  });
233
241
 
234
- test('should transpose an empty matrix correctly', () => {
242
+ it('should transpose an empty matrix correctly', () => {
235
243
  const originalMatrix = new Matrix([]);
236
244
 
237
245
  const transposedMatrix = originalMatrix.transpose();
@@ -241,7 +249,7 @@ describe('Matrix', () => {
241
249
  expect(transposedMatrix.data).toEqual([]);
242
250
  });
243
251
 
244
- test('should throw an error when transposing a non-rectangular matrix', () => {
252
+ it('should throw an error when transposing a non-rectangular matrix', () => {
245
253
  const originalMatrix = new Matrix([
246
254
  [1, 2, 3],
247
255
  [4, 5]
@@ -292,10 +300,32 @@ describe('Matrix', () => {
292
300
  [-0.046511627906976744, -0.023255813953488372, 0.23255813953488372]
293
301
  ]);
294
302
  });
303
+
304
+ it('should throw error when cols does not equal to rows', () => {
305
+ const data: number[][] = [
306
+ [4, 7, 2],
307
+ [2, 6, 3]
308
+ ];
309
+
310
+ const matrix = new Matrix(data);
311
+ expect(() => matrix.inverse()).toThrow('Matrix must be square for inversion.');
312
+ });
313
+
314
+ it('should clone', () => {
315
+ const data: number[][] = [
316
+ [4, 7, 2],
317
+ [2, 6, 3]
318
+ ];
319
+
320
+ const matrix = new Matrix(data);
321
+ const cloned = matrix.clone();
322
+ expect(cloned instanceof Matrix).toBe(true);
323
+ expect(cloned.data).toEqual(data);
324
+ });
295
325
  });
296
326
 
297
327
  describe('dot', () => {
298
- test('should calculate the dot product of two matrices', () => {
328
+ it('should calculate the dot product of two matrices', () => {
299
329
  const matrix1 = new Matrix([
300
330
  [1, 2],
301
331
  [3, 4]
@@ -313,7 +343,7 @@ describe('Matrix', () => {
313
343
  ]);
314
344
  });
315
345
 
316
- test('should throw an error for incompatible matrices', () => {
346
+ it('should throw an error for incompatible matrices', () => {
317
347
  const matrix1 = new Matrix([
318
348
  [1, 2],
319
349
  [3, 4]
@@ -129,4 +129,35 @@ describe('MaxPriorityQueue Operation Test', () => {
129
129
 
130
130
  expect([...maxPQ.sort()]).toEqual([8, 7, 6, 5, 4, 3, 1]);
131
131
  });
132
+
133
+ it('should MaxPriorityQueue filter, map work well', function () {
134
+ const minPQ2 = new MaxPriorityQueue<number>([]);
135
+ minPQ2.refill([2, 5, 8, 1, 6, 7, 4]);
136
+
137
+ const cloned = minPQ2.clone();
138
+ const filtered = cloned.filter(item => item % 2 === 1);
139
+ expect(filtered instanceof MaxPriorityQueue).toBe(true);
140
+ expect([...filtered]).toEqual([7, 1, 5]);
141
+
142
+ const mapped = filtered.map(
143
+ item => ({ key: item }),
144
+ (a, b) => b.key - a.key
145
+ );
146
+ expect(mapped instanceof MaxPriorityQueue).toBe(true);
147
+ expect([...mapped]).toEqual([{ key: 7 }, { key: 1 }, { key: 5 }]);
148
+ });
149
+
150
+ it('should MaxPriorityQueue throw an error while initialed with object data', function () {
151
+ expect(() => {
152
+ new MaxPriorityQueue<{ key: number }>([{ key: 7 }, { key: 1 }, { key: 7 }]);
153
+ }).toThrow(
154
+ "When comparing object types, a custom comparator must be defined in the constructor's options parameter."
155
+ );
156
+ });
157
+
158
+ it('should MaxPriorityQueue comparator return 0 when equal values are added', function () {
159
+ const duplicated = new MaxPriorityQueue<number>([7, 1, 7, 7]);
160
+ expect(duplicated.size).toBe(4);
161
+ expect([...duplicated]).toEqual([7, 7, 7, 1]);
162
+ });
132
163
  });
@@ -60,4 +60,22 @@ describe('MinPriorityQueue Operation Test', () => {
60
60
  const sortedArray = priorityQueue.sort();
61
61
  expect(sortedArray).toEqual([1, 3, 5, 7]);
62
62
  });
63
+
64
+ it('should MinPriorityQueue filter, map work well', function () {
65
+ const minPQ2 = new MinPriorityQueue<number>([]);
66
+ minPQ2.refill([2, 5, 8, 1, 6, 7, 4]);
67
+
68
+ const cloned = minPQ2.clone();
69
+ const filtered = cloned.filter(item => item % 2 === 1);
70
+ expect(filtered instanceof MinPriorityQueue).toBe(true);
71
+ expect([...filtered]).toEqual([1, 5, 7]);
72
+
73
+ const mapped = filtered.map(
74
+ item => ({ key: item }),
75
+ (a, b) => a.key - b.key
76
+ );
77
+ expect(mapped instanceof MinPriorityQueue).toBe(true);
78
+ expect([...mapped]).toEqual([{ key: 1 }, { key: 5 }, { key: 7 }]);
79
+ expect(mapped.print()).toEqual([{ key: 1 }, { key: 5 }, { key: 7 }]);
80
+ });
63
81
  });
@@ -52,6 +52,23 @@ describe('PriorityQueue Operation Test', () => {
52
52
  expect(minPQ1.dfs('POST')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]);
53
53
  expect(minPQ1.dfs('PRE')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]);
54
54
  });
55
+
56
+ it('should PriorityQueue filter, map work well', function () {
57
+ const minPQ2 = new PriorityQueue<number>([], {
58
+ comparator: (a, b) => a - b
59
+ });
60
+ minPQ2.refill([2, 5, 8, 3, 1, 6, 7, 4]);
61
+ const filtered = minPQ2.filter(item => item % 2 === 1);
62
+ expect(filtered instanceof PriorityQueue).toBe(true);
63
+ expect([...filtered]).toEqual([1, 3, 5, 7]);
64
+
65
+ const mapped = filtered.map(
66
+ item => ({ key: item }),
67
+ (a, b) => a.key - b.key
68
+ );
69
+ expect(mapped instanceof PriorityQueue).toBe(true);
70
+ expect([...mapped]).toEqual([{ key: 1 }, { key: 3 }, { key: 5 }, { key: 7 }]);
71
+ });
55
72
  });
56
73
 
57
74
  describe('Priority Queue Performance Test', () => {