tree-set-typed 2.3.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 (273) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +482 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/cjs/index.cjs +12 -0
  20. package/dist/cjs/index.cjs.map +1 -0
  21. package/dist/cjs-legacy/index.cjs +12 -0
  22. package/dist/cjs-legacy/index.cjs.map +1 -0
  23. package/dist/esm/index.mjs +3 -0
  24. package/dist/esm/index.mjs.map +1 -0
  25. package/dist/esm-legacy/index.mjs +3 -0
  26. package/dist/esm-legacy/index.mjs.map +1 -0
  27. package/dist/types/common/index.d.ts +12 -0
  28. package/dist/types/constants/index.d.ts +4 -0
  29. package/dist/types/data-structures/base/index.d.ts +2 -0
  30. package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
  31. package/dist/types/data-structures/base/iterable-entry-base.d.ts +150 -0
  32. package/dist/types/data-structures/base/linear-base.d.ts +335 -0
  33. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -0
  34. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
  35. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
  36. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
  37. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
  38. package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
  39. package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
  40. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
  41. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
  42. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
  43. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
  44. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
  45. package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
  46. package/dist/types/data-structures/graph/index.d.ts +4 -0
  47. package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
  48. package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
  49. package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
  50. package/dist/types/data-structures/hash/index.d.ts +1 -0
  51. package/dist/types/data-structures/heap/heap.d.ts +552 -0
  52. package/dist/types/data-structures/heap/index.d.ts +3 -0
  53. package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
  54. package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
  55. package/dist/types/data-structures/index.d.ts +12 -0
  56. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
  57. package/dist/types/data-structures/linked-list/index.d.ts +3 -0
  58. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
  59. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
  60. package/dist/types/data-structures/matrix/index.d.ts +2 -0
  61. package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
  62. package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
  63. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  64. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
  65. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
  66. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
  67. package/dist/types/data-structures/queue/deque.d.ts +459 -0
  68. package/dist/types/data-structures/queue/index.d.ts +2 -0
  69. package/dist/types/data-structures/queue/queue.d.ts +364 -0
  70. package/dist/types/data-structures/stack/index.d.ts +1 -0
  71. package/dist/types/data-structures/stack/stack.d.ts +324 -0
  72. package/dist/types/data-structures/tree/index.d.ts +1 -0
  73. package/dist/types/data-structures/tree/tree.d.ts +62 -0
  74. package/dist/types/data-structures/trie/index.d.ts +1 -0
  75. package/dist/types/data-structures/trie/trie.d.ts +412 -0
  76. package/dist/types/index.d.ts +23 -0
  77. package/dist/types/interfaces/binary-tree.d.ts +60 -0
  78. package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
  79. package/dist/types/interfaces/graph.d.ts +21 -0
  80. package/dist/types/interfaces/heap.d.ts +1 -0
  81. package/dist/types/interfaces/index.d.ts +8 -0
  82. package/dist/types/interfaces/navigator.d.ts +1 -0
  83. package/dist/types/interfaces/priority-queue.d.ts +1 -0
  84. package/dist/types/interfaces/segment-tree.d.ts +1 -0
  85. package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
  86. package/dist/types/types/common.d.ts +15 -0
  87. package/dist/types/types/data-structures/base/base.d.ts +13 -0
  88. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  89. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  90. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  91. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  92. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  93. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  94. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  95. package/dist/types/types/data-structures/binary-tree/index.d.ts +9 -0
  96. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  97. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  98. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  99. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  100. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  101. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  102. package/dist/types/types/data-structures/graph/index.d.ts +3 -0
  103. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  104. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  105. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  106. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  107. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  108. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  109. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  110. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  111. package/dist/types/types/data-structures/index.d.ts +12 -0
  112. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  113. package/dist/types/types/data-structures/linked-list/index.d.ts +3 -0
  114. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  115. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  116. package/dist/types/types/data-structures/matrix/index.d.ts +2 -0
  117. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  118. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  119. package/dist/types/types/data-structures/priority-queue/index.d.ts +3 -0
  120. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  121. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  122. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  123. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  124. package/dist/types/types/data-structures/queue/index.d.ts +2 -0
  125. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  126. package/dist/types/types/data-structures/stack/index.d.ts +1 -0
  127. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  128. package/dist/types/types/data-structures/tree/index.d.ts +1 -0
  129. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  130. package/dist/types/types/data-structures/trie/index.d.ts +1 -0
  131. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  132. package/dist/types/types/index.d.ts +3 -0
  133. package/dist/types/types/utils/index.d.ts +2 -0
  134. package/dist/types/types/utils/utils.d.ts +22 -0
  135. package/dist/types/types/utils/validate-type.d.ts +19 -0
  136. package/dist/types/utils/index.d.ts +2 -0
  137. package/dist/types/utils/number.d.ts +14 -0
  138. package/dist/types/utils/utils.d.ts +209 -0
  139. package/dist/umd/red-black-tree-typed.js +14578 -0
  140. package/dist/umd/red-black-tree-typed.js.map +1 -0
  141. package/dist/umd/red-black-tree-typed.min.js +44 -0
  142. package/dist/umd/red-black-tree-typed.min.js.map +1 -0
  143. package/docs/.nojekyll +1 -0
  144. package/docs/assets/highlight.css +92 -0
  145. package/docs/assets/main.js +59 -0
  146. package/docs/assets/navigation.js +1 -0
  147. package/docs/assets/search.js +1 -0
  148. package/docs/assets/style.css +1383 -0
  149. package/docs/classes/AVLTree.html +2046 -0
  150. package/docs/classes/AVLTreeNode.html +263 -0
  151. package/docs/index.html +523 -0
  152. package/docs/modules.html +45 -0
  153. package/jest.config.js +8 -0
  154. package/package.json +113 -0
  155. package/src/common/index.ts +23 -0
  156. package/src/constants/index.ts +4 -0
  157. package/src/data-structures/base/index.ts +2 -0
  158. package/src/data-structures/base/iterable-element-base.ts +352 -0
  159. package/src/data-structures/base/iterable-entry-base.ts +246 -0
  160. package/src/data-structures/base/linear-base.ts +643 -0
  161. package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
  162. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
  163. package/src/data-structures/binary-tree/avl-tree.ts +840 -0
  164. package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
  165. package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
  166. package/src/data-structures/binary-tree/bst.ts +2024 -0
  167. package/src/data-structures/binary-tree/index.ts +10 -0
  168. package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
  169. package/src/data-structures/binary-tree/segment-tree.ts +324 -0
  170. package/src/data-structures/binary-tree/tree-counter.ts +575 -0
  171. package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
  172. package/src/data-structures/graph/abstract-graph.ts +1081 -0
  173. package/src/data-structures/graph/directed-graph.ts +715 -0
  174. package/src/data-structures/graph/index.ts +4 -0
  175. package/src/data-structures/graph/map-graph.ts +132 -0
  176. package/src/data-structures/graph/undirected-graph.ts +626 -0
  177. package/src/data-structures/hash/hash-map.ts +813 -0
  178. package/src/data-structures/hash/index.ts +1 -0
  179. package/src/data-structures/heap/heap.ts +1020 -0
  180. package/src/data-structures/heap/index.ts +3 -0
  181. package/src/data-structures/heap/max-heap.ts +47 -0
  182. package/src/data-structures/heap/min-heap.ts +36 -0
  183. package/src/data-structures/index.ts +12 -0
  184. package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
  185. package/src/data-structures/linked-list/index.ts +3 -0
  186. package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
  187. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  188. package/src/data-structures/matrix/index.ts +2 -0
  189. package/src/data-structures/matrix/matrix.ts +491 -0
  190. package/src/data-structures/matrix/navigator.ts +124 -0
  191. package/src/data-structures/priority-queue/index.ts +3 -0
  192. package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
  193. package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
  194. package/src/data-structures/priority-queue/priority-queue.ts +19 -0
  195. package/src/data-structures/queue/deque.ts +1001 -0
  196. package/src/data-structures/queue/index.ts +2 -0
  197. package/src/data-structures/queue/queue.ts +592 -0
  198. package/src/data-structures/stack/index.ts +1 -0
  199. package/src/data-structures/stack/stack.ts +469 -0
  200. package/src/data-structures/tree/index.ts +1 -0
  201. package/src/data-structures/tree/tree.ts +115 -0
  202. package/src/data-structures/trie/index.ts +1 -0
  203. package/src/data-structures/trie/trie.ts +756 -0
  204. package/src/index.ts +24 -0
  205. package/src/interfaces/binary-tree.ts +252 -0
  206. package/src/interfaces/doubly-linked-list.ts +1 -0
  207. package/src/interfaces/graph.ts +44 -0
  208. package/src/interfaces/heap.ts +1 -0
  209. package/src/interfaces/index.ts +8 -0
  210. package/src/interfaces/navigator.ts +1 -0
  211. package/src/interfaces/priority-queue.ts +1 -0
  212. package/src/interfaces/segment-tree.ts +1 -0
  213. package/src/interfaces/singly-linked-list.ts +1 -0
  214. package/src/types/common.ts +25 -0
  215. package/src/types/data-structures/base/base.ts +34 -0
  216. package/src/types/data-structures/base/index.ts +1 -0
  217. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  218. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
  219. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -0
  220. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  221. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  222. package/src/types/data-structures/binary-tree/bst.ts +19 -0
  223. package/src/types/data-structures/binary-tree/index.ts +9 -0
  224. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  225. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  226. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  227. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
  228. package/src/types/data-structures/graph/abstract-graph.ts +18 -0
  229. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  230. package/src/types/data-structures/graph/index.ts +3 -0
  231. package/src/types/data-structures/graph/map-graph.ts +1 -0
  232. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  233. package/src/types/data-structures/hash/hash-map.ts +19 -0
  234. package/src/types/data-structures/hash/index.ts +3 -0
  235. package/src/types/data-structures/heap/heap.ts +6 -0
  236. package/src/types/data-structures/heap/index.ts +1 -0
  237. package/src/types/data-structures/heap/max-heap.ts +1 -0
  238. package/src/types/data-structures/heap/min-heap.ts +1 -0
  239. package/src/types/data-structures/index.ts +12 -0
  240. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
  241. package/src/types/data-structures/linked-list/index.ts +3 -0
  242. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
  243. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  244. package/src/types/data-structures/matrix/index.ts +2 -0
  245. package/src/types/data-structures/matrix/matrix.ts +7 -0
  246. package/src/types/data-structures/matrix/navigator.ts +14 -0
  247. package/src/types/data-structures/priority-queue/index.ts +3 -0
  248. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  249. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  250. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -0
  251. package/src/types/data-structures/queue/deque.ts +5 -0
  252. package/src/types/data-structures/queue/index.ts +2 -0
  253. package/src/types/data-structures/queue/queue.ts +5 -0
  254. package/src/types/data-structures/stack/index.ts +1 -0
  255. package/src/types/data-structures/stack/stack.ts +3 -0
  256. package/src/types/data-structures/tree/index.ts +1 -0
  257. package/src/types/data-structures/tree/tree.ts +1 -0
  258. package/src/types/data-structures/trie/index.ts +1 -0
  259. package/src/types/data-structures/trie/trie.ts +3 -0
  260. package/src/types/index.ts +3 -0
  261. package/src/types/utils/index.ts +2 -0
  262. package/src/types/utils/utils.ts +33 -0
  263. package/src/types/utils/validate-type.ts +35 -0
  264. package/src/utils/index.ts +2 -0
  265. package/src/utils/number.ts +22 -0
  266. package/src/utils/utils.ts +350 -0
  267. package/test/index.test.ts +111 -0
  268. package/tsconfig.base.json +23 -0
  269. package/tsconfig.json +12 -0
  270. package/tsconfig.test.json +8 -0
  271. package/tsconfig.types.json +15 -0
  272. package/tsup.config.js +28 -0
  273. package/tsup.node.config.js +71 -0
