From 5077c0d291513cb7ed62b4b007c08bde7c238822 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 23 Nov 2024 21:57:13 +1300 Subject: [PATCH] docs: Improve the documentation for pushMany, addMany, and similar methods. --- src/data-structures/hash/hash-map.ts | 30 ++++++++++++++ src/data-structures/heap/heap.ts | 20 ++++++++- .../linked-list/doubly-linked-list.ts | 33 +++++++++++++++ .../linked-list/singly-linked-list.ts | 31 ++++++++++++++ src/data-structures/queue/deque.ts | 27 ++++++++++++ src/data-structures/queue/queue.ts | 41 ++++++++++++++++++- src/data-structures/stack/stack.ts | 9 +++- src/data-structures/trie/trie.ts | 9 +++- 8 files changed, 195 insertions(+), 5 deletions(-) diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index 580deee..5d5858b 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -97,6 +97,9 @@ export class HashMap extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBa } /** + * Time Complexity: O(k) + * Space Complexity: O(k) + * * The function `setMany` takes an iterable collection, converts each element into a key-value pair * using a provided function, and sets each key-value pair in the current object, returning an array * of booleans indicating the success of each set operation. @@ -605,6 +632,9 @@ export class LinkedHashMap extends IterableEntryBa } /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * * The function checks if a given key exists in a map, using different logic depending on whether the * key is a weak key or not. * @param {K} key - The `key` parameter is the key that is being checked for existence in the map. diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index e11d95f..c52f88f 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -249,12 +249,30 @@ export class Heap extends IterableElementBase * Time Complexity: O(log n) * Space Complexity: O(1) * + * The add function pushes an element into an array and then triggers a bubble-up operation. + * @param {E} element - The `element` parameter represents the element that you want to add to the + * data structure. + * @returns The `add` method is returning a boolean value, which is the result of calling the + * `_bubbleUp` method with the index `this.elements.length - 1` as an argument. */ add(element: E): boolean { this._elements.push(element as E); return this._bubbleUp(this.elements.length - 1); } + /** + * Time Complexity: O(k log n) + * Space Complexity: O(1) + * + * The `addMany` function iterates over elements and adds them to a collection, returning an array of + * boolean values indicating success or failure. + * @param {Iterable | Iterable} elements - The `elements` parameter in the `addMany` method is + * an iterable containing elements of type `E` or `R`. The method iterates over each element in the + * iterable and adds them to the data structure. If a transformation function `_toElementFn` is + * provided, it transforms the element + * @returns The `addMany` method returns an array of boolean values indicating whether each element + * in the input iterable was successfully added to the data structure. + */ addMany(elements: Iterable | Iterable): boolean[] { const ans: boolean[] = []; for (const el of elements) { @@ -478,7 +496,7 @@ export class Heap extends IterableElementBase } /** - * Time Complexity: O(n log n) + * Time Complexity: O(n) * Space Complexity: O(n) * * The `map` function creates a new heap by applying a callback function to each element of the diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 0110125..2dc15e2 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -697,6 +697,19 @@ export class DoublyLinkedList extends IterableElementBase | Iterable | Iterable>} elements - The `elements` + * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, + * or `DoublyLinkedListNode`. The function iterates over each element in the iterable and pushes + * it onto the linked list. If a transformation function `to + * @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate + * the success or failure of pushing each element into the data structure. + */ pushMany(elements: Iterable | Iterable | Iterable>) { const ans: boolean[] = []; for (const el of elements) { @@ -709,6 +722,20 @@ export class DoublyLinkedList extends IterableElementBase | Iterable | Iterable>} elements - The `elements` + * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, + * `R`, or `DoublyLinkedListNode`. The function iterates over each element in the iterable and + * performs an `unshift` operation on the doubly linked list + * @returns The `unshiftMany` function returns an array of boolean values indicating the success of + * each unshift operation performed on the elements passed as input. + */ unshiftMany(elements: Iterable | Iterable | Iterable>) { const ans: boolean[] = []; for (const el of elements) { @@ -1203,6 +1230,12 @@ export class DoublyLinkedList extends IterableElementBase | ((node: DoublyLinkedListNode) => boolean)} elementOrNode + * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values: + * @returns The `countOccurrences` method returns the number of occurrences of the specified element, + * node, or predicate function in the doubly linked list. */ countOccurrences(elementOrNode: E | DoublyLinkedListNode | ((node: DoublyLinkedListNode) => boolean)): number { const predicate = this._ensurePredicate(elementOrNode); diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index a6d4b7e..5776faf 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -206,6 +206,18 @@ export class SinglyLinkedList extends IterableElementBase | Iterable | Iterable>} elements - The `elements` + * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, + * or `SinglyLinkedListNode`. + * @returns The `pushMany` function returns an array of boolean values indicating whether each + * element was successfully pushed into the data structure. + */ pushMany(elements: Iterable | Iterable | Iterable>) { const ans: boolean[] = []; for (const el of elements) { @@ -218,6 +230,19 @@ export class SinglyLinkedList extends IterableElementBase | Iterable | Iterable>} elements - The `elements` + * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, + * `R`, or `SinglyLinkedListNode`. The function iterates over each element in the iterable and + * performs an `unshift` operation on the linked list for each + * @returns The `unshiftMany` function is returning an array of boolean values, where each value + * represents the result of calling the `unshift` method on the current instance of the class. + */ unshiftMany(elements: Iterable | Iterable | Iterable>) { const ans: boolean[] = []; for (const el of elements) { @@ -418,6 +443,9 @@ export class SinglyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase | IterableWithSizeOrLength} elements - The `elements` + * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E` + * or `R`. It can be either an `IterableWithSizeOrLength` or an `IterableWithSizeOrLength`. The + * function iterates over each element + * @returns The `pushMany` function is returning an array of boolean values, where each value + * represents the result of calling the `push` method on the current object instance with the + * corresponding element from the input `elements` iterable. + */ pushMany(elements: IterableWithSizeOrLength | IterableWithSizeOrLength) { const ans: boolean[] = []; for (const el of elements) { @@ -291,6 +305,19 @@ export class Deque extends IterableElementBase | IterableWithSizeOrLength} elements - The `elements` + * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It + * can be an array or any other iterable data structure that has a known size or length. The function + * iterates over each element in the `elements` iterable and + * @returns The `unshiftMany` function returns an array of boolean values indicating whether each + * element was successfully added to the beginning of the array. + */ unshiftMany(elements: IterableWithSizeOrLength | IterableWithSizeOrLength = []) { const ans: boolean[] = []; for (const el of elements) { diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 49aa52c..fd12a37 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -126,6 +126,17 @@ export class Queue extends IterableElementBase | Iterable} elements - The `elements` parameter in the `pushMany` function + * is an iterable containing elements of type `E` or `R`. + * @returns The `pushMany` function is returning an array of boolean values indicating whether each + * element was successfully pushed into the data structure. + */ pushMany(elements: Iterable | Iterable) { const ans: boolean[] = []; for (const el of elements) { @@ -154,6 +165,9 @@ export class Queue extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase( callback: ElementCallback>, diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index b508fc3..59ede59 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -108,7 +108,14 @@ export class Stack extends IterableElementBase | Iterable} elements - The `elements` parameter in the `pushMany` function + * is an iterable containing elements of type `E` or `R`. The function iterates over each element in + * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is + * provided, it is used to + * @returns The `pushMany` function is returning an array of boolean values indicating whether each + * element was successfully pushed into the data structure. */ pushMany(elements: Iterable | Iterable) { const ans: boolean[] = []; diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index 424d475..f177764 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -366,9 +366,14 @@ export class Trie extends IterableElementBase> { } /** - * Time Complexity: O(n), where n is the total number of nodes in the trie. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * + * The function `getHeight` calculates the height of a trie data structure starting from the root + * node. + * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from + * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie + * tree and returns the maximum depth found. */ getHeight(): number { const startNode = this.root;