heap-typed
Advanced tools
Comparing version 1.47.4 to 1.47.5
@@ -146,3 +146,3 @@ /** | ||
*/ | ||
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V>; | ||
filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V>; | ||
/** | ||
@@ -155,3 +155,3 @@ * The `map` function takes a callback function and returns a new HashMap with the values transformed | ||
*/ | ||
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV>; | ||
map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV>; | ||
/** | ||
@@ -169,3 +169,3 @@ * The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A; | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A; | ||
/** | ||
@@ -172,0 +172,0 @@ * Time Complexity: O(n), where n is the number of elements in the HashMap. |
@@ -290,6 +290,8 @@ "use strict"; | ||
const filteredMap = new HashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], this)) { | ||
if (predicate([key, value], index, this)) { | ||
filteredMap.set(key, value); | ||
} | ||
index++; | ||
} | ||
@@ -307,5 +309,7 @@ return filteredMap; | ||
const mappedMap = new HashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
const newValue = callback([key, value], this); | ||
const newValue = callback([key, value], index, this); | ||
mappedMap.set(key, newValue); | ||
index++; | ||
} | ||
@@ -328,4 +332,6 @@ return mappedMap; | ||
let accumulator = initialValue; | ||
for (const element of this) { | ||
accumulator = callback(accumulator, element, this); | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
@@ -332,0 +338,0 @@ return accumulator; |
@@ -11,7 +11,7 @@ /** | ||
value: V; | ||
next: HashTableNode<K, V> | null; | ||
next: HashTableNode<K, V> | undefined; | ||
constructor(key: K, value: V); | ||
} | ||
import { HashFunction } from '../../types'; | ||
export declare class HashTable<K, V> { | ||
export declare class HashTable<K = any, V = any> { | ||
protected static readonly DEFAULT_CAPACITY = 16; | ||
@@ -24,4 +24,4 @@ protected static readonly LOAD_FACTOR = 0.75; | ||
get size(): number; | ||
protected _buckets: Array<HashTableNode<K, V> | null>; | ||
get buckets(): Array<HashTableNode<K, V> | null>; | ||
protected _buckets: Array<HashTableNode<K, V> | undefined>; | ||
get buckets(): Array<HashTableNode<K, V> | undefined>; | ||
protected _hashFn: HashFunction<K>; | ||
@@ -55,2 +55,7 @@ get hashFn(): HashFunction<K>; | ||
delete(key: K): void; | ||
[Symbol.iterator](): Generator<[K, V], void, undefined>; | ||
forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void; | ||
filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V>; | ||
map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T>; | ||
reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T; | ||
/** | ||
@@ -57,0 +62,0 @@ * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the |
@@ -15,3 +15,3 @@ "use strict"; | ||
this.value = value; | ||
this.next = null; | ||
this.next = undefined; | ||
} | ||
@@ -25,3 +25,3 @@ } | ||
this._size = 0; | ||
this._buckets = new Array(this._capacity).fill(null); | ||
this._buckets = new Array(this._capacity).fill(undefined); | ||
} | ||
@@ -106,3 +106,3 @@ get capacity() { | ||
let currentNode = this._buckets[index]; | ||
let prevNode = null; | ||
let prevNode = undefined; | ||
while (currentNode) { | ||
@@ -117,3 +117,3 @@ if (currentNode.key === key) { | ||
this._size--; | ||
currentNode.next = null; // Release memory | ||
currentNode.next = undefined; // Release memory | ||
return; | ||
@@ -125,2 +125,47 @@ } | ||
} | ||
*[Symbol.iterator]() { | ||
for (const bucket of this._buckets) { | ||
let currentNode = bucket; | ||
while (currentNode) { | ||
yield [currentNode.key, currentNode.value]; | ||
currentNode = currentNode.next; | ||
} | ||
} | ||
} | ||
forEach(callback) { | ||
let index = 0; | ||
for (const entry of this) { | ||
callback(entry, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
const newTable = new HashTable(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], index, this)) { | ||
newTable.set(key, value); | ||
} | ||
index++; | ||
} | ||
return newTable; | ||
} | ||
map(callback) { | ||
const newTable = new HashTable(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTable.set(key, callback([key, value], index, this)); | ||
index++; | ||
} | ||
return newTable; | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -216,3 +261,3 @@ * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the | ||
const newCapacity = this._capacity * 2; | ||
const newBuckets = new Array(newCapacity).fill(null); | ||
const newBuckets = new Array(newCapacity).fill(undefined); | ||
for (const bucket of this._buckets) { | ||
@@ -219,0 +264,0 @@ let currentNode = bucket; |
@@ -150,3 +150,3 @@ /** | ||
*/ | ||
dfs(order: DFSOrderPattern): E[]; | ||
dfs(order?: DFSOrderPattern): E[]; | ||
/** | ||
@@ -199,2 +199,7 @@ * Time Complexity: O(n) | ||
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; | ||
/** | ||
@@ -201,0 +206,0 @@ * Time Complexity: O(log n) |
@@ -207,20 +207,21 @@ "use strict"; | ||
*/ | ||
dfs(order) { | ||
dfs(order = 'pre') { | ||
const result = []; | ||
// Auxiliary recursive function, traverses the binary heap according to the traversal order | ||
const dfsHelper = (index) => { | ||
const _dfs = (index) => { | ||
const left = 2 * index + 1, right = left + 1; | ||
if (index < this.size) { | ||
if (order === 'in') { | ||
dfsHelper(2 * index + 1); | ||
_dfs(left); | ||
result.push(this.elements[index]); | ||
dfsHelper(2 * index + 2); | ||
_dfs(right); | ||
} | ||
else if (order === 'pre') { | ||
result.push(this.elements[index]); | ||
dfsHelper(2 * index + 1); | ||
dfsHelper(2 * index + 2); | ||
_dfs(left); | ||
_dfs(right); | ||
} | ||
else if (order === 'post') { | ||
dfsHelper(2 * index + 1); | ||
dfsHelper(2 * index + 2); | ||
_dfs(left); | ||
_dfs(right); | ||
result.push(this.elements[index]); | ||
@@ -230,3 +231,3 @@ } | ||
}; | ||
dfsHelper(0); // Traverse starting from the root node | ||
_dfs(0); // Traverse starting from the root node | ||
return result; | ||
@@ -299,2 +300,43 @@ } | ||
} | ||
*[Symbol.iterator]() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
const filteredHeap = new Heap({ comparator: this.comparator }); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
filteredHeap.push(el); | ||
} | ||
index++; | ||
} | ||
return filteredHeap; | ||
} | ||
map(callback, comparator) { | ||
const mappedHeap = new Heap({ comparator: comparator }); | ||
let index = 0; | ||
for (const el of this) { | ||
mappedHeap.add(callback(el, index, this)); | ||
index++; | ||
} | ||
return mappedHeap; | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -301,0 +343,0 @@ * Time Complexity: O(log n) |
@@ -260,2 +260,19 @@ /** | ||
* | ||
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* 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 `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -284,14 +301,2 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArray(): E[]; | ||
/** | ||
* The function checks if a variable has a length greater than zero and returns a boolean value. | ||
@@ -353,2 +358,13 @@ * @returns A boolean value is being returned. | ||
* 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 `reverse` function reverses the order of the elements in a doubly linked list. | ||
*/ | ||
reverse(): void; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
@@ -360,18 +376,23 @@ */ | ||
* | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArrayBackward(): E[]; | ||
toArray(): E[]; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
* The `reverse` function reverses the order of the elements in a doubly linked list. | ||
* The `toReversedArray` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toReversedArray()` function returns an array of type `E[]`. | ||
*/ | ||
reverse(): void; | ||
toReversedArray(): E[]; | ||
/** | ||
* 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. | ||
@@ -389,3 +410,3 @@ * Space Complexity: O(1) | ||
*/ | ||
forEach(callback: (value: E, index: number) => void): void; | ||
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void; | ||
/** | ||
@@ -399,10 +420,9 @@ * Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* | ||
* 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 U (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values. | ||
* 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. | ||
*/ | ||
map<U>(callback: (value: E) => U): DoublyLinkedList<U>; | ||
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E>; | ||
/** | ||
@@ -416,9 +436,10 @@ * Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* | ||
* 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 `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. | ||
*/ | ||
filter(callback: (value: E) => boolean): DoublyLinkedList<E>; | ||
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T>; | ||
/** | ||
@@ -436,3 +457,3 @@ * Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -442,24 +463,3 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
*/ | ||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U; | ||
/** | ||
* 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 `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T; | ||
} |
@@ -414,2 +414,42 @@ "use strict"; | ||
* | ||
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode, newValue) { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} | ||
else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.next = existingNode.next; | ||
if (existingNode.next) { | ||
existingNode.next.prev = newNode; | ||
} | ||
newNode.prev = existingNode; | ||
existingNode.next = newNode; | ||
if (existingNode === this.tail) { | ||
this._tail = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* 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 `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -477,22 +517,2 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArray() { | ||
const array = []; | ||
let current = this.head; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.next; | ||
} | ||
return array; | ||
} | ||
/** | ||
* The function checks if a variable has a length greater than zero and returns a boolean value. | ||
@@ -589,2 +609,21 @@ * @returns A boolean value is being returned. | ||
* 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 `reverse` function reverses the order of the elements in a doubly linked list. | ||
*/ | ||
reverse() { | ||
let current = this.head; | ||
[this._head, this._tail] = [this.tail, this.head]; | ||
while (current) { | ||
const next = current.next; | ||
[current.prev, current.next] = [current.next, current.prev]; | ||
current = next; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
@@ -596,11 +635,11 @@ */ | ||
* | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArrayBackward() { | ||
toArray() { | ||
const array = []; | ||
let current = this.tail; | ||
let current = this.head; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.prev; | ||
current = current.next; | ||
} | ||
@@ -611,17 +650,28 @@ return array; | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
* The `reverse` function reverses the order of the elements in a doubly linked list. | ||
* The `toReversedArray` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toReversedArray()` function returns an array of type `E[]`. | ||
*/ | ||
reverse() { | ||
toReversedArray() { | ||
const array = []; | ||
let current = this.tail; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.prev; | ||
} | ||
return array; | ||
} | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
*[Symbol.iterator]() { | ||
let current = this.head; | ||
[this._head, this._tail] = [this.tail, this.head]; | ||
while (current) { | ||
const next = current.next; | ||
[current.prev, current.next] = [current.next, current.prev]; | ||
current = next; | ||
yield current.value; | ||
current = current.next; | ||
} | ||
@@ -643,7 +693,5 @@ } | ||
forEach(callback) { | ||
let current = this.head; | ||
let index = 0; | ||
while (current) { | ||
callback(current.value, index); | ||
current = current.next; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
@@ -660,17 +708,18 @@ } | ||
* | ||
* 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 U (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values. | ||
* 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. | ||
*/ | ||
map(callback) { | ||
const mappedList = new DoublyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.value)); | ||
current = current.next; | ||
filter(callback) { | ||
const filteredList = new DoublyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
index++; | ||
} | ||
return mappedList; | ||
return filteredList; | ||
} | ||
@@ -685,18 +734,17 @@ /** | ||
* | ||
* 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 `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. | ||
*/ | ||
filter(callback) { | ||
const filteredList = new DoublyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.value)) { | ||
filteredList.push(current.value); | ||
} | ||
current = current.next; | ||
map(callback) { | ||
const mappedList = new DoublyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
index++; | ||
} | ||
return filteredList; | ||
return mappedList; | ||
} | ||
@@ -715,3 +763,3 @@ /** | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -723,60 +771,10 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.value); | ||
current = current.next; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* 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 `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode, newValue) { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} | ||
else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.next = existingNode.next; | ||
if (existingNode.next) { | ||
existingNode.next.prev = newNode; | ||
} | ||
newNode.prev = existingNode; | ||
existingNode.next = newNode; | ||
if (existingNode === this.tail) { | ||
this._tail = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* 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; | ||
} | ||
} | ||
} | ||
exports.DoublyLinkedList = DoublyLinkedList; |
@@ -348,8 +348,12 @@ /** | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* Time Complexity: O(n), 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) | ||
* | ||
@@ -361,27 +365,11 @@ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
*/ | ||
forEach(callback: (value: E, index: number) => void): void; | ||
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* 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 U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
*/ | ||
map<U>(callback: (value: E) => U): SinglyLinkedList<U>; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
@@ -393,11 +381,27 @@ * elements that satisfy the given callback function. | ||
*/ | ||
filter(callback: (value: E) => boolean): SinglyLinkedList<E>; | ||
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E>; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* 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. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T>; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* 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 | ||
@@ -407,3 +411,3 @@ * single value. | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -413,7 +417,3 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
*/ | ||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T; | ||
} |
@@ -608,8 +608,18 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* The 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) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* Time Complexity: O(n), 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) | ||
* | ||
@@ -622,7 +632,5 @@ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
forEach(callback) { | ||
let current = this.head; | ||
let index = 0; | ||
while (current) { | ||
callback(current.value, index); | ||
current = current.next; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
@@ -632,33 +640,9 @@ } | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* 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 U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
*/ | ||
map(callback) { | ||
const mappedList = new SinglyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.value)); | ||
current = current.next; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
@@ -672,8 +656,8 @@ * elements that satisfy the given callback function. | ||
const filteredList = new SinglyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.value)) { | ||
filteredList.push(current.value); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
current = current.next; | ||
index++; | ||
} | ||
@@ -683,9 +667,33 @@ return filteredList; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* 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. | ||
*/ | ||
map(callback) { | ||
const mappedList = new SinglyLinkedList(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
index++; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* 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 | ||
@@ -695,3 +703,3 @@ * single value. | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -703,20 +711,10 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.value); | ||
current = current.next; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* 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; | ||
} | ||
} | ||
} | ||
exports.SinglyLinkedList = SinglyLinkedList; |
@@ -321,8 +321,10 @@ /** | ||
* | ||
* 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 `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
*/ | ||
forEach(callback: (element: E, index: number, deque: Deque<E>) => void): void; | ||
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined; | ||
/** | ||
@@ -336,10 +338,10 @@ * Time Complexity: O(n) | ||
* | ||
* The `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
*/ | ||
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined; | ||
indexOf(element: E): number; | ||
/** | ||
@@ -359,16 +361,28 @@ * Time Complexity: O(n) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* 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 above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T>; | ||
[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) | ||
@@ -387,5 +401,19 @@ */ | ||
*/ | ||
filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E>; | ||
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<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 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. | ||
*/ | ||
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -406,33 +434,5 @@ */ | ||
*/ | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T; | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
*/ | ||
indexOf(element: E): 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(n) | ||
@@ -439,0 +439,0 @@ */ |
@@ -597,11 +597,17 @@ "use strict"; | ||
* | ||
* 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 `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
*/ | ||
forEach(callback) { | ||
find(callback) { | ||
for (let i = 0; i < this.size; ++i) { | ||
callback(this.getAt(i), i, this); | ||
const element = this.getAt(i); | ||
if (callback(element, i, this)) { | ||
return element; | ||
} | ||
} | ||
return undefined; | ||
} | ||
@@ -616,17 +622,16 @@ /** | ||
* | ||
* The `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
*/ | ||
find(callback) { | ||
indexOf(element) { | ||
for (let i = 0; i < this.size; ++i) { | ||
const element = this.getAt(i); | ||
if (callback(element, i, this)) { | ||
return element; | ||
if (this.getAt(i) === element) { | ||
return i; | ||
} | ||
} | ||
return undefined; | ||
return -1; | ||
} | ||
@@ -653,22 +658,38 @@ /** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* 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 above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
map(callback) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
*[Symbol.iterator]() { | ||
for (let i = 0; i < this.size; ++i) { | ||
newDeque.push(callback(this.getAt(i), i, this)); | ||
yield this.getAt(i); | ||
} | ||
return newDeque; | ||
} | ||
/** | ||
* 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) | ||
@@ -689,7 +710,8 @@ */ | ||
const newDeque = new Deque([], this._bucketSize); | ||
for (let i = 0; i < this.size; ++i) { | ||
const element = this.getAt(i); | ||
if (predicate(element, i, this)) { | ||
newDeque.push(element); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
index++; | ||
} | ||
@@ -700,2 +722,24 @@ return newDeque; | ||
* 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 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. | ||
*/ | ||
map(callback) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return newDeque; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -718,4 +762,6 @@ */ | ||
let accumulator = initialValue; | ||
for (let i = 0; i < this.size; ++i) { | ||
accumulator = callback(accumulator, this.getAt(i), i, this); | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
@@ -726,41 +772,2 @@ return accumulator; | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
*/ | ||
indexOf(element) { | ||
for (let i = 0; i < this.size; ++i) { | ||
if (this.getAt(i) === element) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
/** | ||
* 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(n) | ||
@@ -767,0 +774,0 @@ */ |
@@ -209,2 +209,47 @@ /** | ||
[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, queue: this) => void): void; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space 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. | ||
*/ | ||
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<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 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. | ||
*/ | ||
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; | ||
} |
@@ -273,3 +273,80 @@ "use strict"; | ||
} | ||
/** | ||
* 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) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space 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. | ||
*/ | ||
filter(predicate) { | ||
const newDeque = new Queue([]); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
index++; | ||
} | ||
return newDeque; | ||
} | ||
/** | ||
* 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 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. | ||
*/ | ||
map(callback) { | ||
const newDeque = new Queue([]); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return newDeque; | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
} | ||
exports.Queue = Queue; |
@@ -21,2 +21,7 @@ /** | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
get size(): number; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
@@ -37,7 +42,2 @@ * Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
size(): number; | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
@@ -108,2 +108,15 @@ * Space Complexity: O(1), as it does not use any additional space. | ||
clone(): Stack<E>; | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
[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. | ||
*/ | ||
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; | ||
} |
@@ -27,2 +27,9 @@ "use strict"; | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
get size() { | ||
return this.elements.length; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
@@ -47,9 +54,2 @@ * Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
size() { | ||
return this.elements.length; | ||
} | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
@@ -137,3 +137,52 @@ * Space Complexity: O(1), as it does not use any additional space. | ||
} | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (let i = this.elements.length - 1; i >= 0; 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. | ||
*/ | ||
forEach(callback) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate) { | ||
const newStack = new Stack(); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newStack.push(el); | ||
} | ||
index++; | ||
} | ||
return newStack; | ||
} | ||
map(callback) { | ||
const newStack = new Stack(); | ||
let index = 0; | ||
for (const el of this) { | ||
newStack.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return newStack; | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const el of this) { | ||
accumulator = callback(accumulator, el, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
} | ||
exports.Stack = Stack; |
@@ -143,2 +143,7 @@ /** | ||
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; | ||
/** | ||
@@ -145,0 +150,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
@@ -308,2 +308,49 @@ "use strict"; | ||
} | ||
*[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) { | ||
const results = []; | ||
let index = 0; | ||
for (const word of this) { | ||
if (predicate(word, index, this)) { | ||
results.push(word); | ||
} | ||
index++; | ||
} | ||
return results; | ||
} | ||
map(callback) { | ||
const newTrie = new Trie(); | ||
let index = 0; | ||
for (const word of this) { | ||
newTrie.add(callback(word, index, this)); | ||
index++; | ||
} | ||
return newTrie; | ||
} | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const word of this) { | ||
accumulator = callback(accumulator, word, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -310,0 +357,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
{ | ||
"name": "heap-typed", | ||
"version": "1.47.4", | ||
"version": "1.47.5", | ||
"description": "Heap. Javascript & Typescript Data Structure.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -316,8 +316,10 @@ /** | ||
*/ | ||
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V> { | ||
filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V> { | ||
const filteredMap = new HashMap<K, V>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], this)) { | ||
if (predicate([key, value], index, this)) { | ||
filteredMap.set(key, value); | ||
} | ||
index++; | ||
} | ||
@@ -334,7 +336,9 @@ return filteredMap; | ||
*/ | ||
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV> { | ||
map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV> { | ||
const mappedMap = new HashMap<K, NV>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
const newValue = callback([key, value], this); | ||
const newValue = callback([key, value], index, this); | ||
mappedMap.set(key, newValue); | ||
index++; | ||
} | ||
@@ -356,6 +360,8 @@ return mappedMap; | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A { | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A { | ||
let accumulator = initialValue; | ||
for (const element of this) { | ||
accumulator = callback(accumulator, element, this); | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
@@ -362,0 +368,0 @@ return accumulator; |
@@ -12,3 +12,3 @@ /** | ||
value: V; | ||
next: HashTableNode<K, V> | null; | ||
next: HashTableNode<K, V> | undefined; | ||
@@ -18,3 +18,3 @@ constructor(key: K, value: V) { | ||
this.value = value; | ||
this.next = null; | ||
this.next = undefined; | ||
} | ||
@@ -25,3 +25,3 @@ } | ||
export class HashTable<K, V> { | ||
export class HashTable<K = any, V = any> { | ||
protected static readonly DEFAULT_CAPACITY = 16; | ||
@@ -34,3 +34,3 @@ protected static readonly LOAD_FACTOR = 0.75; | ||
this._size = 0; | ||
this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null); | ||
this._buckets = new Array<HashTableNode<K, V> | undefined>(this._capacity).fill(undefined); | ||
} | ||
@@ -50,5 +50,5 @@ | ||
protected _buckets: Array<HashTableNode<K, V> | null>; | ||
protected _buckets: Array<HashTableNode<K, V> | undefined>; | ||
get buckets(): Array<HashTableNode<K, V> | null> { | ||
get buckets(): Array<HashTableNode<K, V> | undefined> { | ||
return this._buckets; | ||
@@ -133,3 +133,3 @@ } | ||
let currentNode = this._buckets[index]; | ||
let prevNode: HashTableNode<K, V> | null = null; | ||
let prevNode: HashTableNode<K, V> | undefined = undefined; | ||
@@ -144,3 +144,3 @@ while (currentNode) { | ||
this._size--; | ||
currentNode.next = null; // Release memory | ||
currentNode.next = undefined; // Release memory | ||
return; | ||
@@ -153,2 +153,52 @@ } | ||
* [Symbol.iterator](): Generator<[K, V], void, undefined> { | ||
for (const bucket of this._buckets) { | ||
let currentNode = bucket; | ||
while (currentNode) { | ||
yield [currentNode.key, currentNode.value]; | ||
currentNode = currentNode.next; | ||
} | ||
} | ||
} | ||
forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void { | ||
let index = 0; | ||
for (const entry of this) { | ||
callback(entry, index, this); | ||
index++; | ||
} | ||
} | ||
filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V> { | ||
const newTable = new HashTable<K, V>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate([key, value], index, this)) { | ||
newTable.set(key, value); | ||
} | ||
index++; | ||
} | ||
return newTable; | ||
} | ||
map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T> { | ||
const newTable = new HashTable<K, T>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTable.set(key, callback([key, value], index, this)); | ||
index++; | ||
} | ||
return newTable; | ||
} | ||
reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const entry of this) { | ||
accumulator = callback(accumulator, entry, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -252,3 +302,3 @@ * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the | ||
const newCapacity = this._capacity * 2; | ||
const newBuckets = new Array<HashTableNode<K, V> | null>(newCapacity).fill(null); | ||
const newBuckets = new Array<HashTableNode<K, V> | undefined>(newCapacity).fill(undefined); | ||
@@ -255,0 +305,0 @@ for (const bucket of this._buckets) { |
@@ -229,19 +229,20 @@ /** | ||
*/ | ||
dfs(order: DFSOrderPattern): E[] { | ||
dfs(order: DFSOrderPattern = 'pre'): E[] { | ||
const result: E[] = []; | ||
// Auxiliary recursive function, traverses the binary heap according to the traversal order | ||
const dfsHelper = (index: number) => { | ||
const _dfs = (index: number) => { | ||
const left = 2 * index + 1, right = left + 1; | ||
if (index < this.size) { | ||
if (order === 'in') { | ||
dfsHelper(2 * index + 1); | ||
_dfs(left); | ||
result.push(this.elements[index]); | ||
dfsHelper(2 * index + 2); | ||
_dfs(right); | ||
} else if (order === 'pre') { | ||
result.push(this.elements[index]); | ||
dfsHelper(2 * index + 1); | ||
dfsHelper(2 * index + 2); | ||
_dfs(left); | ||
_dfs(right); | ||
} else if (order === 'post') { | ||
dfsHelper(2 * index + 1); | ||
dfsHelper(2 * index + 2); | ||
_dfs(left); | ||
_dfs(right); | ||
result.push(this.elements[index]); | ||
@@ -252,3 +253,3 @@ } | ||
dfsHelper(0); // Traverse starting from the root node | ||
_dfs(0); // Traverse starting from the root node | ||
@@ -329,2 +330,52 @@ return result; | ||
* [Symbol.iterator]() { | ||
for (const element of this.elements) { | ||
yield element; | ||
} | ||
} | ||
forEach(callback: (element: E, index: number, heap: this) => void): void { | ||
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>({ comparator: this.comparator }); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
filteredHeap.push(el); | ||
} | ||
index++; | ||
} | ||
return filteredHeap; | ||
} | ||
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): 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)); | ||
index++; | ||
} | ||
return mappedHeap; | ||
} | ||
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; | ||
} | ||
/** | ||
@@ -331,0 +382,0 @@ * Time Complexity: O(log n) |
@@ -453,2 +453,46 @@ /** | ||
* | ||
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.next = existingNode.next; | ||
if (existingNode.next) { | ||
existingNode.next.prev = newNode; | ||
} | ||
newNode.prev = existingNode; | ||
existingNode.next = newNode; | ||
if (existingNode === this.tail) { | ||
this._tail = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* 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 `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -516,24 +560,2 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArray(): E[] { | ||
const array: E[] = []; | ||
let current = this.head; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.next; | ||
} | ||
return array; | ||
} | ||
/** | ||
* The function checks if a variable has a length greater than zero and returns a boolean value. | ||
@@ -638,2 +660,23 @@ * @returns A boolean value is being returned. | ||
* 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 `reverse` function reverses the order of the elements in a doubly linked list. | ||
*/ | ||
reverse(): void { | ||
let current = this.head; | ||
[this._head, this._tail] = [this.tail, this.head]; | ||
while (current) { | ||
const next = current.next; | ||
[current.prev, current.next] = [current.next, current.prev]; | ||
current = next; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
@@ -646,11 +689,11 @@ */ | ||
* | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
* The `toArray` function converts a linked list into an array. | ||
* @returns The `toArray()` method is returning an array of type `E[]`. | ||
*/ | ||
toArrayBackward(): E[] { | ||
toArray(): E[] { | ||
const array: E[] = []; | ||
let current = this.tail; | ||
let current = this.head; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.prev; | ||
current = current.next; | ||
} | ||
@@ -662,3 +705,3 @@ return array; | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -668,13 +711,26 @@ | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
* The `reverse` function reverses the order of the elements in a doubly linked list. | ||
* The `toReversedArray` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toReversedArray()` function returns an array of type `E[]`. | ||
*/ | ||
reverse(): void { | ||
toReversedArray(): E[] { | ||
const array: E[] = []; | ||
let current = this.tail; | ||
while (current) { | ||
array.push(current.value); | ||
current = current.prev; | ||
} | ||
return array; | ||
} | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
[this._head, this._tail] = [this.tail, this.head]; | ||
while (current) { | ||
const next = current.next; | ||
[current.prev, current.next] = [current.next, current.prev]; | ||
current = next; | ||
yield current.value; | ||
current = current.next; | ||
} | ||
@@ -697,8 +753,6 @@ } | ||
*/ | ||
forEach(callback: (value: E, index: number) => void): void { | ||
let current = this.head; | ||
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void { | ||
let index = 0; | ||
while (current) { | ||
callback(current.value, index); | ||
current = current.next; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
@@ -717,17 +771,18 @@ } | ||
* | ||
* 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 U (the type of values that will be stored in the mapped | ||
* DoublyLinkedList). | ||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values. | ||
* 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. | ||
*/ | ||
map<U>(callback: (value: E) => U): DoublyLinkedList<U> { | ||
const mappedList = new DoublyLinkedList<U>(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.value)); | ||
current = current.next; | ||
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E> { | ||
const filteredList = new DoublyLinkedList<E>(); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
index++; | ||
} | ||
return mappedList; | ||
return filteredList; | ||
} | ||
@@ -744,18 +799,18 @@ | ||
* | ||
* 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 `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. | ||
*/ | ||
filter(callback: (value: E) => boolean): DoublyLinkedList<E> { | ||
const filteredList = new DoublyLinkedList<E>(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.value)) { | ||
filteredList.push(current.value); | ||
} | ||
current = current.next; | ||
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T> { | ||
const mappedList = new DoublyLinkedList<T>(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
index++; | ||
} | ||
return filteredList; | ||
return mappedList; | ||
} | ||
@@ -776,3 +831,3 @@ | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -782,67 +837,12 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
*/ | ||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U { | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.value); | ||
current = current.next; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* 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 `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.next = existingNode.next; | ||
if (existingNode.next) { | ||
existingNode.next.prev = newNode; | ||
} | ||
newNode.prev = existingNode; | ||
existingNode.next = newNode; | ||
if (existingNode === this.tail) { | ||
this._tail = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* 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; | ||
} | ||
} | ||
} |
@@ -669,22 +669,10 @@ /** | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. | ||
* Space Complexity: O(1) - Constant space. | ||
* | ||
* The `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) => void): void { | ||
let current = this.head; | ||
let index = 0; | ||
while (current) { | ||
callback(current.value, index); | ||
yield current.value; | ||
current = current.next; | ||
index++; | ||
} | ||
@@ -694,35 +682,31 @@ } | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(1) | ||
* | ||
* 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 U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
* 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. | ||
*/ | ||
map<U>(callback: (value: E) => U): SinglyLinkedList<U> { | ||
const mappedList = new SinglyLinkedList<U>(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.value)); | ||
current = current.next; | ||
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
* | ||
@@ -735,10 +719,10 @@ * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
*/ | ||
filter(callback: (value: E) => boolean): SinglyLinkedList<E> { | ||
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E> { | ||
const filteredList = new SinglyLinkedList<E>(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.value)) { | ||
filteredList.push(current.value); | ||
let index = 0; | ||
for (const current of this) { | ||
if (callback(current, index, this)) { | ||
filteredList.push(current); | ||
} | ||
current = current.next; | ||
index++; | ||
} | ||
@@ -749,10 +733,37 @@ return filteredList; | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list. | ||
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays. | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* 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. | ||
*/ | ||
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T> { | ||
const mappedList = new SinglyLinkedList<T>(); | ||
let index = 0; | ||
for (const current of this) { | ||
mappedList.push(callback(current, index, this)); | ||
index++; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* 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 | ||
@@ -762,3 +773,3 @@ * single value. | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
@@ -768,23 +779,12 @@ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
*/ | ||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U { | ||
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.value); | ||
current = current.next; | ||
let index = 0; | ||
for (const current of this) { | ||
accumulator = callback(accumulator, current, index, this); | ||
index++; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* 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; | ||
} | ||
} | ||
} |
@@ -640,11 +640,17 @@ /** | ||
* | ||
* 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 `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
*/ | ||
forEach(callback: (element: E, index: number, deque: Deque<E>) => void) { | ||
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined { | ||
for (let i = 0; i < this.size; ++i) { | ||
callback(this.getAt(i), i, this); | ||
const element = this.getAt(i); | ||
if (callback(element, i, this)) { | ||
return element; | ||
} | ||
} | ||
return undefined; | ||
} | ||
@@ -661,17 +667,16 @@ | ||
* | ||
* The `find` function iterates over the elements in a deque and returns the first element for which | ||
* the callback function returns true, or undefined if no such element is found. | ||
* @param callback - A function that takes three parameters: element, index, and deque. It should | ||
* return a boolean value indicating whether the element satisfies a certain condition. | ||
* @returns The method `find` returns the first element in the deque that satisfies the condition | ||
* specified by the callback function. If no element satisfies the condition, it returns `undefined`. | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
*/ | ||
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined { | ||
indexOf(element: E): number { | ||
for (let i = 0; i < this.size; ++i) { | ||
const element = this.getAt(i); | ||
if (callback(element, i, this)) { | ||
return element; | ||
if (this.getAt(i) === element) { | ||
return i; | ||
} | ||
} | ||
return undefined; | ||
return -1; | ||
} | ||
@@ -701,3 +706,3 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -707,15 +712,11 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* 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 above function is an implementation of the iterator protocol in TypeScript, allowing the | ||
* object to be iterated over using a for...of loop. | ||
*/ | ||
map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> { | ||
const newDeque = new Deque<T>([], this._bucketSize); | ||
* [Symbol.iterator]() { | ||
for (let i = 0; i < this.size; ++i) { | ||
newDeque.push(callback(this.getAt(i), i, this)); | ||
yield this.getAt(i); | ||
} | ||
return newDeque; | ||
} | ||
@@ -725,2 +726,24 @@ | ||
* 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) | ||
@@ -740,9 +763,10 @@ */ | ||
*/ | ||
filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> { | ||
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E> { | ||
const newDeque = new Deque<E>([], this._bucketSize); | ||
for (let i = 0; i < this.size; ++i) { | ||
const element = this.getAt(i); | ||
if (predicate(element, i, this)) { | ||
newDeque.push(element); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
index++; | ||
} | ||
@@ -754,3 +778,3 @@ return newDeque; | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -760,19 +784,17 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space 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 `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. | ||
*/ | ||
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T { | ||
let accumulator = initialValue; | ||
for (let i = 0; i < this.size; ++i) { | ||
accumulator = callback(accumulator, this.getAt(i), i, this); | ||
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T> { | ||
const newDeque = new Deque<T>([], this._bucketSize); | ||
let index = 0; | ||
for (const el of this) { | ||
newDeque.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return accumulator; | ||
return newDeque; | ||
} | ||
@@ -789,16 +811,19 @@ | ||
* | ||
* The function "indexOf" returns the index of the first occurrence of a given element in an array, | ||
* or -1 if the element is not found. | ||
* @param {E} element - The "element" parameter represents the element that you want to find the | ||
* index of in the data structure. | ||
* @returns The indexOf function returns the index of the first occurrence of the specified element | ||
* in the data structure. If the element is not found, it returns -1. | ||
* 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. | ||
*/ | ||
indexOf(element: E): number { | ||
for (let i = 0; i < this.size; ++i) { | ||
if (this.getAt(i) === element) { | ||
return i; | ||
} | ||
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++; | ||
} | ||
return -1; | ||
return accumulator; | ||
} | ||
@@ -808,20 +833,2 @@ | ||
* 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(n) | ||
@@ -828,0 +835,0 @@ */ |
@@ -308,2 +308,86 @@ /** | ||
} | ||
/** | ||
* 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, queue: this) => void) { | ||
let index = 0; | ||
for (const el of this) { | ||
callback(el, index, this); | ||
index++; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space 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. | ||
*/ | ||
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E> { | ||
const newDeque = new Queue<E>([]); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newDeque.push(el); | ||
} | ||
index++; | ||
} | ||
return newDeque; | ||
} | ||
/** | ||
* 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 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. | ||
*/ | ||
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; | ||
} | ||
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; | ||
} | ||
} |
@@ -29,2 +29,10 @@ /** | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
get size(): number { | ||
return this.elements.length; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
@@ -51,10 +59,2 @@ * Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
* @returns The size of the elements array. | ||
*/ | ||
size(): number { | ||
return this.elements.length; | ||
} | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
@@ -152,2 +152,58 @@ * Space Complexity: O(1), as it does not use any additional space. | ||
} | ||
/** | ||
* Custom iterator for the Stack class. | ||
* @returns An iterator object. | ||
*/ | ||
* [Symbol.iterator]() { | ||
for (let i = this.elements.length - 1; i >= 0; 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. | ||
*/ | ||
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> { | ||
const newStack = new Stack<E>(); | ||
let index = 0; | ||
for (const el of this) { | ||
if (predicate(el, index, this)) { | ||
newStack.push(el); | ||
} | ||
index++; | ||
} | ||
return newStack; | ||
} | ||
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T> { | ||
const newStack = new Stack<T>(); | ||
let index = 0; | ||
for (const el of this) { | ||
newStack.push(callback(el, index, this)); | ||
index++; | ||
} | ||
return newStack; | ||
} | ||
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; | ||
} | ||
} |
@@ -327,2 +327,55 @@ /** | ||
* [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); | ||
} | ||
} | ||
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[] { | ||
const results: string[] = []; | ||
let index = 0; | ||
for (const word of this) { | ||
if (predicate(word, index, this)) { | ||
results.push(word); | ||
} | ||
index++; | ||
} | ||
return results; | ||
} | ||
map(callback: (word: string, index: number, trie: this) => string): Trie { | ||
const newTrie = new Trie(); | ||
let index = 0; | ||
for (const word of this) { | ||
newTrie.add(callback(word, index, this)); | ||
index++; | ||
} | ||
return newTrie; | ||
} | ||
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; | ||
} | ||
/** | ||
@@ -329,0 +382,0 @@ * Time Complexity: O(M), where M is the length of the input string. |
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
1856195
34108