@@ -0,0 +1,173 @@
1
+ import type { SkipLinkedListOptions } from '../../types';
2
+
3
+ export class SkipListNode<K, V> {
4
+ key: K;
5
+ value: V;
6
+ forward: SkipListNode<K, V>[];
7
+
8
+ constructor(key: K, value: V, level: number) {
9
+ this.key = key;
10
+ this.value = value;
11
+ this.forward = new Array(level);
12
+ }
13
+ }
14
+
15
+ export class SkipList<K, V> {
16
+ constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
17
+ if (options) {
18
+ const { maxLevel, probability } = options;
19
+ if (typeof maxLevel === 'number') this._maxLevel = maxLevel;
20
+ if (typeof probability === 'number') this._probability = probability;
21
+ }
22
+
23
+ if (elements) {
24
+ for (const [key, value] of elements) this.add(key, value);
25
+ }
26
+ }
27
+
28
+ protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as K, undefined as V, this.maxLevel);
29
+
30
+ get head(): SkipListNode<K, V> {
31
+ return this._head;
32
+ }
33
+
34
+ protected _level: number = 0;
35
+
36
+ get level(): number {
37
+ return this._level;
38
+ }
39
+
40
+ protected _maxLevel: number = 16;
41
+
42
+ get maxLevel(): number {
43
+ return this._maxLevel;
44
+ }
45
+
46
+ protected _probability: number = 0.5;
47
+
48
+ get probability(): number {
49
+ return this._probability;
50
+ }
51
+
52
+ get first(): V | undefined {
53
+ const firstNode = this.head.forward[0];
54
+ return firstNode ? firstNode.value : undefined;
55
+ }
56
+
57
+ get last(): V | undefined {
58
+ let current = this.head;
59
+ for (let i = this.level - 1; i >= 0; i--) {
60
+ while (current.forward[i]) {
61
+ current = current.forward[i];
62
+ }
63
+ }
64
+ return current.value;
65
+ }
66
+
67
+ add(key: K, value: V): void {
68
+ const newNode = new SkipListNode(key, value, this._randomLevel());
69
+ const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
70
+ let current = this.head;
71
+
72
+ for (let i = this.level - 1; i >= 0; i--) {
73
+ while (current.forward[i] && current.forward[i].key < key) {
74
+ current = current.forward[i];
75
+ }
76
+ update[i] = current;
77
+ }
78
+
79
+ for (let i = 0; i < newNode.forward.length; i++) {
80
+ newNode.forward[i] = update[i].forward[i];
81
+ update[i].forward[i] = newNode;
82
+ }
83
+
84
+ if (!newNode.forward[0]) {
85
+ this._level = Math.max(this.level, newNode.forward.length);
86
+ }
87
+ }
88
+
89
+ get(key: K): V | undefined {
90
+ let current = this.head;
91
+ for (let i = this.level - 1; i >= 0; i--) {
92
+ while (current.forward[i] && current.forward[i].key < key) {
93
+ current = current.forward[i];
94
+ }
95
+ }
96
+
97
+ current = current.forward[0];
98
+
99
+ if (current && current.key === key) {
100
+ return current.value;
101
+ }
102
+
103
+ return undefined;
104
+ }
105
+
106
+ has(key: K): boolean {
107
+ return this.get(key) !== undefined;
108
+ }
109
+
110
+ delete(key: K): boolean {
111
+ const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
112
+ let current = this.head;
113
+
114
+ for (let i = this.level - 1; i >= 0; i--) {
115
+ while (current.forward[i] && current.forward[i].key < key) {
116
+ current = current.forward[i];
117
+ }
118
+ update[i] = current;
119
+ }
120
+
121
+ current = current.forward[0];
122
+
123
+ if (current && current.key === key) {
124
+ for (let i = 0; i < this.level; i++) {
125
+ if (update[i].forward[i] !== current) {
126
+ break;
127
+ }
128
+ update[i].forward[i] = current.forward[i];
129
+ }
130
+ while (this.level > 0 && !this.head.forward[this.level - 1]) {
131
+ this._level--;
132
+ }
133
+ return true;
134
+ }
135
+
136
+ return false;
137
+ }
138
+
139
+ higher(key: K): V | undefined {
140
+ let current = this.head;
141
+ for (let i = this.level - 1; i >= 0; i--) {
142
+ while (current.forward[i] && current.forward[i].key <= key) {
143
+ current = current.forward[i];
144
+ }
145
+ }
146
+ const nextNode = current.forward[0];
147
+ return nextNode ? nextNode.value : undefined;
148
+ }
149
+
150
+ lower(key: K): V | undefined {
151
+ let current = this.head;
152
+ let lastLess = undefined;
153
+
154
+ for (let i = this.level - 1; i >= 0; i--) {
155
+ while (current.forward[i] && current.forward[i].key < key) {
156
+ current = current.forward[i];
157
+ }
158
+ if (current.key < key) {
159
+ lastLess = current;
160
+ }
161
+ }
162
+
163
+ return lastLess ? lastLess.value : undefined;
164
+ }
165
+
166
+ protected _randomLevel(): number {
167
+ let level = 1;
168
+ while (Math.random() < this.probability && level < this.maxLevel) {
169
+ level++;
170
+ }
171
+ return level;
172
+ }
173
+ }
@@ -0,0 +1,2 @@
1
+ export * from './matrix';
2
+ export * from './navigator';
@@ -0,0 +1,491 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { MatrixOptions } from '../../types';
9
+
10
+ /**
11
+ *
12
+ */
13
+ export class Matrix {
14
+ /**
15
+ * The constructor function initializes a matrix object with the provided data and options, or with
16
+ * default values if no options are provided.
17
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
18
+ * @param [options] - The `options` parameter is an optional object that can contain the following
19
+ * properties:
20
+ */
21
+ constructor(data: number[][], options?: MatrixOptions) {
22
+ if (options) {
23
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
24
+ if (typeof rows === 'number' && rows > 0) this._rows = rows;
25
+ else this._rows = data.length;
26
+ if (typeof cols === 'number' && cols > 0) this._cols = cols;
27
+ else this._cols = data[0]?.length || 0;
28
+ if (addFn) this._addFn = addFn;
29
+ if (subtractFn) this._subtractFn = subtractFn;
30
+ if (multiplyFn) this._multiplyFn = multiplyFn;
31
+ } else {
32
+ this._rows = data.length;
33
+ this._cols = data[0]?.length ?? 0;
34
+ }
35
+
36
+ if (data.length > 0) {
37
+ this._data = data;
38
+ } else {
39
+ this._data = [];
40
+ for (let i = 0; i < this.rows; i++) {
41
+ this._data[i] = new Array(this.cols).fill(0);
42
+ }
43
+ }
44
+ }
45
+
46
+ protected _rows: number = 0;
47
+
48
+ /**
49
+ * The function returns the number of rows.
50
+ * @returns The number of rows.
51
+ */
52
+ get rows(): number {
53
+ return this._rows;
54
+ }
55
+
56
+ protected _cols: number = 0;
57
+
58
+ /**
59
+ * The function returns the value of the protected variable _cols.
60
+ * @returns The number of columns.
61
+ */
62
+ get cols(): number {
63
+ return this._cols;
64
+ }
65
+
66
+ protected _data: number[][];
67
+
68
+ /**
69
+ * The function returns a two-dimensional array of numbers.
70
+ * @returns The data property, which is a two-dimensional array of numbers.
71
+ */
72
+ get data(): number[][] {
73
+ return this._data;
74
+ }
75
+
76
+ /**
77
+ * The above function returns the value of the _addFn property.
78
+ * @returns The value of the property `_addFn` is being returned.
79
+ */
80
+ get addFn() {
81
+ return this._addFn;
82
+ }
83
+
84
+ /**
85
+ * The function returns the value of the _subtractFn property.
86
+ * @returns The `_subtractFn` property is being returned.
87
+ */
88
+ get subtractFn() {
89
+ return this._subtractFn;
90
+ }
91
+
92
+ /**
93
+ * The function returns the value of the _multiplyFn property.
94
+ * @returns The `_multiplyFn` property is being returned.
95
+ */
96
+ get multiplyFn() {
97
+ return this._multiplyFn;
98
+ }
99
+
100
+ /**
101
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
102
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
103
+ * retrieve from the data array.
104
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
105
+ * retrieve from the data array.
106
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
107
+ * Otherwise, it returns `undefined`.
108
+ */
109
+ get(row: number, col: number): number | undefined {
110
+ if (this.isValidIndex(row, col)) {
111
+ return this.data[row][col];
112
+ }
113
+ }
114
+
115
+ /**
116
+ * The set function updates the value at a specified row and column in a two-dimensional array.
117
+ * @param {number} row - The "row" parameter represents the row index of the element in a
118
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
119
+ * @param {number} col - The "col" parameter represents the column index of the element in a
120
+ * two-dimensional array.
121
+ * @param {number} value - The value parameter represents the number that you want to set at the
122
+ * specified row and column in the data array.
123
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
124
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
125
+ * set.
126
+ */
127
+ set(row: number, col: number, value: number): boolean {
128
+ if (this.isValidIndex(row, col)) {
129
+ this.data[row][col] = value;
130
+ return true;
131
+ }
132
+ return false;
133
+ }
134
+
135
+ /**
136
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
137
+ * matrix.
138
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
139
+ * @returns a boolean value.
140
+ */
141
+ isMatchForCalculate(matrix: Matrix): boolean {
142
+ return this.rows === matrix.rows && this.cols === matrix.cols;
143
+ }
144
+
145
+ /**
146
+ * The `add` function adds two matrices together, returning a new matrix with the result.
147
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
148
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
149
+ * current matrix with the provided `matrix` parameter.
150
+ */
151
+ add(matrix: Matrix): Matrix | undefined {
152
+ if (!this.isMatchForCalculate(matrix)) {
153
+ throw new Error('Matrix dimensions must match for addition.');
154
+ }
155
+
156
+ const resultData: number[][] = [];
157
+ for (let i = 0; i < this.rows; i++) {
158
+ resultData[i] = [];
159
+ for (let j = 0; j < this.cols; j++) {
160
+ const a = this.get(i, j),
161
+ b = matrix.get(i, j);
162
+ if (a !== undefined && b !== undefined) {
163
+ const added = this._addFn(a, b);
164
+ if (added) {
165
+ resultData[i][j] = added;
166
+ }
167
+ }
168
+ }
169
+ }
170
+
171
+ return new Matrix(resultData, {
172
+ rows: this.rows,
173
+ cols: this.cols,
174
+ addFn: this.addFn,
175
+ subtractFn: this.subtractFn,
176
+ multiplyFn: this.multiplyFn
177
+ });
178
+ }
179
+
180
+ /**
181
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
182
+ * matrix with the result.
183
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
184
+ * represents the matrix that you want to subtract from the current matrix.
185
+ * @returns a new Matrix object with the result of the subtraction operation.
186
+ */
187
+ subtract(matrix: Matrix): Matrix | undefined {
188
+ if (!this.isMatchForCalculate(matrix)) {
189
+ throw new Error('Matrix dimensions must match for subtraction.');
190
+ }
191
+
192
+ const resultData: number[][] = [];
193
+ for (let i = 0; i < this.rows; i++) {
194
+ resultData[i] = [];
195
+ for (let j = 0; j < this.cols; j++) {
196
+ const a = this.get(i, j),
197
+ b = matrix.get(i, j);
198
+ if (a !== undefined && b !== undefined) {
199
+ const subtracted = this._subtractFn(a, b);
200
+ if (subtracted) {
201
+ resultData[i][j] = subtracted;
202
+ }
203
+ }
204
+ }
205
+ }
206
+
207
+ return new Matrix(resultData, {
208
+ rows: this.rows,
209
+ cols: this.cols,
210
+ addFn: this.addFn,
211
+ subtractFn: this.subtractFn,
212
+ multiplyFn: this.multiplyFn
213
+ });
214
+ }
215
+
216
+ /**
217
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
218
+ * as a new matrix.
219
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
220
+ * @returns a new Matrix object.
221
+ */
222
+ multiply(matrix: Matrix): Matrix | undefined {
223
+ if (this.cols !== matrix.rows) {
224
+ throw new Error('Matrix dimensions must be compatible for multiplication (A.cols = B.rows).');
225
+ }
226
+
227
+ const resultData: number[][] = [];
228
+ for (let i = 0; i < this.rows; i++) {
229
+ resultData[i] = [];
230
+ for (let j = 0; j < matrix.cols; j++) {
231
+ let sum: number | undefined;
232
+ for (let k = 0; k < this.cols; k++) {
233
+ const a = this.get(i, k),
234
+ b = matrix.get(k, j);
235
+ if (a !== undefined && b !== undefined) {
236
+ const multiplied = this.multiplyFn(a, b);
237
+ if (multiplied !== undefined) {
238
+ sum = this.addFn(sum, multiplied);
239
+ }
240
+ }
241
+ }
242
+ if (sum !== undefined) resultData[i][j] = sum;
243
+ }
244
+ }
245
+
246
+ return new Matrix(resultData, {
247
+ rows: this.rows,
248
+ cols: matrix.cols,
249
+ addFn: this.addFn,
250
+ subtractFn: this.subtractFn,
251
+ multiplyFn: this.multiplyFn
252
+ });
253
+ }
254
+
255
+ /**
256
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
257
+ * original matrix.
258
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
259
+ */
260
+ transpose(): Matrix {
261
+ if (this.data.some(row => row.length !== this.rows)) {
262
+ throw new Error('Matrix must be rectangular for transposition.');
263
+ }
264
+
265
+ const resultData: number[][] = [];
266
+
267
+ for (let j = 0; j < this.cols; j++) {
268
+ resultData[j] = [];
269
+ for (let i = 0; i < this.rows; i++) {
270
+ const trans = this.get(i, j);
271
+ if (trans !== undefined) resultData[j][i] = trans;
272
+ }
273
+ }
274
+
275
+ return new Matrix(resultData, {
276
+ rows: this.cols,
277
+ cols: this.rows,
278
+ addFn: this.addFn,
279
+ subtractFn: this.subtractFn,
280
+ multiplyFn: this.multiplyFn
281
+ });
282
+ }
283
+
284
+ /**
285
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
286
+ * @returns a Matrix object, which represents the inverse of the original matrix.
287
+ */
288
+ inverse(): Matrix | undefined {
289
+ // Check if the matrix is square
290
+ if (this.rows !== this.cols) {
291
+ throw new Error('Matrix must be square for inversion.');
292
+ }
293
+
294
+ // Create an augmented matrix [this | I]
295
+ const augmentedMatrixData: number[][] = [];
296
+ for (let i = 0; i < this.rows; i++) {
297
+ augmentedMatrixData[i] = this.data[i].slice(); // Copy the original matrix
298
+ for (let j = 0; j < this.cols; j++) {
299
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
300
+ }
301
+ }
302
+
303
+ const augmentedMatrix = new Matrix(augmentedMatrixData, {
304
+ rows: this.rows,
305
+ cols: this.cols * 2,
306
+ addFn: this.addFn,
307
+ subtractFn: this.subtractFn,
308
+ multiplyFn: this.multiplyFn
309
+ });
310
+
311
+ // Apply Gaussian elimination to transform the left half into the identity matrix
312
+ for (let i = 0; i < this.rows; i++) {
313
+ // Find pivot
314
+ let pivotRow = i;
315
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
316
+ pivotRow++;
317
+ }
318
+
319
+ if (pivotRow === this.rows) {
320
+ // Matrix is singular, and its inverse does not exist
321
+ throw new Error('Matrix is singular, and its inverse does not exist.');
322
+ }
323
+
324
+ // Swap rows to make the pivot the current row
325
+ augmentedMatrix._swapRows(i, pivotRow);
326
+
327
+ // Scale the pivot row to make the pivot element 1
328
+ const pivotElement = augmentedMatrix.get(i, i) ?? 1;
329
+
330
+ if (pivotElement === 0) {
331
+ // Handle division by zero
332
+ throw new Error('Matrix is singular, and its inverse does not exist (division by zero).');
333
+ }
334
+
335
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
336
+
337
+ // Eliminate other rows to make elements in the current column zero
338
+ for (let j = 0; j < this.rows; j++) {
339
+ if (j !== i) {
340
+ let factor = augmentedMatrix.get(j, i);
341
+ if (factor === undefined) factor = 0;
342
+
343
+ augmentedMatrix._addScaledRow(j, i, -factor);
344
+ }
345
+ }
346
+ }
347
+
348
+ // Extract the right half of the augmented matrix as the inverse
349
+ const inverseData: number[][] = [];
350
+ for (let i = 0; i < this.rows; i++) {
351
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
352
+ }
353
+
354
+ return new Matrix(inverseData, {
355
+ rows: this.rows,
356
+ cols: this.cols,
357
+ addFn: this.addFn,
358
+ subtractFn: this.subtractFn,
359
+ multiplyFn: this.multiplyFn
360
+ });
361
+ }
362
+
363
+ /**
364
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
365
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
366
+ * @returns a new Matrix object.
367
+ */
368
+ dot(matrix: Matrix): Matrix | undefined {
369
+ if (this.cols !== matrix.rows) {
370
+ throw new Error(
371
+ 'Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.'
372
+ );
373
+ }
374
+
375
+ const resultData: number[][] = [];
376
+ for (let i = 0; i < this.rows; i++) {
377
+ resultData[i] = [];
378
+ for (let j = 0; j < matrix.cols; j++) {
379
+ let sum: number | undefined;
380
+ for (let k = 0; k < this.cols; k++) {
381
+ const a = this.get(i, k),
382
+ b = matrix.get(k, j);
383
+ if (a !== undefined && b !== undefined) {
384
+ const multiplied = this.multiplyFn(a, b);
385
+ if (multiplied !== undefined) {
386
+ sum = this.addFn(sum, multiplied);
387
+ }
388
+ }
389
+ }
390
+ if (sum !== undefined) resultData[i][j] = sum;
391
+ }
392
+ }
393
+
394
+ return new Matrix(resultData, {
395
+ rows: this.rows,
396
+ cols: matrix.cols,
397
+ addFn: this.addFn,
398
+ subtractFn: this.subtractFn,
399
+ multiplyFn: this.multiplyFn
400
+ });
401
+ }
402
+
403
+ /**
404
+ * The function checks if a given row and column index is valid within a specified range.
405
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
406
+ * matrix. It is a number that indicates the specific row in the matrix.
407
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
408
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
409
+ * @returns A boolean value is being returned.
410
+ */
411
+ isValidIndex(row: number, col: number): boolean {
412
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
413
+ }
414
+
415
+ /**
416
+ * The `clone` function returns a new instance of the Matrix class with the same data and properties
417
+ * as the original instance.
418
+ * @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
419
+ * and properties as the current instance.
420
+ */
421
+ clone(): Matrix {
422
+ return new Matrix(this.data, {
423
+ rows: this.rows,
424
+ cols: this.cols,
425
+ addFn: this.addFn,
426
+ subtractFn: this.subtractFn,
427
+ multiplyFn: this.multiplyFn
428
+ });
429
+ }
430
+
431
+ protected _addFn(a: number | undefined, b: number): number | undefined {
432
+ if (a === undefined) return b;
433
+ return a + b;
434
+ }
435
+
436
+ protected _subtractFn(a: number, b: number) {
437
+ return a - b;
438
+ }
439
+
440
+ protected _multiplyFn(a: number, b: number) {
441
+ return a * b;
442
+ }
443
+
444
+ /**
445
+ * The function `_swapRows` swaps the positions of two rows in an array.
446
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
447
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
448
+ * with the first row.
449
+ */
450
+ protected _swapRows(row1: number, row2: number): void {
451
+ const temp = this.data[row1];
452
+ this.data[row1] = this.data[row2];
453
+ this.data[row2] = temp;
454
+ }
455
+
456
+ /**
457
+ * The function scales a specific row in a matrix by a given scalar value.
458
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
459
+ * want to scale. It is a number that indicates the position of the row within the matrix.
460
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
461
+ * a specific row of a matrix.
462
+ */
463
+ protected _scaleRow(row: number, scalar: number): void {
464
+ for (let j = 0; j < this.cols; j++) {
465
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
466
+ if (multiplied === undefined) multiplied = 0;
467
+ this.data[row][j] = multiplied;
468
+ }
469
+ }
470
+
471
+ /**
472
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
473
+ * row.
474
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
475
+ * scaled values will be added.
476
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
477
+ * values will be scaled and added to the targetRow.
478
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
479
+ * source row before adding them to the target row.
480
+ */
481
+ protected _addScaledRow(targetRow: number, sourceRow: number, scalar: number): void {
482
+ for (let j = 0; j < this.cols; j++) {
483
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
484
+ if (multiplied === undefined) multiplied = 0;
485
+ const scaledValue = multiplied;
486
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
487
+ if (added === undefined) added = 0;
488
+ this.data[targetRow][j] = added;
489
+ }
490
+ }
491
+ }