035e27c0e20d8de30cedb65512b941abcbc3a4deb850afb2b195ed3e7d7a801964ad3abb839a46c9cf6f643841a52ac41928043f98e9db9c98e2e7dddb63bb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @description The iterator type including `NORMAL` and `REVERSE`.
  3. */
  4. export declare const enum IteratorType {
  5. NORMAL = 0,
  6. REVERSE = 1
  7. }
  8. export 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. }
  74. export declare abstract class Base {
  75. /**
  76. * @returns The size of the container.
  77. * @example
  78. * const container = new Vector([1, 2]);
  79. * console.log(container.length); // 2
  80. */
  81. get length(): number;
  82. /**
  83. * @returns The size of the container.
  84. * @example
  85. * const container = new Vector([1, 2]);
  86. * console.log(container.size()); // 2
  87. */
  88. size(): number;
  89. /**
  90. * @returns Whether the container is empty.
  91. * @example
  92. * container.clear();
  93. * console.log(container.empty()); // true
  94. */
  95. empty(): boolean;
  96. /**
  97. * @description Clear the container.
  98. * @example
  99. * container.clear();
  100. * console.log(container.empty()); // true
  101. */
  102. abstract clear(): void;
  103. }
  104. export declare abstract class Container<T> extends Base {
  105. /**
  106. * @returns Iterator pointing to the beginning element.
  107. * @example
  108. * const begin = container.begin();
  109. * const end = container.end();
  110. * for (const it = begin; !it.equals(end); it.next()) {
  111. * doSomething(it.pointer);
  112. * }
  113. */
  114. abstract begin(): ContainerIterator<T>;
  115. /**
  116. * @returns Iterator pointing to the super end like c++.
  117. * @example
  118. * const begin = container.begin();
  119. * const end = container.end();
  120. * for (const it = begin; !it.equals(end); it.next()) {
  121. * doSomething(it.pointer);
  122. * }
  123. */
  124. abstract end(): ContainerIterator<T>;
  125. /**
  126. * @returns Iterator pointing to the end element.
  127. * @example
  128. * const rBegin = container.rBegin();
  129. * const rEnd = container.rEnd();
  130. * for (const it = rBegin; !it.equals(rEnd); it.next()) {
  131. * doSomething(it.pointer);
  132. * }
  133. */
  134. abstract rBegin(): ContainerIterator<T>;
  135. /**
  136. * @returns Iterator pointing to the super begin like c++.
  137. * @example
  138. * const rBegin = container.rBegin();
  139. * const rEnd = container.rEnd();
  140. * for (const it = rBegin; !it.equals(rEnd); it.next()) {
  141. * doSomething(it.pointer);
  142. * }
  143. */
  144. abstract rEnd(): ContainerIterator<T>;
  145. /**
  146. * @returns The first element of the container.
  147. */
  148. abstract front(): T | undefined;
  149. /**
  150. * @returns The last element of the container.
  151. */
  152. abstract back(): T | undefined;
  153. /**
  154. * @param element - The element you want to find.
  155. * @returns An iterator pointing to the element if found, or super end if not found.
  156. * @example
  157. * container.find(1).equals(container.end());
  158. */
  159. abstract find(element: T): ContainerIterator<T>;
  160. /**
  161. * @description Iterate over all elements in the container.
  162. * @param callback - Callback function like Array.forEach.
  163. * @example
  164. * container.forEach((element, index) => console.log(element, index));
  165. */
  166. abstract forEach(callback: (element: T, index: number, container: Container<T>) => void): void;
  167. /**
  168. * @description Gets the value of the element at the specified position.
  169. * @example
  170. * const val = container.getElementByPos(-1); // throw a RangeError
  171. */
  172. abstract getElementByPos(pos: number): T;
  173. /**
  174. * @description Removes the element at the specified position.
  175. * @param pos - The element's position you want to remove.
  176. * @returns The container length after erasing.
  177. * @example
  178. * container.eraseElementByPos(-1); // throw a RangeError
  179. */
  180. abstract eraseElementByPos(pos: number): number;
  181. /**
  182. * @description Removes element by iterator and move `iter` to next.
  183. * @param iter - The iterator you want to erase.
  184. * @returns The next iterator.
  185. * @example
  186. * container.eraseElementByIterator(container.begin());
  187. * container.eraseElementByIterator(container.end()); // throw a RangeError
  188. */
  189. abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
  190. /**
  191. * @description Using for `for...of` syntax like Array.
  192. * @example
  193. * for (const element of container) {
  194. * console.log(element);
  195. * }
  196. */
  197. abstract [Symbol.iterator](): Generator<T, void>;
  198. }
  199. /**
  200. * @description The initial data type passed in when initializing the container.
  201. */
  202. export declare type initContainer<T> = {
  203. size?: number | (() => number);
  204. length?: number;
  205. forEach: (callback: (el: T) => void) => void;
  206. };