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
@@ -13,7 +13,7 @@ describe('HashMap', () => {
13
13
  expect(hashMap.isEmpty()).toBe(true);
14
14
  });
15
15
 
16
- it('should put and get values', () => {
16
+ it('should set and get values', () => {
17
17
  hashMap.set('one', 1);
18
18
  hashMap.set('two', 2);
19
19
  hashMap.set('three', 3);
@@ -122,15 +122,18 @@ describe('HashMap', () => {
122
122
  it('should add a key-value pair', () => {
123
123
  hashMap.set('key1', 'value1');
124
124
  expect(hashMap.get('key1')).toBe('value1');
125
+ expect(hashMap.hasValue('value1')).toBe(true);
126
+ expect(hashMap.hasValue('value2')).toBe(false);
125
127
  });
126
128
 
127
129
  it('should handle object keys correctly', () => {
128
130
  const keyObj = { id: 1 };
129
131
  hashMap.set(keyObj, 'objectValue');
132
+ expect(hashMap.has(keyObj)).toBe(true);
130
133
  expect(hashMap.get(keyObj)).toBe('objectValue');
131
134
  });
132
135
 
133
- test('Inheritability test', () => {
136
+ it('Inheritability test', () => {
134
137
  class ExtendedHashMap<K, V> extends HashMap<K, V> {
135
138
  someOtherParam?: string;
136
139
 
@@ -154,7 +157,7 @@ describe('HashMap', () => {
154
157
  expect(eHM.get('one')).toBe(1);
155
158
  });
156
159
 
157
- test('should raw elements toEntry', () => {
160
+ it('should raw elements toEntry', () => {
158
161
  const rawCollection = [
159
162
  { id: 1, name: 'item 1' },
160
163
  { id: 2, name: 'item 2' }
@@ -238,17 +241,19 @@ describe('HashMap', () => {
238
241
  describe('HashMap for coordinate object keys', () => {
239
242
  const hashMap: HashMap<[number, number], number> = new HashMap();
240
243
  const codObjs: [number, number][] = [];
241
-
242
- test('set elements in hash map', () => {
243
- for (let i = 0; i < 1000; i++) {
244
- const codObj: [number, number] = [getRandomInt(-10000, 10000), i];
245
- codObjs.push(codObj);
244
+ for (let i = 0; i < 10; i++) {
245
+ const codObj: [number, number] = [getRandomInt(-10000, 10000), i];
246
+ codObjs.push(codObj);
247
+ }
248
+ it('set elements in hash map', () => {
249
+ for (let i = 0; i < codObjs.length; i++) {
250
+ const codObj = codObjs[i];
246
251
  hashMap.set(codObj, i);
247
252
  }
248
253
  });
249
254
 
250
- test('get elements in hash map', () => {
251
- for (let i = 0; i < 1000; i++) {
255
+ it('get elements in hash map', () => {
256
+ for (let i = 0; i < codObjs.length; i++) {
252
257
  const codObj = codObjs[i];
253
258
  if (codObj) {
254
259
  expect(hashMap.get(codObj)).toBe(i);
@@ -256,8 +261,12 @@ describe('HashMap', () => {
256
261
  }
257
262
  });
258
263
 
259
- test('delete elements in hash map', () => {
260
- for (let i = 0; i < 1000; i++) {
264
+ it('should spread elements in hash map', () => {
265
+ expect([...hashMap]).toEqual(codObjs.map(codObj => [codObj, codObj[1]]));
266
+ });
267
+
268
+ it('delete elements in hash map', () => {
269
+ for (let i = 0; i < 10; i++) {
261
270
  if (i === 500) expect(hashMap.size).toBe(500);
262
271
  const codObj = codObjs[i];
263
272
  if (codObj) hashMap.delete(codObj);
@@ -285,17 +294,27 @@ describe('HashMap', () => {
285
294
  ]);
286
295
  });
287
296
 
288
- test('keys', () => {
297
+ it('keys', () => {
289
298
  expect([...hm.keys()]).toEqual([2, 3, 4, 5, 6]);
290
299
  });
291
300
 
292
- test('values', () => {
301
+ it('values', () => {
293
302
  expect([...hm.values()]).toEqual([2, 3, 4, 5, 6]);
294
303
  });
304
+
305
+ it('print', () => {
306
+ expect(hm.print()).toEqual([
307
+ [2, 2],
308
+ [3, 3],
309
+ [4, 4],
310
+ [5, 5],
311
+ [6, 6]
312
+ ]);
313
+ });
295
314
  });
296
315
 
297
316
  describe('HashMap HOF', () => {
298
- let hashMap: HashMap;
317
+ let hashMap: HashMap<string, string>;
299
318
 
300
319
  beforeEach(() => {
301
320
  hashMap = new HashMap<string, string>();
@@ -304,34 +323,91 @@ describe('HashMap', () => {
304
323
  hashMap.set('key3', 'value3');
305
324
  });
306
325
 
307
- test('every() returns true if all elements match the condition', () => {
326
+ it('every() returns true if all elements match the condition', () => {
308
327
  expect(hashMap.every(value => typeof value === 'string')).toBe(true);
309
328
  });
310
329
 
311
- test('some() returns true if any element matches the condition', () => {
330
+ it('some() returns true if any element matches the condition', () => {
312
331
  expect(hashMap.some((value, key) => key === 'key1')).toBe(true);
313
332
  });
314
333
 
315
- test('forEach() should execute a function for each element', () => {
334
+ it('forEach() should execute a function for each element', () => {
316
335
  const mockCallback = jest.fn();
317
336
  hashMap.forEach(mockCallback);
318
337
  expect(mockCallback.mock.calls.length).toBe(3);
319
338
  });
320
339
 
321
- test('map() should transform each element', () => {
340
+ it('map() should transform each element', () => {
322
341
  const newHashMap = hashMap.map(value => value.toUpperCase());
323
342
  expect(newHashMap.get('key1')).toBe('VALUE1');
324
343
  });
325
344
 
326
- test('filter() should remove elements that do not match the condition', () => {
345
+ it('filter() should remove elements that do not match the condition', () => {
327
346
  const filteredHashMap = hashMap.filter((value, key) => key !== 'key1');
328
347
  expect(filteredHashMap.has('key1')).toBe(false);
329
348
  });
330
349
 
331
- test('reduce() should accumulate values', () => {
350
+ it('reduce() should accumulate values', () => {
332
351
  const result = hashMap.reduce((acc, value) => acc + value, '');
333
352
  expect(result).toBe('value1value2value3');
334
353
  });
354
+
355
+ it('should spread in an array', () => {
356
+ expect([...hashMap]).toEqual([
357
+ ['key1', 'value1'],
358
+ ['key2', 'value2'],
359
+ ['key3', 'value3']
360
+ ]);
361
+ });
362
+
363
+ it('should find', () => {
364
+ const found = hashMap.find(value => value === 'value1');
365
+ expect(found).toEqual(['key1', 'value1']);
366
+ const notFound = hashMap.find(value => value === 'value6');
367
+ expect(notFound).toEqual(undefined);
368
+ });
369
+
370
+ it('should every', () => {
371
+ const isEvery = hashMap.every(value => value.substring(0, 5) === 'value');
372
+ expect(isEvery).toEqual(true);
373
+ const isEvery4 = hashMap.every(value => value.substring(0, 4) === 'value');
374
+ expect(isEvery4).toEqual(false);
375
+ });
376
+
377
+ it('should some', () => {
378
+ const isSome = hashMap.some(value => value.substring(5, 6) === '2');
379
+ expect(isSome).toEqual(true);
380
+ const isSome4 = hashMap.some(value => value.substring(0, 5) === 'value');
381
+ expect(isSome4).toEqual(true);
382
+ });
383
+
384
+ it('should forEach', () => {
385
+ hashMap.forEach((value, key, index) => expect(value.substring(5, 6)).toBe(String(index + 1)));
386
+ });
387
+
388
+ it('should entries', () => {
389
+ const entries = hashMap.entries();
390
+ expect(entries.next()).toEqual({ done: false, value: ['key1', 'value1'] });
391
+ expect(entries.next()).toEqual({ done: false, value: ['key2', 'value2'] });
392
+ expect(entries.next()).toEqual({ done: false, value: ['key3', 'value3'] });
393
+ expect(entries.next()).toEqual({ done: true, value: undefined });
394
+ });
395
+
396
+ it('should keys', () => {
397
+ const keys = hashMap.keys();
398
+ expect(keys.next()).toEqual({ done: false, value: 'key1' });
399
+ expect(keys.next()).toEqual({ done: false, value: 'key2' });
400
+ expect(keys.next()).toEqual({ done: false, value: 'key3' });
401
+ expect(keys.next()).toEqual({ done: true, value: undefined });
402
+ });
403
+
404
+ it('should values', () => {
405
+ const values = hashMap.values();
406
+ expect(values.next()).toEqual({ done: false, value: 'value1' });
407
+ expect(values.next()).toEqual({ done: false, value: 'value2' });
408
+ expect(values.next()).toEqual({ done: false, value: 'value3' });
409
+ expect(values.next()).toEqual({ done: true, value: undefined });
410
+ });
335
411
  });
336
412
  });
337
413
 
@@ -347,6 +423,7 @@ describe('LinkedHashMap', () => {
347
423
  });
348
424
 
349
425
  it('should add a key-value pair', () => {
426
+ expect(hashMap.first).toBe(undefined);
350
427
  hashMap.set('key1', 'value1');
351
428
  expect(hashMap.get('key1')).toBe('value1');
352
429
  });
@@ -441,29 +518,99 @@ describe('LinkedHashMap', () => {
441
518
  compareHashMaps(hashMap, stdMap);
442
519
  });
443
520
 
444
- test('should iterate correctly with reverse iterators', () => {
521
+ it('should iterate correctly with reverse iterators', () => {
445
522
  hashMap.set('key1', 'value1');
446
523
  hashMap.set('key2', 'value2');
447
524
  const iterator = hashMap.reverseBegin();
448
525
  expect(iterator.next().value).toEqual(['key2', 'value2']);
449
526
  });
450
527
 
451
- test('should return the last element', () => {
528
+ it('should return the last element', () => {
452
529
  hashMap.set('key1', 'value1');
453
530
  hashMap.set('key2', 'value2');
454
531
  expect(hashMap.last).toEqual(['key2', 'value2']);
455
532
  });
456
533
 
457
- test('should return undefined for empty map', () => {
534
+ it('should return undefined for empty map', () => {
458
535
  expect(hashMap.last).toBeUndefined();
459
536
  });
460
537
 
461
- test('should get element at specific index', () => {
538
+ it('should get element at specific index', () => {
462
539
  hashMap.set('key1', 'value1');
463
540
  hashMap.set('key2', 'value2');
464
541
  expect(hashMap.at(1)).toBe('value2');
465
542
  });
466
543
 
544
+ it('should hashFn, objHashFn, toEntryFn work well', () => {
545
+ const data: Array<{ name: number }> = [{ name: 1 }, { name: 2 }, { name: 3 }];
546
+ const hm = new LinkedHashMap(data, {
547
+ hashFn: key => String(key),
548
+ objHashFn: obj => obj,
549
+ toEntryFn: ({ name }) => [{ name }, name]
550
+ });
551
+
552
+ expect(hm.hashFn).toBeTruthy();
553
+ expect(hm.objHashFn).toBeTruthy();
554
+ expect(hm.toEntryFn).toBeTruthy();
555
+ expect([...hm]).toEqual([
556
+ [{ name: 1 }, 1],
557
+ [{ name: 2 }, 2],
558
+ [{ name: 3 }, 3]
559
+ ]);
560
+ });
561
+
562
+ it('should begin, reverseBegin', () => {
563
+ const data: Array<{ name: number }> = [{ name: 1 }, { name: 2 }, { name: 3 }];
564
+ const hm = new LinkedHashMap(data, {
565
+ hashFn: key => String(key),
566
+ objHashFn: obj => obj,
567
+ toEntryFn: ({ name }) => [{ name }, name]
568
+ });
569
+
570
+ expect(hm.begin().next()).toEqual({
571
+ done: false,
572
+ value: [
573
+ {
574
+ name: 1
575
+ },
576
+ 1
577
+ ]
578
+ });
579
+
580
+ expect(hm.reverseBegin().next()).toEqual({
581
+ done: false,
582
+ value: [
583
+ {
584
+ name: 3
585
+ },
586
+ 3
587
+ ]
588
+ });
589
+ });
590
+
591
+ it('should clone', () => {
592
+ hashMap = new LinkedHashMap<string, number>();
593
+
594
+ hashMap.set('one', 1);
595
+ hashMap.set('two', 2);
596
+ for (let i = 3; i <= 100; i++) {
597
+ hashMap.set(i.toString(), i);
598
+ }
599
+
600
+ expect(hashMap.get('one')).toBe(1);
601
+ expect(hashMap.get('two')).toBe(2);
602
+ expect(hashMap.get('86')).toBe(86);
603
+ expect(hashMap.size).toBe(100);
604
+ hashMap.delete('two');
605
+ expect(hashMap.size).toBe(99);
606
+
607
+ const cloned = hashMap.clone();
608
+ expect(cloned.get('one')).toBe(1);
609
+ expect(cloned.get('two')).toBe(undefined);
610
+ expect(cloned.get('86')).toBe(86);
611
+ expect(cloned.size).toBe(99);
612
+ });
613
+
467
614
  describe('LinkedHashMap basic', () => {
468
615
  let hashMap: LinkedHashMap<string, number>;
469
616
 
@@ -506,6 +653,9 @@ describe('LinkedHashMap', () => {
506
653
  hashMap.delete('one');
507
654
  expect(hashMap.get('one')).toBeUndefined();
508
655
  expect(hashMap.size).toBe(1);
656
+ hashMap.deleteAt(0);
657
+ // expect(hashMap.get('two')).toBe(undefined); // TODO #99
658
+ expect(hashMap.size).toBe(0);
509
659
  });
510
660
 
511
661
  it('should clear the LinkedHashMap', () => {
@@ -570,7 +720,7 @@ describe('LinkedHashMap', () => {
570
720
  const hashMap: LinkedHashMap<[number, number], number> = new LinkedHashMap();
571
721
  const codObjs: [number, number][] = [];
572
722
 
573
- test('set elements in hash map', () => {
723
+ it('set elements in hash map', () => {
574
724
  for (let i = 0; i < 1000; i++) {
575
725
  const codObj: [number, number] = [getRandomInt(-10000, 10000), i];
576
726
  codObjs.push(codObj);
@@ -578,7 +728,7 @@ describe('LinkedHashMap', () => {
578
728
  }
579
729
  });
580
730
 
581
- test('get elements in hash map', () => {
731
+ it('get elements in hash map', () => {
582
732
  for (let i = 0; i < 1000; i++) {
583
733
  const codObj = codObjs[i];
584
734
  if (codObj) {
@@ -587,7 +737,7 @@ describe('LinkedHashMap', () => {
587
737
  }
588
738
  });
589
739
 
590
- test('delete elements in hash map', () => {
740
+ it('delete elements in hash map', () => {
591
741
  for (let i = 0; i < 1000; i++) {
592
742
  if (i === 500) expect(hashMap.size).toBe(500);
593
743
  const codObj = codObjs[i];
@@ -616,15 +766,15 @@ describe('LinkedHashMap', () => {
616
766
  ]);
617
767
  });
618
768
 
619
- test('keys', () => {
769
+ it('keys', () => {
620
770
  expect([...hm.keys()]).toEqual([2, 3, 4, 5, 6]);
621
771
  });
622
772
 
623
- test('values', () => {
773
+ it('values', () => {
624
774
  expect([...hm.values()]).toEqual([2, 3, 4, 5, 6]);
625
775
  });
626
776
 
627
- test('entries', () => {
777
+ it('entries', () => {
628
778
  expect([...hm.entries()]).toEqual([
629
779
  [2, 2],
630
780
  [3, 3],
@@ -634,21 +784,61 @@ describe('LinkedHashMap', () => {
634
784
  ]);
635
785
  });
636
786
 
637
- test('every', () => {
787
+ it('every', () => {
638
788
  expect(hm.every(value => value > 4)).toBe(false);
639
789
  });
640
790
 
641
- test('some', () => {
791
+ it('some', () => {
642
792
  expect(hm.some(value => value > 6)).toBe(false);
643
793
  });
644
794
 
645
- test('hasValue', () => {
795
+ it('hasValue', () => {
646
796
  expect(hm.hasValue(3)).toBe(true);
647
797
  expect(hm.hasValue(7)).toBe(false);
648
798
  });
649
799
 
650
- test('print', () => {
800
+ it('print', () => {
651
801
  // hm.print();
652
802
  });
653
803
  });
804
+
805
+ describe('HashMap HOF', () => {
806
+ let hashMap: LinkedHashMap;
807
+
808
+ beforeEach(() => {
809
+ hashMap = new LinkedHashMap<string, string>();
810
+ hashMap.set('key1', 'value1');
811
+ hashMap.set('key2', 'value2');
812
+ hashMap.set('key3', 'value3');
813
+ });
814
+
815
+ it('every() returns true if all elements match the condition', () => {
816
+ expect(hashMap.every(value => typeof value === 'string')).toBe(true);
817
+ });
818
+
819
+ it('some() returns true if any element matches the condition', () => {
820
+ expect(hashMap.some((value, key) => key === 'key1')).toBe(true);
821
+ });
822
+
823
+ it('forEach() should execute a function for each element', () => {
824
+ const mockCallback = jest.fn();
825
+ hashMap.forEach(mockCallback);
826
+ expect(mockCallback.mock.calls.length).toBe(3);
827
+ });
828
+
829
+ it('map() should transform each element', () => {
830
+ const newHashMap = hashMap.map(value => value.toUpperCase());
831
+ expect(newHashMap.get('key1')).toBe('VALUE1');
832
+ });
833
+
834
+ it('filter() should remove elements that do not match the condition', () => {
835
+ const filteredHashMap = hashMap.filter((value, key) => key !== 'key1');
836
+ expect(filteredHashMap.has('key1')).toBe(false);
837
+ });
838
+
839
+ it('reduce() should accumulate values', () => {
840
+ const result = hashMap.reduce((acc, value) => acc + value, '');
841
+ expect(result).toBe('value1value2value3');
842
+ });
843
+ });
654
844
  });
@@ -2,9 +2,15 @@ import { FibonacciHeap, Heap, MaxHeap, MinHeap } from '../../../../src';
2
2
  import { logBigOMetricsWrap } from '../../../utils';
3
3
 
4
4
  describe('Heap Operation Test', () => {
5
+ it('should initiate heap', function () {
6
+ const hp = new Heap<number>();
7
+ hp.add(1);
8
+ expect(hp.size).toBe(1);
9
+ });
10
+
5
11
  it('should heap add and delete work well', function () {
6
12
  const hp = new MinHeap<number>();
7
-
13
+ expect(hp.delete(1)).toBe(false);
8
14
  hp.add(2);
9
15
  hp.add(3);
10
16
  hp.add(1);
@@ -23,6 +29,7 @@ describe('Heap Operation Test', () => {
23
29
 
24
30
  it('should numeric heap work well', function () {
25
31
  const minNumHeap = new MinHeap<number>();
32
+ expect(minNumHeap.poll()).toBe(undefined);
26
33
  minNumHeap.add(1);
27
34
  minNumHeap.add(6);
28
35
  minNumHeap.add(2);
@@ -219,8 +226,47 @@ describe('Heap Operation Test', () => {
219
226
 
220
227
  expect([...heap.sort()]).toEqual([1, 3, 4, 5, 6, 7, 8]);
221
228
  });
229
+
230
+ it('should getter leaf', function () {
231
+ const hp = new Heap<number>();
232
+ expect(hp.leaf).toBe(undefined);
233
+ hp.add(1);
234
+ expect(hp.leaf).toBe(1);
235
+ });
236
+
237
+ it('should error', function () {
238
+ expect(() => {
239
+ new Heap([{ key: 1 }, { key: 2 }, { key: 3 }]);
240
+ }).toThrow(
241
+ "When comparing object types, a custom comparator must be defined in the constructor's options parameter."
242
+ );
243
+ });
222
244
  });
223
245
 
246
+ describe('Heap HOF', () => {
247
+ let hp: Heap;
248
+
249
+ beforeEach(() => {
250
+ hp = new Heap([{ key: 1 }, { key: 2 }, { key: 3 }], { comparator: (a, b) => a.key - b.key });
251
+ });
252
+
253
+ it('should filter', () => {
254
+ const filtered = hp.filter(({ key }) => key % 2 === 1);
255
+ expect([...filtered]).toEqual([{ key: 1 }, { key: 3 }]);
256
+ });
257
+
258
+ it('should map', () => {
259
+ const mapped = hp.map(
260
+ ({ key }) => [key, key],
261
+ (a, b) => a[0] - b[0]
262
+ );
263
+ expect([...mapped]).toEqual([
264
+ [1, 1],
265
+ [2, 2],
266
+ [3, 3]
267
+ ]);
268
+ });
269
+ });
224
270
  describe('FibonacciHeap', () => {
225
271
  let heap: FibonacciHeap<number>;
226
272
 
@@ -411,41 +457,3 @@ describe('FibonacciHeap Stress Test', () => {
411
457
  );
412
458
  });
413
459
  });
414
-
415
- // describe('Competitor performance compare', () => {
416
- // const minHeap = new MinHeap<number>();
417
- // const cHeap = new CHeap<number>();
418
- // const cPQ = new PriorityQueue<number>(undefined, (a, b) => a - b);
419
- // const n = 100000;
420
- //
421
- // it('should add performance well', () => {
422
- // const heapCost = calcRunTime(() => {
423
- // for (let i = 0; i < n; i++) {
424
- // minHeap.add(i);
425
- // }
426
- // })
427
- //
428
- // console.log(`heapCost: ${heapCost}`)
429
- // });
430
- //
431
- // it('should add performance well', () => {
432
- //
433
- // const cHeapCost = calcRunTime(() => {
434
- // for (let i = 0; i < n; i++) {
435
- // cHeap.push(i);
436
- // }
437
- // })
438
- //
439
- // console.log(`cHeapCost: ${cHeapCost}`)
440
- // });
441
- //
442
- // it('should add performance well', () => {
443
- //
444
- // const cPQCost = calcRunTime(() => {
445
- // for (let i = 0; i < n; i++) {
446
- // cPQ.push(i);
447
- // }
448
- // })
449
- // console.log(`cPQCost: ${cPQCost}`)
450
- // });
451
- // });
@@ -90,22 +90,22 @@ describe('Heap iterative methods', () => {
90
90
  }
91
91
  });
92
92
 
93
- test('Heap is iterable', () => {
93
+ it('Heap is iterable', () => {
94
94
  expect([...heap]).toEqual([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
95
95
  });
96
96
 
97
- test('forEach method calls a function for each element', () => {
97
+ it('forEach method calls a function for each element', () => {
98
98
  const mockCallback = jest.fn();
99
99
  heap.forEach(mockCallback);
100
100
  expect(mockCallback.mock.calls.length).toBe(10);
101
101
  });
102
102
 
103
- test('filter method returns filtered elements', () => {
103
+ it('filter method returns filtered elements', () => {
104
104
  const result = heap.filter(x => x > 50);
105
105
  expect([...result]).toEqual([60, 70, 80, 90, 100]);
106
106
  });
107
107
 
108
- test('map method correctly maps elements', () => {
108
+ it('map method correctly maps elements', () => {
109
109
  const result = heap.map(
110
110
  x => x / 10,
111
111
  (a: number, b: number) => a - b
@@ -113,7 +113,7 @@ describe('Heap iterative methods', () => {
113
113
  expect([...result]).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
114
114
  });
115
115
 
116
- test('reduce method correctly reduces elements', () => {
116
+ it('reduce method correctly reduces elements', () => {
117
117
  const result = heap.reduce((acc, curr) => acc + curr, 0);
118
118
  expect(result).toBe(550); // 10+20+...+100 = 550
119
119
  });
@@ -20,14 +20,27 @@ describe('DoublyLinkedList Operation Test', () => {
20
20
  expect(list.getNodeAt(-1)).toBe(undefined);
21
21
  expect(list.getNodeAt(5)).toBe(undefined);
22
22
  expect(list.addAt(5, 6)).toBe(true);
23
+ expect(list.addAt(-1, 6)).toBe(false);
24
+ expect(list.addAt(7, 6)).toBe(false);
25
+ expect(list.addAt(100, 6)).toBe(false);
23
26
  });
24
27
 
25
28
  it('should addBefore', () => {
26
29
  expect(list.addBefore(1, 0)).toBe(true);
30
+ expect(list.addBefore(list.getNode(1)!, 2)).toBe(true);
31
+ expect([...list]).toEqual([0, 2, 1, 2, 3, 4, 5]);
27
32
  });
28
33
 
29
34
  it('should deleteAt', () => {
30
- expect(list.deleteAt(1)).toBeTruthy();
35
+ expect(list.deleteAt(1)).toBe(true);
36
+ expect(list.deleteAt(-1)).toBe(false);
37
+ expect(list.deleteAt(list.size)).toBe(false);
38
+ expect(list.size).toBe(4);
39
+ expect(list.deleteAt(4)).toBe(false);
40
+ expect([...list]).toEqual([1, 3, 4, 5]);
41
+ expect(list.isEmpty()).toBe(false);
42
+ expect(list.deleteAt(3)).toBe(true);
43
+ expect([...list]).toEqual([1, 3, 4]);
31
44
  });
32
45
 
33
46
  it('should delete tail', () => {
@@ -52,6 +65,7 @@ describe('DoublyLinkedList Operation Test', () => {
52
65
  dList.delete('5');
53
66
  expect([...dList]).toEqual(['1', '6', '0', '9']);
54
67
  expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
68
+ expect(cloned.print()).toEqual(['1', '6', '0', '5', '9']);
55
69
  });
56
70
 
57
71
  it('should find undefined', () => {
@@ -94,6 +108,13 @@ describe('DoublyLinkedList Operation Test', () => {
94
108
  expect(list.tail).toBe(undefined);
95
109
  });
96
110
 
111
+ it('should initialize with toElementFn', () => {
112
+ const dl = new DoublyLinkedList([{ key: 1 }, { key: 2 }, { key: 3 }], { toElementFn: ({ key }) => key });
113
+ expect([...dl]).toEqual([1, 2, 3]);
114
+ expect(dl.first).toBe(1);
115
+ expect(dl.last).toBe(3);
116
+ });
117
+
97
118
  it('should push elements to the list', () => {
98
119
  list.push(1);
99
120
  list.push(2);
@@ -111,8 +132,13 @@ describe('DoublyLinkedList Operation Test', () => {
111
132
  expect(list.size).toBe(1);
112
133
  expect(list.head!.value).toBe(1);
113
134
  expect(list.tail!.value).toBe(1);
135
+ list.pop();
136
+ expect([...list]).toEqual([]);
137
+ list.pop();
138
+ expect([...list]).toEqual([]);
114
139
  });
115
140
  it('should insert elements at specific positions', () => {
141
+ expect(list.at(0)).toBe(undefined);
116
142
  list.push(1);
117
143
  list.push(2);
118
144
  list.push(3);
@@ -134,6 +160,8 @@ describe('DoublyLinkedList Operation Test', () => {
134
160
  expect(list.size).toBe(6);
135
161
  expect(list.at(5)).toBe(4);
136
162
  expect(list.tail!.value).toBe(4);
163
+ expect(list.at(-1)).toBe(undefined);
164
+ expect(list.at(6)).toBe(undefined);
137
165
  });
138
166
 
139
167
  it('should delete elements at specific positions', () => {
@@ -445,9 +473,11 @@ describe('iterable methods', () => {
445
473
  expect(dl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
446
474
  });
447
475
 
448
- test('values', () => {
476
+ it('values', () => {
449
477
  const dl = new DoublyLinkedList<number>();
450
- dl.push(1);
478
+ dl.shift();
479
+ expect([...dl]).toEqual([]);
480
+ dl.unshift(1);
451
481
  dl.push(2);
452
482
  dl.push(3);
453
483
  dl.delete(2);
@@ -458,7 +488,7 @@ describe('iterable methods', () => {
458
488
  expect([...dl.values()]).toEqual([3, 1]);
459
489
  });
460
490
 
461
- test('some', () => {
491
+ it('some', () => {
462
492
  const dl = new DoublyLinkedList<number>();
463
493
  dl.push(1);
464
494
  dl.push(2);