data-structure-typed 1.48.1 → 1.48.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.
Files changed (114) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +182 -182
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  15. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  16. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/cjs/data-structures/hash/hash-map.js +69 -173
  19. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  20. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  21. package/dist/cjs/data-structures/heap/heap.js +60 -30
  22. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  23. package/dist/cjs/data-structures/index.d.ts +1 -0
  24. package/dist/cjs/data-structures/index.js +1 -0
  25. package/dist/cjs/data-structures/index.js.map +1 -1
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  27. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  30. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  33. package/dist/cjs/data-structures/queue/deque.js +36 -71
  34. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  35. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/cjs/data-structures/queue/queue.js +69 -82
  37. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  38. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  39. package/dist/cjs/data-structures/stack/stack.js +50 -31
  40. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  41. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  42. package/dist/cjs/data-structures/trie/trie.js +53 -32
  43. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/cjs/types/data-structures/base/base.js +3 -0
  46. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  47. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  48. package/dist/cjs/types/data-structures/base/index.js +18 -0
  49. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  50. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  51. package/dist/cjs/types/data-structures/index.js +1 -0
  52. package/dist/cjs/types/data-structures/index.js.map +1 -1
  53. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  54. package/dist/mjs/data-structures/base/index.js +1 -0
  55. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  56. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  57. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  58. package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
  59. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  60. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
  62. package/dist/mjs/data-structures/hash/hash-map.js +69 -173
  63. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  64. package/dist/mjs/data-structures/heap/heap.js +60 -30
  65. package/dist/mjs/data-structures/index.d.ts +1 -0
  66. package/dist/mjs/data-structures/index.js +1 -0
  67. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  68. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  69. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  70. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  71. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  72. package/dist/mjs/data-structures/queue/deque.js +36 -71
  73. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  74. package/dist/mjs/data-structures/queue/queue.js +66 -79
  75. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  76. package/dist/mjs/data-structures/stack/stack.js +50 -31
  77. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  78. package/dist/mjs/data-structures/trie/trie.js +53 -32
  79. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  80. package/dist/mjs/types/data-structures/base/base.js +1 -0
  81. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  82. package/dist/mjs/types/data-structures/base/index.js +1 -0
  83. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  84. package/dist/mjs/types/data-structures/index.js +1 -0
  85. package/dist/umd/data-structure-typed.js +846 -715
  86. package/dist/umd/data-structure-typed.min.js +2 -2
  87. package/dist/umd/data-structure-typed.min.js.map +1 -1
  88. package/package.json +1 -1
  89. package/src/data-structures/base/index.ts +1 -0
  90. package/src/data-structures/base/iterable-base.ts +329 -0
  91. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  92. package/src/data-structures/graph/abstract-graph.ts +55 -28
  93. package/src/data-structures/hash/hash-map.ts +76 -185
  94. package/src/data-structures/heap/heap.ts +63 -36
  95. package/src/data-structures/index.ts +1 -0
  96. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  97. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  98. package/src/data-structures/queue/deque.ts +40 -82
  99. package/src/data-structures/queue/queue.ts +72 -87
  100. package/src/data-structures/stack/stack.ts +53 -34
  101. package/src/data-structures/trie/trie.ts +58 -35
  102. package/src/types/data-structures/base/base.ts +6 -0
  103. package/src/types/data-structures/base/index.ts +1 -0
  104. package/src/types/data-structures/index.ts +1 -0
  105. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
  107. package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
  108. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  109. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
  110. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  111. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  112. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  113. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  114. package/test/unit/data-structures/queue/deque.test.ts +25 -0
