data-structure-typed 1.54.2 → 2.0.0

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 (213) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/LICENSE +2 -2
  3. package/README.md +14 -1
  4. package/README_zh-CN.md +1 -1
  5. package/benchmark/report.html +28 -1
  6. package/benchmark/report.json +327 -18
  7. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
  8. package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
  9. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  10. package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
  11. package/dist/cjs/data-structures/base/linear-base.js +553 -0
  12. package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
  13. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  17. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  18. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +89 -21
  20. package/dist/cjs/data-structures/binary-tree/avl-tree.js +76 -8
  21. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +173 -225
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.js +244 -149
  24. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  25. package/dist/cjs/data-structures/binary-tree/bst.d.ts +62 -56
  26. package/dist/cjs/data-structures/binary-tree/bst.js +89 -133
  27. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  28. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  29. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +7 -13
  30. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  31. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +19 -19
  32. package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
  33. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
  34. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  35. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +211 -41
  36. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  37. package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
  38. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
  40. package/dist/cjs/data-structures/heap/heap.js +0 -10
  41. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
  43. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  44. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
  45. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  46. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  49. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +217 -169
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  52. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  53. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  54. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  55. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
  57. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  58. package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
  59. package/dist/cjs/data-structures/queue/deque.js +269 -169
  60. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  61. package/dist/cjs/data-structures/queue/queue.d.ts +84 -40
  62. package/dist/cjs/data-structures/queue/queue.js +134 -50
  63. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  64. package/dist/cjs/data-structures/stack/stack.d.ts +3 -11
  65. package/dist/cjs/data-structures/stack/stack.js +0 -10
  66. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  67. package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
  68. package/dist/cjs/data-structures/trie/trie.js +3 -0
  69. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  70. package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
  71. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  72. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  73. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  74. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  75. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  76. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  77. package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
  78. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
  79. package/dist/cjs/utils/utils.d.ts +2 -2
  80. package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
  81. package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
  82. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  83. package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
  84. package/dist/esm/data-structures/base/linear-base.js +549 -0
  85. package/dist/esm/data-structures/base/linear-base.js.map +1 -0
  86. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  87. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
  88. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  89. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  90. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +52 -38
  91. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  92. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +89 -21
  93. package/dist/esm/data-structures/binary-tree/avl-tree.js +77 -9
  94. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  95. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +173 -225
  96. package/dist/esm/data-structures/binary-tree/binary-tree.js +248 -153
  97. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  98. package/dist/esm/data-structures/binary-tree/bst.d.ts +62 -56
  99. package/dist/esm/data-structures/binary-tree/bst.js +90 -132
  100. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  101. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  102. package/dist/esm/data-structures/binary-tree/red-black-tree.js +8 -14
  103. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  104. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +19 -19
  105. package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
  106. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
  107. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  108. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +212 -41
  109. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  110. package/dist/esm/data-structures/graph/abstract-graph.js +2 -2
  111. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  112. package/dist/esm/data-structures/heap/heap.d.ts +3 -11
  113. package/dist/esm/data-structures/heap/heap.js +0 -10
  114. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  115. package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
  116. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  117. package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
  118. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  119. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  120. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
  121. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  122. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  123. package/dist/esm/data-structures/linked-list/singly-linked-list.js +217 -170
  124. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  125. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  126. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  127. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  128. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  129. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
  130. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  131. package/dist/esm/data-structures/queue/deque.d.ts +130 -91
  132. package/dist/esm/data-structures/queue/deque.js +269 -169
  133. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  134. package/dist/esm/data-structures/queue/queue.d.ts +84 -40
  135. package/dist/esm/data-structures/queue/queue.js +135 -51
  136. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  137. package/dist/esm/data-structures/stack/stack.d.ts +3 -11
  138. package/dist/esm/data-structures/stack/stack.js +0 -10
  139. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  140. package/dist/esm/data-structures/trie/trie.d.ts +4 -3
  141. package/dist/esm/data-structures/trie/trie.js +3 -0
  142. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  143. package/dist/esm/types/data-structures/base/base.d.ts +9 -4
  144. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  145. package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  146. package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
  147. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  148. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  149. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  150. package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
  151. package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
  152. package/dist/esm/utils/utils.d.ts +2 -2
  153. package/dist/umd/data-structure-typed.js +1548 -920
  154. package/dist/umd/data-structure-typed.min.js +5 -12
  155. package/dist/umd/data-structure-typed.min.js.map +1 -1
  156. package/package.json +7 -7
  157. package/src/data-structures/base/iterable-element-base.ts +29 -20
  158. package/src/data-structures/base/linear-base.ts +649 -0
  159. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  160. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  161. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  162. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  163. package/src/data-structures/binary-tree/bst.ts +150 -152
  164. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  165. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  166. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  167. package/src/data-structures/graph/abstract-graph.ts +2 -2
  168. package/src/data-structures/heap/heap.ts +3 -14
  169. package/src/data-structures/heap/max-heap.ts +2 -2
  170. package/src/data-structures/heap/min-heap.ts +2 -2
  171. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  172. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  173. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  174. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  175. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  176. package/src/data-structures/queue/deque.ts +286 -183
  177. package/src/data-structures/queue/queue.ts +149 -63
  178. package/src/data-structures/stack/stack.ts +3 -18
  179. package/src/data-structures/trie/trie.ts +7 -3
  180. package/src/types/data-structures/base/base.ts +17 -8
  181. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  182. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  183. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  184. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  185. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  186. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  187. package/src/types/data-structures/queue/deque.ts +2 -3
  188. package/src/types/data-structures/queue/queue.ts +2 -2
  189. package/src/utils/utils.ts +2 -2
  190. package/test/integration/all-in-one.test.ts +1 -1
  191. package/test/integration/avl-tree.test.ts +1 -1
  192. package/test/integration/bst.test.ts +2 -2
  193. package/test/integration/compile.mjs +21 -21
  194. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
  195. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
  196. package/test/performance/{reportor.js → reportor.mjs} +264 -8
  197. package/test/performance/reportor.ts +1 -1
  198. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
  199. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +172 -5
  200. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -4
  201. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
  202. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
  203. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +5 -13
  204. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
  205. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +168 -10
  206. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
  207. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +111 -11
  208. package/test/unit/data-structures/queue/deque.test.ts +241 -60
  209. package/test/unit/data-structures/queue/queue.test.ts +118 -19
  210. package/test/unit/unrestricted-interconversion.test.ts +1 -1
  211. package/test/utils/json2html.ts +0 -154
  212. package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
  213. /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
