max-priority-queue-typed
Advanced tools
Comparing version 1.46.2 to 1.46.3
@@ -8,44 +8,3 @@ /** | ||
*/ | ||
import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types'; | ||
/** | ||
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays, | ||
* these underlying structures have already dealt with dynamic expansion and hash collisions. | ||
* Therefore, there is no need for additional logic to handle these issues. | ||
*/ | ||
export declare class HashMapIterator<K, V> { | ||
readonly hashMap: HashMap<K, V>; | ||
readonly iterateDirection: IterateDirection; | ||
protected _node: HashMapLinkedNode<K, V>; | ||
protected readonly _sentinel: HashMapLinkedNode<K, V>; | ||
/** | ||
* This is a constructor function for a linked list iterator in a HashMap data structure. | ||
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object | ||
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair | ||
* and references to the previous and next nodes in the linked list. | ||
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It | ||
* is used to mark the beginning or end of the list and is typically used in data structures like | ||
* hash maps or linked lists to simplify operations and boundary checks. | ||
* @param hashMap - A HashMap object that stores key-value pairs. | ||
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional | ||
* parameter that specifies the direction in which the iterator should iterate over the elements of | ||
* the HashMap. It can take one of the following values: | ||
* @returns The constructor does not return anything. It is used to initialize the properties and | ||
* methods of the object being created. | ||
*/ | ||
constructor(node: HashMapLinkedNode<K, V>, sentinel: HashMapLinkedNode<K, V>, hashMap: HashMap<K, V>, iterateDirection?: IterateDirection); | ||
/** | ||
* The above function returns a Proxy object that allows access to the key and value of a node in a | ||
* data structure. | ||
* @returns The code is returning a Proxy object. | ||
*/ | ||
get current(): [K, V]; | ||
/** | ||
* The function checks if a node is accessible. | ||
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`. | ||
*/ | ||
isAccessible(): boolean; | ||
prev(): this; | ||
next(): this; | ||
clone(): HashMapIterator<K, V>; | ||
} | ||
import { HashMapLinkedNode, IterableWithSizeOrLength } from '../../types'; | ||
export declare class HashMap<K = any, V = any> { | ||
@@ -71,37 +30,2 @@ readonly OBJ_KEY_INDEX: symbol; | ||
* | ||
* The function returns a new iterator object for a HashMap. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get begin(): HashMapIterator<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns a new HashMapIterator object with the _sentinel value as both the start and | ||
* end values. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get end(): HashMapIterator<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of | ||
* a HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseBegin(): HashMapIterator<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a | ||
* HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseEnd(): HashMapIterator<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns the key-value pair at the front of a data structure. | ||
@@ -122,2 +46,11 @@ * @returns The front element of the data structure, represented as a tuple with a key (K) and a | ||
/** | ||
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs. | ||
*/ | ||
begin(): Generator<(K | V)[], void, unknown>; | ||
/** | ||
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's | ||
* key and value. | ||
*/ | ||
reverseBegin(): Generator<(K | V)[], void, unknown>; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -169,16 +102,2 @@ * Space Complexity: O(1) | ||
* | ||
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key | ||
* and whether it is an object key or not. | ||
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It | ||
* can be of any type, depending on how the HashMap is implemented. | ||
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that | ||
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that | ||
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false` | ||
* @returns a new instance of the `HashMapIterator` class. | ||
*/ | ||
getIterator(key: K, isObjectKey?: boolean): HashMapIterator<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes a key-value pair from a map-like data structure. | ||
@@ -185,0 +104,0 @@ * @param {K} key - The `key` parameter is the key that you want to delete from the data structure. |
@@ -10,109 +10,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HashMap = exports.HashMapIterator = void 0; | ||
exports.HashMap = void 0; | ||
const utils_1 = require("../../utils"); | ||
/** | ||
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays, | ||
* these underlying structures have already dealt with dynamic expansion and hash collisions. | ||
* Therefore, there is no need for additional logic to handle these issues. | ||
*/ | ||
class HashMapIterator { | ||
/** | ||
* This is a constructor function for a linked list iterator in a HashMap data structure. | ||
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object | ||
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair | ||
* and references to the previous and next nodes in the linked list. | ||
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It | ||
* is used to mark the beginning or end of the list and is typically used in data structures like | ||
* hash maps or linked lists to simplify operations and boundary checks. | ||
* @param hashMap - A HashMap object that stores key-value pairs. | ||
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional | ||
* parameter that specifies the direction in which the iterator should iterate over the elements of | ||
* the HashMap. It can take one of the following values: | ||
* @returns The constructor does not return anything. It is used to initialize the properties and | ||
* methods of the object being created. | ||
*/ | ||
constructor(node, sentinel, hashMap, iterateDirection = 0 /* IterateDirection.DEFAULT */) { | ||
this._node = node; | ||
this._sentinel = sentinel; | ||
this.iterateDirection = iterateDirection; | ||
if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) { | ||
this.prev = function () { | ||
if (this._node.prev === this._sentinel) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this._node = this._node.prev; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this._node === this._sentinel) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this._node = this._node.next; | ||
return this; | ||
}; | ||
} | ||
else { | ||
this.prev = function () { | ||
if (this._node.next === this._sentinel) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this._node = this._node.next; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this._node === this._sentinel) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this._node = this._node.prev; | ||
return this; | ||
}; | ||
} | ||
this.hashMap = hashMap; | ||
} | ||
/** | ||
* The above function returns a Proxy object that allows access to the key and value of a node in a | ||
* data structure. | ||
* @returns The code is returning a Proxy object. | ||
*/ | ||
get current() { | ||
if (this._node === this._sentinel) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
return new Proxy([], { | ||
get: (target, prop) => { | ||
if (prop === '0') | ||
return this._node.key; | ||
else if (prop === '1') | ||
return this._node.value; | ||
target[0] = this._node.key; | ||
target[1] = this._node.value; | ||
return target[prop]; | ||
}, | ||
set: (_, prop, newValue) => { | ||
if (prop !== '1') { | ||
throw new TypeError(`prop should be string '1'`); | ||
} | ||
this._node.value = newValue; | ||
return true; | ||
} | ||
}); | ||
} | ||
/** | ||
* The function checks if a node is accessible. | ||
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`. | ||
*/ | ||
isAccessible() { | ||
return this._node !== this._sentinel; | ||
} | ||
prev() { | ||
return this; | ||
} | ||
next() { | ||
return this; | ||
} | ||
clone() { | ||
return new HashMapIterator(this._node, this._sentinel, this.hashMap, this.iterateDirection); | ||
} | ||
} | ||
exports.HashMapIterator = HashMapIterator; | ||
class HashMap { | ||
@@ -144,45 +39,2 @@ /** | ||
* | ||
* The function returns a new iterator object for a HashMap. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get begin() { | ||
return new HashMapIterator(this._head, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns a new HashMapIterator object with the _sentinel value as both the start and | ||
* end values. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get end() { | ||
return new HashMapIterator(this._sentinel, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of | ||
* a HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseBegin() { | ||
return new HashMapIterator(this._tail, this._sentinel, this, 1 /* IterateDirection.REVERSE */); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a | ||
* HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseEnd() { | ||
return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* IterateDirection.REVERSE */); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns the key-value pair at the front of a data structure. | ||
@@ -211,2 +63,23 @@ * @returns The front element of the data structure, represented as a tuple with a key (K) and a | ||
/** | ||
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs. | ||
*/ | ||
*begin() { | ||
let node = this._head; | ||
while (node !== this._sentinel) { | ||
yield [node.key, node.value]; | ||
node = node.next; | ||
} | ||
} | ||
/** | ||
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's | ||
* key and value. | ||
*/ | ||
*reverseBegin() { | ||
let node = this._tail; | ||
while (node !== this._sentinel) { | ||
yield [node.key, node.value]; | ||
node = node.prev; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -315,31 +188,2 @@ * Space Complexity: O(1) | ||
* | ||
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key | ||
* and whether it is an object key or not. | ||
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It | ||
* can be of any type, depending on how the HashMap is implemented. | ||
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that | ||
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that | ||
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false` | ||
* @returns a new instance of the `HashMapIterator` class. | ||
*/ | ||
getIterator(key, isObjectKey) { | ||
let node; | ||
if (isObjectKey) { | ||
const index = key[this.OBJ_KEY_INDEX]; | ||
if (index === undefined) { | ||
node = this._sentinel; | ||
} | ||
else { | ||
node = this._nodes[index]; | ||
} | ||
} | ||
else { | ||
node = this._orgMap[key] || this._sentinel; | ||
} | ||
return new HashMapIterator(node, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes a key-value pair from a map-like data structure. | ||
@@ -346,0 +190,0 @@ * @param {K} key - The `key` parameter is the key that you want to delete from the data structure. |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import { IterableWithSizeOrLength, IterateDirection } from "../../types"; | ||
import { IterableWithSizeOrLength } from "../../types"; | ||
/** | ||
@@ -16,28 +16,2 @@ * Deque can provide random access with O(1) time complexity | ||
*/ | ||
export declare class DequeIterator<E> { | ||
iterateDirection: IterateDirection; | ||
index: number; | ||
readonly deque: Deque<E>; | ||
/** | ||
* The constructor initializes the index, iterate direction, and prev/next functions for a | ||
* DequeIterator object. | ||
* @param {number} index - The index parameter represents the current index position of the iterator | ||
* within the deque. It is a number that indicates the position of the element that the iterator is | ||
* currently pointing to. | ||
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a | ||
* double-ended queue data structure, which allows elements to be added or removed from both ends. | ||
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies | ||
* the direction in which the iterator should iterate over the elements of the `deque`. It has a | ||
* default value of `IterateDirection.DEFAULT`. | ||
* @returns The constructor is not returning anything. It is used to initialize the properties of the | ||
* object being created. | ||
*/ | ||
constructor(index: number, deque: Deque<E>, iterateDirection?: IterateDirection); | ||
get current(): E; | ||
set current(newElement: E); | ||
isAccessible(): boolean; | ||
prev(): DequeIterator<E>; | ||
next(): DequeIterator<E>; | ||
clone(): DequeIterator<E>; | ||
} | ||
export declare class Deque<E> { | ||
@@ -125,25 +99,11 @@ protected _bucketFirst: number; | ||
/** | ||
* The `begin()` function returns a new iterator for a deque starting from the first element. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The below function is a generator that yields elements from a collection one by one. | ||
*/ | ||
begin(): DequeIterator<E>; | ||
begin(): Generator<E>; | ||
/** | ||
* The `end()` function returns a new `DequeIterator` object with the size and reference to the | ||
* current deque. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from | ||
* the last element. | ||
*/ | ||
end(): DequeIterator<E>; | ||
reverseBegin(): Generator<E>; | ||
/** | ||
* The reverseBegin function returns a new DequeIterator object that starts at the last element of | ||
* the deque and iterates in reverse direction. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseBegin(): DequeIterator<E>; | ||
/** | ||
* The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a | ||
* Deque in reverse order. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseEnd(): DequeIterator<E>; | ||
/** | ||
* Time Complexity - Amortized O(1) (possible reallocation) | ||
@@ -308,30 +268,2 @@ * Space Complexity - O(n) (due to potential resizing). | ||
* | ||
* The function deletes an element from a deque using an iterator and returns the next iterator. | ||
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object | ||
* that is used to iterate over elements in a deque (double-ended queue). | ||
* @returns the updated iterator after deleting an element from the deque. | ||
*/ | ||
deleteByIterator(iter: DequeIterator<E>): DequeIterator<E>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to | ||
* the element if found, otherwise it returns an iterator pointing to the end of the deque. | ||
* @param {E} element - The `element` parameter is the element that you want to find in the deque. | ||
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object. | ||
*/ | ||
findIterator(element: E): DequeIterator<E>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverse() function reverses the order of the buckets and the elements within each bucket in a | ||
@@ -338,0 +270,0 @@ * data structure. |
@@ -10,3 +10,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ObjectDeque = exports.Deque = exports.DequeIterator = void 0; | ||
exports.ObjectDeque = exports.Deque = void 0; | ||
const utils_1 = require("../../utils"); | ||
@@ -19,74 +19,2 @@ /** | ||
*/ | ||
class DequeIterator { | ||
/** | ||
* The constructor initializes the index, iterate direction, and prev/next functions for a | ||
* DequeIterator object. | ||
* @param {number} index - The index parameter represents the current index position of the iterator | ||
* within the deque. It is a number that indicates the position of the element that the iterator is | ||
* currently pointing to. | ||
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a | ||
* double-ended queue data structure, which allows elements to be added or removed from both ends. | ||
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies | ||
* the direction in which the iterator should iterate over the elements of the `deque`. It has a | ||
* default value of `IterateDirection.DEFAULT`. | ||
* @returns The constructor is not returning anything. It is used to initialize the properties of the | ||
* object being created. | ||
*/ | ||
constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) { | ||
this.index = index; | ||
this.iterateDirection = iterateDirection; | ||
if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) { | ||
this.prev = function () { | ||
if (this.index === 0) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this.index -= 1; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this.index === this.deque.size) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this.index += 1; | ||
return this; | ||
}; | ||
} | ||
else { | ||
this.prev = function () { | ||
if (this.index === this.deque.size - 1) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this.index += 1; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this.index === -1) { | ||
(0, utils_1.throwRangeError)(); | ||
} | ||
this.index -= 1; | ||
return this; | ||
}; | ||
} | ||
this.deque = deque; | ||
} | ||
get current() { | ||
return this.deque.getAt(this.index); | ||
} | ||
set current(newElement) { | ||
this.deque.setAt(this.index, newElement); | ||
} | ||
isAccessible() { | ||
return this.index !== this.deque.size; | ||
} | ||
prev() { | ||
return this; | ||
} | ||
next() { | ||
return this; | ||
} | ||
clone() { | ||
return new DequeIterator(this.index, this.deque, this.iterateDirection); | ||
} | ||
} | ||
exports.DequeIterator = DequeIterator; | ||
class Deque { | ||
@@ -225,33 +153,23 @@ /** | ||
/** | ||
* The `begin()` function returns a new iterator for a deque starting from the first element. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The below function is a generator that yields elements from a collection one by one. | ||
*/ | ||
begin() { | ||
return new DequeIterator(0, this); | ||
*begin() { | ||
let index = 0; | ||
while (index < this.size) { | ||
yield this.getAt(index); | ||
index++; | ||
} | ||
} | ||
/** | ||
* The `end()` function returns a new `DequeIterator` object with the size and reference to the | ||
* current deque. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from | ||
* the last element. | ||
*/ | ||
end() { | ||
return new DequeIterator(this.size, this); | ||
*reverseBegin() { | ||
let index = this.size - 1; | ||
while (index >= 0) { | ||
yield this.getAt(index); | ||
index--; | ||
} | ||
} | ||
/** | ||
* The reverseBegin function returns a new DequeIterator object that starts at the last element of | ||
* the deque and iterates in reverse direction. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseBegin() { | ||
return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */); | ||
} | ||
/** | ||
* The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a | ||
* Deque in reverse order. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseEnd() { | ||
return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */); | ||
} | ||
/** | ||
* Time Complexity - Amortized O(1) (possible reallocation) | ||
@@ -405,3 +323,3 @@ * Space Complexity - O(n) (due to potential resizing). | ||
getAt(pos) { | ||
utils_1.rangeCheck(pos, 0, this.size - 1); | ||
(0, utils_1.rangeCheck)(pos, 0, this.size - 1); | ||
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos); | ||
@@ -425,3 +343,3 @@ return this._buckets[bucketIndex][indexInBucket]; | ||
setAt(pos, element) { | ||
utils_1.rangeCheck(pos, 0, this.size - 1); | ||
(0, utils_1.rangeCheck)(pos, 0, this.size - 1); | ||
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos); | ||
@@ -451,3 +369,3 @@ this._buckets[bucketIndex][indexInBucket] = element; | ||
const length = this.size; | ||
utils_1.rangeCheck(pos, 0, length); | ||
(0, utils_1.rangeCheck)(pos, 0, length); | ||
if (pos === 0) { | ||
@@ -515,3 +433,3 @@ while (num--) | ||
deleteAt(pos) { | ||
utils_1.rangeCheck(pos, 0, this.size - 1); | ||
(0, utils_1.rangeCheck)(pos, 0, this.size - 1); | ||
if (pos === 0) | ||
@@ -573,42 +491,2 @@ this.shift(); | ||
* | ||
* The function deletes an element from a deque using an iterator and returns the next iterator. | ||
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object | ||
* that is used to iterate over elements in a deque (double-ended queue). | ||
* @returns the updated iterator after deleting an element from the deque. | ||
*/ | ||
deleteByIterator(iter) { | ||
const index = iter.index; | ||
this.deleteAt(index); | ||
iter = iter.next(); | ||
return iter; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to | ||
* the element if found, otherwise it returns an iterator pointing to the end of the deque. | ||
* @param {E} element - The `element` parameter is the element that you want to find in the deque. | ||
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object. | ||
*/ | ||
findIterator(element) { | ||
for (let i = 0; i < this.size; ++i) { | ||
if (this.getAt(i) === element) { | ||
return new DequeIterator(i, this); | ||
} | ||
} | ||
return this.end(); | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverse() function reverses the order of the buckets and the elements within each bucket in a | ||
@@ -615,0 +493,0 @@ * data structure. |
{ | ||
"name": "max-priority-queue-typed", | ||
"version": "1.46.2", | ||
"version": "1.46.3", | ||
"description": "Max Priority Queue. Javascript & Typescript Data Structure.", | ||
@@ -130,4 +130,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.46.2" | ||
"data-structure-typed": "^1.46.3" | ||
} | ||
} |
@@ -9,124 +9,5 @@ /** | ||
import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils'; | ||
import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types'; | ||
import { isObjOrFunc, rangeCheck } from '../../utils'; | ||
import { HashMapLinkedNode, IterableWithSizeOrLength } from '../../types'; | ||
/** | ||
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays, | ||
* these underlying structures have already dealt with dynamic expansion and hash collisions. | ||
* Therefore, there is no need for additional logic to handle these issues. | ||
*/ | ||
export class HashMapIterator<K, V> { | ||
readonly hashMap: HashMap<K, V>; | ||
readonly iterateDirection: IterateDirection; | ||
protected _node: HashMapLinkedNode<K, V>; | ||
protected readonly _sentinel: HashMapLinkedNode<K, V>; | ||
/** | ||
* This is a constructor function for a linked list iterator in a HashMap data structure. | ||
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object | ||
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair | ||
* and references to the previous and next nodes in the linked list. | ||
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It | ||
* is used to mark the beginning or end of the list and is typically used in data structures like | ||
* hash maps or linked lists to simplify operations and boundary checks. | ||
* @param hashMap - A HashMap object that stores key-value pairs. | ||
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional | ||
* parameter that specifies the direction in which the iterator should iterate over the elements of | ||
* the HashMap. It can take one of the following values: | ||
* @returns The constructor does not return anything. It is used to initialize the properties and | ||
* methods of the object being created. | ||
*/ | ||
constructor( | ||
node: HashMapLinkedNode<K, V>, | ||
sentinel: HashMapLinkedNode<K, V>, | ||
hashMap: HashMap<K, V>, | ||
iterateDirection: IterateDirection = IterateDirection.DEFAULT | ||
) { | ||
this._node = node; | ||
this._sentinel = sentinel; | ||
this.iterateDirection = iterateDirection; | ||
if (this.iterateDirection === IterateDirection.DEFAULT) { | ||
this.prev = function () { | ||
if (this._node.prev === this._sentinel) { | ||
throwRangeError(); | ||
} | ||
this._node = this._node.prev; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this._node === this._sentinel) { | ||
throwRangeError(); | ||
} | ||
this._node = this._node.next; | ||
return this; | ||
}; | ||
} else { | ||
this.prev = function () { | ||
if (this._node.next === this._sentinel) { | ||
throwRangeError(); | ||
} | ||
this._node = this._node.next; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this._node === this._sentinel) { | ||
throwRangeError(); | ||
} | ||
this._node = this._node.prev; | ||
return this; | ||
}; | ||
} | ||
this.hashMap = hashMap; | ||
} | ||
/** | ||
* The above function returns a Proxy object that allows access to the key and value of a node in a | ||
* data structure. | ||
* @returns The code is returning a Proxy object. | ||
*/ | ||
get current() { | ||
if (this._node === this._sentinel) { | ||
throwRangeError(); | ||
} | ||
return new Proxy(<[K, V]>(<unknown>[]), { | ||
get: (target, prop: '0' | '1') => { | ||
if (prop === '0') return this._node.key; | ||
else if (prop === '1') return this._node.value; | ||
target[0] = this._node.key; | ||
target[1] = this._node.value; | ||
return target[prop]; | ||
}, | ||
set: (_, prop: '1', newValue: V) => { | ||
if (prop !== '1') { | ||
throw new TypeError(`prop should be string '1'`); | ||
} | ||
this._node.value = newValue; | ||
return true; | ||
} | ||
}); | ||
} | ||
/** | ||
* The function checks if a node is accessible. | ||
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`. | ||
*/ | ||
isAccessible() { | ||
return this._node !== this._sentinel; | ||
} | ||
prev() { | ||
return this; | ||
} | ||
next() { | ||
return this; | ||
} | ||
clone() { | ||
return new HashMapIterator<K, V>(this._node, this._sentinel, this.hashMap, this.iterateDirection) | ||
} | ||
} | ||
export class HashMap<K = any, V = any> { | ||
@@ -166,49 +47,2 @@ readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX'); | ||
* | ||
* The function returns a new iterator object for a HashMap. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get begin() { | ||
return new HashMapIterator<K, V>(this._head, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns a new HashMapIterator object with the _sentinel value as both the start and | ||
* end values. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get end() { | ||
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of | ||
* a HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseBegin() { | ||
return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a | ||
* HashMap in reverse order. | ||
* @returns A new instance of the HashMapIterator class is being returned. | ||
*/ | ||
get reverseEnd() { | ||
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function returns the key-value pair at the front of a data structure. | ||
@@ -237,2 +71,25 @@ * @returns The front element of the data structure, represented as a tuple with a key (K) and a | ||
/** | ||
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs. | ||
*/ | ||
* begin() { | ||
let node = this._head; | ||
while (node !== this._sentinel) { | ||
yield [node.key, node.value]; | ||
node = node.next; | ||
} | ||
} | ||
/** | ||
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's | ||
* key and value. | ||
*/ | ||
* reverseBegin() { | ||
let node = this._tail; | ||
while (node !== this._sentinel) { | ||
yield [node.key, node.value]; | ||
node = node.prev; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -342,30 +199,2 @@ * Space Complexity: O(1) | ||
* | ||
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key | ||
* and whether it is an object key or not. | ||
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It | ||
* can be of any type, depending on how the HashMap is implemented. | ||
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that | ||
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that | ||
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false` | ||
* @returns a new instance of the `HashMapIterator` class. | ||
*/ | ||
getIterator(key: K, isObjectKey?: boolean) { | ||
let node: HashMapLinkedNode<K, V>; | ||
if (isObjectKey) { | ||
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX]; | ||
if (index === undefined) { | ||
node = this._sentinel; | ||
} else { | ||
node = this._nodes[index]; | ||
} | ||
} else { | ||
node = this._orgMap[<string>(<unknown>key)] || this._sentinel; | ||
} | ||
return new HashMapIterator<K, V>(node, this._sentinel, this); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `delete` function removes a key-value pair from a map-like data structure. | ||
@@ -372,0 +201,0 @@ * @param {K} key - The `key` parameter is the key that you want to delete from the data structure. |
@@ -10,4 +10,4 @@ /** | ||
import { IterableWithSizeOrLength, IterateDirection } from "../../types"; | ||
import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils"; | ||
import { IterableWithSizeOrLength } from "../../types"; | ||
import { calcMinUnitsRequired, rangeCheck } from "../../utils"; | ||
@@ -21,85 +21,2 @@ /** | ||
export class DequeIterator<E> { | ||
iterateDirection: IterateDirection; | ||
index: number; | ||
readonly deque: Deque<E>; | ||
/** | ||
* The constructor initializes the index, iterate direction, and prev/next functions for a | ||
* DequeIterator object. | ||
* @param {number} index - The index parameter represents the current index position of the iterator | ||
* within the deque. It is a number that indicates the position of the element that the iterator is | ||
* currently pointing to. | ||
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a | ||
* double-ended queue data structure, which allows elements to be added or removed from both ends. | ||
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies | ||
* the direction in which the iterator should iterate over the elements of the `deque`. It has a | ||
* default value of `IterateDirection.DEFAULT`. | ||
* @returns The constructor is not returning anything. It is used to initialize the properties of the | ||
* object being created. | ||
*/ | ||
constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) { | ||
this.index = index; | ||
this.iterateDirection = iterateDirection; | ||
if (this.iterateDirection === IterateDirection.DEFAULT) { | ||
this.prev = function () { | ||
if (this.index === 0) { | ||
throwRangeError(); | ||
} | ||
this.index -= 1; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this.index === this.deque.size) { | ||
throwRangeError(); | ||
} | ||
this.index += 1; | ||
return this; | ||
}; | ||
} else { | ||
this.prev = function () { | ||
if (this.index === this.deque.size - 1) { | ||
throwRangeError(); | ||
} | ||
this.index += 1; | ||
return this; | ||
}; | ||
this.next = function () { | ||
if (this.index === -1) { | ||
throwRangeError(); | ||
} | ||
this.index -= 1; | ||
return this; | ||
}; | ||
} | ||
this.deque = deque; | ||
} | ||
get current() { | ||
return this.deque.getAt(this.index); | ||
} | ||
set current(newElement: E) { | ||
this.deque.setAt(this.index, newElement); | ||
} | ||
isAccessible() { | ||
return this.index !== this.deque.size; | ||
} | ||
prev(): DequeIterator<E> { | ||
return this; | ||
} | ||
next(): DequeIterator<E> { | ||
return this; | ||
} | ||
clone() { | ||
return new DequeIterator<E>(this.index, this.deque, this.iterateDirection); | ||
} | ||
} | ||
export class Deque<E> { | ||
@@ -128,3 +45,3 @@ protected _bucketFirst = 0; | ||
} else { | ||
if (elements.size instanceof Function) _size = elements.size();else _size = elements.size; | ||
if (elements.size instanceof Function) _size = elements.size(); else _size = elements.size; | ||
} | ||
@@ -251,37 +168,25 @@ | ||
/** | ||
* The `begin()` function returns a new iterator for a deque starting from the first element. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The below function is a generator that yields elements from a collection one by one. | ||
*/ | ||
begin() { | ||
return new DequeIterator<E>(0, this); | ||
* begin(): Generator<E> { | ||
let index = 0; | ||
while (index < this.size) { | ||
yield this.getAt(index); | ||
index++; | ||
} | ||
} | ||
/** | ||
* The `end()` function returns a new `DequeIterator` object with the size and reference to the | ||
* current deque. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from | ||
* the last element. | ||
*/ | ||
end() { | ||
return new DequeIterator<E>(this.size, this); | ||
* reverseBegin(): Generator<E> { | ||
let index = this.size - 1; | ||
while (index >= 0) { | ||
yield this.getAt(index); | ||
index--; | ||
} | ||
} | ||
/** | ||
* The reverseBegin function returns a new DequeIterator object that starts at the last element of | ||
* the deque and iterates in reverse direction. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseBegin() { | ||
return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE); | ||
} | ||
/** | ||
* The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a | ||
* Deque in reverse order. | ||
* @returns A new instance of the DequeIterator class is being returned. | ||
*/ | ||
reverseEnd() { | ||
return new DequeIterator<E>(-1, this, IterateDirection.REVERSE); | ||
} | ||
/** | ||
* Time Complexity - Amortized O(1) (possible reallocation) | ||
@@ -438,3 +343,3 @@ * Space Complexity - O(n) (due to potential resizing). | ||
getAt(pos: number): E { | ||
rangeCheck!(pos, 0, this.size - 1); | ||
rangeCheck(pos, 0, this.size - 1); | ||
const { | ||
@@ -464,3 +369,3 @@ bucketIndex, | ||
setAt(pos: number, element: E) { | ||
rangeCheck!(pos, 0, this.size - 1); | ||
rangeCheck(pos, 0, this.size - 1); | ||
const { | ||
@@ -495,3 +400,3 @@ bucketIndex, | ||
const length = this.size; | ||
rangeCheck!(pos, 0, length); | ||
rangeCheck(pos, 0, length); | ||
if (pos === 0) { | ||
@@ -560,3 +465,3 @@ while (num--) this.unshift(element); | ||
deleteAt(pos: number) { | ||
rangeCheck!(pos, 0, this.size - 1); | ||
rangeCheck(pos, 0, this.size - 1); | ||
if (pos === 0) this.shift(); | ||
@@ -625,46 +530,2 @@ else if (pos === this.size - 1) this.pop(); | ||
* | ||
* The function deletes an element from a deque using an iterator and returns the next iterator. | ||
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object | ||
* that is used to iterate over elements in a deque (double-ended queue). | ||
* @returns the updated iterator after deleting an element from the deque. | ||
*/ | ||
deleteByIterator(iter: DequeIterator<E>) { | ||
const index = iter.index; | ||
this.deleteAt(index); | ||
iter = iter.next(); | ||
return iter; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to | ||
* the element if found, otherwise it returns an iterator pointing to the end of the deque. | ||
* @param {E} element - The `element` parameter is the element that you want to find in the deque. | ||
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object. | ||
*/ | ||
findIterator(element: E) { | ||
for (let i = 0; i < this.size; ++i) { | ||
if (this.getAt(i) === element) { | ||
return new DequeIterator<E>(i, this); | ||
} | ||
} | ||
return this.end(); | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The reverse() function reverses the order of the buckets and the elements within each bucket in a | ||
@@ -671,0 +532,0 @@ * data structure. |
1630618
33268
Updateddata-structure-typed@^1.46.3