Merge branch 'zrwusa:main' into main

This commit is contained in:
Olivier Azeau 2024-01-02 08:46:30 +01:00 committed by GitHub
commit ba6b666b44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 874 additions and 371 deletions

View file

@ -154,6 +154,105 @@ export abstract class IterableEntryBase<K = any, V = any> {
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `find` function iterates over the entries of a collection and returns the first value for
* which the callback function returns true.
* @param callbackfn - The callback function that will be called for each entry in the collection. It
* takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
* the collection. It should return a boolean value indicating whether the current entry matches the
* desired condition.
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
* be passed as the `this` value to the `callbackfn` function. If `thisArg
* @returns The method `find` returns the value of the first element in the iterable that satisfies
* the provided callback function. If no element satisfies the callback function, `undefined` is
* returned.
*/
find(callbackfn: EntryCallback<K, V, [K, V]>, thisArg?: any): [K, V] | undefined {
let index = 0;
for (const item of this) {
const [key, value] = item;
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
}
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function checks if a given key exists in a collection.
* @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
* the key that we want to check for existence in the data structure.
* @returns a boolean value. It returns true if the key is found in the collection, and false
* otherwise.
*/
has(key: K): boolean {
for (const item of this) {
const [itemKey] = item;
if (itemKey === key) return true;
}
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function checks if a given value exists in a collection.
* @param {V} value - The parameter "value" is the value that we want to check if it exists in the
* collection.
* @returns a boolean value, either true or false.
*/
hasValue(value: V): boolean {
for (const [, elementValue] of this) {
if (elementValue === value) return true;
}
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `get` function retrieves the value associated with a given key from a collection.
* @param {K} key - K (the type of the key) - This parameter represents the key that is being
* searched for in the collection.
* @returns The `get` method returns the value associated with the specified key if it exists in the
* collection, otherwise it returns `undefined`.
*/
get(key: K): V | undefined {
for (const item of this) {
const [itemKey, value] = item;
if (itemKey === key) return value;
}
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -180,13 +279,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
return accumulator;
}
hasValue(value: V): boolean {
for (const [, elementValue] of this) {
if (elementValue === value) return true;
}
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -198,7 +290,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
}
export abstract class IterableElementBase<V> {
export abstract class IterableElementBase<E> {
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -212,7 +304,7 @@ export abstract class IterableElementBase<V> {
* allows the function to accept any number of arguments as an array. In this case, the `args`
* parameter is used to pass any number of arguments to the `_getIterator` method.
*/
* [Symbol.iterator](...args: any[]): IterableIterator<V> {
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
yield* this._getIterator(...args);
}
@ -226,7 +318,7 @@ export abstract class IterableElementBase<V> {
*
* The function returns an iterator that yields all the values in the object.
*/
* values(): IterableIterator<V> {
* values(): IterableIterator<E> {
for (const item of this) {
yield item;
}
@ -250,10 +342,10 @@ export abstract class IterableElementBase<V> {
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
* the array satisfies the provided predicate function, and `false` otherwise.
*/
every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
let index = 0;
for (const item of this) {
if (!predicate.call(thisArg, item as V, index++, this)) {
if (!predicate.call(thisArg, item, index++, this)) {
return false;
}
}
@ -278,10 +370,10 @@ export abstract class IterableElementBase<V> {
* @returns a boolean value. It returns true if the predicate function returns true for any element
* in the collection, and false otherwise.
*/
some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
let index = 0;
for (const item of this) {
if (predicate.call(thisArg, item as V, index++, this)) {
if (predicate.call(thisArg, item, index++, this)) {
return true;
}
}
@ -292,6 +384,7 @@ export abstract class IterableElementBase<V> {
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -305,13 +398,65 @@ export abstract class IterableElementBase<V> {
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
* be passed as the `this` value to the `callbackfn` function. If `thisArg
*/
forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
let index = 0;
for (const item of this) {
callbackfn.call(thisArg, item as V, index++, this);
callbackfn.call(thisArg, item, index++, this);
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `find` function iterates over the elements of an array-like object and returns the first
* element that satisfies the provided callback function.
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
* the array. It takes three arguments: the current element being processed, the index of the current
* element, and the array itself. The function should return a boolean value indicating whether the
* current element matches the desired condition.
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
* be passed as the `this` value to the `callbackfn` function. If `thisArg
* @returns The `find` method returns the first element in the array that satisfies the provided
* callback function. If no element satisfies the callback function, `undefined` is returned.
*/
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
let index = 0;
for (const item of this) {
if (callbackfn.call(thisArg, item, index++, this)) return item;
}
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function checks if a given element exists in a collection.
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
* represents the element that we want to check for existence in the collection.
* @returns a boolean value. It returns true if the element is found in the collection, and false
* otherwise.
*/
has(element: E): boolean {
for (const ele of this) {
if (ele === element) return true;
}
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -329,11 +474,11 @@ export abstract class IterableElementBase<V> {
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
* all the elements in the array and applying the callback function to each element.
*/
reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U {
reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
let accumulator = initialValue;
let index = 0;
for (const item of this) {
accumulator = callbackfn(accumulator, item as V, index++, this);
accumulator = callbackfn(accumulator, item as E, index++, this);
}
return accumulator;
}
@ -346,5 +491,5 @@ export abstract class IterableElementBase<V> {
console.log([...this]);
}
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
}

View file

@ -172,14 +172,14 @@ export class BinaryTree<
}
/**
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
* is provided, it will be `undefined`.
* @returns a value of type N (node), or null, or undefined.
*/
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | null | undefined {
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | null | undefined {
if (keyOrNodeOrEntry === undefined) return;
let node: N | null | undefined;
@ -308,7 +308,7 @@ export class BinaryTree<
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
*/
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return false;
// If the tree is empty, directly set the new node as the root node
@ -768,21 +768,21 @@ export class BinaryTree<
* Space Complexity: O(log n).
*/
has<C extends BTNCallback<N, K>>(
override has<C extends BTNCallback<N, K>>(
identifier: K,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): boolean;
has<C extends BTNCallback<N, N>>(
override has<C extends BTNCallback<N, N>>(
identifier: N | null | undefined,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): boolean;
has<C extends BTNCallback<N>>(
override has<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null | undefined,
callback: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
@ -810,7 +810,7 @@ export class BinaryTree<
* be performed in a pre-order, in-order, or post-order manner.
* @returns a boolean value.
*/
has<C extends BTNCallback<N>>(
override has<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._defaultOneParamCallback as C,
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
@ -925,21 +925,21 @@ export class BinaryTree<
}
}
get<C extends BTNCallback<N, K>>(
override get<C extends BTNCallback<N, K>>(
identifier: K,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): V | undefined;
get<C extends BTNCallback<N, N>>(
override get<C extends BTNCallback<N, N>>(
identifier: N | null | undefined,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): V | undefined;
get<C extends BTNCallback<N>>(
override get<C extends BTNCallback<N>>(
identifier: ReturnType<C>,
callback: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
@ -968,7 +968,7 @@ export class BinaryTree<
* @returns The value of the node with the given identifier is being returned. If the node is not
* found, `undefined` is returned.
*/
get<C extends BTNCallback<N>>(
override get<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._defaultOneParamCallback as C,
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,

View file

@ -151,14 +151,14 @@ export class BST<
}
/**
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
* otherwise it returns undefined.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
* @returns a node of type N or undefined.
*/
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
let node: N | undefined;
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
return;
@ -240,7 +240,7 @@ export class BST<
* node was not added.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return false;
if (this.root === undefined) {

View file

@ -111,14 +111,14 @@ export class RedBlackTree<
}
/**
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
* is provided, it will be used when creating the new node. If no value is provided, the new node
* @returns a node of type N or undefined.
*/
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
let node: N | undefined;
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
@ -179,7 +179,7 @@ export class RedBlackTree<
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return false;
newNode.left = this.Sentinel;

View file

@ -88,7 +88,7 @@ export class TreeMultimap<
}
/**
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
* can be one of the following:
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
@ -98,7 +98,7 @@ export class TreeMultimap<
* times the value should be added to the node. If not provided, it defaults to 1.
* @returns a node of type `N` or `undefined`.
*/
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined {
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined {
let node: N | undefined;
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
return;
@ -152,7 +152,7 @@ export class TreeMultimap<
* was not successful.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
if (newNode === undefined) return false;
const orgNodeCount = newNode?.count || 0;

View file

@ -59,24 +59,46 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
}
};
/**
* The function returns the value of the _toEntryFn property.
* @returns The function being returned is `this._toEntryFn`.
*/
get toEntryFn() {
return this._toEntryFn;
}
protected _size = 0;
/**
* The function returns the size of an object.
* @returns The size of the object, which is a number.
*/
get size(): number {
return this._size;
}
/**
* The function checks if a given element is an array with exactly two elements.
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
* data type.
* @returns a boolean value.
*/
isEntry(rawElement: any): rawElement is [K, V] {
return Array.isArray(rawElement) && rawElement.length === 2;
}
/**
* The function checks if the size of an object is equal to zero and returns a boolean value.
* @returns A boolean value indicating whether the size of the object is 0 or not.
*/
isEmpty(): boolean {
return this.size === 0;
}
/**
* The clear() function resets the state of an object by clearing its internal store, object map, and
* size.
*/
clear() {
this._store = {};
this._objMap.clear();
@ -132,7 +154,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
* or `_store`, otherwise it returns `undefined`.
*/
get(key: K): V | undefined {
override get(key: K): V | undefined {
if (this._isObjKey(key)) {
return this._objMap.get(key);
} else {
@ -147,7 +169,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
* @returns The `has` method is returning a boolean value.
*/
has(key: K): boolean {
override has(key: K): boolean {
if (this._isObjKey(key)) {
return this._objMap.has(key);
} else {
@ -241,6 +263,14 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return filteredMap;
}
/**
* The put function sets a value in a data structure using a specified key.
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
* to the function.
* @param {V} value - The value parameter represents the value that you want to associate with the
* specified key in the data structure.
* @returns The method is returning a boolean value.
*/
put(key: K, value: V): boolean {
return this.set(key, value);
}
@ -288,33 +318,70 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
*/
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
protected _head: HashMapLinkedNode<K, V | undefined>;
protected _tail: HashMapLinkedNode<K, V | undefined>;
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>) {
/**
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
* `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
* then added to the HashMap
* @param [options] - The `options` parameter is an optional object that can contain the following
* properties:
*/
constructor(rawCollection: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
super();
this._sentinel = <HashMapLinkedNode<K, V>>{};
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
if (options) {
const { hashFn, objHashFn } = options;
const { hashFn, objHashFn, toEntryFn } = options;
if (hashFn) this._hashFn = hashFn;
if (objHashFn) this._objHashFn = objHashFn;
if (toEntryFn) {
this._toEntryFn = toEntryFn;
}
}
if (entries) {
for (const el of entries) {
this.set(el[0], el[1]);
if (rawCollection) {
for (const el of rawCollection) {
const [key, value] = this.toEntryFn(el);
this.set(key, value);
}
}
}
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
if (this.isEntry(rawElement)) {
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
return rawElement;
} else {
throw new Error(
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
);
}
};
/**
* The function returns the value of the _toEntryFn property.
* @returns The function being returned is `this._toEntryFn`.
*/
get toEntryFn() {
return this._toEntryFn;
}
protected _size = 0;
/**
* The function returns the size of an object.
* @returns The size of the object.
*/
get size() {
return this._size;
}
@ -425,7 +492,30 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
return true;
}
has(key: K): boolean {
/**
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
* using a provided function, and sets each key-value pair in the current object, returning an array
* of booleans indicating the success of each set operation.
* @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
* R.
* @returns The `setMany` function returns an array of booleans.
*/
setMany(rawCollection: Iterable<R>): boolean[] {
const results: boolean[] = [];
for (const rawEle of rawCollection) {
const [key, value] = this.toEntryFn(rawEle);
results.push(this.set(key, value));
}
return results;
}
/**
* The function checks if a given key exists in a map, using different logic depending on whether the
* key is a weak key or not.
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
* @returns The method `has` is returning a boolean value.
*/
override has(key: K): boolean {
if (isWeakKey(key)) {
const hash = this._objHashFn(key);
return this._objMap.has(hash);
@ -435,15 +525,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
}
}
setMany(entries: Iterable<[K, V]>): boolean[] {
const results: boolean[] = [];
for (const entry of entries) {
const [key, value] = entry;
results.push(this.set(key, value));
}
return results;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -457,7 +538,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
* using the key itself. If the key is not found, `undefined` is
*/
get(key: K): V | undefined {
override get(key: K): V | undefined {
if (isWeakKey(key)) {
const hash = this._objHashFn(key);
const node = this._objMap.get(hash);
@ -473,14 +554,14 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
* Time Complexity: O(n), where n is the index.
* Space Complexity: O(1)
*
* The function `getAt` retrieves the key-value pair at a specified index in a linked list.
* The function `at` retrieves the key-value pair at a specified index in a linked list.
* @param {number} index - The index parameter is a number that represents the position of the
* element we want to retrieve from the data structure.
* @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
* where `K` is the key and `V` is the value.
*/
getAt(index: number): V | undefined {
at(index: number): V | undefined {
rangeCheck(index, 0, this._size - 1);
let node = this._head;
while (index--) {
@ -532,7 +613,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
}
/**
* Time Complexity: O(n), where n is the index.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `deleteAt` function deletes a node at a specified index in a linked list.
@ -561,6 +642,16 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
return this._size === 0;
}
/**
* The function checks if a given element is an array with exactly two elements.
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
* data type.
* @returns a boolean value.
*/
isEntry(rawElement: any): rawElement is [K, V] {
return Array.isArray(rawElement) && rawElement.length === 2;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -573,6 +664,20 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as
* the original.
* @returns The `clone()` method is returning a new instance of `LinkedHashMap<K, V>` that is a clone
* of the original `LinkedHashMap` object.
*/
clone(): LinkedHashMap<K, V> {
const cloned = new LinkedHashMap<K, V>([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
for (const entry of this) {
@ -638,26 +743,33 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The put function sets a value in a data structure using a specified key.
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
* to the function.
* @param {V} value - The value parameter represents the value that you want to associate with the
* specified key in the data structure.
* @returns The method is returning a boolean value.
*/
put(key: K, value: V): boolean {
return this.set(key, value);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
protected _hashFn: (key: K) => string = (key: K) => String(key);
protected _objHashFn: (key: K) => object = (key: K) => <object>key;
/**
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
* Time Complexity: O(n)
* Space Complexity: O(1)
* where n is the number of entries in the LinkedHashMap.
*
* The above function is an iterator that yields key-value pairs from a linked list.
*/

View file

@ -21,6 +21,16 @@ import { IterableElementBase } from '../base';
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
*/
export class Heap<E = any> extends IterableElementBase<E> {
/**
* The constructor initializes a heap data structure with optional elements and options.
* @param elements - The `elements` parameter is an iterable object that contains the initial
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
* be initialized as empty.
* @param [options] - The `options` parameter is an optional object that can contain additional
* configuration options for the heap. In this case, it is used to specify a custom comparator
* function for comparing elements in the heap. The comparator function is used to determine the
* order of elements in the heap.
*/
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
super();
@ -45,12 +55,20 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
};
/**
* The function returns the value of the _comparator property.
* @returns The `_comparator` property is being returned.
*/
get comparator() {
return this._comparator;
}
protected _elements: E[] = [];
/**
* The function returns an array of elements.
* @returns The elements array is being returned.
*/
get elements(): E[] {
return this._elements;
}
@ -81,12 +99,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
* where n is the number of elements in the heap.
*/
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Insert an element into the heap and maintain the heap properties.
@ -98,12 +117,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
* where n is the number of elements in the heap.
*/
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Remove and return the top element (smallest or largest element) from the heap.
@ -121,6 +141,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* Peek at the top element of the heap without removing it.
* @returns The top element or undefined if the heap is empty.
*/
@ -391,6 +414,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
return mappedHeap;
}
/**
* The function `_getIterator` returns an iterable iterator for the elements in the class.
*/
protected* _getIterator(): IterableIterator<E> {
for (const element of this.elements) {
yield element;
@ -458,6 +484,16 @@ export class FibonacciHeapNode<E> {
parent?: FibonacciHeapNode<E>;
marked: boolean;
/**
* The constructor function initializes an object with an element and a degree, and sets the marked
* property to false.
* @param {E} element - The "element" parameter represents the value or data that will be stored in
* the node of a data structure. It can be any type of data, such as a number, string, object, or
* even another data structure.
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
* degree is set to 0 when a new node is created.
*/
constructor(element: E, degree = 0) {
this.element = element;
this.degree = degree;
@ -466,6 +502,13 @@ export class FibonacciHeapNode<E> {
}
export class FibonacciHeap<E> {
/**
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
* will be used to determine the order of elements in the heap. If no comparator function is
* provided, a default comparator function will be used.
*/
constructor(comparator?: Comparator<E>) {
this.clear();
this._comparator = comparator || this._defaultComparator;
@ -477,24 +520,41 @@ export class FibonacciHeap<E> {
protected _root?: FibonacciHeapNode<E>;
/**
* The function returns the root node of a Fibonacci heap.
* @returns The method is returning either a FibonacciHeapNode object or undefined.
*/
get root(): FibonacciHeapNode<E> | undefined {
return this._root;
}
protected _size = 0;
/**
* The function returns the size of an object.
* @returns The size of the object, which is a number.
*/
get size(): number {
return this._size;
}
protected _min?: FibonacciHeapNode<E>;
/**
* The function returns the minimum node in a Fibonacci heap.
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
*/
get min(): FibonacciHeapNode<E> | undefined {
return this._min;
}
protected _comparator: Comparator<E>;
/**
* The function returns the comparator used for comparing elements.
* @returns The `_comparator` property of the object.
*/
get comparator(): Comparator<E> {
return this._comparator;
}
@ -808,12 +868,12 @@ export class FibonacciHeap<E> {
}
/**
* Time Complexity: O(n log n), where n is the number of elements in the heap.
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n log n), where n is the number of elements in the heap.
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*
* Remove and return the top element (smallest or largest element) from the heap.

View file

@ -33,7 +33,10 @@ export class DoublyLinkedListNode<E = any> {
*/
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
/**
* The constructor initializes the linked list with an empty head, tail, and size.
* The constructor initializes a linked list with optional elements.
* @param elements - The `elements` parameter is an optional iterable object that contains the
* initial elements to be added to the data structure. It defaults to an empty array if no elements
* are provided.
*/
constructor(elements: Iterable<E> = []) {
super();
@ -49,29 +52,43 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
protected _head: DoublyLinkedListNode<E> | undefined;
/**
* The `head` function returns the first node of a doubly linked list.
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
*/
get head(): DoublyLinkedListNode<E> | undefined {
return this._head;
}
protected _tail: DoublyLinkedListNode<E> | undefined;
/**
* The `tail` function returns the last node of a doubly linked list.
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
* `undefined`.
*/
get tail(): DoublyLinkedListNode<E> | undefined {
return this._tail;
}
protected _size: number;
/**
* The function returns the size of an object.
* @returns The size of the object, which is a number.
*/
get size(): number {
return this._size;
}
/**
* Time Complexity: O(n), where n is the size of the input array.
* Time Complexity: O(n)
* Space Complexity: O(n)
* where n is the number of elements in the linked list.
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
@ -87,7 +104,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
@ -112,11 +129,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
* @returns The `fromArray` function returns a DoublyLinkedList object.
*/
static fromArray<E>(data: E[]) {
const doublyLinkedList = new DoublyLinkedList<E>();
for (const item of data) {
doublyLinkedList.push(item);
}
return doublyLinkedList;
return new DoublyLinkedList<E>(data);
}
/**
@ -173,7 +186,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
@ -200,7 +213,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
@ -227,21 +240,21 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
* The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
* or the linked list is empty, it will return undefined.
*/
getAt(index: number): E | undefined {
at(index: number): E | undefined {
if (index < 0 || index >= this.size) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
@ -251,12 +264,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
@ -276,12 +289,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
@ -304,12 +317,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `insert` function inserts a value at a specified index in a doubly linked list.
@ -343,12 +356,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
* where n is the number of elements in the linked list.
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
@ -388,12 +402,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
@ -432,7 +446,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
@ -462,7 +476,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
@ -498,7 +512,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
@ -511,7 +525,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
@ -525,38 +539,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
*
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
* the callback function. If no element satisfies the condition, it returns `undefined`.
*/
find(callback: (value: E) => boolean): E | undefined {
let current = this.head;
while (current) {
if (callback(current.value)) {
return current.value;
}
current = current.next;
}
return undefined;
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function returns the index of the first occurrence of a given value in a linked list.
@ -579,12 +567,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
@ -606,12 +594,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `reverse` function reverses the order of the elements in a doubly linked list.
@ -633,7 +621,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `toArray` function converts a linked list into an array.
@ -650,12 +638,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
@ -671,6 +659,24 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
return array;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values
* as the original list.
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
* is a copy of the original list.
*/
clone(): DoublyLinkedList<E> {
return new DoublyLinkedList(this.values());
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -772,7 +778,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
@ -789,7 +795,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/

View file

@ -25,7 +25,10 @@ export class SinglyLinkedListNode<E = any> {
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
/**
* The constructor initializes the linked list with an empty head, tail, and length.
* The constructor initializes a new instance of a class with an optional iterable of elements.
* @param elements - The `elements` parameter is an optional iterable object that contains the
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
* array will be used as the default value.
*/
constructor(elements: Iterable<E> = []) {
super();
@ -36,30 +39,44 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
protected _head: SinglyLinkedListNode<E> | undefined;
/**
* The `head` function returns the first node of a singly linked list.
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
*/
get head(): SinglyLinkedListNode<E> | undefined {
return this._head;
}
protected _tail: SinglyLinkedListNode<E> | undefined;
/**
* The `tail` function returns the last node of a singly linked list.
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
*/
get tail(): SinglyLinkedListNode<E> | undefined {
return this._tail;
}
protected _size: number = 0;
/**
* The function returns the size of an object.
* @returns The size of the object, which is a number.
*/
get size(): number {
return this._size;
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
* Time Complexity: O(n)
* Space Complexity: O(n)
* Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
* Linear space, as it creates a new node for each element in the array.
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
* array.
@ -75,13 +92,15 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
* Space Complexity: O(1) - Constant space, as it only creates a new node.
* Time Complexity: O(1)
* Space Complexity: O(1)
* Constant time, as it involves basic pointer adjustments.
* Constant space, as it only creates a new node.
*/
/**
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
* Space Complexity: O(1) - Constant space, as it only creates a new node.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `push` function adds a new node with the given value to the end of a singly linked list.
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
@ -101,13 +120,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
* Space Complexity: O(1) - Constant space, as it only creates a new node.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
* Space Complexity: O(1) - Constant space, as it only creates a new node.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `push` function adds a new node with the given value to the end of a singly linked list.
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
@ -118,13 +137,14 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
* Linear time in the worst case, as it may need to traverse the list to find the last element.
*/
/**
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
@ -153,13 +173,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
@ -171,13 +191,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `shift()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
@ -191,13 +211,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
@ -207,13 +227,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
@ -233,13 +253,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
@ -250,21 +270,22 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
*/
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
* The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
* @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
* @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
* `undefined` if the index is out of bounds.
*/
getAt(index: number): E | undefined {
at(index: number): E | undefined {
if (index < 0 || index >= this.size) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
@ -274,13 +295,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `getNodeAt` returns the node at a given index in a singly linked list.
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
@ -297,13 +318,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@ -330,13 +351,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The delete function removes a node with a specific value from a singly linked list.
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
@ -379,13 +400,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `addAt` function inserts a value at a specified index in a singly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
@ -433,13 +454,15 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
* Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
* Time Complexity: O(n)
* Space Complexity: O(n)
* Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
* Linear space, as it creates an array with the same length as the list.
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
* Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `toArray` function converts a linked list into an array.
* @returns The `toArray()` method is returning an array of type `E[]`.
@ -455,13 +478,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `reverse` function reverses the order of the nodes in a singly linked list.
* @returns The reverse() method does not return anything. It has a return type of void.
@ -485,39 +508,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
*
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
* the callback function. If no element satisfies the condition, it returns `undefined`.
*/
find(callback: (value: E) => boolean): E | undefined {
let current = this.head;
while (current) {
if (callback(current.value)) {
return current.value;
}
current = current.next;
}
return undefined;
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
@ -540,13 +537,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
* undefined.
@ -568,13 +565,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
@ -613,13 +610,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
@ -652,13 +649,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function counts the number of occurrences of a given value in a linked list.
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
@ -678,6 +675,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
return count;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
* as the original list.
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
* is a clone of the original list.
*/
clone(): SinglyLinkedList<E> {
return new SinglyLinkedList<E>(this.values());
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -741,6 +756,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
return mappedList;
}
/**
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
*/
protected* _getIterator(): IterableIterator<E> {
let current = this.head;

View file

@ -20,6 +20,14 @@ export class SkipListNode<K, V> {
}
export class SkipList<K, V> {
/**
* The constructor function initializes a SkipLinkedList object with optional options and elements.
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
* is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
* provided, the SkipLinkedList will be empty.
* @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
* contain two properties:
*/
constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
if (options) {
const { maxLevel, probability } = options;
@ -34,36 +42,52 @@ export class SkipList<K, V> {
protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
/**
* The function returns the head node of a SkipList.
* @returns The method is returning a SkipListNode object with generic key type K and value type V.
*/
get head(): SkipListNode<K, V> {
return this._head;
}
protected _level: number = 0;
/**
* The function returns the value of the private variable _level.
* @returns The level property of the object.
*/
get level(): number {
return this._level;
}
protected _maxLevel: number = 16;
/**
* The function returns the maximum level.
* @returns The value of the variable `_maxLevel` is being returned.
*/
get maxLevel(): number {
return this._maxLevel;
}
protected _probability: number = 0.5;
/**
* The function returns the probability value.
* @returns The probability value stored in the private variable `_probability` is being returned.
*/
get probability(): number {
return this._probability;
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* Get the value of the first element (the smallest element) in the Skip List.
* @returns The value of the first element, or undefined if the Skip List is empty.
@ -74,13 +98,13 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the last element (the largest element) in the Skip List.
* @returns The value of the last element, or undefined if the Skip List is empty.
@ -96,13 +120,13 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The add function adds a new node with a given key and value to a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
@ -132,13 +156,13 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function `get` retrieves the value associated with a given key from a skip list data structure.
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
@ -163,27 +187,28 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* The function checks if a key exists in a data structure.
* @param {K} key - The parameter "key" is of type K, which represents the type of the key being
* checked.
* @returns a boolean value.
*/
has(key: K): boolean {
return this.get(key) !== undefined;
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `delete` function removes a node with a specific key from a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
@ -220,13 +245,13 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the first element in the Skip List that is greater than the given key.
* @param key - the given key.
@ -244,13 +269,13 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the last element in the Skip List that is less than the given key.
* @param key - the given key.
@ -273,13 +298,14 @@ export class SkipList<K, V> {
}
/**
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
* Space Complexity: O(1) - constant space.
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)
* where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
*/
/**
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
* Space Complexity: O(1) - constant space.
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)
*
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
* @returns the level, which is a number.

View file

@ -42,30 +42,54 @@ export class Matrix {
protected _rows: number = 0;
/**
* The function returns the number of rows.
* @returns The number of rows.
*/
get rows(): number {
return this._rows;
}
protected _cols: number = 0;
/**
* The function returns the value of the private variable _cols.
* @returns The number of columns.
*/
get cols(): number {
return this._cols;
}
protected _data: number[][];
/**
* The function returns a two-dimensional array of numbers.
* @returns The data property, which is a two-dimensional array of numbers.
*/
get data(): number[][] {
return this._data;
}
/**
* The above function returns the value of the _addFn property.
* @returns The value of the property `_addFn` is being returned.
*/
get addFn() {
return this._addFn;
}
/**
* The function returns the value of the _subtractFn property.
* @returns The `_subtractFn` property is being returned.
*/
get subtractFn() {
return this._subtractFn;
}
/**
* The function returns the value of the _multiplyFn property.
* @returns The `_multiplyFn` property is being returned.
*/
get multiplyFn() {
return this._multiplyFn;
}
@ -373,6 +397,34 @@ export class Matrix {
});
}
/**
* The function checks if a given row and column index is valid within a specified range.
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
* matrix. It is a number that indicates the specific row in the matrix.
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
* @returns A boolean value is being returned.
*/
isValidIndex(row: number, col: number): boolean {
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
}
/**
* The `clone` function returns a new instance of the Matrix class with the same data and properties
* as the original instance.
* @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
* and properties as the current instance.
*/
clone(): Matrix {
return new Matrix(this.data, {
rows: this.rows,
cols: this.cols,
addFn: this.addFn,
subtractFn: this.subtractFn,
multiplyFn: this.multiplyFn
});
}
protected _addFn(a: number | undefined, b: number): number | undefined {
if (a === undefined) return b;
return a + b;
@ -386,18 +438,6 @@ export class Matrix {
return a * b;
}
/**
* The function checks if a given row and column index is valid within a specified range.
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
* matrix. It is a number that indicates the specific row in the matrix.
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
* @returns A boolean value is being returned.
*/
protected isValidIndex(row: number, col: number): boolean {
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
}
/**
* The function `_swapRows` swaps the positions of two rows in an array.
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.

View file

@ -24,6 +24,17 @@ export class Deque<E> extends IterableElementBase<E> {
protected _bucketCount = 0;
protected readonly _bucketSize: number = 1 << 12;
/**
* The constructor initializes a Deque object with an optional iterable of elements and options.
* @param elements - An iterable object (such as an array or a Set) that contains the initial
* elements to be added to the deque. It can also be an object with a `length` or `size` property
* that represents the number of elements in the iterable object. If no elements are provided, an
* empty deque
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
* or is not a number
*/
constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) {
super();
@ -54,14 +65,32 @@ export class Deque<E> extends IterableElementBase<E> {
}
}
/**
* The bucketSize function returns the size of the bucket.
*
* @return The size of the bucket
*/
get bucketSize() {
return this._bucketSize;
}
protected _buckets: E[][] = [];
/**
* The buckets function returns the buckets property of the object.
*
* @return The buckets property
*/
get buckets() {
return this._buckets;
}
protected _size = 0;
/**
* The size function returns the number of items in the stack.
* @return The number of values in the set
*/
get size() {
return this._size;
}
@ -76,6 +105,10 @@ export class Deque<E> extends IterableElementBase<E> {
return this._buckets[this._bucketFirst][this._firstInBucket];
}
/**
* The last function returns the last element in the queue.
* @return The last element in the array
*/
get last(): E | undefined {
if (this.size === 0) return;
return this._buckets[this._bucketLast][this._lastInBucket];
@ -235,7 +268,7 @@ export class Deque<E> extends IterableElementBase<E> {
* begin(): Generator<E> {
let index = 0;
while (index < this.size) {
yield this.getAt(index);
yield this.at(index);
index++;
}
}
@ -247,7 +280,7 @@ export class Deque<E> extends IterableElementBase<E> {
* reverseBegin(): Generator<E> {
let index = this.size - 1;
while (index >= 0) {
yield this.getAt(index);
yield this.at(index);
index--;
}
}
@ -261,13 +294,13 @@ export class Deque<E> extends IterableElementBase<E> {
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `getAt` function retrieves an element at a specified position in an array-like data structure.
* The `at` function retrieves an element at a specified position in an array-like data structure.
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
* retrieve from the data structure. It is of type `number` and should be a valid index within the
* range of the data structure.
* @returns The element at the specified position in the data structure is being returned.
*/
getAt(pos: number): E {
at(pos: number): E {
rangeCheck(pos, 0, this.size - 1);
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
return this._buckets[bucketIndex][indexInBucket]!;
@ -325,7 +358,7 @@ export class Deque<E> extends IterableElementBase<E> {
} else {
const arr: E[] = [];
for (let i = pos; i < this.size; ++i) {
arr.push(this.getAt(i));
arr.push(this.at(i));
}
this.cut(pos - 1);
for (let i = 0; i < num; ++i) this.push(element);
@ -416,7 +449,7 @@ export class Deque<E> extends IterableElementBase<E> {
let i = 0;
let index = 0;
while (i < size) {
const oldElement = this.getAt(i);
const oldElement = this.at(i);
if (oldElement !== element) {
this.setAt(index, oldElement!);
index += 1;
@ -471,9 +504,9 @@ export class Deque<E> extends IterableElementBase<E> {
return this;
}
let index = 1;
let prev = this.getAt(0);
let prev = this.at(0);
for (let i = 1; i < this.size; ++i) {
const cur = this.getAt(i);
const cur = this.at(i);
if (cur !== prev) {
prev = cur;
this.setAt(index++, cur);
@ -501,7 +534,7 @@ export class Deque<E> extends IterableElementBase<E> {
sort(comparator?: (x: E, y: E) => number): this {
const arr: E[] = [];
for (let i = 0; i < this.size; ++i) {
arr.push(this.getAt(i));
arr.push(this.at(i));
}
arr.sort(comparator);
for (let i = 0; i < this.size; ++i) {
@ -545,32 +578,6 @@ export class Deque<E> extends IterableElementBase<E> {
this._buckets = newBuckets;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `find` function iterates over the elements in a deque and returns the first element for which
* the callback function returns true, or undefined if no such element is found.
* @param callback - A function that takes three parameters: element, index, and deque. It should
* return a boolean value indicating whether the element satisfies a certain condition.
* @returns The method `find` returns the first element in the deque that satisfies the condition
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
*/
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
for (let i = 0; i < this.size; ++i) {
const element = this.getAt(i);
if (callback(element, i, this)) {
return element;
}
}
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -589,7 +596,7 @@ export class Deque<E> extends IterableElementBase<E> {
*/
indexOf(element: E): number {
for (let i = 0; i < this.size; ++i) {
if (this.getAt(i) === element) {
if (this.at(i) === element) {
return i;
}
}
@ -616,6 +623,25 @@ export class Deque<E> extends IterableElementBase<E> {
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
* bucket size as the original instance.
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
* elements as the original deque (`this`) and the same bucket size.
*/
clone(): Deque<E> {
return new Deque<E>([...this], { bucketSize: this.bucketSize });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -737,7 +763,7 @@ export class Deque<E> extends IterableElementBase<E> {
*/
protected* _getIterator(): IterableIterator<E> {
for (let i = 0; i < this.size; ++i) {
yield this.getAt(i);
yield this.at(i);
}
}

View file

@ -32,12 +32,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
protected _elements: E[] = [];
/**
* The elements function returns the elements of this set.
* @return An array of the elements in the stack
*/
get elements(): E[] {
return this._elements;
}
protected _offset: number = 0;
/**
* The offset function returns the offset of the current page.
* @return The value of the private variable _offset
*/
get offset(): number {
return this._offset;
}
@ -219,7 +227,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
*
* @param index
*/
getAt(index: number): E | undefined {
at(index: number): E | undefined {
return this.elements[index];
}
@ -264,13 +272,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
* Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
* Time Complexity: O(n)
* Space Complexity: O(n)
* where n is the number of elements in the queue. It creates a shallow copy of the internal array. the space required is proportional to the number of elements in the queue.
*/
/**
* Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
* Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
* @returns The `clone()` method is returning a new instance of the `Queue` class.
@ -390,4 +399,21 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
peek(): E | undefined {
return this.first;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* The `clone` function returns a new instance of the `LinkedListQueue` class with the same values as
* the current instance.
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
* values as the original `LinkedListQueue`.
*/
clone(): LinkedListQueue<E> {
return new LinkedListQueue<E>(this.values());
}
}

View file

@ -32,6 +32,10 @@ export class Stack<E = any> extends IterableElementBase<E> {
protected _elements: E[] = [];
/**
* The elements function returns the elements of this set.
* @return An array of elements
*/
get elements(): E[] {
return this._elements;
}

View file

@ -38,6 +38,12 @@ export class TrieNode {
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
*/
export class Trie extends IterableElementBase<string> {
/**
* The constructor function for the Trie class.
* @param words: Iterable string Initialize the trie with a set of words
* @param options?: TrieOptions Allow the user to pass in options for the trie
* @return This
*/
constructor(words: Iterable<string> = [], options?: TrieOptions) {
super();
if (options) {
@ -51,18 +57,31 @@ export class Trie extends IterableElementBase<string> {
protected _size: number = 0;
/**
* The size function returns the size of the stack.
* @return The number of elements in the list
*/
get size(): number {
return this._size;
}
protected _caseSensitive: boolean = true;
/**
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property.
*
* @return The value of the _casesensitive private variable
*/
get caseSensitive(): boolean {
return this._caseSensitive;
}
protected _root: TrieNode = new TrieNode('');
/**
* The root function returns the root node of the tree.
* @return The root node
*/
get root() {
return this._root;
}
@ -351,6 +370,23 @@ export class Trie extends IterableElementBase<string> {
return words;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `clone` function returns a new instance of the Trie class with the same values and case
* sensitivity as the original Trie.
* @returns A new instance of the Trie class is being returned.
*/
clone(): Trie {
return new Trie(this.values(), { caseSensitive: this.caseSensitive });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -5,14 +5,15 @@ export type HashMapLinkedNode<K, V> = {
prev: HashMapLinkedNode<K, V>;
};
export type LinkedHashMapOptions<K> = {
export type LinkedHashMapOptions<K, V, R> = {
hashFn?: (key: K) => string;
objHashFn?: (key: K) => object;
toEntryFn?: (rawElement: R) => [K, V];
};
export type HashMapOptions<K, V, T> = {
export type HashMapOptions<K, V, R> = {
hashFn?: (key: K) => string;
toEntryFn?: (rawElement: T) => [K, V];
toEntryFn?: (rawElement: R) => [K, V];
};
export type HashMapStoreItem<K, V> = { key: K; value: V };

View file

@ -385,7 +385,7 @@ describe('LinkedHashMap', () => {
expect(hashMap.last).toEqual([key, value]);
expect(hashMap.reverseBegin().next().value).toEqual([key, value]);
} else if (index <= 1000) {
expect(hashMap.getAt(index)).toBe(value);
expect(hashMap.at(index)).toBe(value);
}
expect(hashMap.get(key)).toEqual(value);
index++;
@ -434,7 +434,7 @@ describe('LinkedHashMap', () => {
test('should get element at specific index', () => {
hashMap.set('key1', 'value1');
hashMap.set('key2', 'value2');
expect(hashMap.getAt(1)).toBe('value2');
expect(hashMap.at(1)).toBe('value2');
});
describe('LinkedHashMap basic', () => {

View file

@ -39,6 +39,8 @@ describe('DoublyLinkedList Operation Test', () => {
it('should find undefined', () => {
expect(list.find(value => value === 6)).toBe(undefined);
expect(list.find(value => value === 4)).toBe(4);
expect(list.find(value => value === 3)).toBe(3);
});
it('should indexOf -1', () => {
@ -99,19 +101,19 @@ describe('DoublyLinkedList Operation Test', () => {
// Inserting at the beginning
list.addAt(0, 0);
expect(list.size).toBe(4);
expect(list.getAt(0)).toBe(0);
expect(list.getAt(1)).toBe(1);
expect(list.at(0)).toBe(0);
expect(list.at(1)).toBe(1);
// Inserting in the middle
list.addAt(2, 1.5);
expect(list.size).toBe(5);
expect(list.getAt(2)).toBe(1.5);
expect(list.getAt(3)).toBe(2);
expect(list.at(2)).toBe(1.5);
expect(list.at(3)).toBe(2);
// Inserting at the end
list.addAt(5, 4);
expect(list.size).toBe(6);
expect(list.getAt(5)).toBe(4);
expect(list.at(5)).toBe(4);
expect(list.tail!.value).toBe(4);
});

View file

@ -72,14 +72,14 @@ describe('SinglyLinkedList Operation Test', () => {
list.push(1);
list.push(2);
list.push(3);
const element = list.getAt(1);
const element = list.at(1);
expect(element).toBe(2);
expect(list.getNodeAt(2)?.value).toBe(3);
});
it('should return undefined for an out-of-bounds index', () => {
list.push(1);
const element = list.getAt(1);
const element = list.at(1);
expect(element).toBeUndefined();
});
});
@ -327,6 +327,7 @@ describe('SinglyLinkedList Operation Test', () => {
list.push(3);
const result = list.find(data => data % 2 === 0);
expect(result).toBe(2);
expect(list.find(value => value === 3)).toBe(3);
});
it('should return undefined if element is not found', () => {

View file

@ -40,14 +40,14 @@ describe('Deque - Basic Operations', () => {
expect(deque.isEmpty()).toBeTruthy();
});
test('getAt should retrieve the correct element', () => {
expect(deque.getAt(0)).toBe(1);
expect(deque.getAt(1)).toBe(2);
test('at should retrieve the correct element', () => {
expect(deque.at(0)).toBe(1);
expect(deque.at(1)).toBe(2);
});
test('setAt should set the correct element', () => {
deque.setAt(0, 3);
expect(deque.getAt(0)).toBe(3);
expect(deque.at(0)).toBe(3);
});
});
describe('Deque - Complex Operations', () => {

View file

@ -156,17 +156,17 @@ describe('Queue - Additional Methods', () => {
expect(queue.peekLast()).toBeUndefined();
});
test('getAt should return the element at the specified index', () => {
test('at should return the element at the specified index', () => {
queue.push(1);
queue.push(2);
queue.push(3);
expect(queue.getAt(1)).toBe(2);
expect(queue.at(1)).toBe(2);
});
test('getAt should return undefined for an invalid index', () => {
test('at should return undefined for an invalid index', () => {
queue.push(1);
expect(queue.getAt(3)).toBeUndefined();
expect(queue.getAt(-1)).toBeUndefined();
expect(queue.at(3)).toBeUndefined();
expect(queue.at(-1)).toBeUndefined();
});
test('print should not throw any errors', () => {