@@ -524,30 +524,30 @@ describe('RedBlackTree iterative methods test', () => {
524
524
 
525
525
  test('forEach should iterate over all elements', () => {
526
526
  const mockCallback = jest.fn();
527
- rbTree.forEach((entry) => {
528
- mockCallback(entry);
527
+ rbTree.forEach((value, key) => {
528
+ mockCallback(value, key);
529
529
  });
530
530
 
531
531
  expect(mockCallback.mock.calls.length).toBe(3);
532
- expect(mockCallback.mock.calls[0][0]).toEqual([1, 'a']);
533
- expect(mockCallback.mock.calls[1][0]).toEqual([2, 'b']);
534
- expect(mockCallback.mock.calls[2][0]).toEqual([3, 'c']);
532
+ expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
533
+ expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
534
+ expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
535
535
  });
536
536
 
537
537
  test('filter should return a new tree with filtered elements', () => {
538
- const filteredTree = rbTree.filter(([key]) => key > 1);
538
+ const filteredTree = rbTree.filter((value, key) => key > 1);
539
539
  expect(filteredTree.size).toBe(2);
540
540
  expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
541
541
  });
542
542
 
543
543
  test('map should return a new tree with modified elements', () => {
544
- const mappedTree = rbTree.map(([key]) => (key * 2).toString());
544
+ const mappedTree = rbTree.map((value, key) => (key * 2).toString());
545
545
  expect(mappedTree.size).toBe(3);
546
546
  expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
547
547
  });
548
548
 
549
549
  test('reduce should accumulate values', () => {
550
- const sum = rbTree.reduce((acc, [key]) => acc + key, 0);
550
+ const sum = rbTree.reduce((acc, value, key) => acc + key, 0);
551
551
  expect(sum).toBe(6);
552
552
  });
553
553
 
@@ -619,30 +619,30 @@ describe('TreeMultimap iterative methods test', () => {
619
619
 
620
620
  test('forEach should iterate over all elements', () => {
621
621
  const mockCallback = jest.fn();
622
- treeMM.forEach((entry) => {
623
- mockCallback(entry);
622
+ treeMM.forEach((value, key) => {
623
+ mockCallback(value, key);
624
624
  });
625
625
 
626
626
  expect(mockCallback.mock.calls.length).toBe(3);
627
- expect(mockCallback.mock.calls[0][0]).toEqual([1, 'a']);
628
- expect(mockCallback.mock.calls[1][0]).toEqual([2, 'b']);
629
- expect(mockCallback.mock.calls[2][0]).toEqual([3, 'c']);
627
+ expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
628
+ expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
629
+ expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
630
630
  });
631
631
 
632
632
  test('filter should return a new tree with filtered elements', () => {
633
- const filteredTree = treeMM.filter(([key]) => key > 1);
633
+ const filteredTree = treeMM.filter((value, key) => key > 1);
634
634
  expect(filteredTree.size).toBe(2);
635
635
  expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
636
636
  });
637
637
 
638
638
  test('map should return a new tree with modified elements', () => {
639
- const mappedTree = treeMM.map(([key]) => (key * 2).toString());
639
+ const mappedTree = treeMM.map((value, key) => (key * 2).toString());
640
640
  expect(mappedTree.size).toBe(3);
641
641
  expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
642
642
  });
643
643
 
644
644
  test('reduce should accumulate values', () => {
645
- const sum = treeMM.reduce((acc, [key]) => acc + key, 0);
645
+ const sum = treeMM.reduce((acc, value, key) => acc + key, 0);
646
646
  expect(sum).toBe(6);
647
647
  });
648
648
 
@@ -665,11 +665,11 @@ describe('TreeMultimap iterative methods test', () => {
665
665
 
666
666
  test('should keys', () => {
667
667
  const keys = treeMM.keys();
668
- expect(keys).toEqual([1, 2, 3]);
668
+ expect([...keys]).toEqual([1, 2, 3]);
669
669
  });
670
670
 
671
671
  test('should values', () => {
672
672
  const values = treeMM.values();
673
- expect(values).toEqual(['a', 'b', 'c']);
673
+ expect([...values]).toEqual(['a', 'b', 'c']);
674
674
  });
675
675
  });
@@ -616,22 +616,22 @@ describe('DirectedGraph iterative Methods', () => {
616
616
 
617
617
  test('forEach should apply a function to each vertex', () => {
618
618
  const result: VertexKey[] = [];
619
- graph.forEach(vertex => result.push(vertex[0]));
619
+ graph.forEach((value, key) => key && result.push(key));
620
620
  expect(result).toEqual(vertices);
621
621
  });
622
622
 
623
623
  test('filter should return vertices that satisfy the condition', () => {
624
- const filtered = graph.filter(vertex => vertex[0] === 'A' || vertex[0] === 'B');
624
+ const filtered = graph.filter((value, vertex) => vertex === 'A' || vertex === 'B');
625
625
  expect(filtered).toEqual([["A", undefined], ["B", undefined]]);
626
626
  });
627
627
 
628
628
  test('map should apply a function to each vertex and return a new array', () => {
629
- const mapped = graph.map(vertex => vertex[0] + '_mapped');
629
+ const mapped = graph.map((value, vertex) => vertex + '_mapped');
630
630
  expect(mapped).toEqual(vertices.map(v => v + '_mapped'));
631
631
  });
632
632
 
633
633
  test('reduce should accumulate a value based on each vertex', () => {
634
- const concatenated = graph.reduce((acc, vertex) => acc + vertex[0], '');
634
+ const concatenated = graph.reduce((acc, value, key) => acc + key, '');
635
635
  expect(concatenated).toBe(vertices.join(''));
636
636
  });
637
637
  });
@@ -548,10 +548,10 @@ describe('LinkedHashMap setMany, keys, values', () => {
548
548
  })
549
549
 
550
550
  test('keys', () => {
551
- expect(hm.keys()).toEqual([2, 3, 4, 5, 6])
551
+ expect([...hm.keys()]).toEqual([2, 3, 4, 5, 6])
552
552
  });
553
553
 
554
554
  test('values', () => {
555
- expect(hm.values()).toEqual([2, 3, 4, 5, 6])
555
+ expect([...hm.values()]).toEqual([2, 3, 4, 5, 6])
556
556
  });
557
557
  });
@@ -397,3 +397,31 @@ describe('DoublyLinkedList Operation Test', () => {
397
397
  expect(shiftedObj).toBe(obj1);
398
398
  });
399
399
  });
400
+
401
+
402
+ describe('iterable methods', () => {
403
+ it('should forEach, some, every, filter, map, reduce of the deque', () => {
404
+ const dl = new DoublyLinkedList<number>()
405
+ dl.push(1);
406
+ dl.push(2);
407
+ dl.push(3);
408
+
409
+ const mockCallback = jest.fn();
410
+ dl.forEach((element) => {
411
+ mockCallback(element);
412
+ });
413
+
414
+ expect(mockCallback.mock.calls.length).toBe(3);
415
+ expect(mockCallback.mock.calls[0]).toEqual([1]);
416
+ expect(mockCallback.mock.calls[1]).toEqual([2]);
417
+ expect(mockCallback.mock.calls[2]).toEqual([3]);
418
+
419
+ expect(dl.every(element => element > 0)).toBe(true);
420
+ expect(dl.every(element => element > 1)).toBe(false);
421
+ expect(dl.some(element => element > 2)).toBe(true);
422
+
423
+ expect([...dl.filter(element => element > 2)]).toEqual([3]);
424
+ expect([...dl.map(element => element * 2)]).toEqual([2, 4, 6]);
425
+ expect(dl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
426
+ });
427
+ });
@@ -471,3 +471,28 @@ describe('SinglyLinkedList', () => {
471
471
  expect(list1.reduce((acc, value) => acc + value, 0)).toEqual(6);
472
472
  });
473
473
  });
474
+
475
+
476
+ describe('iterable methods', () => {
477
+ it('should forEach, some, every, filter, map, reduce of the deque', () => {
478
+ const sl = new SinglyLinkedList<number>([1, 2, 3])
479
+
480
+ const mockCallback = jest.fn();
481
+ sl.forEach((element) => {
482
+ mockCallback(element);
483
+ });
484
+
485
+ expect(mockCallback.mock.calls.length).toBe(3);
486
+ expect(mockCallback.mock.calls[0]).toEqual([1]);
487
+ expect(mockCallback.mock.calls[1]).toEqual([2]);
488
+ expect(mockCallback.mock.calls[2]).toEqual([3]);
489
+
490
+ expect(sl.every(element => element > 0)).toBe(true);
491
+ expect(sl.every(element => element > 1)).toBe(false);
492
+ expect(sl.some(element => element > 2)).toBe(true);
493
+
494
+ expect([...sl.filter(element => element > 2)]).toEqual([3]);
495
+ expect([...sl.map(element => element * 2)]).toEqual([2, 4, 6]);
496
+ expect(sl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
497
+ });
498
+ });
@@ -447,4 +447,29 @@ describe('Deque', () => {
447
447
  });
448
448
  });
449
449
 
450
+ describe('iterable methods', () => {
451
+ it('should forEach, some, every, filter, map, reduce of the deque', () => {
452
+ deque.push(1);
453
+ deque.push(2);
454
+ deque.push(3);
455
+
456
+ const mockCallback = jest.fn();
457
+ deque.forEach((element) => {
458
+ mockCallback(element);
459
+ });
460
+
461
+ expect(mockCallback.mock.calls.length).toBe(3);
462
+ expect(mockCallback.mock.calls[0]).toEqual([1]);
463
+ expect(mockCallback.mock.calls[1]).toEqual([2]);
464
+ expect(mockCallback.mock.calls[2]).toEqual([3]);
465
+
466
+ expect(deque.every(element => element > 0)).toBe(true);
467
+ expect(deque.every(element => element > 1)).toBe(false);
468
+ expect(deque.some(element => element > 2)).toBe(true);
469
+
470
+ expect([...deque.filter(element => element > 2)]).toEqual([3]);
471
+ expect([...deque.map(element => element * 2)]).toEqual([2, 4, 6]);
472
+ expect(deque.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
473
+ });
474
+ });
450
475
  });