@@ -13,28 +13,28 @@ const entries = [
13
13
  [8, '8']
14
14
  ];
15
15
  const queue = new Queue(orgArr);
16
- queue.print();
16
+
17
17
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
18
18
  const deque = new Deque(orgArr);
19
- deque.print();
19
+
20
20
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
21
21
  const sList = new SinglyLinkedList(orgArr);
22
- sList.print();
22
+
23
23
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
24
24
  const dList = new DoublyLinkedList(orgArr);
25
- dList.print();
25
+
26
26
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
27
27
  const stack = new Stack(orgArr);
28
- stack.print();
28
+
29
29
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
30
30
  const minHeap = new MinHeap(orgArr);
31
- minHeap.print();
31
+
32
32
  // [1, 5, 2, 7, 6, 3, 4, 9, 8]
33
33
  const maxPQ = new MaxPriorityQueue(orgArr);
34
- maxPQ.print();
34
+
35
35
  // [9, 8, 4, 7, 5, 2, 3, 1, 6]
36
36
  const biTree = new BinaryTree(entries);
37
- biTree.print();
37
+
38
38
  // ___6___
39
39
  // / \
40
40
  // ___1_ _2_
@@ -43,7 +43,7 @@ biTree.print();
43
43
  // / \
44
44
  // 9 8
45
45
  const bst = new BST(entries);
46
- bst.print();
46
+
47
47
  // _____5___
48
48
  // / \
49
49
  // _2_ _7_
@@ -52,7 +52,7 @@ bst.print();
52
52
  // \ \
53
53
  // 4 9
54
54
  const rbTree = new RedBlackTree(entries);
55
- rbTree.print();
55
+
56
56
  // ___4___
57
57
  // / \
58
58
  // _2_ _6___
@@ -61,7 +61,7 @@ rbTree.print();
61
61
  // / \
62
62
  // 7 9
63
63
  const avl = new AVLTree(entries);
64
- avl.print();
64
+
65
65
  // ___4___
66
66
  // / \
67
67
  // _2_ _6___
