data-structure-typed 1.41.6 → 1.41.7

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 (107) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +11 -11
  3. package/benchmark/report.html +11 -11
  4. package/benchmark/report.json +111 -111
  5. package/dist/cjs/src/data-structures/graph/abstract-graph.js +5 -5
  6. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  7. package/dist/mjs/src/data-structures/graph/abstract-graph.js +5 -5
  8. package/dist/umd/data-structure-typed.min.js +1 -1
  9. package/dist/umd/data-structure-typed.min.js.map +1 -1
  10. package/package.json +5 -5
  11. package/src/data-structures/graph/abstract-graph.ts +6 -6
  12. package/test/config.ts +1 -0
  13. package/test/integration/avl-tree.test.ts +110 -0
  14. package/test/integration/bst.test.ts +385 -0
  15. package/test/integration/heap.test.js +16 -0
  16. package/test/integration/index.html +51 -0
  17. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +36 -0
  18. package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
  19. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +45 -0
  20. package/test/performance/data-structures/binary-tree/bst.test.ts +36 -0
  21. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  22. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +0 -0
  23. package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
  24. package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
  25. package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
  26. package/test/performance/data-structures/graph/directed-graph.test.ts +49 -0
  27. package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
  28. package/test/performance/data-structures/graph/overall.test.ts +0 -0
  29. package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
  30. package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
  31. package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
  32. package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
  33. package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
  34. package/test/performance/data-structures/heap/heap.test.ts +30 -0
  35. package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
  36. package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
  37. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
  38. package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
  39. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
  40. package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
  41. package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
  42. package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
  43. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  44. package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
  45. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  46. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
  47. package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
  48. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
  49. package/test/performance/data-structures/queue/deque.test.ts +21 -0
  50. package/test/performance/data-structures/queue/queue.test.ts +25 -0
  51. package/test/performance/data-structures/stack/stack.test.ts +0 -0
  52. package/test/performance/data-structures/tree/tree.test.ts +0 -0
  53. package/test/performance/data-structures/trie/trie.test.ts +22 -0
  54. package/test/performance/reportor.ts +186 -0
  55. package/test/performance/types/index.ts +1 -0
  56. package/test/performance/types/reportor.ts +3 -0
  57. package/test/types/index.ts +1 -0
  58. package/test/types/utils/big-o.ts +1 -0
  59. package/test/types/utils/index.ts +2 -0
  60. package/test/types/utils/json2html.ts +1 -0
  61. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +269 -0
  62. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +320 -0
  63. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +486 -0
  64. package/test/unit/data-structures/binary-tree/bst.test.ts +840 -0
  65. package/test/unit/data-structures/binary-tree/overall.test.ts +66 -0
  66. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +435 -0
  67. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
  68. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +542 -0
  69. package/test/unit/data-structures/graph/abstract-graph.test.ts +100 -0
  70. package/test/unit/data-structures/graph/directed-graph.test.ts +564 -0
  71. package/test/unit/data-structures/graph/map-graph.test.ts +126 -0
  72. package/test/unit/data-structures/graph/overall.test.ts +49 -0
  73. package/test/unit/data-structures/graph/salty-edges.json +1 -0
  74. package/test/unit/data-structures/graph/salty-vertexes.json +1 -0
  75. package/test/unit/data-structures/graph/undirected-graph.test.ts +168 -0
  76. package/test/unit/data-structures/hash/coordinate-map.test.ts +74 -0
  77. package/test/unit/data-structures/hash/coordinate-set.test.ts +66 -0
  78. package/test/unit/data-structures/hash/hash-map.test.ts +103 -0
  79. package/test/unit/data-structures/hash/hash-table.test.ts +186 -0
  80. package/test/unit/data-structures/heap/heap.test.ts +254 -0
  81. package/test/unit/data-structures/heap/max-heap.test.ts +52 -0
  82. package/test/unit/data-structures/heap/min-heap.test.ts +52 -0
  83. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +400 -0
  84. package/test/unit/data-structures/linked-list/linked-list.test.ts +8 -0
  85. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +474 -0
  86. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
  87. package/test/unit/data-structures/linked-list/skip-list.test.ts +86 -0
  88. package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
  89. package/test/unit/data-structures/matrix/matrix2d.test.ts +345 -0
  90. package/test/unit/data-structures/matrix/navigator.test.ts +244 -0
  91. package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
  92. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +73 -0
  93. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +63 -0
  94. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +53 -0
  95. package/test/unit/data-structures/queue/deque.test.ts +410 -0
  96. package/test/unit/data-structures/queue/queue.test.ts +207 -0
  97. package/test/unit/data-structures/stack/stack.test.ts +67 -0
  98. package/test/unit/data-structures/tree/tree.test.ts +39 -0
  99. package/test/unit/data-structures/trie/trie.test.ts +825 -0
  100. package/test/utils/array.ts +5514 -0
  101. package/test/utils/big-o.ts +207 -0
  102. package/test/utils/console.ts +31 -0
  103. package/test/utils/index.ts +7 -0
  104. package/test/utils/is.ts +56 -0
  105. package/test/utils/json2html.ts +322 -0
  106. package/test/utils/number.ts +13 -0
  107. package/test/utils/string.ts +1 -0
