doubly-linked-list-typed
Version:
Doubly Linked List
278 lines (277 loc) • 17 kB
TypeScript
import { ElementCallback, LinearBaseOptions, ReduceLinearCallback } from '../../types';
import { IterableElementBase } from './iterable-element-base';
export declare class LinkedListNode<E = any> {
constructor(value: E);
protected _value: E;
get value(): E;
set value(value: E);
protected _next: LinkedListNode<E> | undefined;
get next(): LinkedListNode<E> | undefined;
set next(value: LinkedListNode<E> | undefined);
}
export declare abstract class LinearBase<E, R = any, NODE extends LinkedListNode<E> = LinkedListNode<E>> extends IterableElementBase<E, R> {
/**
* The constructor initializes the LinearBase class with optional options, setting the maximum length
* if provided.
* @param [options] - The `options` parameter is an optional object that can be passed to the
* constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
* object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
*/
protected constructor(options?: LinearBaseOptions<E, R>);
abstract get length(): number;
protected _maxLen: number;
get maxLen(): number;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function indexOf searches for a specified element starting from a given index in an array-like
* object and returns the index of the first occurrence, or -1 if not found.
* @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
* element that you want to find within the array. The function will search for this element starting
* from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
* within the
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
* index at which to start searching for the `searchElement` within the array. If provided, the
* search will begin at this index and continue to the end of the array. If `fromIndex` is not
* specified, the default
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
*/
indexOf(searchElement: E, fromIndex?: number): number;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
* element in an array.
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
* last index of within the array. The `lastIndexOf` method will search the array starting from the
* `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
* of the
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
* index at which to start searching for the `searchElement` in the array. By default, it starts
* searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
* provided
* @returns The last index of the `searchElement` in the array is being returned. If the
* `searchElement` is not found in the array, -1 is returned.
*/
lastIndexOf(searchElement: E, fromIndex?: number): number;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `findIndex` function iterates over an array and returns the index of the first element that
* satisfies the provided predicate function.
* @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
* that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
* value indicating whether the current element satisfies the condition being checked for.
* @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
* parameter that specifies the value to use as `this` when executing the `predicate` function. If
* provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
* @returns The `findIndex` method is returning the index of the first element in the array that
* satisfies the provided predicate function. If no such element is found, it returns -1.
*/
findIndex(predicate: ElementCallback<E, R, boolean>, thisArg?: any): number;
concat(...items: this[]): this;
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*
* The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
* function.
* @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
* two elements `a` and `b` as input and returns a number indicating their relative order. If the
* returned value is negative, `a` comes before `b`. If the returned value is positive, `
* @returns The `sort` method is returning the instance of the object on which it is called (this),
* after sorting the elements based on the provided comparison function (compareFn).
*/
sort(compareFn?: (a: E, b: E) => number): this;
/**
* Time Complexity: O(n + m)
* Space Complexity: O(m)
*
* The `splice` function in TypeScript removes elements from an array and optionally inserts new
* elements at the specified index.
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
* to start modifying the array. If `start` is a negative number, it will count from the end of the
* array.
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
* number of elements to remove from the array starting at the specified `start` index. If
* `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
* at the `start`
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
* will be inserted into the array at the specified `start` index. These elements can be of any type
* and you can pass multiple elements separated by commas. The `splice` method will insert these
* items into the array at the
* @returns The `splice` method returns a list of elements that were removed from the original list
* during the operation.
*/
splice(start: number, deleteCount?: number, ...items: E[]): this;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `join` function in TypeScript returns a string by joining the elements of an array with a
* specified separator.
* @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
* or characters that will be used to separate each element when joining them into a single string.
* By default, the separator is set to a comma (`,`), but you can provide a different separator if
* needed.
* @returns The `join` method is being returned, which takes an optional `separator` parameter
* (defaulting to a comma) and returns a string created by joining all elements of the array after
* converting it to an array.
*/
join(separator?: string): string;
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The function `toReversedArray` takes an array and returns a new array with its elements in reverse
* order.
* @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
* order.
*/
toReversedArray(): E[];
reduceRight(callbackfn: ReduceLinearCallback<E>): E;
reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
/**
* Time Complexity: O(m)
* Space Complexity: O(m)
*
* The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
* the original instance based on the specified start and end indices.
* @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
* which to begin extracting elements from an array-like object. If no `start` parameter is provided,
* the default value is 0, meaning the extraction will start from the beginning of the array.
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
* end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
* array (i.e., `this.length`).
* @returns The `slice` method is returning a new instance of the object with elements sliced from
* the specified start index (default is 0) to the specified end index (default is the length of the
* object).
*/
slice(start?: number, end?: number): this;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `fill` function in TypeScript fills a specified range in an array-like object with a given
* value.
* @param {E} value - The `value` parameter in the `fill` method represents the element that will be
* used to fill the specified range in the array.
* @param [start=0] - The `start` parameter specifies the index at which to start filling the array
* with the specified value. If not provided, it defaults to 0, indicating the beginning of the
* array.
* @param end - The `end` parameter in the `fill` function represents the index at which the filling
* of values should stop. It specifies the end of the range within the array where the `value` should
* be filled.
* @returns The `fill` method is returning the modified object (`this`) after filling the specified
* range with the provided value.
*/
fill(value: E, start?: number, end?: number): this;
abstract setAt(index: number, value: E): boolean;
abstract clone(): this;
abstract reverse(): this;
abstract push(elementOrNode: E | NODE): boolean;
abstract pushMany(elements: Iterable<E> | Iterable<R> | Iterable<NODE>): boolean[];
abstract delete(elementOrNode: E | NODE | undefined): boolean;
abstract at(index: number): E | undefined;
abstract deleteAt(pos: number): E | undefined;
abstract addAt(index: number, newElementOrNode: E | NODE): boolean;
protected abstract _createInstance(options?: LinearBaseOptions<E, R>): this;
protected abstract _getReverseIterator(...args: any[]): IterableIterator<E>;
}
export declare abstract class LinearLinkedBase<E, R = any, NODE extends LinkedListNode<E> = LinkedListNode<E>> extends LinearBase<E, R, NODE> {
/**
* The constructor initializes the LinearBase class with optional options, setting the maximum length
* if provided and valid.
* @param [options] - The `options` parameter is an optional object that can be passed to the
* constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
* `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
*/
protected constructor(options?: LinearBaseOptions<E, R>);
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function overrides the indexOf method to improve performance by searching for an element in a
* custom array implementation starting from a specified index.
* @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
* within the array. The `indexOf` method will return the index of the first occurrence of this
* element within the array.
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
* index in the array at which to start the search for the `searchElement`. If provided, the search
* will begin at the specified index and continue to the end of the array. If not provided, the
* search will start at index
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
*/
indexOf(searchElement: E, fromIndex?: number): number;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function overrides the lastIndexOf method in TypeScript to improve performance by searching
* for an element in reverse order starting from a specified index.
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find
* within the array. The `lastIndexOf` method searches the array for this element starting from the
* end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
* occurrence of the element
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
* index at which to start searching for the `searchElement` in the array. If provided, the search
* will begin at this index and move towards the beginning of the array. If not provided, the search
* will start at the
* @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
* from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
* reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
* occurrence of the `searchElement` if found, or -1 if not found.
*/
lastIndexOf(searchElement: E, fromIndex?: number): number;
concat(...items: LinearBase<E, R>[]): this;
/**
* Time Complexity: O(m)
* Space Complexity: O(m)
*
* The `slice` method is overridden to improve performance by creating a new instance and iterating
* through the array to extract a subset based on the specified start and end indices.
* @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
* which to begin extracting elements from the array. If no `start` parameter is provided, the
* default value is 0, indicating that extraction should start from the beginning of the array.
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
* end the slicing of the array. If not provided, it defaults to the length of the array.
* @returns The `slice` method is returning a new instance of the array implementation with elements
* sliced from the original array based on the `start` and `end` parameters.
*/
slice(start?: number, end?: number): this;
/**
* Time Complexity: O(n + m)
* Space Complexity: O(m)
*
* The function overrides the splice method to handle deletion and insertion of elements in a data
* structure while returning the removed elements.
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
* to start modifying the array.
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
* number of elements to remove from the array starting at the specified `start` index. If
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
* elements can still be inserted at
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
* will be inserted into the array at the specified `start` index. These elements can be of any type
* and there can be multiple elements passed as arguments to be inserted into the array.
* @returns The `splice` method is returning a new instance of the data structure that was modified
* by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
* elements provided in the `items` array.
*/
splice(start: number, deleteCount?: number, ...items: E[]): this;
reduceRight(callbackfn: ReduceLinearCallback<E>): E;
reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
abstract delete(elementOrNode: E | NODE | undefined): boolean;
abstract addBefore(existingElementOrNode: E | NODE, newElementOrNode: E | NODE): boolean;
abstract addAfter(existingElementOrNode: E | NODE, newElementOrNode: E | NODE): boolean;
abstract getNodeAt(index: number): NODE | undefined;
protected abstract _getNodeIterator(...args: any[]): IterableIterator<NODE>;
protected abstract _getPrevNode(node: NODE): NODE | undefined;
}