index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * @description The iterator type including `NORMAL` and `REVERSE`.
  3. */
  4. declare const enum IteratorType {
  5. NORMAL = 0,
  6. REVERSE = 1
  7. }
  8. declare abstract class ContainerIterator<T> {
  9. /**
  10. * @description The container pointed to by the iterator.
  11. */
  12. abstract readonly container: Container<T>;
  13. /**
  14. * @description Iterator's type.
  15. * @example
  16. * console.log(container.end().iteratorType === IteratorType.NORMAL); // true
  17. */
  18. readonly iteratorType: IteratorType;
  19. /**
  20. * @param iter - The other iterator you want to compare.
  21. * @returns Whether this equals to obj.
  22. * @example
  23. * container.find(1).equals(container.end());
  24. */
  25. equals(iter: ContainerIterator<T>): boolean;
  26. /**
  27. * @description Pointers to element.
  28. * @returns The value of the pointer's element.
  29. * @example
  30. * const val = container.begin().pointer;
  31. */
  32. abstract get pointer(): T;
  33. /**
  34. * @description Set pointer's value (some containers are unavailable).
  35. * @param newValue - The new value you want to set.
  36. * @example
  37. * (<LinkList<number>>container).begin().pointer = 1;
  38. */
  39. abstract set pointer(newValue: T);
  40. /**
  41. * @description Move `this` iterator to pre.
  42. * @returns The iterator's self.
  43. * @example
  44. * const iter = container.find(1); // container = [0, 1]
  45. * const pre = iter.pre();
  46. * console.log(pre === iter); // true
  47. * console.log(pre.equals(iter)); // true
  48. * console.log(pre.pointer, iter.pointer); // 0, 0
  49. */
  50. abstract pre(): this;
  51. /**
  52. * @description Move `this` iterator to next.
  53. * @returns The iterator's self.
  54. * @example
  55. * const iter = container.find(1); // container = [1, 2]
  56. * const next = iter.next();
  57. * console.log(next === iter); // true
  58. * console.log(next.equals(iter)); // true
  59. * console.log(next.pointer, iter.pointer); // 2, 2
  60. */
  61. abstract next(): this;
  62. /**
  63. * @description Get a copy of itself.
  64. * @returns The copy of self.
  65. * @example
  66. * const iter = container.find(1); // container = [1, 2]
  67. * const next = iter.copy().next();
  68. * console.log(next === iter); // false
  69. * console.log(next.equals(iter)); // false
  70. * console.log(next.pointer, iter.pointer); // 2, 1
  71. */
  72. abstract copy(): ContainerIterator<T>;
  73. abstract isAccessible(): boolean;
  74. }
  75. declare abstract class Base {
  76. /**
  77. * @returns The size of the container.
  78. * @example
  79. * const container = new Vector([1, 2]);
  80. * console.log(container.length); // 2
  81. */
  82. get length(): number;
  83. /**
  84. * @returns The size of the container.
  85. * @example
  86. * const container = new Vector([1, 2]);
  87. * console.log(container.size()); // 2
  88. */
  89. size(): number;
  90. /**
  91. * @returns Whether the container is empty.
  92. * @example
  93. * container.clear();
  94. * console.log(container.empty()); // true
  95. */
  96. empty(): boolean;
  97. /**
  98. * @description Clear the container.
  99. * @example
  100. * container.clear();
  101. * console.log(container.empty()); // true
  102. */
  103. abstract clear(): void;
  104. }
  105. declare abstract class Container<T> extends Base {
  106. /**
  107. * @returns Iterator pointing to the beginning element.
  108. * @example
  109. * const begin = container.begin();
  110. * const end = container.end();
  111. * for (const it = begin; !it.equals(end); it.next()) {
  112. * doSomething(it.pointer);
  113. * }
  114. */
  115. abstract begin(): ContainerIterator<T>;
  116. /**
  117. * @returns Iterator pointing to the super end like c++.
  118. * @example
  119. * const begin = container.begin();
  120. * const end = container.end();
  121. * for (const it = begin; !it.equals(end); it.next()) {
  122. * doSomething(it.pointer);
  123. * }
  124. */
  125. abstract end(): ContainerIterator<T>;
  126. /**
  127. * @returns Iterator pointing to the end element.
  128. * @example
  129. * const rBegin = container.rBegin();
  130. * const rEnd = container.rEnd();
  131. * for (const it = rBegin; !it.equals(rEnd); it.next()) {
  132. * doSomething(it.pointer);
  133. * }
  134. */
  135. abstract rBegin(): ContainerIterator<T>;
  136. /**
  137. * @returns Iterator pointing to the super begin like c++.
  138. * @example
  139. * const rBegin = container.rBegin();
  140. * const rEnd = container.rEnd();
  141. * for (const it = rBegin; !it.equals(rEnd); it.next()) {
  142. * doSomething(it.pointer);
  143. * }
  144. */
  145. abstract rEnd(): ContainerIterator<T>;
  146. /**
  147. * @returns The first element of the container.
  148. */
  149. abstract front(): T | undefined;
  150. /**
  151. * @returns The last element of the container.
  152. */
  153. abstract back(): T | undefined;
  154. /**
  155. * @param element - The element you want to find.
  156. * @returns An iterator pointing to the element if found, or super end if not found.
  157. * @example
  158. * container.find(1).equals(container.end());
  159. */
  160. abstract find(element: T): ContainerIterator<T>;
  161. /**
  162. * @description Iterate over all elements in the container.
  163. * @param callback - Callback function like Array.forEach.
  164. * @example
  165. * container.forEach((element, index) => console.log(element, index));
  166. */
  167. abstract forEach(callback: (element: T, index: number, container: Container<T>) => void): void;
  168. /**
  169. * @description Gets the value of the element at the specified position.
  170. * @example
  171. * const val = container.getElementByPos(-1); // throw a RangeError
  172. */
  173. abstract getElementByPos(pos: number): T;
  174. /**
  175. * @description Removes the element at the specified position.
  176. * @param pos - The element's position you want to remove.
  177. * @returns The container length after erasing.
  178. * @example
  179. * container.eraseElementByPos(-1); // throw a RangeError
  180. */
  181. abstract eraseElementByPos(pos: number): number;
  182. /**
  183. * @description Removes element by iterator and move `iter` to next.
  184. * @param iter - The iterator you want to erase.
  185. * @returns The next iterator.
  186. * @example
  187. * container.eraseElementByIterator(container.begin());
  188. * container.eraseElementByIterator(container.end()); // throw a RangeError
  189. */
  190. abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
  191. /**
  192. * @description Using for `for...of` syntax like Array.
  193. * @example
  194. * for (const element of container) {
  195. * console.log(element);
  196. * }
  197. */
  198. abstract [Symbol.iterator](): Generator<T, void>;
  199. }
  200. /**
  201. * @description The initial data type passed in when initializing the container.
  202. */
  203. type initContainer<T> = {
  204. size?: number | (() => number);
  205. length?: number;
  206. forEach: (callback: (el: T) => void) => void;
  207. };
  208. declare abstract class TreeIterator<K, V> extends ContainerIterator<K | [
  209. K,
  210. V
  211. ]> {
  212. abstract readonly container: TreeContainer<K, V>;
  213. /**
  214. * @description Get the sequential index of the iterator in the tree container.<br/>
  215. * <strong>Note:</strong>
  216. * This function only takes effect when the specified tree container `enableIndex = true`.
  217. * @returns The index subscript of the node in the tree.
  218. * @example
  219. * const st = new OrderedSet([1, 2, 3], true);
  220. * console.log(st.begin().next().index); // 1
  221. */
  222. get index(): number;
  223. isAccessible(): boolean;
  224. // @ts-ignore
  225. pre(): this;
  226. // @ts-ignore
  227. next(): this;
  228. }
  229. declare const enum TreeNodeColor {
  230. RED = 1,
  231. BLACK = 0
  232. }
  233. declare class TreeNode<K, V> {
  234. _color: TreeNodeColor;
  235. _key: K | undefined;
  236. _value: V | undefined;
  237. _left: TreeNode<K, V> | undefined;
  238. _right: TreeNode<K, V> | undefined;
  239. _parent: TreeNode<K, V> | undefined;
  240. constructor(key?: K, value?: V, color?: TreeNodeColor);
  241. /**
  242. * @description Get the pre node.
  243. * @returns TreeNode about the pre node.
  244. */
  245. _pre(): TreeNode<K, V>;
  246. /**
  247. * @description Get the next node.
  248. * @returns TreeNode about the next node.
  249. */
  250. _next(): TreeNode<K, V>;
  251. /**
  252. * @description Rotate left.
  253. * @returns TreeNode about moved to original position after rotation.
  254. */
  255. _rotateLeft(): TreeNode<K, V>;
  256. /**
  257. * @description Rotate right.
  258. * @returns TreeNode about moved to original position after rotation.
  259. */
  260. _rotateRight(): TreeNode<K, V>;
  261. }
  262. declare abstract class TreeContainer<K, V> extends Container<K | [
  263. K,
  264. V
  265. ]> {
  266. enableIndex: boolean;
  267. protected _inOrderTraversal(): TreeNode<K, V>[];
  268. protected _inOrderTraversal(pos: number): TreeNode<K, V>;
  269. protected _inOrderTraversal(callback: (node: TreeNode<K, V>, index: number, map: this) => void): TreeNode<K, V>;
  270. clear(): void;
  271. /**
  272. * @description Update node's key by iterator.
  273. * @param iter - The iterator you want to change.
  274. * @param key - The key you want to update.
  275. * @returns Whether the modification is successful.
  276. * @example
  277. * const st = new orderedSet([1, 2, 5]);
  278. * const iter = st.find(2);
  279. * st.updateKeyByIterator(iter, 3); // then st will become [1, 3, 5]
  280. */
  281. updateKeyByIterator(iter: TreeIterator<K, V>, key: K): boolean;
  282. eraseElementByPos(pos: number): number;
  283. /**
  284. * @description Remove the element of the specified key.
  285. * @param key - The key you want to remove.
  286. * @returns Whether erase successfully.
  287. */
  288. eraseElementByKey(key: K): boolean;
  289. eraseElementByIterator(iter: TreeIterator<K, V>): TreeIterator<K, V>;
  290. /**
  291. * @description Get the height of the tree.
  292. * @returns Number about the height of the RB-tree.
  293. */
  294. getHeight(): number;
  295. /**
  296. * @param key - The given key you want to compare.
  297. * @returns An iterator to the first element less than the given key.
  298. */
  299. abstract reverseUpperBound(key: K): TreeIterator<K, V>;
  300. /**
  301. * @description Union the other tree to self.
  302. * @param other - The other tree container you want to merge.
  303. * @returns The size of the tree after union.
  304. */
  305. abstract union(other: TreeContainer<K, V>): number;
  306. /**
  307. * @param key - The given key you want to compare.
  308. * @returns An iterator to the first element not greater than the given key.
  309. */
  310. abstract reverseLowerBound(key: K): TreeIterator<K, V>;
  311. /**
  312. * @param key - The given key you want to compare.
  313. * @returns An iterator to the first element not less than the given key.
  314. */
  315. abstract lowerBound(key: K): TreeIterator<K, V>;
  316. /**
  317. * @param key - The given key you want to compare.
  318. * @returns An iterator to the first element greater than the given key.
  319. */
  320. abstract upperBound(key: K): TreeIterator<K, V>;
  321. }
  322. declare class OrderedMapIterator<K, V> extends TreeIterator<K, V> {
  323. container: OrderedMap<K, V>;
  324. constructor(node: TreeNode<K, V>, header: TreeNode<K, V>, container: OrderedMap<K, V>, iteratorType?: IteratorType);
  325. get pointer(): [
  326. K,
  327. V
  328. ];
  329. copy(): OrderedMapIterator<K, V>;
  330. // @ts-ignore
  331. equals(iter: OrderedMapIterator<K, V>): boolean;
  332. }
  333. declare class OrderedMap<K, V> extends TreeContainer<K, V> {
  334. /**
  335. * @param container - The initialization container.
  336. * @param cmp - The compare function.
  337. * @param enableIndex - Whether to enable iterator indexing function.
  338. * @example
  339. * new OrderedMap();
  340. * new OrderedMap([[0, 1], [2, 1]]);
  341. * new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y);
  342. * new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y, true);
  343. */
  344. constructor(container?: initContainer<[
  345. K,
  346. V
  347. ]>, cmp?: (x: K, y: K) => number, enableIndex?: boolean);
  348. begin(): OrderedMapIterator<K, V>;
  349. end(): OrderedMapIterator<K, V>;
  350. rBegin(): OrderedMapIterator<K, V>;
  351. rEnd(): OrderedMapIterator<K, V>;
  352. front(): [
  353. K,
  354. V
  355. ] | undefined;
  356. back(): [
  357. K,
  358. V
  359. ] | undefined;
  360. lowerBound(key: K): OrderedMapIterator<K, V>;
  361. upperBound(key: K): OrderedMapIterator<K, V>;
  362. reverseLowerBound(key: K): OrderedMapIterator<K, V>;
  363. reverseUpperBound(key: K): OrderedMapIterator<K, V>;
  364. forEach(callback: (element: [
  365. K,
  366. V
  367. ], index: number, map: OrderedMap<K, V>) => void): void;
  368. /**
  369. * @description Insert a key-value pair or set value by the given key.
  370. * @param key - The key want to insert.
  371. * @param value - The value want to set.
  372. * @param hint - You can give an iterator hint to improve insertion efficiency.
  373. * @return The size of container after setting.
  374. * @example
  375. * const mp = new OrderedMap([[2, 0], [4, 0], [5, 0]]);
  376. * const iter = mp.begin();
  377. * mp.setElement(1, 0);
  378. * mp.setElement(3, 0, iter); // give a hint will be faster.
  379. */
  380. setElement(key: K, value: V, hint?: OrderedMapIterator<K, V>): number;
  381. getElementByPos(pos: number): [
  382. K,
  383. V
  384. ];
  385. find(key: K): OrderedMapIterator<K, V>;
  386. /**
  387. * @description Get the value of the element of the specified key.
  388. * @param key - The specified key you want to get.
  389. * @example
  390. * const val = container.getElementByKey(1);
  391. */
  392. getElementByKey(key: K): V | undefined;
  393. union(other: OrderedMap<K, V>): number;
  394. [Symbol.iterator](): Generator<[
  395. K,
  396. V
  397. ], void, unknown>;
  398. // @ts-ignore
  399. eraseElementByIterator(iter: OrderedMapIterator<K, V>): OrderedMapIterator<K, V>;
  400. }
  401. export { OrderedMap };
  402. export type { OrderedMapIterator, IteratorType, Container, ContainerIterator, TreeContainer };