@@ -0,0 +1,49 @@
1
+ import {DirectedGraph, UndirectedGraph} from '../../../../src';
2
+
3
+ describe('Overall Graph Operation Test', () => {
4
+ it('Overall DirectedGraph Operation Test', () => {
5
+ const graph = new DirectedGraph();
6
+
7
+ graph.addVertex('A');
8
+ graph.addVertex('B');
9
+
10
+ graph.hasVertex('A'); // true
11
+ graph.hasVertex('B'); // true
12
+ graph.hasVertex('C'); // false
13
+ expect(graph.hasVertex('A')).toBe(true); // true
14
+ expect(graph.hasVertex('B')).toBe(true); // true
15
+ expect(graph.hasVertex('C')).toBe(false); // false
16
+
17
+ graph.addEdge('A', 'B');
18
+ graph.hasEdge('A', 'B'); // true
19
+ graph.hasEdge('B', 'A'); // false
20
+ expect(graph.hasEdge('A', 'B')).toBe(true); // true
21
+ expect(graph.hasEdge('B', 'A')).toBe(false); // false
22
+
23
+ graph.deleteEdgeSrcToDest('A', 'B');
24
+ graph.hasEdge('A', 'B'); // false
25
+ expect(graph.hasEdge('A', 'B')).toBe(false); // false
26
+
27
+ graph.addVertex('C');
28
+
29
+ graph.addEdge('A', 'B');
30
+ graph.addEdge('B', 'C');
31
+
32
+ const topologicalOrderKeys = graph.topologicalSort();
33
+ expect(topologicalOrderKeys).toEqual(['A', 'B', 'C']);
34
+ });
35
+ it('Overall UndirectedGraph Operation Test', () => {
36
+ const graph = new UndirectedGraph();
37
+ graph.addVertex('A');
38
+ graph.addVertex('B');
39
+ graph.addVertex('C');
40
+ graph.addVertex('D');
41
+ graph.deleteVertex('C');
42
+ graph.addEdge('A', 'B');
43
+ graph.addEdge('B', 'D');
44
+
45
+ const dijkstraResult = graph.dijkstra('A');
46
+ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key); // ['A', 'B', 'D']
47
+ expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key)).toEqual(['A', 'B', 'D']);
48
+ });
49
+ });
@@ -0,0 +1 @@
1
+ [[{"name":"Intersection_1"},{"name":"Intersection_2","weight":28}],[{"name":"Intersection_1"},{"name":"Intersection_14","weight":44}],[{"name":"Intersection_2"},{"name":"Intersection_3","weight":8}],[{"name":"Intersection_2"},{"name":"Intersection_14","weight":44}],[{"name":"Intersection_3"},{"name":"Intersection_4","weight":31}],[{"name":"Intersection_3"},{"name":"Intersection_22","weight":45}],[{"name":"Intersection_4"},{"name":"Intersection_5","weight":37}],[{"name":"Intersection_4"},{"name":"Intersection_24","weight":40}],[{"name":"Intersection_5"},{"name":"Intersection_6","weight":32}],[{"name":"Intersection_5"},{"name":"Intersection_26","weight":34}],[{"name":"Intersection_6"},{"name":"Intersection_7","weight":30}],[{"name":"Intersection_7"},{"name":"Intersection_8","weight":53}],[{"name":"Intersection_7"},{"name":"Intersection_27","weight":0}],[{"name":"Intersection_8"},{"name":"Intersection_9","weight":33}],[{"name":"Intersection_8"},{"name":"Intersection_47","weight":38}],[{"name":"Intersection_9"},{"name":"Intersection_10","weight":28}],[{"name":"Intersection_9"},{"name":"Intersection_62","weight":31}],[{"name":"Intersection_10"},{"name":"Intersection_11","weight":18}],[{"name":"Intersection_10"},{"name":"Intersection_59","weight":45}],[{"name":"Intersection_11"},{"name":"Intersection_12","weight":28}],[{"name":"Intersection_11"},{"name":"Intersection_58","weight":21}],[{"name":"Intersection_12"},{"name":"Intersection_13","weight":35}],[{"name":"Intersection_12"},{"name":"Intersection_57","weight":22}],[{"name":"Intersection_14"},{"name":"Intersection_15","weight":40}],[{"name":"Intersection_15"},{"name":"Intersection_16","weight":43}],[{"name":"Intersection_15"},{"name":"Intersection_23","weight":27}],[{"name":"Intersection_16"},{"name":"Intersection_17","weight":14}],[{"name":"Intersection_17"},{"name":"Intersection_18","weight":51}],[{"name":"Intersection_18"},{"name":"Intersection_19","weight":29}],[{"name":"Intersection_19"},{"name":"Intersection_20","weight":34}],[{"name":"Intersection_20"},{"name":"Intersection_21","weight":43}],[{"name":"Intersection_20"},{"name":"Intersection_66","weight":24}],[{"name":"Intersection_21"},{"name":"Intersection_52","weight":36}],[{"name":"Intersection_22"},{"name":"Intersection_23","weight":27}],[{"name":"Intersection_23"},{"name":"Intersection_33","weight":10}],[{"name":"Intersection_24"},{"name":"Intersection_25","weight":24}],[{"name":"Intersection_24"},{"name":"Intersection_22","weight":45}],[{"name":"Intersection_25"},{"name":"Intersection_34","weight":23}],[{"name":"Intersection_26"},{"name":"Intersection_30","weight":5}],[{"name":"Intersection_27"},{"name":"Intersection_28","weight":25}],[{"name":"Intersection_27"},{"name":"Intersection_45","weight":6}],[{"name":"Intersection_28"},{"name":"Intersection_29","weight":21}],[{"name":"Intersection_29"},{"name":"Intersection_26","weight":34}],[{"name":"Intersection_29"},{"name":"Intersection_41","weight":23}],[{"name":"Intersection_30"},{"name":"Intersection_24","weight":40}],[{"name":"Intersection_30"},{"name":"Intersection_31","weight":18}],[{"name":"Intersection_31"},{"name":"Intersection_32","weight":11}],[{"name":"Intersection_32"},{"name":"Intersection_35","weight":21}],[{"name":"Intersection_33"},{"name":"Intersection_25","weight":24}],[{"name":"Intersection_33"},{"name":"Intersection_42","weight":32}],[{"name":"Intersection_34"},{"name":"Intersection_32","weight":11}],[{"name":"Intersection_34"},{"name":"Intersection_42","weight":32}],[{"name":"Intersection_35"},{"name":"Intersection_36","weight":25}],[{"name":"Intersection_35"},{"name":"Intersection_43","weight":23}],[{"name":"Intersection_36"},{"name":"Intersection_37","weight":37}],[{"name":"Intersection_37"},{"name":"Intersection_49","weight":19}],[{"name":"Intersection_38"},{"name":"Intersection_39","weight":29}],[{"name":"Intersection_38"},{"name":"Intersection_46","weight":31}],[{"name":"Intersection_39"},{"name":"Intersection_40","weight":22}],[{"name":"Intersection_39"},{"name":"Intersection_46","weight":31}],[{"name":"Intersection_40"},{"name":"Intersection_41","weight":23}],[{"name":"Intersection_41"},{"name":"Intersection_31","weight":18}],[{"name":"Intersection_42"},{"name":"Intersection_16","weight":43}],[{"name":"Intersection_42"},{"name":"Intersection_43","weight":23}],[{"name":"Intersection_43"},{"name":"Intersection_44","weight":23}],[{"name":"Intersection_43"},{"name":"Intersection_17","weight":14}],[{"name":"Intersection_45"},{"name":"Intersection_39","weight":29}],[{"name":"Intersection_46"},{"name":"Intersection_48","weight":4}],[{"name":"Intersection_47"},{"name":"Intersection_38","weight":0}],[{"name":"Intersection_47"},{"name":"Intersection_45","weight":6}],[{"name":"Intersection_48"},{"name":"Intersection_37","weight":37}],[{"name":"Intersection_48"},{"name":"Intersection_63","weight":38}],[{"name":"Intersection_49"},{"name":"Intersection_19","weight":29}],[{"name":"Intersection_49"},{"name":"Intersection_51","weight":42}],[{"name":"Intersection_50"},{"name":"Intersection_49","weight":19}],[{"name":"Intersection_51"},{"name":"Intersection_66","weight":24}],[{"name":"Intersection_52"},{"name":"Intersection_53","weight":31}],[{"name":"Intersection_53"},{"name":"Intersection_54","weight":42}],[{"name":"Intersection_54"},{"name":"Intersection_55","weight":18}],[{"name":"Intersection_54"},{"name":"Intersection_61","weight":28}],[{"name":"Intersection_55"},{"name":"Intersection_56","weight":15}],[{"name":"Intersection_56"},{"name":"Intersection_13","weight":35}],[{"name":"Intersection_57"},{"name":"Intersection_56","weight":15}],[{"name":"Intersection_58"},{"name":"Intersection_57","weight":22}],[{"name":"Intersection_58"},{"name":"Intersection_65","weight":19}],[{"name":"Intersection_59"},{"name":"Intersection_60","weight":40}],[{"name":"Intersection_59"},{"name":"Intersection_62","weight":31}],[{"name":"Intersection_60"},{"name":"Intersection_53","weight":31}],[{"name":"Intersection_61"},{"name":"Intersection_59","weight":45}],[{"name":"Intersection_62"},{"name":"Intersection_47","weight":38}],[{"name":"Intersection_62"},{"name":"Intersection_63","weight":38}],[{"name":"Intersection_63"},{"name":"Intersection_64","weight":16}],[{"name":"Intersection_64"},{"name":"Intersection_60","weight":40}],[{"name":"Intersection_64"},{"name":"Intersection_51","weight":42}],[{"name":"Intersection_65"},{"name":"Intersection_61","weight":28}],[{"name":"Intersection_65"},{"name":"Intersection_55","weight":18}],[{"name":"Intersection_66"},{"name":"Intersection_52","weight":36}]]
@@ -0,0 +1 @@
1
+ [{"name":"Intersection_1"},{"name":"Intersection_2"},{"name":"Intersection_3"},{"name":"Intersection_4"},{"name":"Intersection_5"},{"name":"Intersection_6"},{"name":"Intersection_7"},{"name":"Intersection_8"},{"name":"Intersection_9"},{"name":"Intersection_10"},{"name":"Intersection_11"},{"name":"Intersection_12"},{"name":"Intersection_13"},{"name":"Intersection_14"},{"name":"Intersection_15"},{"name":"Intersection_16"},{"name":"Intersection_17"},{"name":"Intersection_18"},{"name":"Intersection_19"},{"name":"Intersection_20"},{"name":"Intersection_21"},{"name":"Intersection_22"},{"name":"Intersection_23"},{"name":"Intersection_24"},{"name":"Intersection_25"},{"name":"Intersection_26"},{"name":"Intersection_27"},{"name":"Intersection_28"},{"name":"Intersection_29"},{"name":"Intersection_30"},{"name":"Intersection_31"},{"name":"Intersection_32"},{"name":"Intersection_33"},{"name":"Intersection_34"},{"name":"Intersection_35"},{"name":"Intersection_36"},{"name":"Intersection_37"},{"name":"Intersection_38"},{"name":"Intersection_39"},{"name":"Intersection_40"},{"name":"Intersection_41"},{"name":"Intersection_42"},{"name":"Intersection_43"},{"name":"Intersection_44"},{"name":"Intersection_45"},{"name":"Intersection_46"},{"name":"Intersection_47"},{"name":"Intersection_48"},{"name":"Intersection_49"},{"name":"Intersection_50"},{"name":"Intersection_51"},{"name":"Intersection_52"},{"name":"Intersection_53"},{"name":"Intersection_54"},{"name":"Intersection_55"},{"name":"Intersection_56"},{"name":"Intersection_57"},{"name":"Intersection_58"},{"name":"Intersection_59"},{"name":"Intersection_60"},{"name":"Intersection_61"},{"name":"Intersection_62"},{"name":"Intersection_63"},{"name":"Intersection_64"},{"name":"Intersection_65"},{"name":"Intersection_66"}]
@@ -0,0 +1,168 @@
1
+ import {UndirectedEdge, UndirectedGraph, UndirectedVertex} from '../../../../src';
2
+ import saltyVertexes from './SaltyGateStreetIntersections.json';
3
+ import saltyEdges from './SaltyGateStreetConnections.json';
4
+
5
+ describe('UndirectedGraph Operation Test', () => {
6
+ let graph: UndirectedGraph;
7
+
8
+ beforeEach(() => {
9
+ graph = new UndirectedGraph();
10
+ });
11
+
12
+ it('should edge cases', () => {
13
+ expect(graph.deleteEdge(new UndirectedEdge('c', 'd'))).toBe(null);
14
+ expect(graph.deleteEdgeBetween('c', 'd')).toBe(null);
15
+ expect(graph.degreeOf('c')).toBe(0);
16
+ expect(graph.edgesOf('c').length).toBe(0);
17
+ expect(graph.getEndsOfEdge(new UndirectedEdge('c', 'd'))).toBe(null);
18
+ });
19
+
20
+ it('should add vertices', () => {
21
+ const vertex1 = new UndirectedVertex('A');
22
+ const vertex2 = new UndirectedVertex('B');
23
+
24
+ graph.addVertex(vertex1);
25
+ graph.addVertex(vertex2);
26
+
27
+ expect(graph.hasVertex(vertex1)).toBe(true);
28
+ expect(graph.hasVertex(vertex2)).toBe(true);
29
+ });
30
+
31
+ it('should add edges', () => {
32
+ const vertex1 = new UndirectedVertex('A');
33
+ const vertex2 = new UndirectedVertex('B');
34
+ const edge = new UndirectedEdge('A', 'B');
35
+
36
+ graph.addVertex(vertex1);
37
+ graph.addVertex(vertex2);
38
+ graph.addEdge(edge);
39
+
40
+ expect(graph.hasEdge('A', 'B')).toBe(true);
41
+ expect(graph.hasEdge('B', 'A')).toBe(true);
42
+ });
43
+
44
+ it('should delete edges', () => {
45
+ const vertex1 = new UndirectedVertex('A');
46
+ const vertex2 = new UndirectedVertex('B');
47
+ const edge = new UndirectedEdge('A', 'B');
48
+
49
+ graph.addVertex(vertex1);
50
+ graph.addVertex(vertex2);
51
+ graph.addEdge(edge);
52
+
53
+ expect(graph.deleteEdge(edge)).toBe(edge);
54
+ expect(graph.hasEdge('A', 'B')).toBe(false);
55
+ });
56
+
57
+ it('should perform topological sort', () => {
58
+ graph.addVertex('A');
59
+ graph.addVertex('B');
60
+ graph.addVertex('C');
61
+ graph.addVertex('D');
62
+ graph.deleteVertex('C');
63
+ graph.addEdge('A', 'B');
64
+ graph.addEdge('B', 'D');
65
+
66
+ const dijkstraResult = graph.dijkstra('A');
67
+ expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key)).toEqual(['A', 'B', 'D']);
68
+ });
69
+ });
70
+
71
+ describe('UndirectedGraph', () => {
72
+ let undirectedGraph: UndirectedGraph<string, string>;
73
+
74
+ beforeEach(() => {
75
+ // Create a new UndirectedGraph instance before each test
76
+ undirectedGraph = new UndirectedGraph<string, string>();
77
+ });
78
+
79
+ // Test adding vertices to the graph
80
+ it('should add vertices to the graph', () => {
81
+ const vertexA = new UndirectedVertex('A', 'Location A');
82
+ const vertexB = new UndirectedVertex('B', 'Location B');
83
+
84
+ undirectedGraph.addVertex(vertexA);
85
+ undirectedGraph.addVertex(vertexB);
86
+
87
+ expect(undirectedGraph.hasVertex('A')).toBe(true);
88
+ expect(undirectedGraph.hasVertex('B')).toBe(true);
89
+ });
90
+
91
+ // Test adding edges to the graph
92
+ it('should add edges to the graph', () => {
93
+ const vertexA = new UndirectedVertex('A', 'Location A');
94
+ const vertexB = new UndirectedVertex('B', 'Location B');
95
+ const edgeAB = new UndirectedEdge('A', 'B', 1, 'Edge between A and B');
96
+
97
+ undirectedGraph.addVertex(vertexA);
98
+ undirectedGraph.addVertex(vertexB);
99
+ undirectedGraph.addEdge(edgeAB);
100
+
101
+ expect(undirectedGraph.hasEdge('A', 'B')).toBe(true);
102
+ });
103
+
104
+ // Test getting neighbors of a vertex
105
+ it('should return the neighbors of a vertex', () => {
106
+ const vertexA = new UndirectedVertex('A', 'Location A');
107
+ const vertexB = new UndirectedVertex('B', 'Location B');
108
+ const vertexC = new UndirectedVertex('C', 'Location C');
109
+ const edgeAB = new UndirectedEdge('A', 'B', 1, 'Edge between A and B');
110
+ const edgeBC = new UndirectedEdge('B', 'C', 2, 'Edge between B and C');
111
+
112
+ undirectedGraph.addVertex(vertexA);
113
+ undirectedGraph.addVertex(vertexB);
114
+ undirectedGraph.addVertex(vertexC);
115
+ undirectedGraph.addEdge(edgeAB);
116
+ undirectedGraph.addEdge(edgeBC);
117
+
118
+ const neighborsOfA = undirectedGraph.getNeighbors('A');
119
+ const neighborsOfB = undirectedGraph.getNeighbors('B');
120
+
121
+ expect(neighborsOfA).toEqual([vertexB]);
122
+ expect(neighborsOfB).toEqual([vertexA, vertexC]);
123
+ });
124
+
125
+ // Test degree of a vertex
126
+ it('should return the degree of a vertex', () => {
127
+ const vertexA = new UndirectedVertex('A', 'Location A');
128
+ const vertexB = new UndirectedVertex('B', 'Location B');
129
+ const vertexC = new UndirectedVertex('C', 'Location C');
130
+ const edgeAB = new UndirectedEdge('A', 'B', 3, 'Edge between A and B');
131
+ const edgeBC = new UndirectedEdge('B', 'C', 4, 'Edge between B and C');
132
+
133
+ edgeAB.vertices = edgeAB.vertices;
134
+ expect(undirectedGraph.edges.size).toBe(0);
135
+ undirectedGraph.addVertex(vertexA);
136
+ undirectedGraph.addVertex(vertexB);
137
+ undirectedGraph.addVertex(vertexC);
138
+ undirectedGraph.addEdge(edgeAB);
139
+ undirectedGraph.addEdge(edgeBC);
140
+
141
+ const degreeOfA = undirectedGraph.degreeOf('A');
142
+ const degreeOfB = undirectedGraph.degreeOf('B');
143
+ const degreeOfC = undirectedGraph.degreeOf('C');
144
+ expect(undirectedGraph.edgeSet().length).toBe(2);
145
+ expect(undirectedGraph.getEndsOfEdge(edgeBC)?.length).toBe(2);
146
+
147
+ expect(degreeOfA).toBe(1);
148
+ expect(degreeOfB).toBe(2);
149
+ expect(degreeOfC).toBe(1);
150
+ });
151
+
152
+ it('xxx', () => {
153
+ // const start = performance.now();
154
+ const graph = new UndirectedGraph<{ name: string }, number >()
155
+ for (const v of saltyVertexes) {
156
+ graph.addVertex(v.name, v);
157
+ }
158
+ for (const e of saltyEdges) {
159
+ const [s,d] = e;
160
+ graph.addEdge( s.name, d.name, d.weight );
161
+ }
162
+ // const result = graph.getAllPathsBetween('Intersection_1','Intersection_5');
163
+ // console.log('---xxx', performance.now() - start, result)
164
+ // const result = graph.dijkstra('Intersection_1','Intersection_5', true, true);
165
+ // console.log('---xxx', performance.now() - start, result)
166
+
167
+ })
168
+ });
@@ -0,0 +1,74 @@
1
+ import {CoordinateMap} from '../../../../src';
2
+
3
+ describe('CoordinateMap', () => {
4
+ it('should set and get values correctly', () => {
5
+ const coordinateMap = new CoordinateMap<string>();
6
+ const key = [1, 2, 3];
7
+ const value = 'TestValue';
8
+
9
+ coordinateMap.set(key, value);
10
+ const retrievedValue = coordinateMap.get(key);
11
+
12
+ expect(retrievedValue).toBe(value);
13
+ });
14
+
15
+ it('should return true when key exists', () => {
16
+ const coordinateMap = new CoordinateMap<string>();
17
+ const key = [1, 2, 3];
18
+ const value = 'TestValue';
19
+
20
+ coordinateMap.set(key, value);
21
+
22
+ expect(coordinateMap.has(key)).toBe(true);
23
+ });
24
+
25
+ it('should return false when key does not exist', () => {
26
+ const coordinateMap = new CoordinateMap<string>();
27
+ const key = [1, 2, 3];
28
+
29
+ expect(coordinateMap.has(key)).toBe(false);
30
+ });
31
+
32
+ it('should delete key-value pair correctly', () => {
33
+ const coordinateMap = new CoordinateMap<string>();
34
+ const key = [1, 2, 3];
35
+ const value = 'TestValue';
36
+
37
+ coordinateMap.set(key, value);
38
+ coordinateMap.delete(key);
39
+
40
+ expect(coordinateMap.has(key)).toBe(false);
41
+ });
42
+
43
+ it('should allow changing the joint character', () => {
44
+ const coordinateMap = new CoordinateMap<string>();
45
+ const key = [1, 2, 3];
46
+ const value = 'TestValue';
47
+
48
+ coordinateMap.set(key, value);
49
+ const newKey = [1, 2, 3];
50
+ const retrievedValue = coordinateMap.get(newKey);
51
+
52
+ expect(retrievedValue).toBe(value);
53
+ });
54
+ });
55
+
56
+ describe('CoordinateMap', () => {
57
+ class MyCoordinateMap<V = any> extends CoordinateMap<V> {
58
+ constructor(joint?: string) {
59
+ super(joint);
60
+ this._joint = joint += '-';
61
+ }
62
+ }
63
+
64
+ const cMap = new MyCoordinateMap<number>('*');
65
+
66
+ beforeEach(() => {
67
+ cMap.set([0, 0], 0);
68
+ cMap.set([0, 1], 1);
69
+ cMap.set([1, 1], 11);
70
+ });
71
+ it('should joint to be *-', () => {
72
+ expect(cMap.joint).toBe('*-');
73
+ });
74
+ });
@@ -0,0 +1,66 @@
1
+ import {CoordinateSet} from '../../../../src';
2
+
3
+ describe('CoordinateSet', () => {
4
+ it('should add and check values correctly', () => {
5
+ const coordinateSet = new CoordinateSet();
6
+ const value = [1, 2, 3];
7
+
8
+ coordinateSet.add(value);
9
+ const hasValue = coordinateSet.has(value);
10
+
11
+ expect(hasValue).toBe(true);
12
+ });
13
+
14
+ it('should return false when value does not exist', () => {
15
+ const coordinateSet = new CoordinateSet();
16
+ const value = [1, 2, 3];
17
+
18
+ expect(coordinateSet.has(value)).toBe(false);
19
+ });
20
+
21
+ it('should delete value correctly', () => {
22
+ const coordinateSet = new CoordinateSet();
23
+ const value = [1, 2, 3];
24
+
25
+ coordinateSet.add(value);
26
+ coordinateSet.delete(value);
27
+
28
+ expect(coordinateSet.has(value)).toBe(false);
29
+ });
30
+
31
+ it('should allow changing the joint character', () => {
32
+ const coordinateSet = new CoordinateSet();
33
+ const value = [1, 2, 3];
34
+
35
+ coordinateSet.add(value);
36
+ const newValue = [1, 2, 3];
37
+ const hasValue = coordinateSet.has(newValue);
38
+
39
+ expect(hasValue).toBe(true);
40
+ });
41
+ });
42
+
43
+ describe('MyCoordinateSet', () => {
44
+ class MyCoordinateSet extends CoordinateSet {
45
+ constructor(joint?: string) {
46
+ super(joint);
47
+ this._joint = joint += '-';
48
+ }
49
+ }
50
+
51
+ const mySet = new MyCoordinateSet('*');
52
+
53
+ beforeEach(() => {
54
+ mySet.add([0, 0]);
55
+ mySet.add([0, 1]);
56
+ mySet.add([1, 1]);
57
+ });
58
+ it('should joint to be *-', () => {
59
+ expect(mySet.joint).toBe('*-');
60
+ });
61
+
62
+ it('should has, delete', () => {
63
+ mySet.delete([0, 1]);
64
+ expect(mySet.has([0, 1])).toBe(false);
65
+ });
66
+ });
@@ -0,0 +1,103 @@
1
+ import {HashMap} from '../../../../src';
2
+
3
+ describe('HashMap', () => {
4
+ let hashMap: HashMap<string, number>;
5
+
6
+ beforeEach(() => {
7
+ hashMap = new HashMap<string, number>();
8
+ });
9
+
10
+ it('should initialize correctly', () => {
11
+ expect(hashMap.size).toBe(0);
12
+ expect(hashMap.table.length).toBe(16);
13
+ expect(hashMap.loadFactor).toBe(0.75);
14
+ expect(hashMap.capacityMultiplier).toBe(2);
15
+ expect(hashMap.initialCapacity).toBe(16);
16
+ expect(hashMap.isEmpty()).toBe(true);
17
+ });
18
+
19
+ it('should put and get values', () => {
20
+ hashMap.set('one', 1);
21
+ hashMap.set('two', 2);
22
+ hashMap.set('three', 3);
23
+
24
+ expect(hashMap.get('one')).toBe(1);
25
+ expect(hashMap.get('two')).toBe(2);
26
+ expect(hashMap.get('three')).toBe(3);
27
+ });
28
+
29
+ it('should handle key collisions', () => {
30
+ // Force a collision by setting two different keys to the same bucket
31
+ hashMap.set('key1', 1);
32
+ hashMap.set('key2', 2);
33
+
34
+ expect(hashMap.get('key1')).toBe(1);
35
+ expect(hashMap.get('key2')).toBe(2);
36
+ });
37
+
38
+ it('should delete values', () => {
39
+ hashMap.set('one', 1);
40
+ hashMap.set('two', 2);
41
+
42
+ hashMap.delete('one');
43
+ expect(hashMap.get('one')).toBeUndefined();
44
+ expect(hashMap.size).toBe(1);
45
+ });
46
+
47
+ it('should clear the HashMap', () => {
48
+ hashMap.set('one', 1);
49
+ hashMap.set('two', 2);
50
+
51
+ hashMap.clear();
52
+ expect(hashMap.size).toBe(0);
53
+ expect(hashMap.isEmpty()).toBe(true);
54
+ });
55
+
56
+ it('should iterate over entries', () => {
57
+ hashMap.set('one', 1);
58
+ hashMap.set('two', 2);
59
+ hashMap.set('three', 3);
60
+
61
+ const entries = Array.from(hashMap.entries());
62
+ expect(entries).toEqual(
63
+ expect.arrayContaining([
64
+ ['one', 1],
65
+ ['two', 2],
66
+ ['three', 3]
67
+ ])
68
+ );
69
+ });
70
+
71
+ it('should resize the table when load factor is exceeded', () => {
72
+ // Set a small initial capacity for testing resizing
73
+ hashMap = new HashMap<string, number>(4, 0.5);
74
+
75
+ hashMap.set('one', 1);
76
+ hashMap.set('two', 2);
77
+ hashMap.set('three', 3);
78
+ hashMap.set('four', 4); // This should trigger a resize
79
+
80
+ expect(hashMap.table.length).toBe(8);
81
+ expect(hashMap.get('one')).toBe(1);
82
+ expect(hashMap.get('two')).toBe(2);
83
+ expect(hashMap.get('three')).toBe(3);
84
+ expect(hashMap.get('four')).toBe(4);
85
+ });
86
+
87
+ it('should allow using a custom hash function', () => {
88
+ const customHashFn = () => {
89
+ // A simple custom hash function that always returns 0
90
+ return 0;
91
+ };
92
+ hashMap = new HashMap<string, number>(16, 0.75, customHashFn);
93
+
94
+ hashMap.set('one', 1);
95
+ hashMap.set('two', 2);
96
+
97
+ expect(hashMap.get('one')).toBe(1);
98
+ expect(hashMap.get('two')).toBe(2);
99
+ // Since the custom hash function always returns 0, these keys will collide.
100
+ // Make sure they are stored separately.
101
+ expect(hashMap.table[0].length).toBe(2);
102
+ });
103
+ });
@@ -0,0 +1,186 @@
1
+ import {HashTable, HashTableNode} from '../../../../src';
2
+
3
+ describe('HashNode', () => {
4
+ it('should create a HashNode with key and value', () => {
5
+ const key = 'testKey';
6
+ const value = 'testValue';
7
+ const hashNode = new HashTableNode(key, value);
8
+
9
+ expect(hashNode.key).toBe(key);
10
+ expect(hashNode.value).toBe(value);
11
+ expect(hashNode.next).toBe(null);
12
+ });
13
+ });
14
+
15
+ describe('HashTable', () => {
16
+ it('should initialize with default capacity', () => {
17
+ const hashTable = new HashTable<string, string>();
18
+ expect(hashTable.capacity).toBe(16);
19
+ expect(hashTable.buckets).toEqual(new Array(16).fill(null));
20
+ expect(hashTable.hashFn('a')).toBe(6);
21
+ expect(hashTable.capacity).toBe(16);
22
+ expect(hashTable.size).toBe(0);
23
+ expect(hashTable.buckets.length).toBe(16);
24
+ });
25
+
26
+ it('should initialize with custom capacity', () => {
27
+ const customCapacity = 500;
28
+ const hashTable = new HashTable<string, string>(customCapacity);
29
+
30
+ expect(hashTable.capacity).toBe(customCapacity);
31
+ expect(hashTable.size).toBe(0);
32
+ expect(hashTable.buckets.length).toBe(customCapacity);
33
+ });
34
+
35
+ it('should put and get values correctly', () => {
36
+ const hashTable = new HashTable<string, string>();
37
+ const key = 'testKey';
38
+ const value = 'testValue';
39
+
40
+ hashTable.set(key, value);
41
+ const retrievedValue = hashTable.get(key);
42
+
43
+ expect(retrievedValue).toBe(value);
44
+ });
45
+
46
+ it('should handle collisions by chaining', () => {
47
+ const hashTable = new HashTable<string, string>();
48
+ const key1 = 'testKey1';
49
+ const value1 = 'testValue1';
50
+ const key2 = 'testKey2';
51
+ const value2 = 'testValue2';
52
+
53
+ hashTable.set(key1, value1);
54
+ hashTable.set(key2, value2);
55
+
56
+ const retrievedValue1 = hashTable.get(key1);
57
+ const retrievedValue2 = hashTable.get(key2);
58
+
59
+ expect(retrievedValue1).toBe(value1);
60
+ expect(retrievedValue2).toBe(value2);
61
+ });
62
+
63
+ it('should update value for an existing key', () => {
64
+ const hashTable = new HashTable<string, string>();
65
+ const key = 'testKey';
66
+ const initialValue = 'testValue1';
67
+ const updatedValue = 'testValue2';
68
+
69
+ hashTable.set(key, initialValue);
70
+ hashTable.set(key, updatedValue);
71
+
72
+ const retrievedValue = hashTable.get(key);
73
+
74
+ expect(retrievedValue).toBe(updatedValue);
75
+ });
76
+
77
+ it('should return undefined for non-existent key', () => {
78
+ const hashTable = new HashTable<string, string>();
79
+ const key = 'nonExistentKey';
80
+
81
+ const retrievedValue = hashTable.get(key);
82
+
83
+ expect(retrievedValue).toBeUndefined();
84
+ });
85
+
86
+ it('should delete key-value pair correctly', () => {
87
+ const hashTable = new HashTable<string, string>();
88
+ const key = 'testKey';
89
+ const value = 'testValue';
90
+
91
+ hashTable.set(key, value);
92
+ hashTable.delete(key);
93
+
94
+ const retrievedValue = hashTable.get(key);
95
+
96
+ expect(retrievedValue).toBeUndefined();
97
+ expect(hashTable.size).toBe(0);
98
+ });
99
+ });
100
+
101
+ describe('HashTable', () => {
102
+ let hashTable: HashTable<string, number>;
103
+
104
+ beforeEach(() => {
105
+ hashTable = new HashTable<string, number>();
106
+ });
107
+
108
+ it('should insert and retrieve values correctly', () => {
109
+ hashTable.set('one', 1);
110
+ hashTable.set('two', 2);
111
+
112
+ expect(hashTable.get('one')).toBe(1);
113
+ expect(hashTable.get('two')).toBe(2);
114
+ });
115
+
116
+ it('should update values correctly', () => {
117
+ hashTable.set('one', 1);
118
+ expect(hashTable.get('one')).toBe(1);
119
+
120
+ hashTable.set('one', 100); // Update the value
121
+ expect(hashTable.get('one')).toBe(100);
122
+ });
123
+
124
+ it('should handle collisions correctly', () => {
125
+ hashTable = new HashTable<string, number>(1); // Set a small capacity to force collisions
126
+ hashTable.set('one', 1);
127
+ hashTable.set('two', 2);
128
+
129
+ expect(hashTable.get('one')).toBe(1);
130
+ expect(hashTable.get('two')).toBe(2);
131
+ });
132
+
133
+ it('should delete values correctly', () => {
134
+ hashTable.set('one', 1);
135
+ hashTable.set('two', 2);
136
+ hashTable.delete('one');
137
+
138
+ expect(hashTable.get('one')).toBeUndefined();
139
+ expect(hashTable.get('two')).toBe(2);
140
+ });
141
+
142
+ it('should handle non-existent keys correctly', () => {
143
+ expect(hashTable.get('non-existent')).toBeUndefined();
144
+ hashTable.delete('non-existent'); // Removing a non-existent key should not cause errors
145
+ });
146
+
147
+ it('should handle custom hash function correctly', () => {
148
+ // const customHashFn = () => {
149
+ // // Custom hash function that returns a fixed value for all keys
150
+ // return 42;
151
+ // };
152
+
153
+ hashTable = new HashTable<string, number>(16);
154
+ hashTable.set('one', 1);
155
+ expect(hashTable.get('one')).toBe(1);
156
+ expect(hashTable.get('two')).toBeUndefined();
157
+ });
158
+
159
+ it('should expand when load factor exceeds threshold', () => {
160
+ hashTable = new HashTable<string, number>(2); // Set a small capacity to trigger expansion
161
+ hashTable.set('one', 1);
162
+ hashTable.set('two', 2);
163
+ hashTable.set('three', 3); // This should trigger an expansion
164
+
165
+ expect(hashTable.capacity).toBe(16);
166
+ expect(hashTable.get('one')).toBe(1);
167
+ expect(hashTable.get('two')).toBe(2);
168
+ expect(hashTable.get('three')).toBe(3);
169
+ });
170
+ });
171
+
172
+ describe('HashTable performance', function () {
173
+ it('Items set performance', function () {
174
+ const mag = 100000;
175
+ const ht = new HashTable();
176
+ // const s = performance.now();
177
+ for (let i = 0; i < mag; i++) {
178
+ ht.set(i, i);
179
+ }
180
+ // const s1 = performance.now();
181
+ const map = new Map();
182
+ for (let i = 0; i < mag; i++) {
183
+ map.set(i, i);
184
+ }
185
+ });
186
+ });