/** * @description The iterator type including `NORMAL` and `REVERSE`. */ declare const enum IteratorType { NORMAL = 0, REVERSE = 1 } declare abstract class ContainerIterator { /** * @description The container pointed to by the iterator. */ abstract readonly container: Container; /** * @description Iterator's type. * @example * console.log(container.end().iteratorType === IteratorType.NORMAL); // true */ readonly iteratorType: IteratorType; /** * @param iter - The other iterator you want to compare. * @returns Whether this equals to obj. * @example * container.find(1).equals(container.end()); */ equals(iter: ContainerIterator): boolean; /** * @description Pointers to element. * @returns The value of the pointer's element. * @example * const val = container.begin().pointer; */ abstract get pointer(): T; /** * @description Set pointer's value (some containers are unavailable). * @param newValue - The new value you want to set. * @example * (>container).begin().pointer = 1; */ abstract set pointer(newValue: T); /** * @description Move `this` iterator to pre. * @returns The iterator's self. * @example * const iter = container.find(1); // container = [0, 1] * const pre = iter.pre(); * console.log(pre === iter); // true * console.log(pre.equals(iter)); // true * console.log(pre.pointer, iter.pointer); // 0, 0 */ abstract pre(): this; /** * @description Move `this` iterator to next. * @returns The iterator's self. * @example * const iter = container.find(1); // container = [1, 2] * const next = iter.next(); * console.log(next === iter); // true * console.log(next.equals(iter)); // true * console.log(next.pointer, iter.pointer); // 2, 2 */ abstract next(): this; /** * @description Get a copy of itself. * @returns The copy of self. * @example * const iter = container.find(1); // container = [1, 2] * const next = iter.copy().next(); * console.log(next === iter); // false * console.log(next.equals(iter)); // false * console.log(next.pointer, iter.pointer); // 2, 1 */ abstract copy(): ContainerIterator; abstract isAccessible(): boolean; } declare abstract class Base { /** * @returns The size of the container. * @example * const container = new Vector([1, 2]); * console.log(container.length); // 2 */ get length(): number; /** * @returns The size of the container. * @example * const container = new Vector([1, 2]); * console.log(container.size()); // 2 */ size(): number; /** * @returns Whether the container is empty. * @example * container.clear(); * console.log(container.empty()); // true */ empty(): boolean; /** * @description Clear the container. * @example * container.clear(); * console.log(container.empty()); // true */ abstract clear(): void; } declare abstract class Container extends Base { /** * @returns Iterator pointing to the beginning element. * @example * const begin = container.begin(); * const end = container.end(); * for (const it = begin; !it.equals(end); it.next()) { * doSomething(it.pointer); * } */ abstract begin(): ContainerIterator; /** * @returns Iterator pointing to the super end like c++. * @example * const begin = container.begin(); * const end = container.end(); * for (const it = begin; !it.equals(end); it.next()) { * doSomething(it.pointer); * } */ abstract end(): ContainerIterator; /** * @returns Iterator pointing to the end element. * @example * const rBegin = container.rBegin(); * const rEnd = container.rEnd(); * for (const it = rBegin; !it.equals(rEnd); it.next()) { * doSomething(it.pointer); * } */ abstract rBegin(): ContainerIterator; /** * @returns Iterator pointing to the super begin like c++. * @example * const rBegin = container.rBegin(); * const rEnd = container.rEnd(); * for (const it = rBegin; !it.equals(rEnd); it.next()) { * doSomething(it.pointer); * } */ abstract rEnd(): ContainerIterator; /** * @returns The first element of the container. */ abstract front(): T | undefined; /** * @returns The last element of the container. */ abstract back(): T | undefined; /** * @param element - The element you want to find. * @returns An iterator pointing to the element if found, or super end if not found. * @example * container.find(1).equals(container.end()); */ abstract find(element: T): ContainerIterator; /** * @description Iterate over all elements in the container. * @param callback - Callback function like Array.forEach. * @example * container.forEach((element, index) => console.log(element, index)); */ abstract forEach(callback: (element: T, index: number, container: Container) => void): void; /** * @description Gets the value of the element at the specified position. * @example * const val = container.getElementByPos(-1); // throw a RangeError */ abstract getElementByPos(pos: number): T; /** * @description Removes the element at the specified position. * @param pos - The element's position you want to remove. * @returns The container length after erasing. * @example * container.eraseElementByPos(-1); // throw a RangeError */ abstract eraseElementByPos(pos: number): number; /** * @description Removes element by iterator and move `iter` to next. * @param iter - The iterator you want to erase. * @returns The next iterator. * @example * container.eraseElementByIterator(container.begin()); * container.eraseElementByIterator(container.end()); // throw a RangeError */ abstract eraseElementByIterator(iter: ContainerIterator): ContainerIterator; /** * @description Using for `for...of` syntax like Array. * @example * for (const element of container) { * console.log(element); * } */ abstract [Symbol.iterator](): Generator; } /** * @description The initial data type passed in when initializing the container. */ type initContainer = { size?: number | (() => number); length?: number; forEach: (callback: (el: T) => void) => void; }; type HashLinkNode = { _key: K; _value: V; _pre: HashLinkNode; _next: HashLinkNode; }; declare abstract class HashContainerIterator extends ContainerIterator { abstract readonly container: HashContainer; isAccessible(): boolean; // @ts-ignore pre(): this; // @ts-ignore next(): this; } declare abstract class HashContainer extends Container { /** * @description Unique symbol used to tag object. */ readonly HASH_TAG: symbol; clear(): void; /** * @description Remove the element of the specified key. * @param key - The key you want to remove. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.
* If a `undefined` value is passed in, the type will be automatically judged. * @returns Whether erase successfully. */ eraseElementByKey(key: K, isObject?: boolean): boolean; eraseElementByIterator(iter: HashContainerIterator): HashContainerIterator; eraseElementByPos(pos: number): number; } declare class HashSetIterator extends HashContainerIterator { readonly container: HashSet; constructor(node: HashLinkNode, header: HashLinkNode, container: HashSet, iteratorType?: IteratorType); get pointer(): K; copy(): HashSetIterator; // @ts-ignore equals(iter: HashSetIterator): boolean; } declare class HashSet extends HashContainer { constructor(container?: initContainer); begin(): HashSetIterator; end(): HashSetIterator; rBegin(): HashSetIterator; rEnd(): HashSetIterator; front(): K | undefined; back(): K | undefined; /** * @description Insert element to set. * @param key - The key want to insert. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.
* If a `undefined` value is passed in, the type will be automatically judged. * @returns The size of container after inserting. */ insert(key: K, isObject?: boolean): number; getElementByPos(pos: number): K; /** * @description Check key if exist in container. * @param key - The element you want to search. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.
* If a `undefined` value is passed in, the type will be automatically judged. * @returns An iterator pointing to the element if found, or super end if not found. */ find(key: K, isObject?: boolean): HashSetIterator; forEach(callback: (element: K, index: number, container: HashSet) => void): void; [Symbol.iterator](): Generator; } export { HashSet }; export type { HashSetIterator, IteratorType, Container, ContainerIterator, HashContainer };