@@ -70,7 +70,7 @@ avl.print();
70
70
  // / \
71
71
  // 7 9
72
72
  const treeMulti = new TreeMultiMap(entries);
73
- treeMulti.print();
73
+
74
74
  // ___4___
75
75
  // / \
76
76
  // _2_ _6___
@@ -79,10 +79,10 @@ treeMulti.print();
79
79
  // / \
80
80
  // 7 9
81
81
  const hm = new HashMap(entries);
82
- hm.print();
82
+
83
83
  // [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]]
84
84
  const rbTreeH = new RedBlackTree(hm);
85
- rbTreeH.print();
85
+
86
86
  // ___4___
87
87
  // / \
88
88
  // _2_ _6___
@@ -91,10 +91,10 @@ rbTreeH.print();
91
91
  // / \
92
92
  // 7 9
93
93
  const pq = new MinPriorityQueue(orgArr);
94
- pq.print();
94
+
95
95
  // [1, 5, 2, 7, 6, 3, 4, 9, 8]
96
96
  const bst1 = new BST(pq);
97
- bst1.print();
97
+
98
98
  // _____5___
99
99
  // / \
100
100
  // _2_ _7_
@@ -103,10 +103,10 @@ bst1.print();
103
103
  // \ \
104
104
  // 4 9
105
105
  const dq1 = new Deque(orgArr);
106
- dq1.print();
106
+
107
107
  // [6, 1, 2, 7, 5, 3, 4, 9, 8]
108
108
  const rbTree1 = new RedBlackTree(dq1);
109
- rbTree1.print();
109
+
110
110
  // _____5___
111
111
  // / \
112
112
  // _2___ _7___
@@ -115,13 +115,13 @@ rbTree1.print();
115
115
  // / /
116
116
  // 3 8
117
117
  const trie2 = new Trie(orgStrArr);
118
- trie2.print();
118
+
119
119
  // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
120
120
  const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
121
- heap2.print();
121
+
122
122
  // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
123
123
  const dq2 = new Deque(heap2);
124
- dq2.print();
124
+
125
125
  // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
126
126
  const entries2 = dq2.map((el, i) => [i, el]);
127
127
  const avl2 = new AVLTree(entries2);
