red-black-tree-typed
Advanced tools
Comparing version 1.48.1 to 1.48.2
@@ -9,4 +9,5 @@ /** | ||
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types'; | ||
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types'; | ||
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout, PairCallback } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { IterablePairBase } from "../base"; | ||
/** | ||
@@ -45,3 +46,3 @@ * Represents a node in a binary tree. | ||
*/ | ||
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> { | ||
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> extends IterablePairBase<BTNKey, V | undefined> implements IBinaryTree<V, N, TREE> { | ||
iterationType: IterationType; | ||
@@ -473,27 +474,2 @@ /** | ||
* | ||
* The function "keys" returns an array of keys from a given object. | ||
* @returns an array of BTNKey objects. | ||
*/ | ||
keys(): BTNKey[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function "values" returns an array of values from a map-like object. | ||
* @returns The `values()` method is returning an array of values (`V`) from the entries in the | ||
* object. | ||
*/ | ||
values(): (V | undefined)[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a new tree object and copies all the nodes from the original tree to | ||
@@ -505,52 +481,42 @@ * the new tree. | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* The `forEach` function iterates over each entry in a tree and calls a callback function with the | ||
* entry and the tree as arguments. | ||
* @param callback - The callback parameter is a function that will be called for each entry in the | ||
* tree. It takes two parameters: entry and tree. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new tree by iterating over the elements of the current tree and | ||
* adding only the elements that satisfy the given predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`, | ||
* `key`, and `index`. It should return a boolean value indicating whether the pair should be | ||
* included in the filtered tree or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided, | ||
* it will be passed as the first argument to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that | ||
* pass the given predicate function. | ||
*/ | ||
forEach(callback: (entry: [BTNKey, V | undefined], tree: this) => void): void; | ||
filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any): TREE; | ||
/** | ||
* The `filter` function creates a new tree by iterating over the entries of the current tree and | ||
* adding the entries that satisfy the given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and | ||
* `tree`. | ||
* @returns The `filter` method is returning a new tree object that contains only the entries that | ||
* satisfy the given predicate function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean): TREE; | ||
/** | ||
* The `map` function creates a new tree by applying a callback function to each entry in the current | ||
* tree. | ||
* @param callback - The callback parameter is a function that takes two arguments: entry and tree. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new tree by applying a callback function to each key-value pair in | ||
* the original tree. | ||
* @param callback - The callback parameter is a function that will be called for each key-value pair | ||
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair, | ||
* the index of the current pair, and a reference to the tree itself. The callback function should | ||
* return a new | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it | ||
* will be used as the `this` value when the callback function is called. If you don't pass a value | ||
* @returns The `map` method is returning a new tree object. | ||
*/ | ||
map(callback: (entry: [BTNKey, V | undefined], tree: this) => V): TREE; | ||
map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any): TREE; | ||
/** | ||
* The `reduce` function iterates over the entries of a tree and applies a callback function to each | ||
* entry, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value | ||
* based on the logic defined in the callback function. | ||
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the callback function when reducing the | ||
* elements of the tree. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the entries in the tree and applying the callback function to each entry. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T; | ||
/** | ||
* The above function is an iterator for a binary tree that can be used to traverse the tree in | ||
* either an iterative or recursive manner. | ||
* @param node - The `node` parameter represents the current node in the binary tree from which the | ||
* iteration starts. It is an optional parameter with a default value of `this.root`, which means | ||
* that if no node is provided, the iteration will start from the root of the binary tree. | ||
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the | ||
* binary tree nodes in a specific order. | ||
*/ | ||
[Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>; | ||
/** | ||
* The `print` function is used to display a binary tree structure in a visually appealing way. | ||
@@ -563,2 +529,3 @@ * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null | | ||
print(beginRoot?: BTNodeKeyOrNode<N>, options?: BinaryTreePrintOptions): void; | ||
protected _getIterator(node?: N | null | undefined): IterableIterator<[BTNKey, V | undefined]>; | ||
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
@@ -565,0 +532,0 @@ protected _defaultOneParamCallback: (node: N) => number; |
import type { DijkstraResult, VertexKey } from '../../types'; | ||
import { PairCallback } from "../../types"; | ||
import { IGraph } from '../../interfaces'; | ||
import { IterablePairBase } from "../base"; | ||
export declare abstract class AbstractVertex<V = any> { | ||
@@ -31,3 +33,4 @@ key: VertexKey; | ||
} | ||
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> { | ||
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> extends IterablePairBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> { | ||
constructor(); | ||
protected _vertices: Map<VertexKey, VO>; | ||
@@ -447,7 +450,42 @@ get vertices(): Map<VertexKey, VO>; | ||
getBridges(): EO[]; | ||
[Symbol.iterator](): Iterator<[VertexKey, V | undefined]>; | ||
forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void; | ||
filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][]; | ||
map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[]; | ||
reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function iterates over key-value pairs in a data structure and returns an array of | ||
* pairs that satisfy a given predicate. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included | ||
* in the filtered array. The callback function should return `true` if the element should be | ||
* included, and ` | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]` | ||
* that satisfy the given predicate function. | ||
*/ | ||
filter(predicate: PairCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function iterates over the elements of a collection and applies a callback function to | ||
* each element, returning an array of the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* map. It takes four arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be | ||
* used as the `this` value when calling the callback function. If `thisArg` is not provided, ` | ||
* @returns The `map` function is returning an array of type `T[]`. | ||
*/ | ||
map<T>(callback: PairCallback<VertexKey, V | undefined, T>, thisArg?: any): T[]; | ||
protected _getIterator(): IterableIterator<[VertexKey, V | undefined]>; | ||
protected abstract _addEdgeOnly(edge: EO): boolean; | ||
@@ -454,0 +492,0 @@ protected _addVertexOnly(newVertex: VO): boolean; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const queue_1 = require("../queue"); | ||
const base_1 = require("../base"); | ||
class AbstractVertex { | ||
@@ -49,4 +50,5 @@ /** | ||
exports.AbstractEdge = AbstractEdge; | ||
class AbstractGraph { | ||
class AbstractGraph extends base_1.IterablePairBase { | ||
constructor() { | ||
super(); | ||
this._vertices = new Map(); | ||
@@ -1033,20 +1035,28 @@ } | ||
} | ||
*[Symbol.iterator]() { | ||
for (const vertex of this._vertices.values()) { | ||
yield [vertex.key, vertex.value]; | ||
} | ||
} | ||
forEach(callback) { | ||
let index = 0; | ||
for (const vertex of this) { | ||
callback(vertex, index, this._vertices); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function iterates over key-value pairs in a data structure and returns an array of | ||
* pairs that satisfy a given predicate. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included | ||
* in the filtered array. The callback function should return `true` if the element should be | ||
* included, and ` | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]` | ||
* that satisfy the given predicate function. | ||
*/ | ||
filter(predicate, thisArg) { | ||
const filtered = []; | ||
let index = 0; | ||
for (const entry of this) { | ||
if (predicate(entry, index, this._vertices)) { | ||
filtered.push(entry); | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index, this)) { | ||
filtered.push([key, value]); | ||
} | ||
@@ -1057,7 +1067,24 @@ index++; | ||
} | ||
map(callback) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function iterates over the elements of a collection and applies a callback function to | ||
* each element, returning an array of the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* map. It takes four arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be | ||
* used as the `this` value when calling the callback function. If `thisArg` is not provided, ` | ||
* @returns The `map` function is returning an array of type `T[]`. | ||
*/ | ||
map(callback, thisArg) { | ||
const mapped = []; | ||
let index = 0; | ||
for (const entry of this) { | ||
mapped.push(callback(entry, index, this._vertices)); | ||
for (const [key, value] of this) { | ||
mapped.push(callback.call(thisArg, value, key, index, this)); | ||
index++; | ||
@@ -1067,10 +1094,6 @@ } | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this._vertices); | ||
index++; | ||
*_getIterator() { | ||
for (const vertex of this._vertices.values()) { | ||
yield [vertex.key, vertex.value]; | ||
} | ||
return accumulator; | ||
} | ||
@@ -1077,0 +1100,0 @@ _addVertexOnly(newVertex) { |
@@ -8,4 +8,5 @@ /** | ||
*/ | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
export declare class HashMap<K = any, V = any> { | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem, PairCallback } from '../../types'; | ||
import { IterablePairBase } from "../base"; | ||
export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> { | ||
protected _store: { | ||
@@ -71,51 +72,9 @@ [key: string]: HashMapStoreItem<K, V>; | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
[Symbol.iterator](): IterableIterator<[K, V]>; | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
entries(): IterableIterator<[K, V]>; | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
keys(): IterableIterator<K>; | ||
values(): IterableIterator<V>; | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean; | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean; | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @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 inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
@@ -131,4 +90,11 @@ * key-value pair in the original HashMap. | ||
*/ | ||
map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U>; | ||
map<U>(callbackfn: PairCallback<K, V, U>, thisArg?: any): HashMap<K, U>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
@@ -146,16 +112,9 @@ * that satisfy a given predicate function. | ||
*/ | ||
filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V>; | ||
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>; | ||
print(): void; | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U; | ||
print(): void; | ||
protected _getIterator(): IterableIterator<[K, V]>; | ||
protected _hashFn: (key: K) => string; | ||
@@ -165,3 +124,3 @@ protected _isObjKey(key: any): key is (object | ((...args: any[]) => any)); | ||
} | ||
export declare class LinkedHashMap<K = any, V = any> { | ||
export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K, V> { | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>; | ||
@@ -219,4 +178,2 @@ protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>; | ||
setMany(entries: Iterable<[K, V]>): void; | ||
keys(): K[]; | ||
values(): V[]; | ||
/** | ||
@@ -287,41 +244,44 @@ * Time Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* LinkedHashMap. It takes three arguments: | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void): void; | ||
/** | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* map that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the | ||
* current element should be included in the filtered map or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this | ||
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original | ||
* `LinkedHashMap` object that satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V>; | ||
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>; | ||
/** | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV>; | ||
/** | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the map. | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
* each key-value pair in the original map. | ||
* @param callback - The callback parameter is a function that will be called for each key-value pair | ||
* in the map. It takes four arguments: the value of the current key-value pair, the key of the | ||
* current key-value pair, the index of the current key-value pair, and the map itself. The callback | ||
* function should | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If provided, the callback function will | ||
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current | ||
* map | ||
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback | ||
* function. | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A; | ||
map<NV>(callback: PairCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>; | ||
print(): void; | ||
/** | ||
@@ -333,4 +293,3 @@ * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
*/ | ||
[Symbol.iterator](): Generator<[K, V], void, unknown>; | ||
print(): void; | ||
protected _getIterator(): Generator<[K, V], void, unknown>; | ||
/** | ||
@@ -337,0 +296,0 @@ * Time Complexity: O(1) |
@@ -12,3 +12,4 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
class HashMap { | ||
const base_1 = require("../base"); | ||
class HashMap extends base_1.IterablePairBase { | ||
/** | ||
@@ -23,2 +24,3 @@ * The constructor function initializes a new instance of a class with optional elements and options. | ||
constructor(elements = [], options) { | ||
super(); | ||
this._store = {}; | ||
@@ -140,91 +142,9 @@ this._objMap = new Map(); | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value]; | ||
} | ||
for (const node of this._objMap) { | ||
yield node; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
*entries() { | ||
for (const item of this) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
*keys() { | ||
for (const [key] of this) { | ||
yield key; | ||
} | ||
} | ||
*values() { | ||
for (const [, value] of this) { | ||
yield value; | ||
} | ||
} | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (!predicate.call(thisArg, value, key, index++, this)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @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 inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
callbackfn.call(thisArg, value, key, index++, this); | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
@@ -249,2 +169,9 @@ * key-value pair in the original HashMap. | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
@@ -272,24 +199,17 @@ * that satisfy a given predicate function. | ||
} | ||
print() { | ||
console.log([...this.entries()]); | ||
} | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
reduce(callbackfn, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
accumulator = callbackfn(accumulator, value, key, index++, this); | ||
*_getIterator() { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value]; | ||
} | ||
return accumulator; | ||
for (const node of this._objMap) { | ||
yield node; | ||
} | ||
} | ||
print() { | ||
console.log([...this.entries()]); | ||
} | ||
_isObjKey(key) { | ||
@@ -318,3 +238,3 @@ const keyType = typeof key; | ||
exports.HashMap = HashMap; | ||
class LinkedHashMap { | ||
class LinkedHashMap extends base_1.IterablePairBase { | ||
constructor(elements, options = { | ||
@@ -324,2 +244,3 @@ hashFn: (key) => String(key), | ||
}) { | ||
super(); | ||
this._noObjMap = {}; | ||
@@ -460,14 +381,2 @@ this._objMap = new WeakMap(); | ||
} | ||
keys() { | ||
const keys = []; | ||
for (const [key] of this) | ||
keys.push(key); | ||
return keys; | ||
} | ||
values() { | ||
const values = []; | ||
for (const [, value] of this) | ||
values.push(value); | ||
return values; | ||
} | ||
/** | ||
@@ -602,31 +511,25 @@ * Time Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* LinkedHashMap. It takes three arguments: | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
let node = this._head; | ||
while (node !== this._sentinel) { | ||
callback([node.key, node.value], index++, this); | ||
node = node.next; | ||
} | ||
} | ||
/** | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* map that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the | ||
* current element should be included in the filtered map or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this | ||
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original | ||
* `LinkedHashMap` object that satisfy the given predicate function. | ||
*/ | ||
filter(predicate) { | ||
filter(predicate, thisArg) { | ||
const filteredMap = new LinkedHashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], index, this)) { | ||
if (predicate.call(thisArg, value, key, index, this)) { | ||
filteredMap.set(key, value); | ||
@@ -639,13 +542,27 @@ } | ||
/** | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
map(callback) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
* each key-value pair in the original map. | ||
* @param callback - The callback parameter is a function that will be called for each key-value pair | ||
* in the map. It takes four arguments: the value of the current key-value pair, the key of the | ||
* current key-value pair, the index of the current key-value pair, and the map itself. The callback | ||
* function should | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If provided, the callback function will | ||
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current | ||
* map | ||
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback | ||
* function. | ||
*/ | ||
map(callback, thisArg) { | ||
const mappedMap = new LinkedHashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
const newValue = callback([key, value], index, this); | ||
const newValue = callback.call(thisArg, value, key, index, this); | ||
mappedMap.set(key, newValue); | ||
@@ -656,22 +573,4 @@ index++; | ||
} | ||
/** | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the map. | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
*/ | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
print() { | ||
console.log([...this]); | ||
} | ||
@@ -684,3 +583,3 @@ /** | ||
*/ | ||
*[Symbol.iterator]() { | ||
*_getIterator() { | ||
let node = this._head; | ||
@@ -692,5 +591,2 @@ while (node !== this._sentinel) { | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
@@ -697,0 +593,0 @@ * Time Complexity: O(1) |
@@ -7,5 +7,6 @@ /** | ||
*/ | ||
import type { Comparator, DFSOrderPattern } from '../../types'; | ||
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types'; | ||
import { HeapOptions } from "../../types"; | ||
export declare class Heap<E = any> { | ||
import { IterableElementBase } from "../base"; | ||
export declare class Heap<E = any> extends IterableElementBase<E> { | ||
options: HeapOptions<E>; | ||
@@ -196,8 +197,49 @@ constructor(elements?: Iterable<E>, options?: HeapOptions<E>); | ||
fix(): void; | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
forEach(callback: (element: E, index: number, heap: this) => void): void; | ||
filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E>; | ||
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T>; | ||
reduce<T>(callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, initialValue: T): T; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new Heap object containing elements that pass a given callback | ||
* function. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the heap. It takes three arguments: the current element, the index of the current element, and the | ||
* heap itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass | ||
* the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new heap by applying a callback function to each element of the | ||
* original heap. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* original heap. It takes three arguments: the current element, the index of the current element, | ||
* and the original heap itself. The callback function should return a value of type T, which will be | ||
* added to the mapped heap. | ||
* @param comparator - The `comparator` parameter is a function that is used to compare elements in | ||
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than | ||
* `b`, a positive number if `a` is greater than `b`, or | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. It is used when you want to bind a | ||
* specific object as the context for the callback function. If `thisArg` is not provided, | ||
* `undefined` is used as | ||
* @returns a new instance of the Heap class, which is created using the mapped elements from the | ||
* original Heap. | ||
*/ | ||
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>; | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -207,2 +249,3 @@ * Space Complexity: O(1) | ||
print(): void; | ||
protected _getIterator(): Generator<E, void, unknown>; | ||
/** | ||
@@ -209,0 +252,0 @@ * Time Complexity: O(n) |
@@ -10,4 +10,6 @@ "use strict"; | ||
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0; | ||
class Heap { | ||
const base_1 = require("../base"); | ||
class Heap extends base_1.IterableElementBase { | ||
constructor(elements, options) { | ||
super(); | ||
this._elements = []; | ||
@@ -314,30 +316,62 @@ const defaultComparator = (a, b) => { | ||
} | ||
*[Symbol.iterator]() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
forEach(callback) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new Heap object containing elements that pass a given callback | ||
* function. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the heap. It takes three arguments: the current element, the index of the current element, and the | ||
* heap itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass | ||
* the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback, thisArg) { | ||
const filteredList = new Heap(); | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
const filteredHeap = new Heap([], this.options); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
filteredHeap.push(el); | ||
for (const current of this) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
index++; | ||
} | ||
return filteredHeap; | ||
return filteredList; | ||
} | ||
map(callback, comparator) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new heap by applying a callback function to each element of the | ||
* original heap. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* original heap. It takes three arguments: the current element, the index of the current element, | ||
* and the original heap itself. The callback function should return a value of type T, which will be | ||
* added to the mapped heap. | ||
* @param comparator - The `comparator` parameter is a function that is used to compare elements in | ||
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than | ||
* `b`, a positive number if `a` is greater than `b`, or | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. It is used when you want to bind a | ||
* specific object as the context for the callback function. If `thisArg` is not provided, | ||
* `undefined` is used as | ||
* @returns a new instance of the Heap class, which is created using the mapped elements from the | ||
* original Heap. | ||
*/ | ||
map(callback, comparator, thisArg) { | ||
const mappedHeap = new Heap([], { comparator: comparator }); | ||
let index = 0; | ||
for (const el of this) { | ||
mappedHeap.add(callback(el, index, this)); | ||
mappedHeap.add(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -347,11 +381,2 @@ } | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -364,2 +389,7 @@ * Time Complexity: O(log n) | ||
} | ||
*_getIterator() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
/** | ||
@@ -366,0 +396,0 @@ * Time Complexity: O(n) |
@@ -12,1 +12,2 @@ export * from './hash'; | ||
export * from './trie'; | ||
export * from './base'; |
@@ -28,1 +28,2 @@ "use strict"; | ||
__exportStar(require("./trie"), exports); | ||
__exportStar(require("./base"), exports); |
@@ -0,1 +1,3 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -19,3 +21,3 @@ * data-structure-typed | ||
} | ||
export declare class DoublyLinkedList<E = any> { | ||
export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -391,34 +393,23 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void; | ||
/** | ||
* 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 `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the DoublyLinkedList class. | ||
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element, returning only the elements for which the | ||
* callback function returns true. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current | ||
* element, and the DoublyLinkedList itself. The callback function should return a boolean value | ||
* indicating whether the current element should be included | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E>; | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E>; | ||
/** | ||
@@ -429,13 +420,19 @@ * Time 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(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new | ||
* DoublyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element | ||
* in the original list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element, | ||
* and the DoublyLinkedList itself. The callback function should return a value that will be added to | ||
* the new DoublyLinkedList that | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original `DoublyLinkedList` | ||
* object. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T>; | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T>; | ||
/** | ||
@@ -445,17 +442,7 @@ * Time Complexity: O(n), where n is the number of elements in the linked list. | ||
*/ | ||
print(): void; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T; | ||
print(): void; | ||
protected _getIterator(): IterableIterator<E>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0; | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -24,3 +25,3 @@ * data-structure-typed | ||
exports.DoublyLinkedListNode = DoublyLinkedListNode; | ||
class DoublyLinkedList { | ||
class DoublyLinkedList extends base_1.IterableElementBase { | ||
/** | ||
@@ -30,2 +31,3 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
constructor(elements) { | ||
super(); | ||
this._head = undefined; | ||
@@ -672,50 +674,27 @@ this._tail = undefined; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
*[Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* 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 `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the DoublyLinkedList class. | ||
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element, returning only the elements for which the | ||
* callback function returns true. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current | ||
* element, and the DoublyLinkedList itself. The callback function should return a boolean value | ||
* indicating whether the current element should be included | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback) { | ||
filter(callback, thisArg) { | ||
const filteredList = new DoublyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
@@ -732,17 +711,23 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new | ||
* DoublyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element | ||
* in the original list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element, | ||
* and the DoublyLinkedList itself. The callback function should return a value that will be added to | ||
* the new DoublyLinkedList that | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original `DoublyLinkedList` | ||
* object. | ||
*/ | ||
map(callback) { | ||
map(callback, thisArg) { | ||
const mappedList = new DoublyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
mappedList.push(callback.call(thisArg, current, index, this)); | ||
index++; | ||
@@ -756,28 +741,16 @@ } | ||
*/ | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
*_getIterator() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
} | ||
exports.DoublyLinkedList = DoublyLinkedList; |
@@ -0,1 +1,3 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -18,3 +20,3 @@ * data-structure-typed | ||
} | ||
export declare class SinglyLinkedList<E = any> { | ||
export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -349,34 +351,23 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void; | ||
/** | ||
* 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 `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element to determine if it should be included in the | ||
* filtered list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* list. It takes three arguments: the current element, the index of the current element, and the | ||
* list itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list or not | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E>; | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E>; | ||
/** | ||
@@ -387,13 +378,16 @@ * Time 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(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element | ||
* of the original list. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the linked list. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original list. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T>; | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>; | ||
/** | ||
@@ -403,17 +397,4 @@ * Time 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. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T; | ||
print(): void; | ||
protected _getIterator(): IterableIterator<E>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0; | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -23,3 +24,3 @@ * data-structure-typed | ||
exports.SinglyLinkedListNode = SinglyLinkedListNode; | ||
class SinglyLinkedList { | ||
class SinglyLinkedList extends base_1.IterableElementBase { | ||
/** | ||
@@ -29,2 +30,3 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
constructor(elements) { | ||
super(); | ||
this._head = undefined; | ||
@@ -614,50 +616,27 @@ this._tail = undefined; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
*[Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* 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 `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element to determine if it should be included in the | ||
* filtered list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* list. It takes three arguments: the current element, the index of the current element, and the | ||
* list itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list or not | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback) { | ||
filter(callback, thisArg) { | ||
const filteredList = new SinglyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
@@ -674,17 +653,20 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element | ||
* of the original list. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the linked list. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original list. | ||
*/ | ||
map(callback) { | ||
map(callback, thisArg) { | ||
const mappedList = new SinglyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
mappedList.push(callback.call(thisArg, current, index, this)); | ||
index++; | ||
@@ -698,28 +680,13 @@ } | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
*_getIterator() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
} | ||
exports.SinglyLinkedList = SinglyLinkedList; |
@@ -8,3 +8,4 @@ /** | ||
*/ | ||
import { IterableWithSizeOrLength } from "../../types"; | ||
import { ElementCallback, IterableWithSizeOrLength } from "../../types"; | ||
import { IterableElementBase } from "../base"; | ||
/** | ||
@@ -16,3 +17,3 @@ * Deque can provide random access with O(1) time complexity | ||
*/ | ||
export declare class Deque<E> { | ||
export declare class Deque<E> extends IterableElementBase<E> { | ||
protected _bucketFirst: number; | ||
@@ -361,28 +362,2 @@ protected _firstInBucket: number; | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
*/ | ||
forEach(callback: (element: E, index: number, deque: this) => void): void; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -394,10 +369,15 @@ */ | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Deque` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `filter` function creates a new deque containing elements from the original deque that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the deque itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* deque or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Deque` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E>; | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E>; | ||
/** | ||
@@ -411,12 +391,17 @@ * Time Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Deque` object with the transformed elements. | ||
* The `map` function creates a new Deque by applying a callback function to each element of the | ||
* original Deque. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the deque. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns a new Deque object with the mapped values. | ||
*/ | ||
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>; | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
print(): void; | ||
/** | ||
@@ -426,13 +411,6 @@ * Time Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over the elements of a deque and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The `callback` parameter is a function that takes four arguments: | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the deque. | ||
* @returns the final value of the accumulator after iterating over all elements in the deque and | ||
* applying the callback function to each element. | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T; | ||
print(): void; | ||
protected _getIterator(): Generator<E, void, unknown>; | ||
/** | ||
@@ -439,0 +417,0 @@ * Time Complexity: O(n) |
@@ -12,2 +12,3 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -19,3 +20,3 @@ * Deque can provide random access with O(1) time complexity | ||
*/ | ||
class Deque { | ||
class Deque extends base_1.IterableElementBase { | ||
/** | ||
@@ -31,2 +32,3 @@ * The constructor initializes a data structure with a specified bucket size and populates it with | ||
constructor(elements = [], bucketSize = (1 << 12)) { | ||
super(); | ||
this._bucketFirst = 0; | ||
@@ -659,38 +661,2 @@ this._firstInBucket = 0; | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (let i = 0; i < this.size; ++i) { | ||
yield this.getAt(i); | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -702,14 +668,19 @@ */ | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Deque` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `filter` function creates a new deque containing elements from the original deque that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the deque itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* deque or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Deque` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate) { | ||
filter(predicate, thisArg) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newDeque.push(el); | ||
@@ -729,12 +700,16 @@ } | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Deque` object with the transformed elements. | ||
* The `map` function creates a new Deque by applying a callback function to each element of the | ||
* original Deque. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the deque. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns a new Deque object with the mapped values. | ||
*/ | ||
map(callback) { | ||
map(callback, thisArg) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
newDeque.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -746,4 +721,7 @@ } | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
@@ -753,23 +731,10 @@ * Time Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over the elements of a deque and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The `callback` parameter is a function that takes four arguments: | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the deque. | ||
* @returns the final value of the accumulator after iterating over all elements in the deque and | ||
* applying the callback function to each element. | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
*_getIterator() { | ||
for (let i = 0; i < this.size; ++i) { | ||
yield this.getAt(i); | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
@@ -776,0 +741,0 @@ * Time Complexity: O(n) |
@@ -7,26 +7,6 @@ /** | ||
import { SinglyLinkedList } from '../linked-list'; | ||
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> { | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
export declare class Queue<E = any> extends IterableElementBase<E> { | ||
/** | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
enqueue(value: E): void; | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue(): E | undefined; | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst(): E | undefined; | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek(): E | undefined; | ||
} | ||
export declare class Queue<E = any> { | ||
/** | ||
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0. | ||
@@ -210,17 +190,23 @@ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
print(): void; | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
* The `filter` function creates a new `Queue` object containing elements from the original `Queue` | ||
* that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the queue itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* queue or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Queue` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
forEach(callback: (element: E, index: number, queue: this) => void): void; | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E>; | ||
/** | ||
@@ -234,10 +220,13 @@ * Time Complexity: O(n) | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Queue` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `map` function takes a callback function and applies it to each element in the queue, | ||
* returning a new queue with the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* queue. It takes three arguments: the current element, the index of the current element, and the | ||
* queue itself. The callback function should return a new value that will be added to the new queue. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `Queue` object with the transformed elements. | ||
*/ | ||
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>; | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T>; | ||
/** | ||
@@ -247,13 +236,25 @@ * Time Complexity: O(n) | ||
*/ | ||
protected _getIterator(): Generator<E, void, unknown>; | ||
} | ||
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Queue` object with the transformed elements. | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>; | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T; | ||
enqueue(value: E): void; | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue(): E | undefined; | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst(): E | undefined; | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek(): E | undefined; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Queue = exports.LinkedListQueue = void 0; | ||
exports.LinkedListQueue = exports.Queue = void 0; | ||
/** | ||
@@ -10,36 +10,5 @@ * @license MIT | ||
const linked_list_1 = require("../linked-list"); | ||
class LinkedListQueue extends linked_list_1.SinglyLinkedList { | ||
const base_1 = require("../base"); | ||
class Queue extends base_1.IterableElementBase { | ||
/** | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
enqueue(value) { | ||
this.push(value); | ||
} | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue() { | ||
return this.shift(); | ||
} | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst() { | ||
var _a; | ||
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value; | ||
} | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek() { | ||
return this.getFirst(); | ||
} | ||
} | ||
exports.LinkedListQueue = LinkedListQueue; | ||
class Queue { | ||
/** | ||
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0. | ||
@@ -51,2 +20,3 @@ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
constructor(elements) { | ||
super(); | ||
this._nodes = elements || []; | ||
@@ -273,29 +243,4 @@ this._offset = 0; | ||
} | ||
*[Symbol.iterator]() { | ||
for (const item of this.nodes) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -307,14 +252,19 @@ */ | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Queue` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `filter` function creates a new `Queue` object containing elements from the original `Queue` | ||
* that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the queue itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* queue or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Queue` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate) { | ||
filter(predicate, thisArg) { | ||
const newDeque = new Queue([]); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newDeque.push(el); | ||
@@ -334,12 +284,17 @@ } | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Queue` object with the transformed elements. | ||
* The `map` function takes a callback function and applies it to each element in the queue, | ||
* returning a new queue with the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* queue. It takes three arguments: the current element, the index of the current element, and the | ||
* queue itself. The callback function should return a new value that will be added to the new queue. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `Queue` object with the transformed elements. | ||
*/ | ||
map(callback) { | ||
map(callback, thisArg) { | ||
const newDeque = new Queue([]); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
newDeque.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -349,12 +304,44 @@ } | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
*_getIterator() { | ||
for (const item of this.nodes) { | ||
yield item; | ||
} | ||
return accumulator; | ||
} | ||
} | ||
exports.Queue = Queue; | ||
class LinkedListQueue extends linked_list_1.SinglyLinkedList { | ||
/** | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
enqueue(value) { | ||
this.push(value); | ||
} | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue() { | ||
return this.shift(); | ||
} | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst() { | ||
var _a; | ||
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value; | ||
} | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek() { | ||
return this.getFirst(); | ||
} | ||
} | ||
exports.LinkedListQueue = LinkedListQueue; |
@@ -0,1 +1,3 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -6,3 +8,3 @@ * @license MIT | ||
*/ | ||
export declare class Stack<E = any> { | ||
export declare class Stack<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -108,15 +110,46 @@ * The constructor initializes an array of elements, which can be provided as an optional parameter. | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* Applies a function to each element of the stack. | ||
* @param {function(E): void} callback - A function to apply to each element. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new stack containing elements from the original stack that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the stack itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* stack or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Stack` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
forEach(callback: (element: E, index: number, stack: this) => void): void; | ||
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>; | ||
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>; | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T; | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the stack, | ||
* returning a new stack with the results. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the stack. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` method is returning a new `Stack` object. | ||
*/ | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>; | ||
print(): void; | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
protected _getIterator(): Generator<E, void, unknown>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Stack = void 0; | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -9,3 +10,3 @@ * @license MIT | ||
*/ | ||
class Stack { | ||
class Stack extends base_1.IterableElementBase { | ||
/** | ||
@@ -18,2 +19,3 @@ * The constructor initializes an array of elements, which can be provided as an optional parameter. | ||
constructor(elements) { | ||
super(); | ||
this._elements = []; | ||
@@ -143,26 +145,26 @@ if (elements) { | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (let i = 0; i < this.elements.length; i++) { | ||
yield this.elements[i]; | ||
} | ||
} | ||
/** | ||
* Applies a function to each element of the stack. | ||
* @param {function(E): void} callback - A function to apply to each element. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new stack containing elements from the original stack that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the stack itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* stack or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Stack` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
filter(predicate, thisArg) { | ||
const newStack = new Stack(); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newStack.push(el); | ||
@@ -174,7 +176,24 @@ } | ||
} | ||
map(callback) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the stack, | ||
* returning a new stack with the results. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the stack. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` method is returning a new `Stack` object. | ||
*/ | ||
map(callback, thisArg) { | ||
const newStack = new Stack(); | ||
let index = 0; | ||
for (const el of this) { | ||
newStack.push(callback(el, index, this)); | ||
newStack.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -184,15 +203,15 @@ } | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
*_getIterator() { | ||
for (let i = 0; i < this.elements.length; i++) { | ||
yield this.elements[i]; | ||
} | ||
} | ||
} | ||
exports.Stack = Stack; |
@@ -8,2 +8,4 @@ /** | ||
*/ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -22,3 +24,3 @@ * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
*/ | ||
export declare class Trie { | ||
export declare class Trie extends IterableElementBase<string> { | ||
constructor(words?: string[], caseSensitive?: boolean); | ||
@@ -147,8 +149,41 @@ protected _size: number; | ||
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[]; | ||
[Symbol.iterator](): IterableIterator<string>; | ||
forEach(callback: (word: string, index: number, trie: this) => void): void; | ||
filter(predicate: (word: string, index: number, trie: this) => boolean): string[]; | ||
map(callback: (word: string, index: number, trie: this) => string): Trie; | ||
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function takes a predicate function and returns a new array containing all the | ||
* elements for which the predicate function returns true. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current | ||
* element should be included in the filtered results or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method is returning an array of strings (`string[]`). | ||
*/ | ||
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new Trie by applying a callback function to each element in the Trie. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* Trie. It takes three arguments: the current element in the Trie, the index of the current element, | ||
* and the Trie itself. The callback function should return a new value for the element. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new Trie object. | ||
*/ | ||
map(callback: ElementCallback<string, string>, thisArg?: any): Trie; | ||
print(): void; | ||
protected _getIterator(): IterableIterator<string>; | ||
/** | ||
@@ -155,0 +190,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
@@ -11,2 +11,3 @@ "use strict"; | ||
exports.Trie = exports.TrieNode = void 0; | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -27,4 +28,5 @@ * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
*/ | ||
class Trie { | ||
class Trie extends base_1.IterableElementBase { | ||
constructor(words, caseSensitive = true) { | ||
super(); | ||
this._root = new TrieNode(''); | ||
@@ -322,25 +324,25 @@ this._caseSensitive = caseSensitive; | ||
} | ||
*[Symbol.iterator]() { | ||
function* _dfs(node, path) { | ||
if (node.isEnd) { | ||
yield path; | ||
} | ||
for (const [char, childNode] of node.children) { | ||
yield* _dfs(childNode, path + char); | ||
} | ||
} | ||
yield* _dfs(this.root, ''); | ||
} | ||
forEach(callback) { | ||
let index = 0; | ||
for (const word of this) { | ||
callback(word, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function takes a predicate function and returns a new array containing all the | ||
* elements for which the predicate function returns true. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current | ||
* element should be included in the filtered results or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method is returning an array of strings (`string[]`). | ||
*/ | ||
filter(predicate, thisArg) { | ||
const results = []; | ||
let index = 0; | ||
for (const word of this) { | ||
if (predicate(word, index, this)) { | ||
if (predicate.call(thisArg, word, index, this)) { | ||
results.push(word); | ||
@@ -352,7 +354,24 @@ } | ||
} | ||
map(callback) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new Trie by applying a callback function to each element in the Trie. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* Trie. It takes three arguments: the current element in the Trie, the index of the current element, | ||
* and the Trie itself. The callback function should return a new value for the element. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new Trie object. | ||
*/ | ||
map(callback, thisArg) { | ||
const newTrie = new Trie(); | ||
let index = 0; | ||
for (const word of this) { | ||
newTrie.add(callback(word, index, this)); | ||
newTrie.add(callback.call(thisArg, word, index, this)); | ||
index++; | ||
@@ -362,14 +381,16 @@ } | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const word of this) { | ||
accumulator = callback(accumulator, word, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this]); | ||
} | ||
*_getIterator() { | ||
function* _dfs(node, path) { | ||
if (node.isEnd) { | ||
yield path; | ||
} | ||
for (const [char, childNode] of node.children) { | ||
yield* _dfs(childNode, path + char); | ||
} | ||
} | ||
yield* _dfs(this.root, ''); | ||
} | ||
/** | ||
@@ -376,0 +397,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
@@ -12,1 +12,2 @@ export * from './binary-tree'; | ||
export * from './trie'; | ||
export * from './base'; |
@@ -28,1 +28,2 @@ "use strict"; | ||
__exportStar(require("./trie"), exports); | ||
__exportStar(require("./base"), exports); |
{ | ||
"name": "red-black-tree-typed", | ||
"version": "1.48.1", | ||
"version": "1.48.2", | ||
"description": "RedBlackTree. Javascript & Typescript Data Structure.", | ||
@@ -145,4 +145,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.48.1" | ||
"data-structure-typed": "^1.48.2" | ||
} | ||
} |
@@ -11,4 +11,6 @@ /** | ||
import type { DijkstraResult, VertexKey } from '../../types'; | ||
import { PairCallback } from "../../types"; | ||
import { IGraph } from '../../interfaces'; | ||
import { Queue } from '../queue'; | ||
import { IterablePairBase } from "../base"; | ||
@@ -68,3 +70,7 @@ export abstract class AbstractVertex<V = any> { | ||
EO extends AbstractEdge<E> = AbstractEdge<E> | ||
> implements IGraph<V, E, VO, EO> { | ||
> extends IterablePairBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> { | ||
constructor() { | ||
super(); | ||
} | ||
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>(); | ||
@@ -1164,22 +1170,29 @@ | ||
* [Symbol.iterator](): Iterator<[VertexKey, V | undefined]> { | ||
for (const vertex of this._vertices.values()) { | ||
yield [vertex.key, vertex.value]; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void { | ||
let index = 0; | ||
for (const vertex of this) { | ||
callback(vertex, index, this._vertices); | ||
index++; | ||
} | ||
} | ||
filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][] { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function iterates over key-value pairs in a data structure and returns an array of | ||
* pairs that satisfy a given predicate. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included | ||
* in the filtered array. The callback function should return `true` if the element should be | ||
* included, and ` | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]` | ||
* that satisfy the given predicate function. | ||
*/ | ||
filter(predicate: PairCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][] { | ||
const filtered: [VertexKey, V | undefined][] = []; | ||
let index = 0; | ||
for (const entry of this) { | ||
if (predicate(entry, index, this._vertices)) { | ||
filtered.push(entry); | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index, this)) { | ||
filtered.push([key, value]); | ||
} | ||
@@ -1191,7 +1204,25 @@ index++; | ||
map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[] { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function iterates over the elements of a collection and applies a callback function to | ||
* each element, returning an array of the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* map. It takes four arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be | ||
* used as the `this` value when calling the callback function. If `thisArg` is not provided, ` | ||
* @returns The `map` function is returning an array of type `T[]`. | ||
*/ | ||
map<T>(callback: PairCallback<VertexKey, V | undefined, T>, thisArg?: any): T[] { | ||
const mapped: T[] = []; | ||
let index = 0; | ||
for (const entry of this) { | ||
mapped.push(callback(entry, index, this._vertices)); | ||
for (const [key, value] of this) { | ||
mapped.push(callback.call(thisArg, value, key, index, this)); | ||
index++; | ||
@@ -1202,10 +1233,6 @@ } | ||
reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T { | ||
let accumulator: T = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this._vertices); | ||
index++; | ||
protected* _getIterator(): IterableIterator<[VertexKey, V | undefined]> { | ||
for (const vertex of this._vertices.values()) { | ||
yield [vertex.key, vertex.value]; | ||
} | ||
return accumulator; | ||
} | ||
@@ -1212,0 +1239,0 @@ |
@@ -10,5 +10,6 @@ /** | ||
import { isWeakKey, rangeCheck } from '../../utils'; | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem, PairCallback } from '../../types'; | ||
import { IterablePairBase } from "../base"; | ||
export class HashMap<K = any, V = any> { | ||
export class HashMap<K = any, V = any> extends IterablePairBase<K, V> { | ||
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {}; | ||
@@ -28,2 +29,3 @@ protected _objMap: Map<object, V> = new Map(); | ||
}) { | ||
super(); | ||
if (options) { | ||
@@ -150,98 +152,10 @@ const { hashFn } = options; | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
* [Symbol.iterator](): IterableIterator<[K, V]> { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value] as [K, V]; | ||
} | ||
for (const node of this._objMap) { | ||
yield node as [K, V]; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
* entries(): IterableIterator<[K, V]> { | ||
for (const item of this) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
* keys(): IterableIterator<K> { | ||
for (const [key] of this) { | ||
yield key; | ||
} | ||
} | ||
* values(): IterableIterator<V> { | ||
for (const [, value] of this) { | ||
yield value; | ||
} | ||
} | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (!predicate.call(thisArg, value, key, index++, this)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @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 inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
callbackfn.call(thisArg, value, key, index++, this); | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
@@ -257,3 +171,3 @@ * key-value pair in the original HashMap. | ||
*/ | ||
map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U> { | ||
map<U>(callbackfn: PairCallback<K, V, U>, thisArg?: any): HashMap<K, U> { | ||
const resultMap = new HashMap<K, U>(); | ||
@@ -268,2 +182,10 @@ let index = 0; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
@@ -281,3 +203,3 @@ * that satisfy a given predicate function. | ||
*/ | ||
filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V> { | ||
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): HashMap<K, V> { | ||
const filteredMap = new HashMap<K, V>(); | ||
@@ -293,26 +215,19 @@ let index = 0; | ||
print(): void { | ||
console.log([...this.entries()]); | ||
} | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
accumulator = callbackfn(accumulator, value, key, index++, this); | ||
protected* _getIterator(): IterableIterator<[K, V]> { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value] as [K, V]; | ||
} | ||
return accumulator; | ||
for (const node of this._objMap) { | ||
yield node as [K, V]; | ||
} | ||
} | ||
print(): void{ | ||
console.log([...this.entries()]); | ||
} | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
@@ -343,3 +258,3 @@ | ||
export class LinkedHashMap<K = any, V = any> { | ||
export class LinkedHashMap<K = any, V = any> extends IterablePairBase<K, V> { | ||
@@ -360,2 +275,3 @@ protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {}; | ||
}) { | ||
super(); | ||
this._sentinel = <HashMapLinkedNode<K, V>>{}; | ||
@@ -504,14 +420,2 @@ this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel; | ||
keys(): K[] { | ||
const keys: K[] = []; | ||
for (const [key] of this) keys.push(key); | ||
return keys; | ||
} | ||
values(): V[] { | ||
const values: V[] = []; | ||
for (const [, value] of this) values.push(value); | ||
return values; | ||
} | ||
/** | ||
@@ -657,32 +561,26 @@ * Time Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* LinkedHashMap. It takes three arguments: | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void) { | ||
let index = 0; | ||
let node = this._head; | ||
while (node !== this._sentinel) { | ||
callback(<[K, V]>[node.key, node.value], index++, this); | ||
node = node.next; | ||
} | ||
} | ||
/** | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* map that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes four arguments: | ||
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the | ||
* current element should be included in the filtered map or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this | ||
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original | ||
* `LinkedHashMap` object that satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V> { | ||
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V> { | ||
const filteredMap = new LinkedHashMap<K, V>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], index, this)) { | ||
if (predicate.call(thisArg, value, key, index, this)) { | ||
filteredMap.set(key, value); | ||
@@ -696,13 +594,28 @@ } | ||
/** | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV> { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
* each key-value pair in the original map. | ||
* @param callback - The callback parameter is a function that will be called for each key-value pair | ||
* in the map. It takes four arguments: the value of the current key-value pair, the key of the | ||
* current key-value pair, the index of the current key-value pair, and the map itself. The callback | ||
* function should | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. If provided, the callback function will | ||
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current | ||
* map | ||
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback | ||
* function. | ||
*/ | ||
map<NV>(callback: PairCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV> { | ||
const mappedMap = new LinkedHashMap<K, NV>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
const newValue = callback([key, value], index, this); | ||
const newValue = callback.call(thisArg, value, key, index, this); | ||
mappedMap.set(key, newValue); | ||
@@ -714,22 +627,4 @@ index++; | ||
/** | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the map. | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
print() { | ||
console.log([...this]); | ||
} | ||
@@ -743,3 +638,3 @@ | ||
*/ | ||
* [Symbol.iterator]() { | ||
protected* _getIterator() { | ||
let node = this._head; | ||
@@ -752,6 +647,2 @@ while (node !== this._sentinel) { | ||
print() { | ||
console.log([...this]); | ||
} | ||
/** | ||
@@ -758,0 +649,0 @@ * Time Complexity: O(1) |
@@ -8,9 +8,11 @@ /** | ||
import type { Comparator, DFSOrderPattern } from '../../types'; | ||
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types'; | ||
import { HeapOptions } from "../../types"; | ||
import { IterableElementBase } from "../base"; | ||
export class Heap<E = any> { | ||
export class Heap<E = any> extends IterableElementBase<E> { | ||
options: HeapOptions<E>; | ||
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) { | ||
super(); | ||
const defaultComparator = (a: E, b: E) => { | ||
@@ -343,34 +345,66 @@ if (!(typeof a === 'number' && typeof b === 'number')) { | ||
* [Symbol.iterator]() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
forEach(callback: (element: E, index: number, heap: this) => void): void { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new Heap object containing elements that pass a given callback | ||
* function. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the heap. It takes three arguments: the current element, the index of the current element, and the | ||
* heap itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass | ||
* the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E> { | ||
const filteredList = new Heap<E>(); | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E> { | ||
const filteredHeap: Heap<E> = new Heap<E>([], this.options); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
filteredHeap.push(el); | ||
for (const current of this) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
index++; | ||
} | ||
return filteredHeap; | ||
return filteredList; | ||
} | ||
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T> { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new heap by applying a callback function to each element of the | ||
* original heap. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* original heap. It takes three arguments: the current element, the index of the current element, | ||
* and the original heap itself. The callback function should return a value of type T, which will be | ||
* added to the mapped heap. | ||
* @param comparator - The `comparator` parameter is a function that is used to compare elements in | ||
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than | ||
* `b`, a positive number if `a` is greater than `b`, or | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the callback function. It is used when you want to bind a | ||
* specific object as the context for the callback function. If `thisArg` is not provided, | ||
* `undefined` is used as | ||
* @returns a new instance of the Heap class, which is created using the mapped elements from the | ||
* original Heap. | ||
*/ | ||
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> { | ||
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator }); | ||
let index = 0; | ||
for (const el of this) { | ||
mappedHeap.add(callback(el, index, this)); | ||
mappedHeap.add(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -381,15 +415,2 @@ } | ||
reduce<T>( | ||
callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, | ||
initialValue: T | ||
): T { | ||
let accumulator: T = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -404,2 +425,8 @@ * Time Complexity: O(log n) | ||
protected* _getIterator() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
/** | ||
@@ -406,0 +433,0 @@ * Time Complexity: O(n) |
@@ -12,1 +12,2 @@ export * from './hash'; | ||
export * from './trie'; | ||
export * from './base'; |
@@ -0,1 +1,4 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -25,3 +28,3 @@ * data-structure-typed | ||
export class DoublyLinkedList<E = any> { | ||
export class DoublyLinkedList<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -31,2 +34,3 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
constructor(elements?: Iterable<E>) { | ||
super(); | ||
this._head = undefined; | ||
@@ -729,37 +733,3 @@ this._tail = undefined; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -769,16 +739,23 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the DoublyLinkedList class. | ||
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element, returning only the elements for which the | ||
* callback function returns true. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current | ||
* element, and the DoublyLinkedList itself. The callback function should return a boolean value | ||
* indicating whether the current element should be included | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E> { | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> { | ||
const filteredList = new DoublyLinkedList<E>(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
@@ -797,17 +774,23 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new | ||
* DoublyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element | ||
* in the original list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element, | ||
* and the DoublyLinkedList itself. The callback function should return a value that will be added to | ||
* the new DoublyLinkedList that | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original `DoublyLinkedList` | ||
* object. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T> { | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> { | ||
const mappedList = new DoublyLinkedList<T>(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
mappedList.push(callback.call(thisArg, current, index, this)); | ||
index++; | ||
@@ -824,29 +807,17 @@ } | ||
print(): void { | ||
console.log([...this]); | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
protected* _getIterator(): IterableIterator<E> { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
return accumulator; | ||
} | ||
print(): void { | ||
console.log([...this]); | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -23,3 +26,3 @@ * data-structure-typed | ||
export class SinglyLinkedList<E = any> { | ||
export class SinglyLinkedList<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -29,2 +32,3 @@ * The constructor initializes the linked list with an empty head, tail, and length. | ||
constructor(elements?: Iterable<E>) { | ||
super(); | ||
this._head = undefined; | ||
@@ -675,37 +679,3 @@ this._tail = undefined; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
/** | ||
* 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. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -715,16 +685,23 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current | ||
* list and applying a callback function to each element to determine if it should be included in the | ||
* filtered list. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* list. It takes three arguments: the current element, the index of the current element, and the | ||
* list itself. The callback function should return a boolean value indicating whether the current | ||
* element should be included in the filtered list or not | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the | ||
* elements that pass the filter condition specified by the `callback` function. | ||
*/ | ||
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E> { | ||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> { | ||
const filteredList = new SinglyLinkedList<E>(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
if (callback.call(thisArg, current, index, this)) { | ||
filteredList.push(current); | ||
@@ -737,2 +714,3 @@ } | ||
/** | ||
@@ -742,19 +720,21 @@ * Time 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(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values. | ||
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element | ||
* of the original list. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the linked list. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results | ||
* of applying the provided `callback` function to each element in the original list. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T> { | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> { | ||
const mappedList = new SinglyLinkedList<T>(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
mappedList.push(callback.call(thisArg, current, index, this)); | ||
index++; | ||
@@ -771,29 +751,14 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print(): void { | ||
console.log([...this]); | ||
} | ||
protected* _getIterator(): IterableIterator<E> { | ||
let current = this.head; | ||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
} |
@@ -10,4 +10,5 @@ /** | ||
import { IterableWithSizeOrLength } from "../../types"; | ||
import { ElementCallback, IterableWithSizeOrLength } from "../../types"; | ||
import { calcMinUnitsRequired, rangeCheck } from "../../utils"; | ||
import { IterableElementBase } from "../base"; | ||
@@ -21,3 +22,3 @@ /** | ||
export class Deque<E> { | ||
export class Deque<E> extends IterableElementBase<E> { | ||
protected _bucketFirst = 0; | ||
@@ -40,3 +41,3 @@ protected _firstInBucket = 0; | ||
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) { | ||
super(); | ||
let _size: number; | ||
@@ -707,45 +708,4 @@ if ('length' in elements) { | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
* [Symbol.iterator]() { | ||
for (let i = 0; i < this.size; ++i) { | ||
yield this.getAt(i); | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
*/ | ||
forEach(callback: (element: E, index: number, deque: this) => void) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
@@ -755,14 +715,19 @@ * Time Complexity: O(n) | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Deque` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `filter` function creates a new deque containing elements from the original deque that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the deque itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* deque or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Deque` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E> { | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> { | ||
const newDeque = new Deque<E>([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newDeque.push(el); | ||
@@ -779,3 +744,2 @@ } | ||
*/ | ||
/** | ||
@@ -785,12 +749,16 @@ * Time Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Deque` object with the transformed elements. | ||
* The `map` function creates a new Deque by applying a callback function to each element of the | ||
* original Deque. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the deque. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns a new Deque object with the mapped values. | ||
*/ | ||
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T> { | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> { | ||
const newDeque = new Deque<T>([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
newDeque.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -803,5 +771,9 @@ } | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
print(): void { | ||
console.log([...this]) | ||
} | ||
/** | ||
@@ -811,25 +783,11 @@ * Time Complexity: O(n) | ||
* | ||
* The `reduce` function iterates over the elements of a deque and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The `callback` parameter is a function that takes four arguments: | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* is the value that will be passed as the first argument to the `callback` function when reducing | ||
* the elements of the deque. | ||
* @returns the final value of the accumulator after iterating over all elements in the deque and | ||
* applying the callback function to each element. | ||
* The above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
protected* _getIterator() { | ||
for (let i = 0; i < this.size; ++i) { | ||
yield this.getAt(i); | ||
} | ||
return accumulator; | ||
} | ||
print(): void { | ||
console.log([...this]) | ||
} | ||
/** | ||
@@ -836,0 +794,0 @@ * Time Complexity: O(n) |
@@ -7,39 +7,7 @@ /** | ||
import { SinglyLinkedList } from '../linked-list'; | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> { | ||
export class Queue<E = any> extends IterableElementBase<E> { | ||
/** | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
enqueue(value: E) { | ||
this.push(value); | ||
} | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue(): E | undefined { | ||
return this.shift(); | ||
} | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst(): E | undefined { | ||
return this.head?.value; | ||
} | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek(): E | undefined { | ||
return this.getFirst(); | ||
} | ||
} | ||
export class Queue<E = any> { | ||
/** | ||
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0. | ||
@@ -51,2 +19,3 @@ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
constructor(elements?: E[]) { | ||
super(); | ||
this._nodes = elements || []; | ||
@@ -309,11 +278,5 @@ this._offset = 0; | ||
* [Symbol.iterator]() { | ||
for (const item of this.nodes) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -323,15 +286,26 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
* The `forEach` function iterates over each element in a deque and applies a callback function to | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* deque. It takes three parameters: | ||
* The `filter` function creates a new `Queue` object containing elements from the original `Queue` | ||
* that satisfy a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the queue itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* queue or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Queue` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
forEach(callback: (element: E, index: number, queue: this) => void) { | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E> { | ||
const newDeque = new Queue<E>([]); | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
index++; | ||
} | ||
return newDeque; | ||
} | ||
@@ -343,3 +317,2 @@ | ||
*/ | ||
/** | ||
@@ -349,16 +322,17 @@ * Time Complexity: O(n) | ||
* | ||
* The `filter` function creates a new deque containing only the elements that satisfy the given | ||
* predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`, | ||
* `index`, and `deque`. | ||
* @returns The `filter` method is returning a new `Queue` object that contains only the elements | ||
* that satisfy the given `predicate` function. | ||
* The `map` function takes a callback function and applies it to each element in the queue, | ||
* returning a new queue with the results. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* queue. It takes three arguments: the current element, the index of the current element, and the | ||
* queue itself. The callback function should return a new value that will be added to the new queue. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new `Queue` object with the transformed elements. | ||
*/ | ||
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E> { | ||
const newDeque = new Queue<E>([]); | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T> { | ||
const newDeque = new Queue<T>([]); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
newDeque.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -374,30 +348,41 @@ } | ||
protected* _getIterator() { | ||
for (const item of this.nodes) { | ||
yield item; | ||
} | ||
} | ||
} | ||
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the deque, | ||
* returning a new deque with the results. | ||
* @param callback - The `callback` parameter is a function that takes three arguments: | ||
* @returns The `map` method is returning a new `Queue` object with the transformed elements. | ||
* The enqueue function adds a value to the end of an array. | ||
* @param {E} value - The value parameter represents the value that you want to add to the queue. | ||
*/ | ||
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T> { | ||
const newDeque = new Queue<T>([]); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return newDeque; | ||
enqueue(value: E) { | ||
this.push(value); | ||
} | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
/** | ||
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty. | ||
*/ | ||
dequeue(): E | undefined { | ||
return this.shift(); | ||
} | ||
} | ||
/** | ||
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
getFirst(): E | undefined { | ||
return this.head?.value; | ||
} | ||
/** | ||
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. | ||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. | ||
*/ | ||
peek(): E | undefined { | ||
return this.getFirst(); | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -6,3 +9,3 @@ * @license MIT | ||
*/ | ||
export class Stack<E = any> { | ||
export class Stack<E = any> extends IterableElementBase<E> { | ||
/** | ||
@@ -15,2 +18,3 @@ * The constructor initializes an array of elements, which can be provided as an optional parameter. | ||
constructor(elements?: Iterable<E>) { | ||
super(); | ||
this._elements = []; | ||
@@ -159,29 +163,27 @@ if (elements) { | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
* [Symbol.iterator]() { | ||
for (let i = 0; i < this.elements.length; i++) { | ||
yield this.elements[i]; | ||
} | ||
} | ||
/** | ||
* Applies a function to each element of the stack. | ||
* @param {function(E): void} callback - A function to apply to each element. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function creates a new stack containing elements from the original stack that satisfy | ||
* a given predicate function. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* the current element being iterated over, the index of the current element, and the stack itself. | ||
* It should return a boolean value indicating whether the element should be included in the filtered | ||
* stack or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `Stack` object that contains the elements that | ||
* satisfy the given predicate function. | ||
*/ | ||
forEach(callback: (element: E, index: number, stack: this) => void): void { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E> { | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E> { | ||
const newStack = new Stack<E>(); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
if (predicate.call(thisArg, el, index, this)) { | ||
newStack.push(el); | ||
@@ -194,8 +196,25 @@ } | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T> { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function takes a callback function and applies it to each element in the stack, | ||
* returning a new stack with the results. | ||
* @param callback - The `callback` parameter is a function that will be called for each element in | ||
* the stack. It takes three arguments: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` method is returning a new `Stack` object. | ||
*/ | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T> { | ||
const newStack = new Stack<T>(); | ||
let index = 0; | ||
for (const el of this) { | ||
newStack.push(callback(el, index, this)); | ||
newStack.push(callback.call(thisArg, el, index, this)); | ||
index++; | ||
@@ -206,15 +225,15 @@ } | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print(): void { | ||
console.log([...this]); | ||
} | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
protected* _getIterator() { | ||
for (let i = 0; i < this.elements.length; i++) { | ||
yield this.elements[i]; | ||
} | ||
} | ||
} |
@@ -9,2 +9,5 @@ /** | ||
import { IterableElementBase } from "../base"; | ||
import { ElementCallback } from "../../types"; | ||
/** | ||
@@ -29,4 +32,5 @@ * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
*/ | ||
export class Trie { | ||
export class Trie extends IterableElementBase<string> { | ||
constructor(words?: string[], caseSensitive = true) { | ||
super(); | ||
this._root = new TrieNode(''); | ||
@@ -344,28 +348,26 @@ this._caseSensitive = caseSensitive; | ||
* [Symbol.iterator](): IterableIterator<string> { | ||
function* _dfs(node: TrieNode, path: string): IterableIterator<string> { | ||
if (node.isEnd) { | ||
yield path; | ||
} | ||
for (const [char, childNode] of node.children) { | ||
yield* _dfs(childNode, path + char); | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
yield* _dfs(this.root, ''); | ||
} | ||
forEach(callback: (word: string, index: number, trie: this) => void): void { | ||
let index = 0; | ||
for (const word of this) { | ||
callback(word, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate: (word: string, index: number, trie: this) => boolean): string[] { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `filter` function takes a predicate function and returns a new array containing all the | ||
* elements for which the predicate function returns true. | ||
* @param predicate - The `predicate` parameter is a callback function that takes three arguments: | ||
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current | ||
* element should be included in the filtered results or not. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to | ||
* specify the value of `this` within the `predicate` function. It is used when you want to bind a | ||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be | ||
* @returns The `filter` method is returning an array of strings (`string[]`). | ||
*/ | ||
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[] { | ||
const results: string[] = []; | ||
let index = 0; | ||
for (const word of this) { | ||
if (predicate(word, index, this)) { | ||
if (predicate.call(thisArg, word, index, this)) { | ||
results.push(word); | ||
@@ -378,7 +380,25 @@ } | ||
map(callback: (word: string, index: number, trie: this) => string): Trie { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function creates a new Trie by applying a callback function to each element in the Trie. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* Trie. It takes three arguments: the current element in the Trie, the index of the current element, | ||
* and the Trie itself. The callback function should return a new value for the element. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `callback` function. If `thisArg` is | ||
* @returns The `map` function is returning a new Trie object. | ||
*/ | ||
map(callback: ElementCallback<string, string>, thisArg?: any): Trie { | ||
const newTrie = new Trie(); | ||
let index = 0; | ||
for (const word of this) { | ||
newTrie.add(callback(word, index, this)); | ||
newTrie.add(callback.call(thisArg, word, index, this)); | ||
index++; | ||
@@ -389,12 +409,2 @@ } | ||
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const word of this) { | ||
accumulator = callback(accumulator, word, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
@@ -404,2 +414,15 @@ console.log([...this]); | ||
protected* _getIterator(): IterableIterator<string> { | ||
function* _dfs(node: TrieNode, path: string): IterableIterator<string> { | ||
if (node.isEnd) { | ||
yield path; | ||
} | ||
for (const [char, childNode] of node.children) { | ||
yield* _dfs(childNode, path + char); | ||
} | ||
} | ||
yield* _dfs(this.root, ''); | ||
} | ||
/** | ||
@@ -406,0 +429,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
@@ -12,1 +12,2 @@ export * from './binary-tree'; | ||
export * from './trie'; | ||
export * from './base'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1988173
353
35858
Updateddata-structure-typed@^1.48.2