undirected-graph-typed
Advanced tools
Comparing version 1.48.0 to 1.48.1
@@ -465,2 +465,40 @@ /** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function "keys" returns an array of keys from a given object. | ||
* @returns an array of BTNKey objects. | ||
*/ | ||
keys(): BTNKey[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function "values" returns an array of values from a map-like object. | ||
* @returns The `values()` method is returning an array of values (`V`) from the entries in the | ||
* object. | ||
*/ | ||
values(): (V | undefined)[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a new tree object and copies all the nodes from the original tree to | ||
* the new tree. | ||
* @returns The `clone()` method is returning a cloned instance of the `TREE` object. | ||
*/ | ||
clone(): TREE; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(1) | ||
@@ -467,0 +505,0 @@ */ |
@@ -141,2 +141,14 @@ /** | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a deep copy of a tree object. | ||
* @returns The `clone()` method is returning a cloned instance of the `TREE` object. | ||
*/ | ||
clone(): TREE; | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
@@ -143,0 +155,0 @@ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. |
@@ -284,2 +284,18 @@ "use strict"; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a deep copy of a tree object. | ||
* @returns The `clone()` method is returning a cloned instance of the `TREE` object. | ||
*/ | ||
clone() { | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add([node.key, node.value], node.count)); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
@@ -286,0 +302,0 @@ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. |
@@ -8,4 +8,156 @@ /** | ||
*/ | ||
import { HashMapLinkedNode, HashMapOptions } from '../../types'; | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
export declare class HashMap<K = any, V = any> { | ||
protected _store: { | ||
[key: string]: HashMapStoreItem<K, V>; | ||
}; | ||
protected _objMap: Map<object, V>; | ||
/** | ||
* The constructor function initializes a new instance of a class with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It | ||
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with | ||
* key-value pairs. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
* configuration options for the constructor. In this case, it has one property: | ||
*/ | ||
constructor(elements?: Iterable<[K, V]>, options?: { | ||
hashFn: (key: K) => string; | ||
}); | ||
protected _size: number; | ||
get size(): number; | ||
isEmpty(): boolean; | ||
clear(): void; | ||
/** | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
* the key is not already present. | ||
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It | ||
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be | ||
* stored in a regular JavaScript object. | ||
* @param {V} value - The value parameter represents the value that you want to associate with the | ||
* key in the data structure. | ||
*/ | ||
set(key: K, value: V): void; | ||
/** | ||
* The function "setMany" sets multiple key-value pairs in a map. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two elements: the key and the value. | ||
*/ | ||
setMany(elements: Iterable<[K, V]>): void; | ||
/** | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
* a string map. | ||
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be | ||
* of any type, but it should be compatible with the key type used when the map was created. | ||
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap` | ||
* or `_store`, otherwise it returns `undefined`. | ||
*/ | ||
get(key: K): V | undefined; | ||
/** | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
* is an object key or not. | ||
* @param {K} key - The parameter "key" is of type K, which means it can be any type. | ||
* @returns The `has` method is returning a boolean value. | ||
*/ | ||
has(key: K): boolean; | ||
/** | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
* data structure. | ||
* @returns The `delete` method returns a boolean value. It returns `true` if the key was | ||
* successfully deleted from the map, and `false` if the key was not found in the map. | ||
*/ | ||
delete(key: K): boolean; | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<[K, V]>; | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
entries(): IterableIterator<[K, V]>; | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
keys(): IterableIterator<K>; | ||
values(): IterableIterator<V>; | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean; | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean; | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void; | ||
/** | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
* key-value pair in the original HashMap. | ||
* @param callbackfn - The callback function that will be called for each key-value pair in the | ||
* HashMap. It takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value to the `callbackfn` function. If `thisArg | ||
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on | ||
* the provided callback function. | ||
*/ | ||
map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U>; | ||
/** | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
* that satisfy a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to determine whether an element should be included in the filtered map | ||
* or not. The function should return a boolean value - true if the element should be included, and | ||
* false otherwise. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs | ||
* from the original `HashMap` that pass the provided `predicate` function. | ||
*/ | ||
filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V>; | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
*/ | ||
reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U; | ||
print(): void; | ||
protected _hashFn: (key: K) => string; | ||
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any)); | ||
protected _getNoObjKey(key: K): string; | ||
} | ||
export declare class LinkedHashMap<K = any, V = any> { | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>; | ||
@@ -61,2 +213,6 @@ protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>; | ||
set(key: K, value?: V): number; | ||
has(key: K): boolean; | ||
setMany(entries: Iterable<[K, V]>): void; | ||
keys(): K[]; | ||
values(): V[]; | ||
/** | ||
@@ -125,34 +281,35 @@ * Time Complexity: O(1) | ||
clear(): void; | ||
clone(): LinkedHashMap<K, V>; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a HashMap and executes a callback function on | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* HashMap. It takes three arguments: | ||
* LinkedHashMap. It takes three arguments: | ||
*/ | ||
forEach(callback: (element: [K, V], index: number, hashMap: HashMap<K, V>) => void): void; | ||
forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void): void; | ||
/** | ||
* The `filter` function takes a predicate function and returns a new HashMap containing only the | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V>; | ||
filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V>; | ||
/** | ||
* The `map` function takes a callback function and returns a new HashMap with the values transformed | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object with the values mapped according to the provided callback function. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
*/ | ||
map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV>; | ||
map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV>; | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the HashMap and is used to accumulate a single | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
@@ -163,7 +320,7 @@ * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the HashMap and applying the callback function to each element. | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A; | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -170,0 +327,0 @@ * |
@@ -10,5 +10,306 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HashMap = void 0; | ||
exports.LinkedHashMap = exports.HashMap = void 0; | ||
const utils_1 = require("../../utils"); | ||
class HashMap { | ||
/** | ||
* The constructor function initializes a new instance of a class with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It | ||
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with | ||
* key-value pairs. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
* configuration options for the constructor. In this case, it has one property: | ||
*/ | ||
constructor(elements = [], options) { | ||
this._store = {}; | ||
this._objMap = new Map(); | ||
this._size = 0; | ||
this._hashFn = (key) => String(key); | ||
if (options) { | ||
const { hashFn } = options; | ||
if (hashFn) { | ||
this._hashFn = hashFn; | ||
} | ||
} | ||
if (elements) { | ||
this.setMany(elements); | ||
} | ||
} | ||
get size() { | ||
return this._size; | ||
} | ||
isEmpty() { | ||
return this.size === 0; | ||
} | ||
clear() { | ||
this._store = {}; | ||
this._objMap.clear(); | ||
this._size = 0; | ||
} | ||
/** | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
* the key is not already present. | ||
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It | ||
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be | ||
* stored in a regular JavaScript object. | ||
* @param {V} value - The value parameter represents the value that you want to associate with the | ||
* key in the data structure. | ||
*/ | ||
set(key, value) { | ||
if (this._isObjKey(key)) { | ||
if (!this._objMap.has(key)) { | ||
this._size++; | ||
} | ||
this._objMap.set(key, value); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
if (this._store[strKey] === undefined) { | ||
this._size++; | ||
} | ||
this._store[strKey] = { key, value }; | ||
} | ||
} | ||
/** | ||
* The function "setMany" sets multiple key-value pairs in a map. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two elements: the key and the value. | ||
*/ | ||
setMany(elements) { | ||
for (const [key, value] of elements) | ||
this.set(key, value); | ||
} | ||
/** | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
* a string map. | ||
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be | ||
* of any type, but it should be compatible with the key type used when the map was created. | ||
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap` | ||
* or `_store`, otherwise it returns `undefined`. | ||
*/ | ||
get(key) { | ||
var _a; | ||
if (this._isObjKey(key)) { | ||
return this._objMap.get(key); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
return (_a = this._store[strKey]) === null || _a === void 0 ? void 0 : _a.value; | ||
} | ||
} | ||
/** | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
* is an object key or not. | ||
* @param {K} key - The parameter "key" is of type K, which means it can be any type. | ||
* @returns The `has` method is returning a boolean value. | ||
*/ | ||
has(key) { | ||
if (this._isObjKey(key)) { | ||
return this._objMap.has(key); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
return strKey in this._store; | ||
} | ||
} | ||
/** | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
* data structure. | ||
* @returns The `delete` method returns a boolean value. It returns `true` if the key was | ||
* successfully deleted from the map, and `false` if the key was not found in the map. | ||
*/ | ||
delete(key) { | ||
if (this._isObjKey(key)) { | ||
if (this._objMap.has(key)) { | ||
this._size--; | ||
} | ||
return this._objMap.delete(key); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
if (strKey in this._store) { | ||
delete this._store[strKey]; | ||
this._size--; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value]; | ||
} | ||
for (const node of this._objMap) { | ||
yield node; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
*entries() { | ||
for (const item of this) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
*keys() { | ||
for (const [key] of this) { | ||
yield key; | ||
} | ||
} | ||
*values() { | ||
for (const [, value] of this) { | ||
yield value; | ||
} | ||
} | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (!predicate.call(thisArg, value, key, index++, this)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn, thisArg) { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
callbackfn.call(thisArg, value, key, index++, this); | ||
} | ||
} | ||
/** | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
* key-value pair in the original HashMap. | ||
* @param callbackfn - The callback function that will be called for each key-value pair in the | ||
* HashMap. It takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value to the `callbackfn` function. If `thisArg | ||
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on | ||
* the provided callback function. | ||
*/ | ||
map(callbackfn, thisArg) { | ||
const resultMap = new HashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this)); | ||
} | ||
return resultMap; | ||
} | ||
/** | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
* that satisfy a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to determine whether an element should be included in the filtered map | ||
* or not. The function should return a boolean value - true if the element should be included, and | ||
* false otherwise. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs | ||
* from the original `HashMap` that pass the provided `predicate` function. | ||
*/ | ||
filter(predicate, thisArg) { | ||
const filteredMap = new HashMap(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
filteredMap.set(key, value); | ||
} | ||
} | ||
return filteredMap; | ||
} | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
*/ | ||
reduce(callbackfn, initialValue) { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
accumulator = callbackfn(accumulator, value, key, index++, this); | ||
} | ||
return accumulator; | ||
} | ||
print() { | ||
console.log([...this.entries()]); | ||
} | ||
_isObjKey(key) { | ||
const keyType = typeof key; | ||
return (keyType === 'object' || keyType === 'function') && key !== null; | ||
} | ||
_getNoObjKey(key) { | ||
const keyType = typeof key; | ||
let strKey; | ||
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") { | ||
strKey = this._hashFn(key); | ||
} | ||
else { | ||
if (keyType === "number") { | ||
// TODO numeric key should has its own hash | ||
strKey = key; | ||
} | ||
else { | ||
strKey = key; | ||
} | ||
} | ||
return strKey; | ||
} | ||
} | ||
exports.HashMap = HashMap; | ||
class LinkedHashMap { | ||
constructor(elements, options = { | ||
@@ -96,44 +397,71 @@ hashFn: (key) => String(key), | ||
let node; | ||
const isNewKey = !this.has(key); // Check if the key is new | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
node = this._objMap.get(hash); | ||
if (node) { | ||
// If the node already exists, update its value | ||
node.value = value; | ||
} | ||
else { | ||
if (!node && isNewKey) { | ||
// Create new node | ||
node = { key: hash, value, prev: this._tail, next: this._sentinel }; | ||
// Add new nodes to _objMap and linked list | ||
this._objMap.set(hash, node); | ||
} | ||
else if (node) { | ||
// Update the value of an existing node | ||
node.value = value; | ||
} | ||
} | ||
else { | ||
const hash = this._hashFn(key); | ||
// Non-object keys are handled in the same way as the original implementation | ||
node = this._noObjMap[hash]; | ||
if (node) { | ||
if (!node && isNewKey) { | ||
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel }; | ||
} | ||
else if (node) { | ||
// Update the value of an existing node | ||
node.value = value; | ||
} | ||
} | ||
if (node && isNewKey) { | ||
// Update the head and tail of the linked list | ||
if (this._size === 0) { | ||
this._head = node; | ||
this._sentinel.next = node; | ||
} | ||
else { | ||
this._noObjMap[hash] = node = { | ||
key, | ||
value, | ||
prev: this._tail, | ||
next: this._sentinel | ||
}; | ||
this._tail.next = node; | ||
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node | ||
} | ||
this._tail = node; | ||
this._sentinel.prev = node; | ||
this._size++; | ||
} | ||
if (this._size === 0) { | ||
this._head = node; | ||
this._sentinel.next = node; | ||
return this._size; | ||
} | ||
has(key) { | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
return this._objMap.has(hash); | ||
} | ||
else { | ||
this._tail.next = node; | ||
const hash = this._hashFn(key); | ||
return hash in this._noObjMap; | ||
} | ||
this._tail = node; | ||
this._sentinel.prev = node; | ||
this._size++; | ||
return this._size; | ||
} | ||
setMany(entries) { | ||
for (const entry of entries) { | ||
const [key, value] = entry; | ||
this.set(key, value); | ||
} | ||
} | ||
keys() { | ||
const keys = []; | ||
for (const [key] of this) | ||
keys.push(key); | ||
return keys; | ||
} | ||
values() { | ||
const values = []; | ||
for (const [, value] of this) | ||
values.push(value); | ||
return values; | ||
} | ||
/** | ||
@@ -259,10 +587,18 @@ * Time Complexity: O(1) | ||
} | ||
clone() { | ||
const cloned = new LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn }); | ||
for (const entry of this) { | ||
const [key, value] = entry; | ||
cloned.set(key, value); | ||
} | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a HashMap and executes a callback function on | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* HashMap. It takes three arguments: | ||
* LinkedHashMap. It takes three arguments: | ||
*/ | ||
@@ -278,11 +614,11 @@ forEach(callback) { | ||
/** | ||
* The `filter` function takes a predicate function and returns a new HashMap containing only the | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate) { | ||
const filteredMap = new HashMap(); | ||
const filteredMap = new LinkedHashMap(); | ||
let index = 0; | ||
@@ -298,10 +634,10 @@ for (const [key, value] of this) { | ||
/** | ||
* The `map` function takes a callback function and returns a new HashMap with the values transformed | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object with the values mapped according to the provided callback function. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
*/ | ||
map(callback) { | ||
const mappedMap = new HashMap(); | ||
const mappedMap = new LinkedHashMap(); | ||
let index = 0; | ||
@@ -316,6 +652,6 @@ for (const [key, value] of this) { | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the HashMap and is used to accumulate a single | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
@@ -326,3 +662,3 @@ * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the HashMap and applying the callback function to each element. | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
*/ | ||
@@ -339,3 +675,3 @@ reduce(callback, initialValue) { | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -378,2 +714,2 @@ * | ||
} | ||
exports.HashMap = HashMap; | ||
exports.LinkedHashMap = LinkedHashMap; |
@@ -11,1 +11,5 @@ export type HashMapLinkedNode<K, V> = { | ||
}; | ||
export type HashMapStoreItem<K, V> = { | ||
key: K; | ||
value: V; | ||
}; |
{ | ||
"name": "undirected-graph-typed", | ||
"version": "1.48.0", | ||
"version": "1.48.1", | ||
"description": "Undirected Graph. Javascript & Typescript Data Structure.", | ||
@@ -148,4 +148,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.48.0" | ||
"data-structure-typed": "^1.48.1" | ||
} | ||
} |
@@ -322,2 +322,20 @@ /** | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a deep copy of a tree object. | ||
* @returns The `clone()` method is returning a cloned instance of the `TREE` object. | ||
*/ | ||
override clone(): TREE { | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add([node.key, node.value], node.count)); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
@@ -324,0 +342,0 @@ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. |
@@ -10,6 +10,330 @@ /** | ||
import { isWeakKey, rangeCheck } from '../../utils'; | ||
import { HashMapLinkedNode, HashMapOptions } from '../../types'; | ||
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
export class HashMap<K = any, V = any> { | ||
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {}; | ||
protected _objMap: Map<object, V> = new Map(); | ||
/** | ||
* The constructor function initializes a new instance of a class with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It | ||
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with | ||
* key-value pairs. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
* configuration options for the constructor. In this case, it has one property: | ||
*/ | ||
constructor(elements: Iterable<[K, V]> = [], options?: { | ||
hashFn: (key: K) => string | ||
}) { | ||
if (options) { | ||
const { hashFn } = options; | ||
if (hashFn) { | ||
this._hashFn = hashFn; | ||
} | ||
} | ||
if (elements) { | ||
this.setMany(elements); | ||
} | ||
} | ||
protected _size = 0; | ||
get size(): number { | ||
return this._size; | ||
} | ||
isEmpty(): boolean { | ||
return this.size === 0; | ||
} | ||
clear() { | ||
this._store = {}; | ||
this._objMap.clear(); | ||
this._size = 0; | ||
} | ||
/** | ||
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if | ||
* the key is not already present. | ||
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It | ||
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be | ||
* stored in a regular JavaScript object. | ||
* @param {V} value - The value parameter represents the value that you want to associate with the | ||
* key in the data structure. | ||
*/ | ||
set(key: K, value: V) { | ||
if (this._isObjKey(key)) { | ||
if (!this._objMap.has(key)) { | ||
this._size++; | ||
} | ||
this._objMap.set(key, value); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
if (this._store[strKey] === undefined) { | ||
this._size++; | ||
} | ||
this._store[strKey] = { key, value }; | ||
} | ||
} | ||
/** | ||
* The function "setMany" sets multiple key-value pairs in a map. | ||
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two elements: the key and the value. | ||
*/ | ||
setMany(elements: Iterable<[K, V]>) { | ||
for (const [key, value] of elements) this.set(key, value); | ||
} | ||
/** | ||
* The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
* a string map. | ||
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be | ||
* of any type, but it should be compatible with the key type used when the map was created. | ||
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap` | ||
* or `_store`, otherwise it returns `undefined`. | ||
*/ | ||
get(key: K): V | undefined { | ||
if (this._isObjKey(key)) { | ||
return this._objMap.get(key); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
return this._store[strKey]?.value; | ||
} | ||
} | ||
/** | ||
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it | ||
* is an object key or not. | ||
* @param {K} key - The parameter "key" is of type K, which means it can be any type. | ||
* @returns The `has` method is returning a boolean value. | ||
*/ | ||
has(key: K): boolean { | ||
if (this._isObjKey(key)) { | ||
return this._objMap.has(key); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
return strKey in this._store; | ||
} | ||
} | ||
/** | ||
* The `delete` function removes an element from a map-like data structure based on the provided key. | ||
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the | ||
* data structure. | ||
* @returns The `delete` method returns a boolean value. It returns `true` if the key was | ||
* successfully deleted from the map, and `false` if the key was not found in the map. | ||
*/ | ||
delete(key: K): boolean { | ||
if (this._isObjKey(key)) { | ||
if (this._objMap.has(key)) { | ||
this._size-- | ||
} | ||
return this._objMap.delete(key); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
if (strKey in this._store) { | ||
delete this._store[strKey]; | ||
this._size--; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from both an object store and an | ||
* object map. | ||
*/ | ||
* [Symbol.iterator](): IterableIterator<[K, V]> { | ||
for (const node of Object.values(this._store)) { | ||
yield [node.key, node.value] as [K, V]; | ||
} | ||
for (const node of this._objMap) { | ||
yield node as [K, V]; | ||
} | ||
} | ||
/** | ||
* The function returns an iterator that yields key-value pairs from the object. | ||
*/ | ||
* entries(): IterableIterator<[K, V]> { | ||
for (const item of this) { | ||
yield item; | ||
} | ||
} | ||
/** | ||
* The function `keys()` returns an iterator that yields all the keys of the object. | ||
*/ | ||
* keys(): IterableIterator<K> { | ||
for (const [key] of this) { | ||
yield key; | ||
} | ||
} | ||
* values(): IterableIterator<V> { | ||
for (const [, value] of this) { | ||
yield value; | ||
} | ||
} | ||
/** | ||
* The `every` function checks if every element in a HashMap satisfies a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to test each element in the map against a condition. If the predicate | ||
* function returns false for any element, the every() method will return false. If the predicate | ||
* function returns true for all | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The method is returning a boolean value. It returns true if the predicate function | ||
* returns true for every element in the map, and false otherwise. | ||
*/ | ||
every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (!predicate.call(thisArg, value, key, index++, this)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* The "some" function checks if at least one element in a HashMap satisfies a given predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`, | ||
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given | ||
* key-value pair in the `HashMap`. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns a boolean value. It returns true if the predicate function returns true for any element | ||
* in the map, and false otherwise. | ||
*/ | ||
some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element. | ||
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It | ||
* takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value inside the `callbackfn` function. If `thisArg | ||
*/ | ||
forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void { | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
callbackfn.call(thisArg, value, key, index++, this); | ||
} | ||
} | ||
/** | ||
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each | ||
* key-value pair in the original HashMap. | ||
* @param callbackfn - The callback function that will be called for each key-value pair in the | ||
* HashMap. It takes four parameters: | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will | ||
* be passed as the `this` value to the `callbackfn` function. If `thisArg | ||
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on | ||
* the provided callback function. | ||
*/ | ||
map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U> { | ||
const resultMap = new HashMap<K, U>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this)); | ||
} | ||
return resultMap; | ||
} | ||
/** | ||
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
* that satisfy a given predicate function. | ||
* @param predicate - The predicate parameter is a function that takes four arguments: value, key, | ||
* index, and map. It is used to determine whether an element should be included in the filtered map | ||
* or not. The function should return a boolean value - true if the element should be included, and | ||
* false otherwise. | ||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value | ||
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be | ||
* passed as the `this` value to the `predicate` function. If `thisArg` is | ||
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs | ||
* from the original `HashMap` that pass the provided `predicate` function. | ||
*/ | ||
filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V> { | ||
const filteredMap = new HashMap<K, V>(); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
if (predicate.call(thisArg, value, key, index++, this)) { | ||
filteredMap.set(key, value); | ||
} | ||
} | ||
return filteredMap; | ||
} | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callbackfn - The callback function that will be called for each element in the HashMap. It | ||
* takes five parameters: | ||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It | ||
* is the value that will be used as the first argument of the callback function when reducing the | ||
* elements of the map. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over | ||
* all the elements in the `HashMap`. | ||
*/ | ||
reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U { | ||
let accumulator = initialValue; | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
accumulator = callbackfn(accumulator, value, key, index++, this); | ||
} | ||
return accumulator; | ||
} | ||
print(): void{ | ||
console.log([...this.entries()]); | ||
} | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any)) { | ||
const keyType = typeof key; | ||
return (keyType === 'object' || keyType === 'function') && key !== null | ||
} | ||
protected _getNoObjKey(key: K): string { | ||
const keyType = typeof key; | ||
let strKey: string; | ||
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") { | ||
strKey = this._hashFn(key); | ||
} else { | ||
if (keyType === "number") { | ||
// TODO numeric key should has its own hash | ||
strKey = <string>key; | ||
} else { | ||
strKey = <string>key; | ||
} | ||
} | ||
return strKey; | ||
} | ||
} | ||
export class LinkedHashMap<K = any, V = any> { | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {}; | ||
@@ -112,2 +436,3 @@ protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>(); | ||
let node; | ||
const isNewKey = !this.has(key); // Check if the key is new | ||
@@ -118,42 +443,68 @@ if (isWeakKey(key)) { | ||
if (node) { | ||
// If the node already exists, update its value | ||
node.value = value; | ||
} else { | ||
if (!node && isNewKey) { | ||
// Create new node | ||
node = { key: <K>hash, value, prev: this._tail, next: this._sentinel }; | ||
// Add new nodes to _objMap and linked list | ||
this._objMap.set(hash, node); | ||
} else if (node) { | ||
// Update the value of an existing node | ||
node.value = value; | ||
} | ||
} else { | ||
const hash = this._hashFn(key); | ||
// Non-object keys are handled in the same way as the original implementation | ||
node = this._noObjMap[hash]; | ||
if (node) { | ||
if (!node && isNewKey) { | ||
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel }; | ||
} else if (node) { | ||
// Update the value of an existing node | ||
node.value = value; | ||
} | ||
} | ||
if (node && isNewKey) { | ||
// Update the head and tail of the linked list | ||
if (this._size === 0) { | ||
this._head = node; | ||
this._sentinel.next = node; | ||
} else { | ||
this._noObjMap[hash] = node = { | ||
key, | ||
value, | ||
prev: this._tail, | ||
next: this._sentinel | ||
}; | ||
this._tail.next = node; | ||
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node | ||
} | ||
this._tail = node; | ||
this._sentinel.prev = node; | ||
this._size++; | ||
} | ||
if (this._size === 0) { | ||
this._head = node; | ||
this._sentinel.next = node; | ||
return this._size; | ||
} | ||
has(key: K): boolean { | ||
if (isWeakKey(key)) { | ||
const hash = this._objHashFn(key); | ||
return this._objMap.has(hash); | ||
} else { | ||
this._tail.next = node; | ||
const hash = this._hashFn(key); | ||
return hash in this._noObjMap; | ||
} | ||
} | ||
this._tail = node; | ||
this._sentinel.prev = node; | ||
this._size++; | ||
setMany(entries: Iterable<[K, V]>): void { | ||
for (const entry of entries) { | ||
const [key, value] = entry; | ||
this.set(key, value); | ||
} | ||
} | ||
return this._size; | ||
keys(): K[] { | ||
const keys: K[] = []; | ||
for (const [key] of this) keys.push(key); | ||
return keys; | ||
} | ||
values(): V[] { | ||
const values: V[] = []; | ||
for (const [, value] of this) values.push(value); | ||
return values; | ||
} | ||
/** | ||
@@ -289,12 +640,21 @@ * Time Complexity: O(1) | ||
clone(): LinkedHashMap<K, V> { | ||
const cloned = new LinkedHashMap<K, V>([], { hashFn: this._hashFn, objHashFn: this._objHashFn }); | ||
for (const entry of this) { | ||
const [key, value] = entry; | ||
cloned.set(key, value); | ||
} | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
* | ||
* The `forEach` function iterates over each element in a HashMap and executes a callback function on | ||
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on | ||
* each element. | ||
* @param callback - The callback parameter is a function that will be called for each element in the | ||
* HashMap. It takes three arguments: | ||
* LinkedHashMap. It takes three arguments: | ||
*/ | ||
forEach(callback: (element: [K, V], index: number, hashMap: HashMap<K, V>) => void) { | ||
forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void) { | ||
let index = 0; | ||
@@ -309,11 +669,11 @@ let node = this._head; | ||
/** | ||
* The `filter` function takes a predicate function and returns a new HashMap containing only the | ||
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the | ||
* key-value pairs that satisfy the predicate. | ||
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that | ||
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that | ||
* satisfy the given predicate function. | ||
*/ | ||
filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V> { | ||
const filteredMap = new HashMap<K, V>(); | ||
filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V> { | ||
const filteredMap = new LinkedHashMap<K, V>(); | ||
let index = 0; | ||
@@ -330,10 +690,10 @@ for (const [key, value] of this) { | ||
/** | ||
* The `map` function takes a callback function and returns a new HashMap with the values transformed | ||
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed | ||
* by the callback. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and | ||
* `map`. | ||
* @returns a new HashMap object with the values mapped according to the provided callback function. | ||
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function. | ||
*/ | ||
map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV> { | ||
const mappedMap = new HashMap<K, NV>(); | ||
map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV> { | ||
const mappedMap = new LinkedHashMap<K, NV>(); | ||
let index = 0; | ||
@@ -349,6 +709,6 @@ for (const [key, value] of this) { | ||
/** | ||
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to | ||
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to | ||
* each element, accumulating a single value. | ||
* @param callback - The callback parameter is a function that takes three arguments: accumulator, | ||
* element, and map. It is called for each element in the HashMap and is used to accumulate a single | ||
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single | ||
* result. | ||
@@ -359,5 +719,5 @@ * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It | ||
* @returns The `reduce` function is returning the final value of the accumulator after iterating | ||
* over all the elements in the HashMap and applying the callback function to each element. | ||
* over all the elements in the LinkedHashMap and applying the callback function to each element. | ||
*/ | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A { | ||
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A { | ||
let accumulator = initialValue; | ||
@@ -373,3 +733,3 @@ let index = 0; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the HashMap. | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -376,0 +736,0 @@ * |
@@ -12,1 +12,3 @@ export type HashMapLinkedNode<K, V> = { | ||
} | ||
export type HashMapStoreItem<K, V> = { key: K, value: V }; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1874403
35647
Updateddata-structure-typed@^1.48.1