@@ -0,0 +1,71 @@
1
+ import { AVLTree } from 'data-structure-typed';
2
+ import Benchmark from 'benchmark';
3
+
4
+ const magnitude = {
5
+ THOUSAND: 1000,
6
+ TEN_THOUSAND: 10000,
7
+ HUNDRED_THOUSAND: 100000,
8
+ MILLION: 1000000,
9
+ TEN_MILLION: 10000000,
10
+ BILLION: 100000000
11
+ };
12
+
13
+ function getRandomIntArray(length = 1000, min = -1000, max = 1000, unique = true) {
14
+ if (unique) {
15
+ if (max - min + 1 < length) {
16
+ throw new Error('Range too small for unique values with the specified length');
17
+ }
18
+ const allNumbers = Array.from({ length: max - min + 1 }, (_, i) => i + min);
19
+ for (let i = allNumbers.length - 1; i > 0; i--) {
20
+ const j = Math.floor(Math.random() * (i + 1));
21
+ [allNumbers[i], allNumbers[j]] = [allNumbers[j], allNumbers[i]];
22
+ }
23
+ return allNumbers.slice(0, length);
24
+ }
25
+ else {
26
+ return Array.from({ length }, () => Math.floor(Math.random() * (max - min + 1)) + min);
27
+ }
28
+ }
29
+
30
+ const suite = new Benchmark.Suite();
31
+ const avlTree = new AVLTree();
32
+ const { HUNDRED_THOUSAND } = magnitude;
33
+ const randomArray = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND - 1, true);
34
+ suite
35
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
36
+ avlTree.clear();
37
+ for (let i = 0; i < randomArray.length; i++)
38
+ avlTree.add(randomArray[i]);
39
+ })
40
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
41
+ avlTree.clear();
42
+ for (let i = 0; i < randomArray.length; i++)
43
+ avlTree.add(i);
44
+ })
45
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
46
+ for (let i = 0; i < randomArray.length; i++)
47
+ avlTree.get(randomArray[i]);
48
+ })
49
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} getNode`, () => {
50
+ for (let i = 0; i < randomArray.length; i++)
51
+ avlTree.getNode(randomArray[i]);
52
+ })
53
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} iterator`, () => {
54
+ const entries = [...avlTree];
55
+ return entries.length === HUNDRED_THOUSAND;
56
+ })
57
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete orderly`, () => {
58
+ avlTree.clear();
59
+ for (let i = 0; i < randomArray.length; i++)
60
+ avlTree.add(i);
61
+ for (let i = 0; i < randomArray.length; i++)
62
+ avlTree.delete(i);
63
+ })
64
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete randomly`, () => {
65
+ avlTree.clear();
66
+ for (let i = 0; i < randomArray.length; i++)
67
+ avlTree.add(randomArray[i]);
68
+ for (let i = 0; i < randomArray.length; i++)
69
+ avlTree.delete(randomArray[i]);
70
+ });
71
+ export { suite };
@@ -0,0 +1,81 @@
1
+ import { RedBlackTree } from 'data-structure-typed';
2
+ import Benchmark from 'benchmark';
3
+
4
+ const magnitude = {
5
+ THOUSAND: 1000,
6
+ TEN_THOUSAND: 10000,
7
+ HUNDRED_THOUSAND: 100000,
8
+ MILLION: 1000000,
9
+ TEN_MILLION: 10000000,
10
+ BILLION: 100000000
11
+ };
12
+
13
+ function getRandomIntArray(length = 1000, min = -1000, max = 1000, unique = true) {
14
+ if (unique) {
15
+ if (max - min + 1 < length) {
16
+ throw new Error('Range too small for unique values with the specified length');
17
+ }
18
+ const allNumbers = Array.from({ length: max - min + 1 }, (_, i) => i + min);
19
+ for (let i = allNumbers.length - 1; i > 0; i--) {
20
+ const j = Math.floor(Math.random() * (i + 1));
21
+ [allNumbers[i], allNumbers[j]] = [allNumbers[j], allNumbers[i]];
22
+ }
23
+ return allNumbers.slice(0, length);
24
+ }
25
+ else {
26
+ return Array.from({ length }, () => Math.floor(Math.random() * (max - min + 1)) + min);
27
+ }
28
+ }
29
+ const suite = new Benchmark.Suite();
30
+ const rbTree = new RedBlackTree();
31
+ const rbTreeNodeMode = new RedBlackTree([], { isMapMode: false });
32
+ const { HUNDRED_THOUSAND } = magnitude;
33
+ const randomArray = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND - 1, true);
34
+ suite
35
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
36
+ rbTree.clear();
37
+ for (let i = 0; i < randomArray.length; i++)
38
+ rbTree.add(randomArray[i]);
39
+ })
40
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
41
+ rbTree.clear();
42
+ for (let i = 0; i < randomArray.length; i++)
43
+ rbTree.add(i);
44
+ })
45
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
46
+ for (let i = 0; i < randomArray.length; i++)
47
+ rbTree.get(randomArray[i]);
48
+ })
49
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} getNode`, () => {
50
+ for (let i = 0; i < randomArray.length; i++)
51
+ rbTree.getNode(randomArray[i]);
52
+ })
53
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} node mode add randomly`, () => {
54
+ rbTreeNodeMode.clear();
55
+ for (let i = 0; i < randomArray.length; i++)
56
+ rbTreeNodeMode.add(randomArray[i]);
57
+ })
58
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} node mode get`, () => {
59
+ for (let i = 0; i < randomArray.length; i++)
60
+ rbTreeNodeMode.get(randomArray[i]);
61
+ })
62
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} iterator`, () => {
63
+ const entries = [...rbTree];
64
+ return entries.length === HUNDRED_THOUSAND;
65
+ })
66
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete orderly`, () => {
67
+ rbTree.clear();
68
+ for (let i = 0; i < randomArray.length; i++)
69
+ rbTree.add(i);
70
+ for (let i = 0; i < randomArray.length; i++)
71
+ rbTree.delete(i);
72
+ })
73
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete randomly`, () => {
74
+ rbTree.clear();
75
+ for (let i = 0; i < randomArray.length; i++)
76
+ rbTree.add(randomArray[i]);
77
+ for (let i = 0; i < randomArray.length; i++)
78
+ rbTree.delete(randomArray[i]);
79
+ });
80
+
81
+ export { suite };
@@ -1,15 +1,268 @@
1
1
  import * as path from 'path';
