data-structure-typed 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +5 -1
  7. package/README.md +124 -29
  8. package/dist/cjs/binary-tree.cjs +26282 -0
  9. package/dist/cjs/graph.cjs +5422 -0
  10. package/dist/cjs/hash.cjs +1310 -0
  11. package/dist/cjs/heap.cjs +1602 -0
  12. package/dist/cjs/index.cjs +31257 -14673
  13. package/dist/cjs/linked-list.cjs +4576 -0
  14. package/dist/cjs/matrix.cjs +1080 -0
  15. package/dist/cjs/priority-queue.cjs +1376 -0
  16. package/dist/cjs/queue.cjs +4264 -0
  17. package/dist/cjs/stack.cjs +907 -0
  18. package/dist/cjs/trie.cjs +1223 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +26319 -0
  20. package/dist/cjs-legacy/graph.cjs +5420 -0
  21. package/dist/cjs-legacy/hash.cjs +1310 -0
  22. package/dist/cjs-legacy/heap.cjs +1599 -0
  23. package/dist/cjs-legacy/index.cjs +31268 -14679
  24. package/dist/cjs-legacy/linked-list.cjs +4582 -0
  25. package/dist/cjs-legacy/matrix.cjs +1083 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1374 -0
  27. package/dist/cjs-legacy/queue.cjs +4262 -0
  28. package/dist/cjs-legacy/stack.cjs +907 -0
  29. package/dist/cjs-legacy/trie.cjs +1222 -0
  30. package/dist/esm/binary-tree.mjs +26267 -0
  31. package/dist/esm/graph.mjs +5409 -0
  32. package/dist/esm/hash.mjs +1307 -0
  33. package/dist/esm/heap.mjs +1596 -0
  34. package/dist/esm/index.mjs +31254 -14674
  35. package/dist/esm/linked-list.mjs +4569 -0
  36. package/dist/esm/matrix.mjs +1076 -0
  37. package/dist/esm/priority-queue.mjs +1372 -0
  38. package/dist/esm/queue.mjs +4260 -0
  39. package/dist/esm/stack.mjs +905 -0
  40. package/dist/esm/trie.mjs +1220 -0
  41. package/dist/esm-legacy/binary-tree.mjs +26304 -0
  42. package/dist/esm-legacy/graph.mjs +5407 -0
  43. package/dist/esm-legacy/hash.mjs +1307 -0
  44. package/dist/esm-legacy/heap.mjs +1593 -0
  45. package/dist/esm-legacy/index.mjs +31265 -14680
  46. package/dist/esm-legacy/linked-list.mjs +4575 -0
  47. package/dist/esm-legacy/matrix.mjs +1079 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1370 -0
  49. package/dist/esm-legacy/queue.mjs +4258 -0
  50. package/dist/esm-legacy/stack.mjs +905 -0
  51. package/dist/esm-legacy/trie.mjs +1219 -0
  52. package/dist/types/common/error.d.ts +9 -0
  53. package/dist/types/common/index.d.ts +1 -1
  54. package/dist/types/data-structures/base/index.d.ts +1 -0
  55. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  56. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  57. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  61. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  62. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  63. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  64. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  65. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  66. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  70. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  71. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  72. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  73. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  74. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  75. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  76. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  77. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  78. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  79. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  80. package/dist/types/interfaces/graph.d.ts +1 -1
  81. package/dist/types/types/common.d.ts +2 -2
  82. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  83. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  84. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  85. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  86. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  87. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  88. package/dist/types/types/utils/validate-type.d.ts +4 -4
  89. package/dist/umd/data-structure-typed.js +31196 -14608
  90. package/dist/umd/data-structure-typed.min.js +11 -5
  91. package/docs-site-docusaurus/README.md +41 -0
  92. package/docs-site-docusaurus/docs/api/README.md +52 -0
  93. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
  94. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  95. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  96. package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
  97. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  98. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  99. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  100. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  101. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  102. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  103. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  104. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  105. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  106. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  107. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  108. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  109. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  110. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  111. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  112. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  113. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  116. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  117. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  118. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  119. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  120. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  121. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  122. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  123. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  124. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  125. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
  126. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  127. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  128. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  129. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  130. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  131. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
  132. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
  133. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
  134. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  135. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  136. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  137. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  138. package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
  139. package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
  140. package/docs-site-docusaurus/docs/guide/faq.md +180 -0
  141. package/docs-site-docusaurus/docs/guide/guides.md +597 -0
  142. package/docs-site-docusaurus/docs/guide/installation.md +62 -0
  143. package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
  144. package/docs-site-docusaurus/docs/guide/overview.md +645 -0
  145. package/docs-site-docusaurus/docs/guide/performance.md +835 -0
  146. package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
  147. package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
  148. package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
  149. package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
  150. package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
  151. package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
  152. package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
  153. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  154. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  155. package/docs-site-docusaurus/package-lock.json +18667 -0
  156. package/docs-site-docusaurus/package.json +50 -0
  157. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  158. package/docs-site-docusaurus/sidebars.ts +23 -0
  159. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  160. package/docs-site-docusaurus/src/css/custom.css +96 -0
  161. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  162. package/docs-site-docusaurus/src/pages/index.tsx +120 -0
  163. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  164. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  165. package/docs-site-docusaurus/static/.nojekyll +0 -0
  166. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  167. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  168. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  169. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  170. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  171. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  172. package/docs-site-docusaurus/static/img/logo.png +0 -0
  173. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  174. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  175. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  176. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  177. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  178. package/docs-site-docusaurus/static/llms.txt +37 -0
  179. package/docs-site-docusaurus/static/robots.txt +4 -0
  180. package/docs-site-docusaurus/typedoc.json +23 -0
  181. package/llms.txt +37 -0
  182. package/package.json +159 -55
  183. package/src/common/error.ts +19 -1
  184. package/src/common/index.ts +1 -1
  185. package/src/data-structures/base/index.ts +1 -0
  186. package/src/data-structures/base/iterable-element-base.ts +3 -2
  187. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  188. package/src/data-structures/base/linear-base.ts +3 -3
  189. package/src/data-structures/binary-tree/avl-tree.ts +287 -0
  190. package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
  191. package/src/data-structures/binary-tree/binary-tree.ts +581 -6
  192. package/src/data-structures/binary-tree/bst.ts +922 -7
  193. package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
  194. package/src/data-structures/binary-tree/segment-tree.ts +139 -2
  195. package/src/data-structures/binary-tree/tree-map.ts +3300 -495
  196. package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
  197. package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
  198. package/src/data-structures/binary-tree/tree-set.ts +3122 -440
  199. package/src/data-structures/graph/abstract-graph.ts +6 -6
  200. package/src/data-structures/graph/directed-graph.ts +230 -0
  201. package/src/data-structures/graph/undirected-graph.ts +207 -0
  202. package/src/data-structures/hash/hash-map.ts +270 -19
  203. package/src/data-structures/heap/heap.ts +326 -4
  204. package/src/data-structures/heap/max-heap.ts +2 -2
  205. package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
  206. package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
  207. package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
  208. package/src/data-structures/matrix/matrix.ts +194 -10
  209. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  210. package/src/data-structures/queue/deque.ts +350 -5
  211. package/src/data-structures/queue/queue.ts +276 -0
  212. package/src/data-structures/stack/stack.ts +230 -0
  213. package/src/data-structures/trie/trie.ts +283 -7
  214. package/src/interfaces/graph.ts +1 -1
  215. package/src/types/common.ts +2 -2
  216. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  217. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  218. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  219. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  220. package/src/types/data-structures/heap/heap.ts +1 -0
  221. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  222. package/src/types/utils/validate-type.ts +4 -4
  223. package/vercel.json +6 -0
  224. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  225. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  226. package/dist/leetcode/avl-tree.mjs +0 -2720
  227. package/dist/leetcode/binary-tree.mjs +0 -1594
  228. package/dist/leetcode/bst.mjs +0 -2398
  229. package/dist/leetcode/deque.mjs +0 -683
  230. package/dist/leetcode/directed-graph.mjs +0 -1733
  231. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  232. package/dist/leetcode/hash-map.mjs +0 -493
  233. package/dist/leetcode/heap.mjs +0 -542
  234. package/dist/leetcode/max-heap.mjs +0 -375
  235. package/dist/leetcode/max-priority-queue.mjs +0 -383
  236. package/dist/leetcode/min-heap.mjs +0 -363
  237. package/dist/leetcode/min-priority-queue.mjs +0 -371
  238. package/dist/leetcode/priority-queue.mjs +0 -363
  239. package/dist/leetcode/queue.mjs +0 -943
  240. package/dist/leetcode/red-black-tree.mjs +0 -2765
  241. package/dist/leetcode/singly-linked-list.mjs +0 -754
  242. package/dist/leetcode/stack.mjs +0 -217
  243. package/dist/leetcode/tree-counter.mjs +0 -3039
  244. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  245. package/dist/leetcode/trie.mjs +0 -413
  246. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -1,683 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- var rangeCheck = __name((index, min, max, message = "Index out of bounds.") => {
4
- if (index < min || index > max) throw new RangeError(message);
5
- }, "rangeCheck");
6
- var calcMinUnitsRequired = __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
7
- var IterableElementBase = class {
8
- static {
9
- __name(this, "IterableElementBase");
10
- }
11
- constructor(options) {
12
- if (options) {
13
- const { toElementFn } = options;
14
- if (typeof toElementFn === "function") this._toElementFn = toElementFn;
15
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
16
- }
17
- }
18
- _toElementFn;
19
- get toElementFn() {
20
- return this._toElementFn;
21
- }
22
- *[Symbol.iterator](...args) {
23
- yield* this._getIterator(...args);
24
- }
25
- *values() {
26
- for (const item of this) yield item;
27
- }
28
- every(predicate, thisArg) {
29
- let index = 0;
30
- for (const item of this) {
31
- if (thisArg === void 0) {
32
- if (!predicate(item, index++, this)) return false;
33
- } else {
34
- const fn = predicate;
35
- if (!fn.call(thisArg, item, index++, this)) return false;
36
- }
37
- }
38
- return true;
39
- }
40
- some(predicate, thisArg) {
41
- let index = 0;
42
- for (const item of this) {
43
- if (thisArg === void 0) {
44
- if (predicate(item, index++, this)) return true;
45
- } else {
46
- const fn = predicate;
47
- if (fn.call(thisArg, item, index++, this)) return true;
48
- }
49
- }
50
- return false;
51
- }
52
- forEach(callbackfn, thisArg) {
53
- let index = 0;
54
- for (const item of this) {
55
- if (thisArg === void 0) {
56
- callbackfn(item, index++, this);
57
- } else {
58
- const fn = callbackfn;
59
- fn.call(thisArg, item, index++, this);
60
- }
61
- }
62
- }
63
- find(predicate, thisArg) {
64
- let index = 0;
65
- for (const item of this) {
66
- if (thisArg === void 0) {
67
- if (predicate(item, index++, this)) return item;
68
- } else {
69
- const fn = predicate;
70
- if (fn.call(thisArg, item, index++, this)) return item;
71
- }
72
- }
73
- return;
74
- }
75
- has(element) {
76
- for (const ele of this) if (ele === element) return true;
77
- return false;
78
- }
79
- reduce(callbackfn, initialValue) {
80
- let index = 0;
81
- const iter = this[Symbol.iterator]();
82
- let acc;
83
- if (arguments.length >= 2) {
84
- acc = initialValue;
85
- } else {
86
- const first = iter.next();
87
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
88
- acc = first.value;
89
- index = 1;
90
- }
91
- for (const value of iter) {
92
- acc = callbackfn(acc, value, index++, this);
93
- }
94
- return acc;
95
- }
96
- toArray() {
97
- return [...this];
98
- }
99
- toVisual() {
100
- return [...this];
101
- }
102
- print() {
103
- console.log(this.toVisual());
104
- }
105
- };
106
- var LinearBase = class _LinearBase extends IterableElementBase {
107
- static {
108
- __name(this, "LinearBase");
109
- }
110
- constructor(options) {
111
- super(options);
112
- if (options) {
113
- const { maxLen } = options;
114
- if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
115
- }
116
- }
117
- _maxLen = -1;
118
- get maxLen() {
119
- return this._maxLen;
120
- }
121
- indexOf(searchElement, fromIndex = 0) {
122
- if (this.length === 0) return -1;
123
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
124
- if (fromIndex < 0) fromIndex = 0;
125
- for (let i = fromIndex; i < this.length; i++) {
126
- const element = this.at(i);
127
- if (element === searchElement) return i;
128
- }
129
- return -1;
130
- }
131
- lastIndexOf(searchElement, fromIndex = this.length - 1) {
132
- if (this.length === 0) return -1;
133
- if (fromIndex >= this.length) fromIndex = this.length - 1;
134
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
135
- for (let i = fromIndex; i >= 0; i--) {
136
- const element = this.at(i);
137
- if (element === searchElement) return i;
138
- }
139
- return -1;
140
- }
141
- findIndex(predicate, thisArg) {
142
- for (let i = 0; i < this.length; i++) {
143
- const item = this.at(i);
144
- if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
145
- }
146
- return -1;
147
- }
148
- concat(...items) {
149
- const newList = this.clone();
150
- for (const item of items) {
151
- if (item instanceof _LinearBase) {
152
- newList.pushMany(item);
153
- } else {
154
- newList.push(item);
155
- }
156
- }
157
- return newList;
158
- }
159
- sort(compareFn) {
160
- const arr = this.toArray();
161
- arr.sort(compareFn);
162
- this.clear();
163
- for (const item of arr) this.push(item);
164
- return this;
165
- }
166
- splice(start, deleteCount = 0, ...items) {
167
- const removedList = this._createInstance();
168
- start = start < 0 ? this.length + start : start;
169
- start = Math.max(0, Math.min(start, this.length));
170
- deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
171
- for (let i = 0; i < deleteCount; i++) {
172
- const removed = this.deleteAt(start);
173
- if (removed !== void 0) {
174
- removedList.push(removed);
175
- }
176
- }
177
- for (let i = 0; i < items.length; i++) {
178
- this.addAt(start + i, items[i]);
179
- }
180
- return removedList;
181
- }
182
- join(separator = ",") {
183
- return this.toArray().join(separator);
184
- }
185
- toReversedArray() {
186
- const array = [];
187
- for (let i = this.length - 1; i >= 0; i--) {
188
- array.push(this.at(i));
189
- }
190
- return array;
191
- }
192
- reduceRight(callbackfn, initialValue) {
193
- let accumulator = initialValue ?? 0;
194
- for (let i = this.length - 1; i >= 0; i--) {
195
- accumulator = callbackfn(accumulator, this.at(i), i, this);
196
- }
197
- return accumulator;
198
- }
199
- slice(start = 0, end = this.length) {
200
- start = start < 0 ? this.length + start : start;
201
- end = end < 0 ? this.length + end : end;
202
- const newList = this._createInstance();
203
- for (let i = start; i < end; i++) {
204
- newList.push(this.at(i));
205
- }
206
- return newList;
207
- }
208
- fill(value, start = 0, end = this.length) {
209
- start = start < 0 ? this.length + start : start;
210
- end = end < 0 ? this.length + end : end;
211
- if (start < 0) start = 0;
212
- if (end > this.length) end = this.length;
213
- if (start >= end) return this;
214
- for (let i = start; i < end; i++) {
215
- this.setAt(i, value);
216
- }
217
- return this;
218
- }
219
- };
220
- var Deque = class extends LinearBase {
221
- static {
222
- __name(this, "Deque");
223
- }
224
- _equals = Object.is;
225
- constructor(elements = [], options) {
226
- super(options);
227
- if (options) {
228
- const { bucketSize } = options;
229
- if (typeof bucketSize === "number") this._bucketSize = bucketSize;
230
- }
231
- let _size;
232
- if ("length" in elements) {
233
- _size = typeof elements.length === "function" ? elements.length() : elements.length;
234
- } else {
235
- _size = typeof elements.size === "function" ? elements.size() : elements.size;
236
- }
237
- this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
238
- for (let i = 0; i < this._bucketCount; ++i) {
239
- this._buckets.push(new Array(this._bucketSize));
240
- }
241
- const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
242
- this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
243
- this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
244
- this.pushMany(elements);
245
- }
246
- _bucketSize = 1 << 12;
247
- get bucketSize() {
248
- return this._bucketSize;
249
- }
250
- _bucketFirst = 0;
251
- get bucketFirst() {
252
- return this._bucketFirst;
253
- }
254
- _firstInBucket = 0;
255
- get firstInBucket() {
256
- return this._firstInBucket;
257
- }
258
- _bucketLast = 0;
259
- get bucketLast() {
260
- return this._bucketLast;
261
- }
262
- _lastInBucket = 0;
263
- get lastInBucket() {
264
- return this._lastInBucket;
265
- }
266
- _bucketCount = 0;
267
- get bucketCount() {
268
- return this._bucketCount;
269
- }
270
- _buckets = [];
271
- get buckets() {
272
- return this._buckets;
273
- }
274
- _length = 0;
275
- get length() {
276
- return this._length;
277
- }
278
- get first() {
279
- if (this._length === 0) return;
280
- return this._buckets[this._bucketFirst][this._firstInBucket];
281
- }
282
- get last() {
283
- if (this._length === 0) return;
284
- return this._buckets[this._bucketLast][this._lastInBucket];
285
- }
286
- static fromArray(data, options) {
287
- return new this(data, options);
288
- }
289
- push(element) {
290
- if (this._length) {
291
- if (this._lastInBucket < this._bucketSize - 1) {
292
- this._lastInBucket += 1;
293
- } else if (this._bucketLast < this._bucketCount - 1) {
294
- this._bucketLast += 1;
295
- this._lastInBucket = 0;
296
- } else {
297
- this._bucketLast = 0;
298
- this._lastInBucket = 0;
299
- }
300
- if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
301
- }
302
- this._length += 1;
303
- this._buckets[this._bucketLast][this._lastInBucket] = element;
304
- if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
305
- return true;
306
- }
307
- pop() {
308
- if (this._length === 0) return;
309
- const element = this._buckets[this._bucketLast][this._lastInBucket];
310
- if (this._length !== 1) {
311
- if (this._lastInBucket > 0) {
312
- this._lastInBucket -= 1;
313
- } else if (this._bucketLast > 0) {
314
- this._bucketLast -= 1;
315
- this._lastInBucket = this._bucketSize - 1;
316
- } else {
317
- this._bucketLast = this._bucketCount - 1;
318
- this._lastInBucket = this._bucketSize - 1;
319
- }
320
- }
321
- this._length -= 1;
322
- return element;
323
- }
324
- shift() {
325
- if (this._length === 0) return;
326
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
327
- if (this._length !== 1) {
328
- if (this._firstInBucket < this._bucketSize - 1) {
329
- this._firstInBucket += 1;
330
- } else if (this._bucketFirst < this._bucketCount - 1) {
331
- this._bucketFirst += 1;
332
- this._firstInBucket = 0;
333
- } else {
334
- this._bucketFirst = 0;
335
- this._firstInBucket = 0;
336
- }
337
- }
338
- this._length -= 1;
339
- return element;
340
- }
341
- unshift(element) {
342
- if (this._length) {
343
- if (this._firstInBucket > 0) {
344
- this._firstInBucket -= 1;
345
- } else if (this._bucketFirst > 0) {
346
- this._bucketFirst -= 1;
347
- this._firstInBucket = this._bucketSize - 1;
348
- } else {
349
- this._bucketFirst = this._bucketCount - 1;
350
- this._firstInBucket = this._bucketSize - 1;
351
- }
352
- if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
353
- }
354
- this._length += 1;
355
- this._buckets[this._bucketFirst][this._firstInBucket] = element;
356
- if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
357
- return true;
358
- }
359
- pushMany(elements) {
360
- const ans = [];
361
- for (const el of elements) {
362
- if (this.toElementFn) {
363
- ans.push(this.push(this.toElementFn(el)));
364
- } else {
365
- ans.push(this.push(el));
366
- }
367
- }
368
- return ans;
369
- }
370
- unshiftMany(elements = []) {
371
- const ans = [];
372
- for (const el of elements) {
373
- if (this.toElementFn) {
374
- ans.push(this.unshift(this.toElementFn(el)));
375
- } else {
376
- ans.push(this.unshift(el));
377
- }
378
- }
379
- return ans;
380
- }
381
- isEmpty() {
382
- return this._length === 0;
383
- }
384
- clear() {
385
- this._buckets = [new Array(this._bucketSize)];
386
- this._bucketCount = 1;
387
- this._bucketFirst = this._bucketLast = this._length = 0;
388
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
389
- }
390
- at(pos) {
391
- if (pos < 0 || pos >= this._length) return void 0;
392
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
393
- return this._buckets[bucketIndex][indexInBucket];
394
- }
395
- setAt(pos, element) {
396
- rangeCheck(pos, 0, this._length - 1);
397
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
398
- this._buckets[bucketIndex][indexInBucket] = element;
399
- return true;
400
- }
401
- addAt(pos, element, num = 1) {
402
- const length = this._length;
403
- rangeCheck(pos, 0, length);
404
- if (pos === 0) {
405
- while (num--) this.unshift(element);
406
- } else if (pos === this._length) {
407
- while (num--) this.push(element);
408
- } else {
409
- const arr = [];
410
- for (let i = pos; i < this._length; ++i) {
411
- const v = this.at(i);
412
- if (v !== void 0) arr.push(v);
413
- }
414
- this.cut(pos - 1, true);
415
- for (let i = 0; i < num; ++i) this.push(element);
416
- for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
417
- }
418
- return true;
419
- }
420
- cut(pos, isCutSelf = false) {
421
- if (isCutSelf) {
422
- if (pos < 0) {
423
- this.clear();
424
- return this;
425
- }
426
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
427
- this._bucketLast = bucketIndex;
428
- this._lastInBucket = indexInBucket;
429
- this._length = pos + 1;
430
- return this;
431
- } else {
432
- const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
433
- newDeque._setBucketSize(this._bucketSize);
434
- for (let i = 0; i <= pos; i++) {
435
- const v = this.at(i);
436
- if (v !== void 0) newDeque.push(v);
437
- }
438
- return newDeque;
439
- }
440
- }
441
- splice(start, deleteCount = this._length - start, ...items) {
442
- rangeCheck(start, 0, this._length);
443
- if (deleteCount < 0) deleteCount = 0;
444
- if (start + deleteCount > this._length) deleteCount = this._length - start;
445
- const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
446
- removed._setBucketSize(this._bucketSize);
447
- for (let i = 0; i < deleteCount; i++) {
448
- const v = this.at(start + i);
449
- if (v !== void 0) removed.push(v);
450
- }
451
- const tail = [];
452
- for (let i = start + deleteCount; i < this._length; i++) {
453
- const v = this.at(i);
454
- if (v !== void 0) tail.push(v);
455
- }
456
- this.cut(start - 1, true);
457
- for (const it of items) this.push(it);
458
- for (const v of tail) this.push(v);
459
- return removed;
460
- }
461
- cutRest(pos, isCutSelf = false) {
462
- if (isCutSelf) {
463
- if (pos < 0) {
464
- return this;
465
- }
466
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
467
- this._bucketFirst = bucketIndex;
468
- this._firstInBucket = indexInBucket;
469
- this._length = this._length - pos;
470
- return this;
471
- } else {
472
- const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
473
- newDeque._setBucketSize(this._bucketSize);
474
- if (pos < 0) pos = 0;
475
- for (let i = pos; i < this._length; i++) {
476
- const v = this.at(i);
477
- if (v !== void 0) newDeque.push(v);
478
- }
479
- return newDeque;
480
- }
481
- }
482
- deleteAt(pos) {
483
- rangeCheck(pos, 0, this._length - 1);
484
- let deleted;
485
- if (pos === 0) {
486
- return this.shift();
487
- } else if (pos === this._length - 1) {
488
- deleted = this.last;
489
- this.pop();
490
- return deleted;
491
- } else {
492
- const length = this._length - 1;
493
- const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
494
- deleted = this._buckets[targetBucket][targetPointer];
495
- for (let i = pos; i < length; i++) {
496
- const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
497
- const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
498
- this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
499
- }
500
- this.pop();
501
- return deleted;
502
- }
503
- }
504
- delete(element) {
505
- const size = this._length;
506
- if (size === 0) return false;
507
- let i = 0;
508
- let index = 0;
509
- while (i < size) {
510
- const oldElement = this.at(i);
511
- if (!this._equals(oldElement, element)) {
512
- this.setAt(index, oldElement);
513
- index += 1;
514
- }
515
- i += 1;
516
- }
517
- this.cut(index - 1, true);
518
- return true;
519
- }
520
- deleteWhere(predicate) {
521
- for (let i = 0; i < this._length; i++) {
522
- const v = this.at(i);
523
- if (predicate(v, i, this)) {
524
- this.deleteAt(i);
525
- return true;
526
- }
527
- }
528
- return false;
529
- }
530
- setEquality(equals) {
531
- this._equals = equals;
532
- return this;
533
- }
534
- reverse() {
535
- this._buckets.reverse().forEach(function(bucket) {
536
- bucket.reverse();
537
- });
538
- const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
539
- this._bucketFirst = this._bucketCount - _bucketLast - 1;
540
- this._bucketLast = this._bucketCount - _bucketFirst - 1;
541
- this._firstInBucket = this._bucketSize - _lastInBucket - 1;
542
- this._lastInBucket = this._bucketSize - _firstInBucket - 1;
543
- return this;
544
- }
545
- unique() {
546
- if (this._length <= 1) {
547
- return this;
548
- }
549
- let index = 1;
550
- let prev = this.at(0);
551
- for (let i = 1; i < this._length; ++i) {
552
- const cur = this.at(i);
553
- if (!this._equals(cur, prev)) {
554
- prev = cur;
555
- this.setAt(index++, cur);
556
- }
557
- }
558
- this.cut(index - 1, true);
559
- return this;
560
- }
561
- shrinkToFit() {
562
- if (this._length === 0) return;
563
- const newBuckets = [];
564
- if (this._bucketFirst === this._bucketLast) return;
565
- else if (this._bucketFirst < this._bucketLast) {
566
- for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
567
- newBuckets.push(this._buckets[i]);
568
- }
569
- } else {
570
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
571
- newBuckets.push(this._buckets[i]);
572
- }
573
- for (let i = 0; i <= this._bucketLast; ++i) {
574
- newBuckets.push(this._buckets[i]);
575
- }
576
- }
577
- this._bucketFirst = 0;
578
- this._bucketLast = newBuckets.length - 1;
579
- this._buckets = newBuckets;
580
- }
581
- clone() {
582
- return this._createLike(this, {
583
- bucketSize: this.bucketSize,
584
- toElementFn: this.toElementFn,
585
- maxLen: this._maxLen
586
- });
587
- }
588
- filter(predicate, thisArg) {
589
- const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
590
- out._setBucketSize(this._bucketSize);
591
- let index = 0;
592
- for (const el of this) {
593
- if (predicate.call(thisArg, el, index, this)) out.push(el);
594
- index++;
595
- }
596
- return out;
597
- }
598
- mapSame(callback, thisArg) {
599
- const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
600
- out._setBucketSize(this._bucketSize);
601
- let index = 0;
602
- for (const v of this) {
603
- const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
604
- out.push(mv);
605
- }
606
- return out;
607
- }
608
- map(callback, options, thisArg) {
609
- const out = this._createLike([], {
610
- ...options ?? {},
611
- bucketSize: this._bucketSize,
612
- maxLen: this._maxLen
613
- });
614
- let index = 0;
615
- for (const el of this) {
616
- const mv = thisArg === void 0 ? callback(el, index, this) : callback.call(thisArg, el, index, this);
617
- out.push(mv);
618
- index++;
619
- }
620
- return out;
621
- }
622
- _setBucketSize(size) {
623
- this._bucketSize = size;
624
- }
625
- *_getIterator() {
626
- for (let i = 0; i < this._length; ++i) {
627
- const v = this.at(i);
628
- if (v !== void 0) yield v;
629
- }
630
- }
631
- _reallocate(needBucketNum) {
632
- const newBuckets = [];
633
- const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
634
- for (let i = 0; i < addBucketNum; ++i) {
635
- newBuckets[i] = new Array(this._bucketSize);
636
- }
637
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
638
- newBuckets[newBuckets.length] = this._buckets[i];
639
- }
640
- for (let i = 0; i < this._bucketLast; ++i) {
641
- newBuckets[newBuckets.length] = this._buckets[i];
642
- }
643
- newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
644
- this._bucketFirst = addBucketNum;
645
- this._bucketLast = newBuckets.length - 1;
646
- for (let i = 0; i < addBucketNum; ++i) {
647
- newBuckets[newBuckets.length] = new Array(this._bucketSize);
648
- }
649
- this._buckets = newBuckets;
650
- this._bucketCount = newBuckets.length;
651
- }
652
- _getBucketAndPosition(pos) {
653
- let bucketIndex;
654
- let indexInBucket;
655
- const overallIndex = this._firstInBucket + pos;
656
- bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
657
- if (bucketIndex >= this._bucketCount) {
658
- bucketIndex -= this._bucketCount;
659
- }
660
- indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
661
- if (indexInBucket < 0) {
662
- indexInBucket = this._bucketSize - 1;
663
- }
664
- return { bucketIndex, indexInBucket };
665
- }
666
- _createInstance(options) {
667
- const Ctor = this.constructor;
668
- return new Ctor([], options);
669
- }
670
- _createLike(elements = [], options) {
671
- const Ctor = this.constructor;
672
- return new Ctor(elements, options);
673
- }
674
- *_getReverseIterator() {
675
- for (let i = this._length - 1; i > -1; i--) {
676
- const v = this.at(i);
677
- if (v !== void 0) yield v;
678
- }
679
- }
680
- };
681
- export {
682
- Deque
683
- };