queue-typed
Advanced tools
Comparing version 1.53.7 to 1.53.8
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Range = exports.DFSOperation = void 0; | ||
const utils_1 = require("../utils"); | ||
var DFSOperation; | ||
@@ -15,2 +16,6 @@ (function (DFSOperation) { | ||
this.includeHigh = includeHigh; | ||
if (!((0, utils_1.isComparable)(low) && (0, utils_1.isComparable)(high))) | ||
throw new RangeError('low or high is not comparable'); | ||
if (low > high) | ||
throw new RangeError('low must be less than or equal to high'); | ||
} | ||
@@ -17,0 +22,0 @@ // Determine whether a key is within the range |
@@ -105,3 +105,3 @@ /** | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; | ||
/** | ||
@@ -108,0 +108,0 @@ * Time Complexity: O(log n) |
@@ -123,3 +123,3 @@ "use strict"; | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) { | ||
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) | ||
@@ -164,3 +164,3 @@ return [undefined, undefined]; | ||
add(keyNodeEntryOrRaw, value, count = 1) { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
if (newNode === undefined) | ||
@@ -167,0 +167,0 @@ return false; |
@@ -95,3 +95,3 @@ /** | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined]; | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined]; | ||
/** | ||
@@ -98,0 +98,0 @@ * Time Complexity: O(n) |
@@ -50,16 +50,24 @@ /** | ||
* @example | ||
* // Find kth smallest element | ||
* // Create a BST with some elements | ||
* const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]); | ||
* const sortedKeys = bst.dfs(node => node.key, 'IN'); | ||
* // Merge 3 sorted datasets | ||
* const dataset1 = new BST<number, string>([ | ||
* [1, 'A'], | ||
* [7, 'G'] | ||
* ]); | ||
* const dataset2 = [ | ||
* [2, 'B'], | ||
* [6, 'F'] | ||
* ]; | ||
* const dataset3 = new BST<number, string>([ | ||
* [3, 'C'], | ||
* [5, 'E'], | ||
* [4, 'D'] | ||
* ]); | ||
* | ||
* // Helper function to find kth smallest | ||
* const findKthSmallest = (k: number): number | undefined => { | ||
* return sortedKeys[k - 1]; | ||
* }; | ||
* // Merge datasets into a single BinarySearchTree | ||
* const merged = new BST<number, string>(dataset1); | ||
* merged.addMany(dataset2); | ||
* merged.merge(dataset3); | ||
* | ||
* // Assertions | ||
* console.log(findKthSmallest(1)); // 1 | ||
* console.log(findKthSmallest(3)); // 4 | ||
* console.log(findKthSmallest(7)); // 8 | ||
* // Verify merged dataset is in sorted order | ||
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'] | ||
* @example | ||
@@ -69,5 +77,5 @@ * // Find elements in a range | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [10, 12, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
* console.log(bst.search(new Range(15, 20, false))); // [18] | ||
@@ -78,2 +86,10 @@ * @example | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined { | ||
@@ -88,10 +104,2 @@ * for (const num of arr1) { | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* // Assertions | ||
@@ -151,3 +159,3 @@ * console.log(findLCA(3, 10)); // 7 | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined]; | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined]; | ||
/** | ||
@@ -261,2 +269,24 @@ * Time Complexity: O(log n) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be | ||
* either a `Range` object or an array of two elements representing the range boundaries. | ||
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
* begin. It is the starting point for the range search operation. | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function | ||
* is used to specify the type of iteration to be performed during the search operation. It has a | ||
* default value of `this.iterationType`, which suggests that it is likely a property of the class or | ||
* object that the `rangeSearch` | ||
* @returns The `rangeSearch` function is returning the result of calling the `search` method with | ||
* the specified parameters. | ||
*/ | ||
rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -263,0 +293,0 @@ * |
@@ -7,2 +7,3 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const common_1 = require("../../common"); | ||
class BSTNode extends binary_tree_1.BinaryTreeNode { | ||
@@ -63,16 +64,24 @@ constructor(key, value) { | ||
* @example | ||
* // Find kth smallest element | ||
* // Create a BST with some elements | ||
* const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]); | ||
* const sortedKeys = bst.dfs(node => node.key, 'IN'); | ||
* // Merge 3 sorted datasets | ||
* const dataset1 = new BST<number, string>([ | ||
* [1, 'A'], | ||
* [7, 'G'] | ||
* ]); | ||
* const dataset2 = [ | ||
* [2, 'B'], | ||
* [6, 'F'] | ||
* ]; | ||
* const dataset3 = new BST<number, string>([ | ||
* [3, 'C'], | ||
* [5, 'E'], | ||
* [4, 'D'] | ||
* ]); | ||
* | ||
* // Helper function to find kth smallest | ||
* const findKthSmallest = (k: number): number | undefined => { | ||
* return sortedKeys[k - 1]; | ||
* }; | ||
* // Merge datasets into a single BinarySearchTree | ||
* const merged = new BST<number, string>(dataset1); | ||
* merged.addMany(dataset2); | ||
* merged.merge(dataset3); | ||
* | ||
* // Assertions | ||
* console.log(findKthSmallest(1)); // 1 | ||
* console.log(findKthSmallest(3)); // 4 | ||
* console.log(findKthSmallest(7)); // 8 | ||
* // Verify merged dataset is in sorted order | ||
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'] | ||
* @example | ||
@@ -82,5 +91,5 @@ * // Find elements in a range | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [10, 12, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
* console.log(bst.search(new Range(15, 20, false))); // [18] | ||
@@ -91,2 +100,10 @@ * @example | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined { | ||
@@ -101,10 +118,2 @@ * for (const num of arr1) { | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* // Assertions | ||
@@ -203,4 +212,4 @@ * console.log(findLCA(3, 10)); // 7 | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) { | ||
const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) { | ||
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (node === null) | ||
@@ -262,3 +271,3 @@ return [undefined, undefined]; | ||
add(keyNodeEntryOrRaw, value) { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (newNode === undefined) | ||
@@ -559,2 +568,27 @@ return false; | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be | ||
* either a `Range` object or an array of two elements representing the range boundaries. | ||
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
* begin. It is the starting point for the range search operation. | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function | ||
* is used to specify the type of iteration to be performed during the search operation. It has a | ||
* default value of `this.iterationType`, which suggests that it is likely a property of the class or | ||
* object that the `rangeSearch` | ||
* @returns The `rangeSearch` function is returning the result of calling the `search` method with | ||
* the specified parameters. | ||
*/ | ||
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) { | ||
const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]); | ||
return this.search(searchRange, false, callback, startNode, iterationType); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -561,0 +595,0 @@ * |
@@ -6,4 +6,4 @@ export * from './binary-tree'; | ||
export * from './avl-tree'; | ||
export * from './rb-tree'; | ||
export * from './red-black-tree'; | ||
export * from './avl-tree-multi-map'; | ||
export * from './tree-multi-map'; |
@@ -22,4 +22,4 @@ "use strict"; | ||
__exportStar(require("./avl-tree"), exports); | ||
__exportStar(require("./rb-tree"), exports); | ||
__exportStar(require("./red-black-tree"), exports); | ||
__exportStar(require("./avl-tree-multi-map"), exports); | ||
__exportStar(require("./tree-multi-map"), exports); |
@@ -10,3 +10,3 @@ /** | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { RedBlackTree, RedBlackTreeNode } from './rb-tree'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> { | ||
@@ -100,3 +100,3 @@ /** | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; | ||
/** | ||
@@ -103,0 +103,0 @@ * The function checks if the input is an instance of the TreeMultiMapNode class. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0; | ||
const rb_tree_1 = require("./rb-tree"); | ||
class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode { | ||
const red_black_tree_1 = require("./red-black-tree"); | ||
class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode { | ||
/** | ||
@@ -40,3 +40,3 @@ * The constructor function initializes a Red-Black Tree node with a key, value, count, and color. | ||
exports.TreeMultiMapNode = TreeMultiMapNode; | ||
class TreeMultiMap extends rb_tree_1.RedBlackTree { | ||
class TreeMultiMap extends red_black_tree_1.RedBlackTree { | ||
/** | ||
@@ -117,3 +117,3 @@ * The constructor function initializes a TreeMultiMap object with optional initial data. | ||
*/ | ||
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) { | ||
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) | ||
@@ -168,3 +168,3 @@ return [undefined, undefined]; | ||
add(keyNodeEntryOrRaw, value, count = 1) { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0; | ||
@@ -171,0 +171,0 @@ const isSuccessAdded = super.add(newNode, newValue); |
@@ -65,2 +65,5 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -73,2 +76,5 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the size of an object is equal to zero and returns a boolean value. | ||
@@ -79,2 +85,5 @@ * @returns A boolean value indicating whether the size of the object is 0 or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of an object by clearing its internal store, object map, and | ||
@@ -85,2 +94,5 @@ * size. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
@@ -96,2 +108,5 @@ * the key is not already present. | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value | ||
@@ -105,2 +120,5 @@ * pair using a mapping function, and sets each key-value pair in the current object. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
@@ -115,2 +133,5 @@ * a string map. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
@@ -123,2 +144,5 @@ * is an object key or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
@@ -303,2 +327,5 @@ * @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection, converts each element into a key-value pair | ||
@@ -313,2 +340,5 @@ * using a provided function, and sets each key-value pair in the current object, returning an array | ||
/** | ||
* 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 | ||
@@ -315,0 +345,0 @@ * key is a weak key or not. |
@@ -78,2 +78,5 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -88,2 +91,5 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the size of an object is equal to zero and returns a boolean value. | ||
@@ -96,2 +102,5 @@ * @returns A boolean value indicating whether the size of the object is 0 or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of an object by clearing its internal store, object map, and | ||
@@ -106,2 +115,5 @@ * size. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
@@ -132,2 +144,5 @@ * the key is not already present. | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value | ||
@@ -158,2 +173,5 @@ * pair using a mapping function, and sets each key-value pair in the current object. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
@@ -177,2 +195,5 @@ * a string map. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
@@ -193,2 +214,5 @@ * is an object key or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
@@ -534,2 +558,5 @@ * @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection, converts each element into a key-value pair | ||
@@ -561,2 +588,5 @@ * using a provided function, and sets each key-value pair in the current object, returning an array | ||
/** | ||
* 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 | ||
@@ -563,0 +593,0 @@ * key is a weak key or not. |
@@ -227,7 +227,24 @@ /** | ||
* | ||
* Insert an element into the heap and maintain the heap properties. | ||
* @param element - The element to be inserted. | ||
* 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; | ||
/** | ||
* 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<E> | Iterable<R>} 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<E> | Iterable<R>): boolean[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -344,3 +361,3 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n log n) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -347,0 +364,0 @@ * |
@@ -221,10 +221,3 @@ "use strict"; | ||
} | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) | ||
this.add(this.toElementFn(el)); | ||
else | ||
this.add(el); | ||
} | ||
} | ||
this.addMany(elements); | ||
} | ||
@@ -265,4 +258,7 @@ /** | ||
* | ||
* Insert an element into the heap and maintain the heap properties. | ||
* @param element - The element to be inserted. | ||
* 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. | ||
*/ | ||
@@ -274,2 +270,26 @@ add(element) { | ||
/** | ||
* 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<E> | Iterable<R>} 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this._toElementFn) { | ||
ans.push(this.add(this._toElementFn(el))); | ||
continue; | ||
} | ||
ans.push(this.add(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -476,3 +496,3 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n log n) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -479,0 +499,0 @@ * |
@@ -500,3 +500,3 @@ /** | ||
*/ | ||
constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>); | ||
constructor(elements?: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>, options?: DoublyLinkedListOptions<E, R>); | ||
protected _head: DoublyLinkedListNode<E> | undefined; | ||
@@ -590,2 +590,31 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `DoublyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[]; | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates through a collection of elements and adds them to the | ||
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of | ||
* each insertion. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `DoublyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[]; | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -835,2 +864,8 @@ * Space Complexity: O(1) | ||
* | ||
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences | ||
* of a specified element or nodes that satisfy a given predicate. | ||
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => 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. | ||
*/ | ||
@@ -837,0 +872,0 @@ countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number; |
@@ -514,11 +514,3 @@ "use strict"; | ||
this._size = 0; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el)); | ||
} | ||
else | ||
this.push(el); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -678,2 +670,51 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `DoublyLinkedListNode<E>`. 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el))); | ||
continue; | ||
} | ||
ans.push(this.push(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates through a collection of elements and adds them to the | ||
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of | ||
* each insertion. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `DoublyLinkedListNode<E>`. 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el))); | ||
continue; | ||
} | ||
ans.push(this.unshift(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -1129,2 +1170,8 @@ * Space Complexity: O(1) | ||
* | ||
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences | ||
* of a specified element or nodes that satisfy a given predicate. | ||
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => 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. | ||
*/ | ||
@@ -1131,0 +1178,0 @@ countOccurrences(elementOrNode) { |
@@ -44,3 +44,3 @@ /** | ||
export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> { | ||
constructor(elements?: Iterable<E> | Iterable<R>, options?: SinglyLinkedListOptions<E, R>); | ||
constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>); | ||
protected _head: SinglyLinkedListNode<E> | undefined; | ||
@@ -116,2 +116,29 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `SinglyLinkedListNode<E>`. | ||
* @returns The `pushMany` function returns an array of boolean values indicating whether each | ||
* element was successfully pushed into the data structure. | ||
*/ | ||
pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[]; | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally | ||
* converting them using a provided function. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `SinglyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[]; | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -204,2 +231,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating | ||
@@ -211,2 +241,5 @@ * whether it is empty or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively. | ||
@@ -213,0 +246,0 @@ */ |
@@ -52,12 +52,3 @@ "use strict"; | ||
this._size = 0; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el)); | ||
} | ||
else { | ||
this.push(el); | ||
} | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -193,2 +184,49 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `SinglyLinkedListNode<E>`. | ||
* @returns The `pushMany` function returns an array of boolean values indicating whether each | ||
* element was successfully pushed into the data structure. | ||
*/ | ||
pushMany(elements) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el))); | ||
continue; | ||
} | ||
ans.push(this.push(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally | ||
* converting them using a provided function. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `SinglyLinkedListNode<E>`. 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el))); | ||
continue; | ||
} | ||
ans.push(this.unshift(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -371,2 +409,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating | ||
@@ -380,2 +421,5 @@ * whether it is empty or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively. | ||
@@ -382,0 +426,0 @@ */ |
@@ -119,2 +119,12 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
*/ | ||
shift(): E | undefined; | ||
/** | ||
* Time Complexity: Amortized O(1) | ||
@@ -131,12 +141,31 @@ * Space Complexity: O(n) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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<E>` or an `IterableWithSizeOrLength<R>`. 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. | ||
*/ | ||
shift(): E | undefined; | ||
pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>): boolean[]; | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of | ||
* an array, optionally converting them using a provided function. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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<E> | IterableWithSizeOrLength<R>): boolean[]; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -143,0 +172,0 @@ * Space Complexity: O(1) |
@@ -63,10 +63,3 @@ "use strict"; | ||
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el)); | ||
} | ||
else { | ||
this.push(el); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -219,2 +212,31 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
*/ | ||
shift() { | ||
if (this._size === 0) | ||
return; | ||
const element = this._buckets[this._bucketFirst][this._firstInBucket]; | ||
if (this._size !== 1) { | ||
if (this._firstInBucket < this._bucketSize - 1) { | ||
this._firstInBucket += 1; | ||
} | ||
else if (this._bucketFirst < this._bucketCount - 1) { | ||
this._bucketFirst += 1; | ||
this._firstInBucket = 0; | ||
} | ||
else { | ||
this._bucketFirst = 0; | ||
this._firstInBucket = 0; | ||
} | ||
} | ||
this._size -= 1; | ||
return element; | ||
} | ||
/** | ||
* Time Complexity: Amortized O(1) | ||
@@ -252,29 +274,51 @@ * Space Complexity: O(n) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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<E>` or an `IterableWithSizeOrLength<R>`. 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. | ||
*/ | ||
shift() { | ||
if (this._size === 0) | ||
return; | ||
const element = this._buckets[this._bucketFirst][this._firstInBucket]; | ||
if (this._size !== 1) { | ||
if (this._firstInBucket < this._bucketSize - 1) { | ||
this._firstInBucket += 1; | ||
pushMany(elements) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el))); | ||
} | ||
else if (this._bucketFirst < this._bucketCount - 1) { | ||
this._bucketFirst += 1; | ||
this._firstInBucket = 0; | ||
else { | ||
ans.push(this.push(el)); | ||
} | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of | ||
* an array, optionally converting them using a provided function. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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 = []) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el))); | ||
} | ||
else { | ||
this._bucketFirst = 0; | ||
this._firstInBucket = 0; | ||
ans.push(this.unshift(el)); | ||
} | ||
} | ||
this._size -= 1; | ||
return element; | ||
return ans; | ||
} | ||
@@ -281,0 +325,0 @@ /** |
@@ -88,2 +88,14 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `pushMany` function iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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<E> | Iterable<R>): boolean[]; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -98,2 +110,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The delete function removes an element from the list. | ||
@@ -105,2 +120,5 @@ * @param {E} element - Specify the element to be deleted | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The deleteAt function deletes the element at a given index. | ||
@@ -115,3 +133,8 @@ * @param {number} index - Determine the index of the element to be deleted | ||
* | ||
* @param index | ||
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined` | ||
* if the index is out of bounds. | ||
* @param {number} index - The `index` parameter represents the position of the element you want to | ||
* retrieve from the data structure. | ||
* @returns The `at` method is returning the element at the specified index adjusted by the offset | ||
* `_offset`. | ||
*/ | ||
@@ -143,2 +166,5 @@ at(index: number): E | undefined; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `compact` function in TypeScript slices the elements array based on the offset and resets the | ||
@@ -177,2 +203,16 @@ * offset to zero. | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new Queue by applying a callback function to each | ||
* element in the original Queue. | ||
* @param callback - The `callback` parameter is a function that will be applied to each element in | ||
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns | ||
* a new element. | ||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to | ||
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the | ||
* `map` method to transform each raw element before passing it to the `callback` function. If | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the | ||
* value of `this` when executing the `callback` function. It allows you to set the context (the | ||
* value of `this`) within the callback function. If `thisArg` is provided, it will be | ||
* @returns A new Queue object containing elements of type EM, which are the result of applying the | ||
* callback function to each element in the original Queue object. | ||
*/ | ||
@@ -179,0 +219,0 @@ map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>; |
@@ -25,10 +25,3 @@ "use strict"; | ||
} | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) | ||
this.push(this.toElementFn(el)); | ||
else | ||
this.push(el); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -119,2 +112,23 @@ /** | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `pushMany` function iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) | ||
ans.push(this.push(this.toElementFn(el))); | ||
else | ||
ans.push(this.push(el)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -137,2 +151,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The delete function removes an element from the list. | ||
@@ -147,2 +164,5 @@ * @param {E} element - Specify the element to be deleted | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The deleteAt function deletes the element at a given index. | ||
@@ -160,3 +180,8 @@ * @param {number} index - Determine the index of the element to be deleted | ||
* | ||
* @param index | ||
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined` | ||
* if the index is out of bounds. | ||
* @param {number} index - The `index` parameter represents the position of the element you want to | ||
* retrieve from the data structure. | ||
* @returns The `at` method is returning the element at the specified index adjusted by the offset | ||
* `_offset`. | ||
*/ | ||
@@ -197,2 +222,5 @@ at(index) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `compact` function in TypeScript slices the elements array based on the offset and resets the | ||
@@ -247,2 +275,16 @@ * offset to zero. | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new Queue by applying a callback function to each | ||
* element in the original Queue. | ||
* @param callback - The `callback` parameter is a function that will be applied to each element in | ||
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns | ||
* a new element. | ||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to | ||
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the | ||
* `map` method to transform each raw element before passing it to the `callback` function. If | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the | ||
* value of `this` when executing the `callback` function. It allows you to set the context (the | ||
* value of `this`) within the callback function. If `thisArg` is provided, it will be | ||
* @returns A new Queue object containing elements of type EM, which are the result of applying the | ||
* callback function to each element in the original Queue object. | ||
*/ | ||
@@ -249,0 +291,0 @@ map(callback, toElementFn, thisArg) { |
@@ -34,6 +34,2 @@ /** | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -47,2 +43,5 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if an array is empty and returns a boolean value. | ||
@@ -79,11 +78,29 @@ * @returns A boolean value indicating whether the `_elements` array is empty or not. | ||
/** | ||
* The delete function removes an element from the stack. | ||
* @param element: E Specify the element to be deleted | ||
* @return A boolean value indicating whether the element was successfully deleted or not | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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<E> | Iterable<R>): boolean[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
delete(element: E): boolean; | ||
/** | ||
* The deleteAt function deletes the element at a given index. | ||
* @param index: number Determine the index of the element to be deleted | ||
* @return A boolean value | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
@@ -90,0 +107,0 @@ deleteAt(index: number): boolean; |
@@ -17,12 +17,3 @@ "use strict"; | ||
this._elements = []; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el)); | ||
} | ||
else { | ||
this.push(el); | ||
} | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -46,6 +37,2 @@ /** | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -61,2 +48,5 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if an array is empty and returns a boolean value. | ||
@@ -106,6 +96,33 @@ * @returns A boolean value indicating whether the `_elements` array is empty or not. | ||
/** | ||
* The delete function removes an element from the stack. | ||
* @param element: E Specify the element to be deleted | ||
* @return A boolean value indicating whether the element was successfully deleted or not | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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) { | ||
const ans = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el))); | ||
} | ||
else { | ||
ans.push(this.push(el)); | ||
} | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
delete(element) { | ||
@@ -116,5 +133,7 @@ const index = this.elements.indexOf(element); | ||
/** | ||
* The deleteAt function deletes the element at a given index. | ||
* @param index: number Determine the index of the element to be deleted | ||
* @return A boolean value | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
@@ -121,0 +140,0 @@ deleteAt(index) { |
@@ -203,3 +203,3 @@ /** | ||
*/ | ||
addMany(words?: Iterable<string> | Iterable<R>): boolean[]; | ||
addMany(words: Iterable<string> | Iterable<R>): boolean[]; | ||
/** | ||
@@ -239,5 +239,10 @@ * Time Complexity: O(l), where l is the length of the input word. | ||
/** | ||
* 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. | ||
*/ | ||
@@ -244,0 +249,0 @@ getHeight(): number; |
@@ -246,3 +246,3 @@ "use strict"; | ||
*/ | ||
addMany(words = []) { | ||
addMany(words) { | ||
const ans = []; | ||
@@ -342,5 +342,10 @@ for (const word of words) { | ||
/** | ||
* 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. | ||
*/ | ||
@@ -347,0 +352,0 @@ getHeight() { |
{ | ||
"name": "queue-typed", | ||
"version": "1.53.7", | ||
"version": "1.53.8", | ||
"description": "Queue, ArrayQueue. Javascript & Typescript Data Structure.", | ||
@@ -118,4 +118,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.53.7" | ||
"data-structure-typed": "^1.53.8" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
import { isComparable } from '../utils'; | ||
export enum DFSOperation { | ||
@@ -5,2 +7,3 @@ VISIT = 0, | ||
} | ||
export class Range<K> { | ||
@@ -12,3 +15,6 @@ constructor( | ||
public includeHigh: boolean = true | ||
) {} | ||
) { | ||
if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable'); | ||
if (low > high) throw new RangeError('low must be less than or equal to high'); | ||
} | ||
@@ -15,0 +21,0 @@ // Determine whether a key is within the range |
@@ -176,3 +176,3 @@ /** | ||
*/ | ||
override keyValueNodeEntryRawToNodeAndValue( | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
@@ -221,3 +221,3 @@ value?: V, | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
if (newNode === undefined) return false; | ||
@@ -224,0 +224,0 @@ |
@@ -98,16 +98,24 @@ /** | ||
* @example | ||
* // Find kth smallest element | ||
* // Create a BST with some elements | ||
* const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]); | ||
* const sortedKeys = bst.dfs(node => node.key, 'IN'); | ||
* // Merge 3 sorted datasets | ||
* const dataset1 = new BST<number, string>([ | ||
* [1, 'A'], | ||
* [7, 'G'] | ||
* ]); | ||
* const dataset2 = [ | ||
* [2, 'B'], | ||
* [6, 'F'] | ||
* ]; | ||
* const dataset3 = new BST<number, string>([ | ||
* [3, 'C'], | ||
* [5, 'E'], | ||
* [4, 'D'] | ||
* ]); | ||
* | ||
* // Helper function to find kth smallest | ||
* const findKthSmallest = (k: number): number | undefined => { | ||
* return sortedKeys[k - 1]; | ||
* }; | ||
* // Merge datasets into a single BinarySearchTree | ||
* const merged = new BST<number, string>(dataset1); | ||
* merged.addMany(dataset2); | ||
* merged.merge(dataset3); | ||
* | ||
* // Assertions | ||
* console.log(findKthSmallest(1)); // 1 | ||
* console.log(findKthSmallest(3)); // 4 | ||
* console.log(findKthSmallest(7)); // 8 | ||
* // Verify merged dataset is in sorted order | ||
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'] | ||
* @example | ||
@@ -117,5 +125,5 @@ * // Find elements in a range | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [10, 12, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
* console.log(bst.search(new Range(15, 20, false))); // [18] | ||
@@ -126,2 +134,10 @@ * @example | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined { | ||
@@ -136,10 +152,2 @@ * for (const num of arr1) { | ||
* | ||
* // LCA helper function | ||
* const findLCA = (num1: number, num2: number): number | undefined => { | ||
* const path1 = bst.getPathToRoot(num1); | ||
* const path2 = bst.getPathToRoot(num2); | ||
* // Find the first common ancestor | ||
* return findFirstCommon(path1, path2); | ||
* }; | ||
* | ||
* // Assertions | ||
@@ -240,7 +248,7 @@ * console.log(findLCA(3, 10)); // 7 | ||
*/ | ||
override keyValueNodeEntryRawToNodeAndValue( | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
value?: V | ||
): [OptNode<NODE>, V | undefined] { | ||
const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (node === null) return [undefined, undefined]; | ||
@@ -307,3 +315,3 @@ return [node, value ?? entryValue]; | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (newNode === undefined) return false; | ||
@@ -620,2 +628,33 @@ | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be | ||
* either a `Range` object or an array of two elements representing the range boundaries. | ||
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
* begin. It is the starting point for the range search operation. | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function | ||
* is used to specify the type of iteration to be performed during the search operation. It has a | ||
* default value of `this.iterationType`, which suggests that it is likely a property of the class or | ||
* object that the `rangeSearch` | ||
* @returns The `rangeSearch` function is returning the result of calling the `search` method with | ||
* the specified parameters. | ||
*/ | ||
rangeSearch<C extends NodeCallback<NODE>>( | ||
range: Range<K> | [K, K], | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
iterationType: IterationType = this.iterationType | ||
) { | ||
const searchRange: Range<K> = range instanceof Range ? range : new Range(range[0], range[1]); | ||
return this.search(searchRange, false, callback, startNode, iterationType); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -622,0 +661,0 @@ * |
@@ -6,4 +6,4 @@ export * from './binary-tree'; | ||
export * from './avl-tree'; | ||
export * from './rb-tree'; | ||
export * from './red-black-tree'; | ||
export * from './avl-tree-multi-map'; | ||
export * from './tree-multi-map'; |
@@ -20,3 +20,3 @@ /** | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { RedBlackTree, RedBlackTreeNode } from './rb-tree'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
@@ -161,3 +161,3 @@ export class TreeMultiMapNode< | ||
*/ | ||
override keyValueNodeEntryRawToNodeAndValue( | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
@@ -217,3 +217,3 @@ value?: V, | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean { | ||
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const orgCount = newNode?.count || 0; | ||
@@ -220,0 +220,0 @@ const isSuccessAdded = super.add(newNode, newValue); |
@@ -100,2 +100,5 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -111,2 +114,5 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the size of an object is equal to zero and returns a boolean value. | ||
@@ -120,2 +126,5 @@ * @returns A boolean value indicating whether the size of the object is 0 or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of an object by clearing its internal store, object map, and | ||
@@ -131,2 +140,5 @@ * size. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
@@ -157,2 +169,5 @@ * the key is not already present. | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value | ||
@@ -183,2 +198,5 @@ * pair using a mapping function, and sets each key-value pair in the current object. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
@@ -201,2 +219,5 @@ * a string map. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
@@ -217,2 +238,5 @@ * is an object key or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
@@ -590,2 +614,5 @@ * @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `setMany` takes an iterable collection, converts each element into a key-value pair | ||
@@ -617,2 +644,5 @@ * using a provided function, and sets each key-value pair in the current object, returning an array | ||
/** | ||
* 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 | ||
@@ -619,0 +649,0 @@ * key is a weak key or not. |
@@ -210,8 +210,3 @@ /** | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) this.add(this.toElementFn(el as R)); | ||
else this.add(el as E); | ||
} | ||
} | ||
this.addMany(elements); | ||
} | ||
@@ -258,7 +253,10 @@ | ||
* | ||
* Insert an element into the heap and maintain the heap properties. | ||
* @param element - The element to be inserted. | ||
* 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); | ||
this._elements.push(element as E); | ||
return this._bubbleUp(this.elements.length - 1); | ||
@@ -268,2 +266,27 @@ } | ||
/** | ||
* 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<E> | Iterable<R>} 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<E> | Iterable<R>): boolean[] { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this._toElementFn) { | ||
ans.push(this.add(this._toElementFn(el as R))); | ||
continue; | ||
} | ||
ans.push(this.add(el as E)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -479,3 +502,3 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n log n) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -482,0 +505,0 @@ * |
@@ -527,3 +527,6 @@ /** | ||
*/ | ||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) { | ||
constructor( | ||
elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>> = [], | ||
options?: DoublyLinkedListOptions<E, R> | ||
) { | ||
super(options); | ||
@@ -533,9 +536,3 @@ this._head = undefined; | ||
this._size = 0; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el as R)); | ||
} else this.push(el as E); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -706,2 +703,53 @@ | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `DoublyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el as R))); | ||
continue; | ||
} | ||
ans.push(this.push(el as E | DoublyLinkedListNode<E>)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates through a collection of elements and adds them to the | ||
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of | ||
* each insertion. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `DoublyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el as R))); | ||
continue; | ||
} | ||
ans.push(this.unshift(el as E | DoublyLinkedListNode<E>)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -1188,2 +1236,8 @@ * Space Complexity: O(1) | ||
* | ||
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences | ||
* of a specified element or nodes that satisfy a given predicate. | ||
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => 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. | ||
*/ | ||
@@ -1190,0 +1244,0 @@ countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number { |
@@ -63,13 +63,8 @@ /** | ||
export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> { | ||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) { | ||
constructor( | ||
elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [], | ||
options?: SinglyLinkedListOptions<E, R> | ||
) { | ||
super(options); | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el as R)); | ||
} else { | ||
this.push(el as E); | ||
} | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -216,2 +211,51 @@ | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`, | ||
* or `SinglyLinkedListNode<E>`. | ||
* @returns The `pushMany` function returns an array of boolean values indicating whether each | ||
* element was successfully pushed into the data structure. | ||
*/ | ||
pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el as R))); | ||
continue; | ||
} | ||
ans.push(this.push(el as E | SinglyLinkedListNode<E>)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally | ||
* converting them using a provided function. | ||
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements` | ||
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`, | ||
* `R`, or `SinglyLinkedListNode<E>`. 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<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el as R))); | ||
continue; | ||
} | ||
ans.push(this.unshift(el as E | SinglyLinkedListNode<E>)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -404,2 +448,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating | ||
@@ -414,2 +461,5 @@ * whether it is empty or not. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively. | ||
@@ -416,0 +466,0 @@ */ |
@@ -56,10 +56,3 @@ /** | ||
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el as R)); | ||
} else { | ||
this.push(el as E); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -235,2 +228,29 @@ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
*/ | ||
shift(): E | undefined { | ||
if (this._size === 0) return; | ||
const element = this._buckets[this._bucketFirst][this._firstInBucket]; | ||
if (this._size !== 1) { | ||
if (this._firstInBucket < this._bucketSize - 1) { | ||
this._firstInBucket += 1; | ||
} else if (this._bucketFirst < this._bucketCount - 1) { | ||
this._bucketFirst += 1; | ||
this._firstInBucket = 0; | ||
} else { | ||
this._bucketFirst = 0; | ||
this._firstInBucket = 0; | ||
} | ||
} | ||
this._size -= 1; | ||
return element; | ||
} | ||
/** | ||
* Time Complexity: Amortized O(1) | ||
@@ -265,29 +285,53 @@ * Space Complexity: O(n) | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `shift()` function removes and returns the first element from a data structure, updating the | ||
* internal state variables accordingly. | ||
* @returns The element that is being removed from the beginning of the data structure is being | ||
* returned. | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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<E>` or an `IterableWithSizeOrLength<R>`. 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. | ||
*/ | ||
shift(): E | undefined { | ||
if (this._size === 0) return; | ||
const element = this._buckets[this._bucketFirst][this._firstInBucket]; | ||
if (this._size !== 1) { | ||
if (this._firstInBucket < this._bucketSize - 1) { | ||
this._firstInBucket += 1; | ||
} else if (this._bucketFirst < this._bucketCount - 1) { | ||
this._bucketFirst += 1; | ||
this._firstInBucket = 0; | ||
pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el as R))); | ||
} else { | ||
this._bucketFirst = 0; | ||
this._firstInBucket = 0; | ||
ans.push(this.push(el as E)); | ||
} | ||
} | ||
this._size -= 1; | ||
return element; | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of | ||
* an array, optionally converting them using a provided function. | ||
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} 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<E> | IterableWithSizeOrLength<R> = []) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.unshift(this.toElementFn(el as R))); | ||
} else { | ||
ans.push(this.unshift(el as E)); | ||
} | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -294,0 +338,0 @@ * Space Complexity: O(1) |
@@ -28,8 +28,3 @@ /** | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) this.push(this.toElementFn(el as R)); | ||
else this.push(el as E); | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -136,2 +131,22 @@ | ||
/** | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(k) | ||
* | ||
* The `pushMany` function iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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<E> | Iterable<R>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) ans.push(this.push(this.toElementFn(el as R))); | ||
else ans.push(this.push(el as E)); | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -155,2 +170,5 @@ * Space Complexity: O(1) | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The delete function removes an element from the list. | ||
@@ -166,2 +184,5 @@ * @param {E} element - Specify the element to be deleted | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The deleteAt function deletes the element at a given index. | ||
@@ -180,3 +201,8 @@ * @param {number} index - Determine the index of the element to be deleted | ||
* | ||
* @param index | ||
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined` | ||
* if the index is out of bounds. | ||
* @param {number} index - The `index` parameter represents the position of the element you want to | ||
* retrieve from the data structure. | ||
* @returns The `at` method is returning the element at the specified index adjusted by the offset | ||
* `_offset`. | ||
*/ | ||
@@ -221,2 +247,5 @@ at(index: number): E | undefined { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `compact` function in TypeScript slices the elements array based on the offset and resets the | ||
@@ -274,2 +303,16 @@ * offset to zero. | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript creates a new Queue by applying a callback function to each | ||
* element in the original Queue. | ||
* @param callback - The `callback` parameter is a function that will be applied to each element in | ||
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns | ||
* a new element. | ||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to | ||
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the | ||
* `map` method to transform each raw element before passing it to the `callback` function. If | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the | ||
* value of `this` when executing the `callback` function. It allows you to set the context (the | ||
* value of `this`) within the callback function. If `thisArg` is provided, it will be | ||
* @returns A new Queue object containing elements of type EM, which are the result of applying the | ||
* callback function to each element in the original Queue object. | ||
*/ | ||
@@ -276,0 +319,0 @@ map<EM, RM>( |
@@ -22,11 +22,3 @@ /** | ||
super(options); | ||
if (elements) { | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
this.push(this.toElementFn(el as R)); | ||
} else { | ||
this.push(el as E); | ||
} | ||
} | ||
} | ||
this.pushMany(elements); | ||
} | ||
@@ -55,7 +47,2 @@ | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -72,2 +59,5 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if an array is empty and returns a boolean value. | ||
@@ -121,6 +111,33 @@ * @returns A boolean value indicating whether the `_elements` array is empty or not. | ||
/** | ||
* The delete function removes an element from the stack. | ||
* @param element: E Specify the element to be deleted | ||
* @return A boolean value indicating whether the element was successfully deleted or not | ||
* Time Complexity: O(k) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `pushMany` iterates over elements and pushes them into an array after applying a | ||
* transformation function if provided. | ||
* @param {Iterable<E> | Iterable<R>} 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<E> | Iterable<R>) { | ||
const ans: boolean[] = []; | ||
for (const el of elements) { | ||
if (this.toElementFn) { | ||
ans.push(this.push(this.toElementFn(el as R))); | ||
} else { | ||
ans.push(this.push(el as E)); | ||
} | ||
} | ||
return ans; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
delete(element: E): boolean { | ||
@@ -132,5 +149,7 @@ const index = this.elements.indexOf(element); | ||
/** | ||
* The deleteAt function deletes the element at a given index. | ||
* @param index: number Determine the index of the element to be deleted | ||
* @return A boolean value | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The toArray function returns a copy of the elements in an array. | ||
* @returns An array of type E. | ||
*/ | ||
@@ -137,0 +156,0 @@ deleteAt(index: number): boolean { |
@@ -271,3 +271,3 @@ /** | ||
*/ | ||
addMany(words: Iterable<string> | Iterable<R> = []): boolean[] { | ||
addMany(words: Iterable<string> | Iterable<R>): boolean[] { | ||
const ans: boolean[] = []; | ||
@@ -370,5 +370,10 @@ for (const word of words) { | ||
/** | ||
* 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. | ||
*/ | ||
@@ -375,0 +380,0 @@ getHeight(): number { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2227685
43590
Updateddata-structure-typed@^1.53.8