2
2
  import * as fs from 'fs';
3
- import * as fastGlob from 'fast-glob';
4
- import { ConsoleColor, numberFix, render } from '../utils';
3
+ import fastGlob from 'fast-glob';
4
+ import { fileURLToPath } from 'url';
5
5
 
6
+ const isNumber = (value) => {
7
+ return typeof value === 'number';
8
+ };
9
+ const isString = (value) => {
10
+ return typeof value === 'string';
11
+ };
12
+ const isBoolean = (value) => {
13
+ return typeof value === 'boolean';
14
+ };
15
+ const isDate = (value) => {
16
+ return value instanceof Date;
17
+ };
18
+ const isNull = (value) => {
19
+ return value === null;
20
+ };
21
+ const isUndefined = (value) => {
22
+ return typeof value === 'undefined';
23
+ };
24
+ const isFunction = (value) => {
25
+ return typeof value === 'function';
26
+ };
27
+ const isObject = (value) => {
28
+ return typeof value === 'object';
29
+ };
30
+ const isArray = (value) => {
31
+ return Array.isArray(value);
32
+ };
33
+ const isEqual = (objA, objB) => {
34
+ if (objA === objB) {
35
+ return true;
36
+ }
37
+ if (typeof objA !== 'object' || typeof objB !== 'object' || objA === null || objB === null) {
38
+ return false;
39
+ }
40
+ const keysA = Object.keys(objA);
41
+ const keysB = Object.keys(objB);
42
+ if (keysA.length !== keysB.length) {
43
+ return false;
44
+ }
45
+ for (const key of keysA) {
46
+ if (!keysB.includes(key)) {
47
+ return false;
48
+ }
49
+ if (!isEqual(objA[key], objB[key])) {
50
+ return false;
51
+ }
52
+ }
53
+ return true;
54
+ };
55
+
56
+ function toggleJS(options) {
57
+ if (options === null || options === void 0 ? void 0 : options.plainHtml) {
58
+ return '';
59
+ }
60
+ else {
61
+ return 'onclick="json-to-html.toggleVisibility(this);return false"';
62
+ }
63
+ }
64
+ function makeLabelDiv(options, level, keyName, datatype) {
65
+ if (typeof keyName === 'number') {
66
+ return `<div class='index'><span class='json-to-html-label'>${keyName}&nbsp;</span></div>`;
67
+ }
68
+ else if (typeof keyName === 'string') {
69
+ if (datatype === 'array') {
70
+ return `<div class='collapsible level${level}' ${toggleJS(options)}><span class='json-to-html-label'>${keyName}</span></div>`;
71
+ }
72
+ else if (datatype === 'object') {
73
+ return `<div class='attribute collapsible level${level}' ${toggleJS(options)}><span class='json-to-html-label'>${keyName}:</span></div>`;
74
+ }
75
+ else {
76
+ return `<div class='leaf level${level}'><span class='json-to-html-label'>${keyName}:</span></div>`;
77
+ }
78
+ }
79
+ else {
80
+ return '';
81
+ }
82
+ }
83
+ function getContentClass(keyName) {
84
+ if (typeof keyName === 'string') {
85
+ return 'content';
86
+ }
87
+ else {
88
+ return '';
89
+ }
90
+ }
91
+ function isPlainObject(val) {
92
+ let lastKey;
93
+ let lastOwnKey;
94
+ for (const key in val) {
95
+ if (val.hasOwnProperty(key)) {
96
+ lastOwnKey = key;
97
+ }
98
+ }
99
+ for (const key in val) {
100
+ lastKey = key;
101
+ }
102
+ return lastOwnKey === lastKey;
103
+ }
104
+ function isLeafValue(val) {
105
+ return (isNumber(val) ||
106
+ isString(val) ||
107
+ isBoolean(val) ||
108
+ isDate(val) ||
109
+ isNull(val) ||
110
+ isUndefined(val) ||
111
+ isNaN(val) ||
112
+ isFunction(val) ||
113
+ !isPlainObject(val));
114
+ }
115
+ function isLeafObject(obj) {
116
+ if (!isObject(obj)) {
117
+ return false;
118
+ }
119
+ for (const key in obj) {
120
+ const val = obj[key];
121
+ if (!isLeafValue(val)) {
122
+ return false;
123
+ }
124
+ }
125
+ return true;
126
+ }
127
+ function isTable(arr) {
128
+ if (!isArray(arr)) {
129
+ return false;
130
+ }
131
+ if (arr.length === 0 || !isObject(arr[0])) {
132
+ return false;
133
+ }
134
+ else {
135
+ let nonCompliant = arr.find(row => !isLeafObject(row));
136
+ if (nonCompliant) {
137
+ return false;
138
+ }
139
+ else {
140
+ const cols = Object.keys(arr[0]);
141
+ nonCompliant = arr.find((row) => !isEqual(cols, Object.keys(row)));
142
+ return !nonCompliant;
143
+ }
144
+ }
145
+ }
146
+ function drawTable(arr) {
147
+ function drawRow(headers, rowObj) {
148
+ return '<td>' + headers.map(header => rowObj[header]).join('</td><td>') + '</td>';
149
+ }
150
+ const cols = Object.keys(arr[0]);
151
+ const content = arr.map(rowObj => drawRow(cols, rowObj));
152
+ const headingHtml = '<tr><th>' + cols.join('</th><th>') + '</th></tr>';
153
+ const contentHtml = '<tr>' + content.join('</tr><tr>') + '</tr>';
154
+ return '<table style="display: table; width:100%; table-layout: fixed;">' + headingHtml + contentHtml + '</table>';
155
+ }
156
+ function _render(name, data, options, level, altRow) {
157
+ const contentClass = getContentClass(name);
158
+ if (isArray(data)) {
159
+ const title = makeLabelDiv(options, level, `${name}`, 'array');
160
+ let subs;
161
+ if (isTable(data)) {
162
+ subs = drawTable(data);
163
+ }
164
+ else {
165
+ subs =
166
+ "<div class='altRows'>" +
167
+ data
168
+ .map((val, idx) => _render(idx.toString(), val, options, level + 1, idx % 2))
169
+ .join("</div><div class='altRows'>") +
170
+ '</div>';
171
+ }
172
+ return `<div class="json-to-html-collapse clearfix ${altRow}">
173
+ ${title}
174
+ <div class="${contentClass}">${subs}</div>
175
+ </div>`;
176
+ }
177
+ else if (isLeafValue(data)) {
178
+ const title = makeLabelDiv(options, level, name);
179
+ if (isFunction(data)) {
180
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;-function() can't _render-</span>`;
181
+ }
182
+ else if (!isPlainObject(data)) {
183
+ if (isFunction(data.toString)) {
184
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;${data.toString()}</span>`;
185
+ }
186
+ else {
187
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;-instance object, can't render-</span>`;
188
+ }
189
+ }
190
+ else {
191
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;${data}</span>`;
192
+ }
193
+ }
194
+ else {
195
+ const title = makeLabelDiv(options, level, name, 'object');
196
+ let count = 0;
197
+ const subs = '<div>' +
198
+ Object.entries(data)
199
+ .map(([key, val]) => _render(key, val, options, level + 1, count++ % 2))
200
+ .join('</div><div>') +
201
+ '</div>';
202
+ const inner = `<div class="json-to-html-expand clearfix ${altRow}">
203
+ ${title}
204
+ <div class="${contentClass}">${subs}</div>
205
+ </div>`;
206
+ return `${level === 0 ? "<div id='json-to-html'>" : ''}
207
+ ${inner}
208
+ ${level === 0 ? '</div>' : ''}`;
209
+ }
210
+ }
211
+ export function render(name, json, options) {
212
+ return `${_render(name, json, options, 0, 0)}`;
213
+ }
214
+
215
+
216
+ const __filename = fileURLToPath(import.meta.url);
217
+ const __dirname = path.dirname(__filename);
218
+
219
+ function numberFix(num, decimalPlaces) {
220
+ if (num > 10000 || num < 0.001) {
221
+ const [mantissa, exponent] = num.toExponential().split('e');
222
+ const formattedMantissa = Number(mantissa).toFixed(decimalPlaces);
223
+ return `${formattedMantissa}e${exponent}`;
224
+ } else {
225
+ return num.toFixed(decimalPlaces);
226
+ }
227
+ }
228
+ const ConsoleColor = {
229
+ END: '\x1b[0m',
230
+ BOLD: '\x1b[1m',
231
+ DIM: '\x1b[2m',
232
+ ITALIC: '\x1b[3m',
233
+ UNDERLINE: '\x1b[4m',
234
+ INVERSE: '\x1b[7m',
235
+ STRIKETHROUGH: '\x1b[9m',
236
+ NO_BOLD: '\x1b[22m',
237
+ NO_ITALIC: '\x1b[23m',
238
+ NO_UNDERLINE: '\x1b[24m',
239
+ NO_INVERSE: '\x1b[27m',
240
+ NO_STRIKETHROUGH: '\x1b[29m',
241
+ BLACK: '\x1b[30m',
242
+ RED: '\x1b[31m',
243
+ GREEN: '\x1b[32m',
244
+ YELLOW: '\x1b[33m',
245
+ BLUE: '\x1b[34m',
246
+ MAGENTA: '\x1b[35m',
247
+ GRAY: '\x1b[90m',
248
+ CYAN: '\x1b[36m',
249
+ WHITE: '\x1b[37m',
250
+ BG_BLACK: '\x1b[40m',
251
+ BG_RED: '\x1b[41m',
252
+ BG_GREEN: '\x1b[42m',
253
+ BG_YELLOW: '\x1b[43m',
254
+ BG_BLUE: '\x1b[44m',
255
+ BG_MAGENTA: '\x1b[45m',
256
+ BG_CYAN: '\x1b[46m',
257
+ BG_WHITE: '\x1b[47m'
258
+ };
6
259
  const args = process.argv.slice(2);
7
260
  const { GREEN, BOLD, END, YELLOW, GRAY, CYAN, BG_YELLOW } = ConsoleColor;
8
261
  const isOnlyOrdered = true;
9
262
  const runOrder = [
10
263
  'heap',
11
264
  'avl-tree',
12
- 'rb-tree',
265
+ 'red-black-tree',
13
266
  'doubly-linked-list',
14
267
  'directed-graph',
15
268
  'queue',
@@ -25,6 +278,7 @@ const getRelativePath = file => {
25
278
  return path.relative(__dirname, file);
26
279
  };
27
280
  const coloredLabeled = (label, file) => {
281
+
28
282
  const relativeFilePath = getRelativePath(file);
29
283
  const directory = path.dirname(relativeFilePath);
30
284
  const fileName = path.basename(relativeFilePath);
@@ -33,7 +287,8 @@ const coloredLabeled = (label, file) => {
33
287
  const parentDirectory = path.resolve(__dirname, '../..');
34
288
  const reportDistPath = path.join(parentDirectory, 'benchmark');
35
289
  const testDir = path.join(__dirname, 'data-structures');
36
- const allFiles = fastGlob.sync(path.join(testDir, '**', '*.test.js'));
290
+ let allFiles = fastGlob.sync(path.join(testDir, '**', '*.test.mjs'));
291
+
37
292
  let testFiles;
38
293
  let isIndividual = false;
39
294
  if (args.length > 0) {
@@ -51,9 +306,9 @@ if (args.length > 0) {
51
306
  const report = {};
52
307
  let completedCount = 0;
53
308
  const performanceTests = [];
54
- testFiles.forEach(file => {
55
- const testName = path.basename(file, '.test.ts');
56
- const testFunction = require(file);
309
+ for (const file of testFiles) {
310
+ const testName = path.basename(file, '.test.mjs');
311
+ const testFunction = await import(file);
57
312
  const { suite } = testFunction;
58
313
  if (suite)
59
314
  performanceTests.push({
@@ -61,7 +316,8 @@ testFiles.forEach(file => {
61
316
  suite,
62
317
  file
63
318
  });
64
- });
319
+ }
320
+
65
321
  const composeReport = () => {
66
322
  if (!fs.existsSync(reportDistPath))
67
323
  fs.mkdirSync(reportDistPath, {
@@ -12,7 +12,7 @@ const isOnlyOrdered = true;
12
12
  const runOrder = [
13
13
  'heap',
14
14
  'avl-tree',
15
- 'rb-tree',
15
+ 'red-black-tree',
16
16
  'doubly-linked-list',
17
17
  'directed-graph',
18
18
  'queue',
@@ -91,7 +91,7 @@ describe('AVLTreeCounter operations test1', () => {
91
91
  expect(minNodeBySpecificNode?.key).toBe(15);
92
92
 
93
93
  let subTreeSum = 0;
94
- if (node15) avlCounter.dfs(node => (subTreeSum += node.key), 'PRE', 15);
94
+ if (node15) avlCounter.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
95
95
  expect(subTreeSum).toBe(31);
96
96
  let lesserSum = 0;
97
97
  avlCounter.lesserOrGreaterTraverse((node: AVLTreeCounterNode<number>) => (lesserSum += node.key), -1, 10);
@@ -99,7 +99,7 @@ describe('AVLTreeCounter operations test1', () => {
99
99
 
100
100
  expect(node15 instanceof AVLTreeCounterNode);
101
101
  if (node15 instanceof AVLTreeCounterNode) {
102
- const subTreeAdd = avlCounter.dfs(node => (node.count += 1), 'PRE', 15);
102
+ const subTreeAdd = avlCounter.dfs(node => (node.count += 1), 'PRE', false, 15);
103
103
  expect(subTreeAdd);
104
104
  }
105
105
  const node11 = avlCounter.getNode(11);
@@ -347,7 +347,7 @@ describe('AVLTreeCounter operations test recursively1', () => {
347
347
  expect(minNodeBySpecificNode?.key).toBe(15);
348
348
 
349
349
  let subTreeSum = 0;
350
- if (node15) avlCounter.dfs(node => (subTreeSum += node.key), 'PRE', 15);
350
+ if (node15) avlCounter.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
351
351
  expect(subTreeSum).toBe(31);
352
352
  let lesserSum = 0;
353
353
  avlCounter.lesserOrGreaterTraverse((node: AVLTreeCounterNode<number>) => (lesserSum += node.key), -1, 10);
@@ -355,7 +355,7 @@ describe('AVLTreeCounter operations test recursively1', () => {
355
355
 
356
356
  expect(node15 instanceof AVLTreeCounterNode);
357
357
  if (node15 instanceof AVLTreeCounterNode) {
358
- const subTreeAdd = avlCounter.dfs(node => (node.count += 1), 'PRE', 15);
358
+ const subTreeAdd = avlCounter.dfs(node => (node.count += 1), 'PRE', false, 15);
359
359
  expect(subTreeAdd);
360
360
  }
361
361
  const node11 = avlCounter.getNode(11);
@@ -709,7 +709,7 @@ describe('AVLTreeCounter toEntryFn', () => {
709
709
 
710
710
  expect(avlCounter.morris(node => node.key, 'IN')).toEqual(expected);
711
711
  expect(avlCounter.dfs(node => node.key, 'IN')).toEqual(expected);
712
- expect(avlCounter.dfs(node => node.key, 'IN', avlCounter.root, 'RECURSIVE')).toEqual(expected);
712
+ expect(avlCounter.dfs(node => node.key, 'IN', false, avlCounter.root, 'RECURSIVE')).toEqual(expected);
713
713
  });
714
714
 
715
715
  it('should toEntryFn 2', () => {
@@ -724,7 +724,7 @@ describe('AVLTreeCounter toEntryFn', () => {
724
724
 
725
725
  expect(avlCounter.morris(node => node.key, 'IN')).toEqual(expected);
726
726
  expect(avlCounter.dfs(node => node.key, 'IN')).toEqual(expected);
727
- expect(avlCounter.dfs(node => node.key, 'IN', avlCounter.root, 'RECURSIVE')).toEqual(expected);
727
+ expect(avlCounter.dfs(node => node.key, 'IN', false, avlCounter.root, 'RECURSIVE')).toEqual(expected);
728
728
  });
729
729
 
730
730
  it('should toEntryFn throw error', () => {
@@ -760,7 +760,7 @@ describe('AVLTreeCounter toEntryFn', () => {
760
760
 
761
761
  expect(avlCounter.morris(node => node.key, 'IN')).toEqual(expected);
762
762
  expect(avlCounter.dfs(node => node.key, 'IN')).toEqual(expected);
763
- expect(avlCounter.dfs(node => node.key, 'IN', avlCounter.root, 'RECURSIVE')).toEqual(expected);
763
+ expect(avlCounter.dfs(node => node.key, 'IN', false, avlCounter.root, 'RECURSIVE')).toEqual(expected);
764
764
  });
765
765
  });
766
766