import { DeleteCallback, CloneCallback, ReturnValue, ReturnValueRaw } from "../types"; import { Basic, Container, Options } from "./types"; /** * linked list element * @sealed */ export declare class ListElement { data?: T | undefined; /** * which list the element is in */ private list_?; /** * Prohibit third-party instantiation */ private constructor(); /** * return next element */ next(): ListElement | undefined; /** * return previous element */ prev(): ListElement | undefined; } /** * Doubly linked list. Refer to the golang standard library implementation * @sealed */ export declare class List extends Basic implements Container { /** * sentinel list element, only root, root.prev, and root.next are used */ private root_; /** * record linked list length */ private length_; /** * returns the length of the linked list * @override */ get length(): number; /** * returns the capacity of the linked list * @override */ get capacity(): number; constructor(opts?: Options); /** * returns the first element of list l or undefined if the list is empty. */ front(): ListElement | undefined; /** * returns the last element of list l or nil if the list is empty. */ back(): ListElement | undefined; /** * insert inserts e after at, increments length */ private _insert; private _insertValue; /** * remove removes e from its list, decrements length */ private _remove; /** * move moves e to next to at and returns e. */ private _move; /** * clear the list * * @remarks * It will cause a bug if a call to remove is passed after calling clear and an element before clear is passed in. like this * * ``` * const l = new List() * const ele = l.pushBack(1) * l.cear() * console.log((l.remove(ele)) // output true * console.log(l.length) // output -1 * ``` * * This bug is generated because the element records which linked list it is in and the clear function does not clear the mark . * * So remove(ele) mistakenly deletes the expired element. To solve this problem, it is necessary to traverse the linked list once in 'clear' to clear all tags, but this will damage efficiency. However, this bug is not common and the caller can be avoided, so I'm not going to fix it. * * You can also avoid this bug by passing in an empty implementation of the callback function, because calling the callback for all elements also requires traversing the list once, so in this case the code also empties the token before the callback. * * ``` * const l = new List() * const ele = l.pushBack(1) * l.cear(()=>{}) * console.log((l.remove(ele)) // output false * console.log(l.length) // output 0 * ``` * * @param callback call the callback on the removed element */ clear(callback?: DeleteCallback): void; /** * remove e from list if e is an element of list. * @param e element to remove * @param callback call the callback on the removed element */ remove(e: ListElement, callback?: DeleteCallback): boolean; /** * If the list is not empty delete the element at the back * @param callback call the callback on the removed element * @returns deleted data */ popBack(callback?: DeleteCallback): ReturnValue; /** * If the list is not empty delete the element at the back * @param callback call the callback on the removed element * @returns deleted data */ popBackRaw(callback?: DeleteCallback): ReturnValueRaw; /** * If the list is not empty delete the element at the front * @param callback call the callback on the removed element * @returns deleted data */ popFront(callback?: DeleteCallback): ReturnValue; /** * If the list is not empty delete the element at the front * @param callback call the callback on the removed element * @returns deleted data */ popFrontRaw(callback?: DeleteCallback): ReturnValueRaw; /** * inserts a new element e with value v at the back of list and returns e. */ pushBack(v: T): ListElement; /** * inserts a new element e with value v at the front of list and returns e. */ pushFront(v: T): ListElement; /** * inserts a new element e with value v immediately before mark and returns e. */ insertBefore(v: T, mark: ListElement): ListElement | undefined; /** * inserts a new element e with value v immediately after mark and returns e. */ insertAfter(v: T, mark: ListElement): ListElement | undefined; /** * moves element e to the front of list. */ moveToFront(e: ListElement): boolean; /** * moves element e to the back of list. */ moveToBack(e: ListElement): boolean; /** * moves element e to its new position before mark. */ moveBefore(e: ListElement, mark: ListElement): boolean; /** * moves element e to its new position after mark. */ moveAfter(e: ListElement, mark: ListElement): boolean; /** * return js iterator * @param reverse If true, returns an iterator to traverse in reverse * @override */ iterator(reverse?: boolean): Iterator; /** * inserts a copy of another container at the back of list. */ pushBackList(vals: Iterable, callback?: CloneCallback): void; /** * inserts a copy of another container at the front of list l. */ pushFrontList(vals: Iterable, callback?: CloneCallback): void; /** * Create a full copy of the container * @param callback How to create a duplicate copy of an element */ clone(callback?: CloneCallback): List; }