heap-typed
Advanced tools
Comparing version 1.49.6 to 1.49.7
@@ -26,4 +26,4 @@ /** | ||
/** | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
@@ -35,3 +35,3 @@ * initialization. | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>); | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: AVLTreeOptions<K>); | ||
/** | ||
@@ -38,0 +38,0 @@ * The function creates a new AVL tree node with the specified key and value. |
@@ -30,4 +30,4 @@ "use strict"; | ||
/** | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
@@ -39,6 +39,6 @@ * initialization. | ||
*/ | ||
constructor(nodes, options) { | ||
constructor(keysOrNodesOrEntries = [], options) { | ||
super([], options); | ||
if (nodes) | ||
super.addMany(nodes); | ||
if (keysOrNodesOrEntries) | ||
super.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -45,0 +45,0 @@ /** |
@@ -44,4 +44,4 @@ /** | ||
/** | ||
* The constructor function initializes a binary tree object with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the | ||
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the | ||
* nodes to be added to the binary tree. | ||
@@ -53,3 +53,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>); | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: BinaryTreeOptions<K>); | ||
protected _extractor: (key: K) => number; | ||
@@ -56,0 +56,0 @@ get extractor(): (key: K) => number; |
@@ -48,4 +48,4 @@ /** | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -55,3 +55,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>); | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: BSTOptions<K>); | ||
protected _root?: N; | ||
@@ -58,0 +58,0 @@ get root(): N | undefined; |
@@ -60,4 +60,4 @@ "use strict"; | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -67,3 +67,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(nodes, options) { | ||
constructor(keysOrNodesOrEntries = [], options) { | ||
super([], options); | ||
@@ -73,9 +73,8 @@ this._variant = types_1.BSTVariant.STANDARD; | ||
const { variant } = options; | ||
if (variant) { | ||
if (variant) | ||
this._variant = variant; | ||
} | ||
} | ||
this._root = undefined; | ||
if (nodes) | ||
this.addMany(nodes); | ||
if (keysOrNodesOrEntries) | ||
this.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -82,0 +81,0 @@ get root() { |
@@ -27,3 +27,3 @@ /** | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
@@ -36,3 +36,3 @@ * construction. If this parameter is provided, the `addMany` method is called to add all the | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>); | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: RBTreeOptions<K>); | ||
protected _root: N; | ||
@@ -39,0 +39,0 @@ get root(): N; |
@@ -31,3 +31,3 @@ "use strict"; | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
@@ -40,3 +40,3 @@ * construction. If this parameter is provided, the `addMany` method is called to add all the | ||
*/ | ||
constructor(nodes, options) { | ||
constructor(keysOrNodesOrEntries = [], options) { | ||
super([], options); | ||
@@ -46,4 +46,4 @@ this.Sentinel = new RedBlackTreeNode(NaN); | ||
this._root = this.Sentinel; | ||
if (nodes) | ||
super.addMany(nodes); | ||
if (keysOrNodesOrEntries) | ||
super.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -50,0 +50,0 @@ get root() { |
@@ -30,3 +30,3 @@ /** | ||
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>); | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: TreeMultimapOptions<K>); | ||
private _count; | ||
@@ -33,0 +33,0 @@ get count(): number; |
@@ -27,7 +27,7 @@ "use strict"; | ||
class TreeMultimap extends avl_tree_1.AVLTree { | ||
constructor(nodes, options) { | ||
constructor(keysOrNodesOrEntries = [], options) { | ||
super([], options); | ||
this._count = 0; | ||
if (nodes) | ||
this.addMany(nodes); | ||
if (keysOrNodesOrEntries) | ||
this.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -34,0 +34,0 @@ // TODO the _count is not accurate after nodes count modified |
@@ -8,9 +8,9 @@ /** | ||
*/ | ||
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types'; | ||
import { IterableEntryBase } from '../base'; | ||
/** | ||
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key. | ||
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time. | ||
*/ | ||
@@ -23,4 +23,4 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, 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 | ||
* The constructor function initializes a new instance of a class with optional entries and options. | ||
* @param entries - The `entries` 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 | ||
@@ -31,5 +31,3 @@ * key-value pairs. | ||
*/ | ||
constructor(elements?: Iterable<[K, V]>, options?: { | ||
hashFn: (key: K) => string; | ||
}); | ||
constructor(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>); | ||
protected _size: number; | ||
@@ -51,6 +49,6 @@ get size(): number; | ||
* 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. | ||
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two entries: the key and the value. | ||
*/ | ||
setMany(elements: Iterable<[K, V]>): boolean[]; | ||
setMany(entries: Iterable<[K, V]>): boolean[]; | ||
/** | ||
@@ -120,3 +118,2 @@ * The `get` function retrieves a value from a map based on a given key, either from an object map or | ||
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>; | ||
print(): void; | ||
put(key: K, value: V): boolean; | ||
@@ -133,4 +130,4 @@ /** | ||
/** | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list. | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list. | ||
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases. | ||
@@ -144,5 +141,3 @@ */ | ||
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>; | ||
protected _hashFn: (key: K) => string; | ||
protected _objHashFn: (key: K) => object; | ||
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>); | ||
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>); | ||
protected _size: number; | ||
@@ -252,3 +247,3 @@ get size(): number; | ||
* | ||
* The `clear` function clears all the elements in a data structure and resets its properties. | ||
* The `clear` function clears all the entries in a data structure and resets its properties. | ||
*/ | ||
@@ -260,6 +255,2 @@ clear(): void; | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -281,6 +272,2 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -301,5 +288,15 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
put(key: K, value: V): boolean; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
protected _hashFn: (key: K) => string; | ||
protected _objHashFn: (key: K) => object; | ||
/** | ||
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -306,0 +303,0 @@ * |
@@ -8,10 +8,10 @@ "use strict"; | ||
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key. | ||
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time. | ||
*/ | ||
class HashMap extends base_1.IterableEntryBase { | ||
/** | ||
* 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 | ||
* The constructor function initializes a new instance of a class with optional entries and options. | ||
* @param entries - The `entries` 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 | ||
@@ -22,3 +22,3 @@ * key-value pairs. | ||
*/ | ||
constructor(elements = [], options) { | ||
constructor(entries = [], options) { | ||
super(); | ||
@@ -35,4 +35,4 @@ this._store = {}; | ||
} | ||
if (elements) { | ||
this.setMany(elements); | ||
if (entries) { | ||
this.setMany(entries); | ||
} | ||
@@ -78,8 +78,8 @@ } | ||
* 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. | ||
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two entries: the key and the value. | ||
*/ | ||
setMany(elements) { | ||
setMany(entries) { | ||
const results = []; | ||
for (const [key, value] of elements) | ||
for (const [key, value] of entries) | ||
results.push(this.set(key, value)); | ||
@@ -201,5 +201,2 @@ return results; | ||
} | ||
print() { | ||
console.log([...this.entries()]); | ||
} | ||
put(key, value) { | ||
@@ -244,11 +241,8 @@ return this.set(key, value); | ||
/** | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list. | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list. | ||
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases. | ||
*/ | ||
class LinkedHashMap extends base_1.IterableEntryBase { | ||
constructor(elements, options = { | ||
hashFn: (key) => String(key), | ||
objHashFn: (key) => key | ||
}) { | ||
constructor(entries, options) { | ||
super(); | ||
@@ -258,9 +252,19 @@ this._noObjMap = {}; | ||
this._size = 0; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
this._hashFn = (key) => String(key); | ||
this._objHashFn = (key) => key; | ||
this._sentinel = {}; | ||
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel; | ||
const { hashFn, objHashFn } = options; | ||
this._hashFn = hashFn; | ||
this._objHashFn = objHashFn; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (options) { | ||
const { hashFn, objHashFn } = options; | ||
if (hashFn) | ||
this._hashFn = hashFn; | ||
if (objHashFn) | ||
this._objHashFn = objHashFn; | ||
} | ||
if (entries) { | ||
for (const el of entries) { | ||
this.set(el[0], el[1]); | ||
@@ -505,3 +509,3 @@ } | ||
* | ||
* The `clear` function clears all the elements in a data structure and resets its properties. | ||
* The `clear` function clears all the entries in a data structure and resets its properties. | ||
*/ | ||
@@ -524,6 +528,2 @@ clear() { | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -555,6 +555,2 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -584,2 +580,6 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
put(key, value) { | ||
@@ -589,3 +589,3 @@ return this.set(key, value); | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -592,0 +592,0 @@ * |
@@ -1,2 +0,1 @@ | ||
export * from './hash-table'; | ||
export * from './hash-map'; |
@@ -17,3 +17,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./hash-table"), exports); | ||
__exportStar(require("./hash-map"), exports); |
@@ -22,4 +22,5 @@ /** | ||
export declare class Heap<E = any> extends IterableElementBase<E> { | ||
options: HeapOptions<E>; | ||
constructor(elements?: Iterable<E>, options?: HeapOptions<E>); | ||
protected _comparator: (a: E, b: E) => number; | ||
get comparator(): (a: E, b: E) => number; | ||
protected _elements: E[]; | ||
@@ -26,0 +27,0 @@ get elements(): E[]; |
@@ -24,6 +24,5 @@ "use strict"; | ||
class Heap extends base_1.IterableElementBase { | ||
constructor(elements, options) { | ||
constructor(elements = [], options) { | ||
super(); | ||
this._elements = []; | ||
const defaultComparator = (a, b) => { | ||
this._comparator = (a, b) => { | ||
if (!(typeof a === 'number' && typeof b === 'number')) { | ||
@@ -36,10 +35,8 @@ throw new Error('The a, b params of compare function must be number'); | ||
}; | ||
this._elements = []; | ||
if (options) { | ||
this.options = options; | ||
const { comparator } = options; | ||
if (comparator) | ||
this._comparator = comparator; | ||
} | ||
else { | ||
this.options = { | ||
comparator: defaultComparator | ||
}; | ||
} | ||
if (elements) { | ||
@@ -52,2 +49,5 @@ for (const el of elements) { | ||
} | ||
get comparator() { | ||
return this._comparator; | ||
} | ||
get elements() { | ||
@@ -262,3 +262,3 @@ return this._elements; | ||
clone() { | ||
const clonedHeap = new Heap([], this.options); | ||
const clonedHeap = new Heap([], { comparator: this.comparator }); | ||
clonedHeap._elements = [...this.elements]; | ||
@@ -389,3 +389,3 @@ return clonedHeap; | ||
const parentItem = this.elements[parent]; | ||
if (this.options.comparator(parentItem, element) <= 0) | ||
if (this.comparator(parentItem, element) <= 0) | ||
break; | ||
@@ -412,7 +412,7 @@ this.elements[index] = parentItem; | ||
let minItem = this.elements[left]; | ||
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) { | ||
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) { | ||
left = right; | ||
minItem = this.elements[right]; | ||
} | ||
if (this.options.comparator(minItem, element) >= 0) | ||
if (this.comparator(minItem, element) >= 0) | ||
break; | ||
@@ -419,0 +419,0 @@ this.elements[index] = minItem; |
@@ -16,3 +16,3 @@ "use strict"; | ||
class MaxHeap extends heap_1.Heap { | ||
constructor(elements, options = { | ||
constructor(elements = [], options = { | ||
comparator: (a, b) => { | ||
@@ -19,0 +19,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -16,3 +16,3 @@ "use strict"; | ||
class MinHeap extends heap_1.Heap { | ||
constructor(elements, options = { | ||
constructor(elements = [], options = { | ||
comparator: (a, b) => { | ||
@@ -19,0 +19,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -28,3 +28,3 @@ "use strict"; | ||
*/ | ||
constructor(elements) { | ||
constructor(elements = []) { | ||
super(); | ||
@@ -31,0 +31,0 @@ this._head = undefined; |
@@ -21,6 +21,4 @@ "use strict"; | ||
*/ | ||
constructor(elements) { | ||
constructor(elements = []) { | ||
super(); | ||
this._head = undefined; | ||
this._tail = undefined; | ||
this._size = 0; | ||
@@ -27,0 +25,0 @@ if (elements) { |
@@ -8,2 +8,3 @@ /** | ||
*/ | ||
import type { SkipLinkedListOptions } from '../../types'; | ||
export declare class SkipListNode<K, V> { | ||
@@ -16,10 +17,3 @@ key: K; | ||
export declare class SkipList<K, V> { | ||
/** | ||
* The constructor initializes a SkipList with a specified maximum level and probability. | ||
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines | ||
* the maximum number of levels that can be created in the skip list. | ||
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher | ||
* level in the skip list. It is used to determine the height of each node in the skip list. | ||
*/ | ||
constructor(maxLevel?: number, probability?: number); | ||
constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions); | ||
protected _head: SkipListNode<K, V>; | ||
@@ -26,0 +20,0 @@ get head(): SkipListNode<K, V>; |
"use strict"; | ||
/** | ||
* data-structure-typed | ||
* | ||
* @author Tyler Zeng | ||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com> | ||
* @license MIT License | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -20,14 +13,18 @@ exports.SkipList = exports.SkipListNode = void 0; | ||
class SkipList { | ||
/** | ||
* The constructor initializes a SkipList with a specified maximum level and probability. | ||
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines | ||
* the maximum number of levels that can be created in the skip list. | ||
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher | ||
* level in the skip list. It is used to determine the height of each node in the skip list. | ||
*/ | ||
constructor(maxLevel = 16, probability = 0.5) { | ||
this._head = new SkipListNode(undefined, undefined, maxLevel); | ||
constructor(elements = [], options) { | ||
this._head = new SkipListNode(undefined, undefined, this.maxLevel); | ||
this._level = 0; | ||
this._maxLevel = maxLevel; | ||
this._probability = probability; | ||
this._maxLevel = 16; | ||
this._probability = 0.5; | ||
if (options) { | ||
const { maxLevel, probability } = options; | ||
if (typeof maxLevel === 'number') | ||
this._maxLevel = maxLevel; | ||
if (typeof probability === 'number') | ||
this._probability = probability; | ||
} | ||
if (elements) { | ||
for (const [key, value] of elements) | ||
this.add(key, value); | ||
} | ||
} | ||
@@ -34,0 +31,0 @@ get head() { |
@@ -8,2 +8,3 @@ /** | ||
*/ | ||
import type { MatrixOptions } from '../../types'; | ||
export declare class Matrix { | ||
@@ -17,9 +18,3 @@ /** | ||
*/ | ||
constructor(data: number[][], options?: { | ||
rows?: number; | ||
cols?: number; | ||
addFn?: (a: number, b: number) => any; | ||
subtractFn?: (a: number, b: number) => any; | ||
multiplyFn?: (a: number, b: number) => any; | ||
}); | ||
constructor(data: number[][], options?: MatrixOptions); | ||
protected _rows: number; | ||
@@ -26,0 +21,0 @@ get rows(): number; |
"use strict"; | ||
/** | ||
* data-structure-typed | ||
* | ||
* @author Tyler Zeng | ||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com> | ||
* @license MIT License | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -10,0 +3,0 @@ exports.Matrix = void 0; |
@@ -6,3 +6,3 @@ "use strict"; | ||
class MaxPriorityQueue extends priority_queue_1.PriorityQueue { | ||
constructor(elements, options = { | ||
constructor(elements = [], options = { | ||
comparator: (a, b) => { | ||
@@ -9,0 +9,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -6,3 +6,3 @@ "use strict"; | ||
class MinPriorityQueue extends priority_queue_1.PriorityQueue { | ||
constructor(elements, options = { | ||
constructor(elements = [], options = { | ||
comparator: (a, b) => { | ||
@@ -9,0 +9,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -14,3 +14,3 @@ "use strict"; | ||
class PriorityQueue extends heap_1.Heap { | ||
constructor(elements, options) { | ||
constructor(elements = [], options) { | ||
super(elements, options); | ||
@@ -17,0 +17,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { ElementCallback, IterableWithSizeOrLength } from '../../types'; | ||
import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types'; | ||
import { IterableElementBase } from '../base'; | ||
@@ -25,12 +25,3 @@ /** | ||
protected readonly _bucketSize: number; | ||
/** | ||
* The constructor initializes a data structure with a specified bucket size and populates it with | ||
* elements from an iterable. | ||
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that | ||
* contains the initial elements to be stored in the data structure. It can also be an object with a | ||
* `length` property or a `size` property, which represents the number of elements in the iterable. | ||
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be | ||
* stored in each bucket. It determines the size of each bucket in the data structure. | ||
*/ | ||
constructor(elements?: IterableWithSizeOrLength<E>, bucketSize?: number); | ||
constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions); | ||
protected _buckets: E[][]; | ||
@@ -37,0 +28,0 @@ get buckets(): E[][]; |
@@ -14,12 +14,3 @@ "use strict"; | ||
class Deque extends base_1.IterableElementBase { | ||
/** | ||
* The constructor initializes a data structure with a specified bucket size and populates it with | ||
* elements from an iterable. | ||
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that | ||
* contains the initial elements to be stored in the data structure. It can also be an object with a | ||
* `length` property or a `size` property, which represents the number of elements in the iterable. | ||
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be | ||
* stored in each bucket. It determines the size of each bucket in the data structure. | ||
*/ | ||
constructor(elements = [], bucketSize = 1 << 12) { | ||
constructor(elements = [], options) { | ||
super(); | ||
@@ -31,4 +22,10 @@ this._bucketFirst = 0; | ||
this._bucketCount = 0; | ||
this._bucketSize = 1 << 12; | ||
this._buckets = []; | ||
this._size = 0; | ||
if (options) { | ||
const { bucketSize } = options; | ||
if (typeof bucketSize === 'number') | ||
this._bucketSize = bucketSize; | ||
} | ||
let _size; | ||
@@ -47,3 +44,2 @@ if ('length' in elements) { | ||
} | ||
this._bucketSize = bucketSize; | ||
this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1; | ||
@@ -618,3 +614,3 @@ for (let i = 0; i < this._bucketCount; ++i) { | ||
filter(predicate, thisArg) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
const newDeque = new Deque([], { bucketSize: this._bucketSize }); | ||
let index = 0; | ||
@@ -647,3 +643,3 @@ for (const el of this) { | ||
map(callback, thisArg) { | ||
const newDeque = new Deque([], this._bucketSize); | ||
const newDeque = new Deque([], { bucketSize: this._bucketSize }); | ||
let index = 0; | ||
@@ -650,0 +646,0 @@ for (const el of this) { |
@@ -15,3 +15,3 @@ /** | ||
* 5. Data Buffering: Acting as a buffer for data packets in network communication. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited. | ||
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets. | ||
@@ -23,8 +23,8 @@ */ | ||
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be | ||
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be | ||
* initialized as an empty array. | ||
*/ | ||
constructor(elements?: E[]); | ||
protected _nodes: E[]; | ||
get nodes(): E[]; | ||
constructor(elements?: Iterable<E>); | ||
protected _elements: E[]; | ||
get elements(): E[]; | ||
protected _offset: number; | ||
@@ -41,4 +41,4 @@ get offset(): number; | ||
* | ||
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
@@ -56,3 +56,3 @@ */ | ||
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
@@ -108,4 +108,4 @@ */ | ||
* | ||
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
@@ -123,3 +123,3 @@ */ | ||
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
@@ -183,3 +183,3 @@ */ | ||
* | ||
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array. | ||
* The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
* @returns An array of type E is being returned. | ||
@@ -189,3 +189,3 @@ */ | ||
/** | ||
* The clear function resets the nodes array and offset to their initial values. | ||
* The clear function resets the elements array and offset to their initial values. | ||
*/ | ||
@@ -192,0 +192,0 @@ clear(): void; |
@@ -12,3 +12,3 @@ "use strict"; | ||
* 5. Data Buffering: Acting as a buffer for data packets in network communication. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited. | ||
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets. | ||
@@ -20,12 +20,16 @@ */ | ||
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be | ||
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be | ||
* initialized as an empty array. | ||
*/ | ||
constructor(elements) { | ||
constructor(elements = []) { | ||
super(); | ||
this._nodes = elements || []; | ||
this._elements = []; | ||
this._offset = 0; | ||
if (elements) { | ||
for (const el of elements) | ||
this.push(el); | ||
} | ||
} | ||
get nodes() { | ||
return this._nodes; | ||
get elements() { | ||
return this._elements; | ||
} | ||
@@ -40,3 +44,3 @@ get offset() { | ||
get size() { | ||
return this.nodes.length - this.offset; | ||
return this.elements.length - this.offset; | ||
} | ||
@@ -47,8 +51,8 @@ /** | ||
* | ||
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
*/ | ||
get first() { | ||
return this.size > 0 ? this.nodes[this.offset] : undefined; | ||
return this.size > 0 ? this.elements[this.offset] : undefined; | ||
} | ||
@@ -64,7 +68,7 @@ /** | ||
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
*/ | ||
get last() { | ||
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; | ||
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined; | ||
} | ||
@@ -99,3 +103,3 @@ /** | ||
push(element) { | ||
this.nodes.push(element); | ||
this.elements.push(element); | ||
return true; | ||
@@ -120,7 +124,7 @@ } | ||
this._offset += 1; | ||
if (this.offset * 2 < this.nodes.length) | ||
if (this.offset * 2 < this.elements.length) | ||
return first; | ||
// only delete dequeued elements when reaching half size | ||
// to decrease latency of shifting elements. | ||
this._nodes = this.nodes.slice(this.offset); | ||
this._elements = this.elements.slice(this.offset); | ||
this._offset = 0; | ||
@@ -137,4 +141,4 @@ return first; | ||
* | ||
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
@@ -154,3 +158,3 @@ */ | ||
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
@@ -200,3 +204,3 @@ */ | ||
getAt(index) { | ||
return this.nodes[index]; | ||
return this.elements[index]; | ||
} | ||
@@ -225,13 +229,13 @@ /** | ||
* | ||
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array. | ||
* The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
* @returns An array of type E is being returned. | ||
*/ | ||
toArray() { | ||
return this.nodes.slice(this.offset); | ||
return this.elements.slice(this.offset); | ||
} | ||
/** | ||
* The clear function resets the nodes array and offset to their initial values. | ||
* The clear function resets the elements array and offset to their initial values. | ||
*/ | ||
clear() { | ||
this._nodes = []; | ||
this._elements = []; | ||
this._offset = 0; | ||
@@ -251,3 +255,3 @@ } | ||
clone() { | ||
return new Queue(this.nodes.slice(this.offset)); | ||
return new Queue(this.elements.slice(this.offset)); | ||
} | ||
@@ -317,3 +321,3 @@ /** | ||
*_getIterator() { | ||
for (const item of this.nodes) { | ||
for (const item of this.elements) { | ||
yield item; | ||
@@ -320,0 +324,0 @@ } |
@@ -20,9 +20,8 @@ "use strict"; | ||
*/ | ||
constructor(elements) { | ||
constructor(elements = []) { | ||
super(); | ||
this._elements = []; | ||
if (elements) { | ||
for (const el of elements) { | ||
for (const el of elements) | ||
this.push(el); | ||
} | ||
} | ||
@@ -29,0 +28,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { ElementCallback } from '../../types'; | ||
import type { ElementCallback, TrieOptions } from '../../types'; | ||
import { IterableElementBase } from '../base'; | ||
@@ -35,3 +35,3 @@ /** | ||
export declare class Trie extends IterableElementBase<string> { | ||
constructor(words?: string[], caseSensitive?: boolean); | ||
constructor(words?: Iterable<string>, options?: TrieOptions); | ||
protected _size: number; | ||
@@ -38,0 +38,0 @@ get size(): number; |
@@ -31,11 +31,15 @@ "use strict"; | ||
class Trie extends base_1.IterableElementBase { | ||
constructor(words, caseSensitive = true) { | ||
constructor(words = [], options) { | ||
super(); | ||
this._size = 0; | ||
this._caseSensitive = true; | ||
this._root = new TrieNode(''); | ||
this._caseSensitive = caseSensitive; | ||
this._size = 0; | ||
if (options) { | ||
const { caseSensitive } = options; | ||
if (caseSensitive !== undefined) | ||
this._caseSensitive = caseSensitive; | ||
} | ||
if (words) { | ||
for (const word of words) { | ||
for (const word of words) | ||
this.add(word); | ||
} | ||
} | ||
@@ -42,0 +46,0 @@ } |
@@ -6,4 +6,4 @@ import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; | ||
export type BinaryTreeOptions<K> = { | ||
iterationType: IterationType; | ||
extractor: (key: K) => number; | ||
iterationType?: IterationType; | ||
extractor?: (key: K) => number; | ||
}; |
@@ -7,3 +7,3 @@ import { BST, BSTNode } from '../../../data-structures'; | ||
export type BSTOptions<K> = BinaryTreeOptions<K> & { | ||
variant: BSTVariant; | ||
variant?: BSTVariant; | ||
}; |
@@ -5,2 +5,2 @@ import { TreeMultimap, TreeMultimapNode } from '../../../data-structures'; | ||
export type TreeMultimapNested<K, V, N extends TreeMultimapNode<K, V, N>> = TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; | ||
export type TreeMultimapOptions<K> = Omit<AVLTreeOptions<K>, 'isMergeDuplicatedNodeByKey'> & {}; | ||
export type TreeMultimapOptions<K> = AVLTreeOptions<K> & {}; |
@@ -7,5 +7,8 @@ export type HashMapLinkedNode<K, V> = { | ||
}; | ||
export type LinkedHashMapOptions<K> = { | ||
hashFn?: (key: K) => string; | ||
objHashFn?: (key: K) => object; | ||
}; | ||
export type HashMapOptions<K> = { | ||
hashFn: (key: K) => string; | ||
objHashFn: (key: K) => object; | ||
hashFn?: (key: K) => string; | ||
}; | ||
@@ -12,0 +15,0 @@ export type HashMapStoreItem<K, V> = { |
export * from './hash-map'; | ||
export * from './hash-table'; | ||
export type HashFunction<K> = (key: K) => number; |
@@ -18,2 +18,1 @@ "use strict"; | ||
__exportStar(require("./hash-map"), exports); | ||
__exportStar(require("./hash-table"), exports); |
import { Comparator } from '../../common'; | ||
export type HeapOptions<T> = { | ||
comparator: Comparator<T>; | ||
comparator?: Comparator<T>; | ||
}; |
export * from './singly-linked-list'; | ||
export * from './doubly-linked-list'; | ||
export * from './skip-linked-list'; |
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./doubly-linked-list"), exports); | ||
__exportStar(require("./skip-linked-list"), exports); |
@@ -1,1 +0,4 @@ | ||
export {}; | ||
export type SkipLinkedListOptions = { | ||
maxLevel?: number; | ||
probability?: number; | ||
}; |
export * from './navigator'; | ||
export * from './matrix'; |
@@ -18,1 +18,2 @@ "use strict"; | ||
__exportStar(require("./navigator"), exports); | ||
__exportStar(require("./matrix"), exports); |
@@ -1,1 +0,7 @@ | ||
export {}; | ||
export type MatrixOptions = { | ||
rows?: number; | ||
cols?: number; | ||
addFn?: (a: number, b: number) => any; | ||
subtractFn?: (a: number, b: number) => any; | ||
multiplyFn?: (a: number, b: number) => any; | ||
}; |
@@ -1,1 +0,3 @@ | ||
export {}; | ||
export type DequeOptions = { | ||
bucketSize?: number; | ||
}; |
@@ -1,1 +0,3 @@ | ||
export {}; | ||
export type TrieOptions = { | ||
caseSensitive?: boolean; | ||
}; |
{ | ||
"name": "heap-typed", | ||
"version": "1.49.6", | ||
"version": "1.49.7", | ||
"description": "Heap. Javascript & Typescript Data Structure.", | ||
@@ -134,4 +134,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.49.5" | ||
"data-structure-typed": "^1.49.7" | ||
} | ||
} |
@@ -51,4 +51,4 @@ /** | ||
/** | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
@@ -60,5 +60,5 @@ * initialization. | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) { | ||
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: AVLTreeOptions<K>) { | ||
super([], options); | ||
if (nodes) super.addMany(nodes); | ||
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -65,0 +65,0 @@ |
@@ -95,4 +95,4 @@ /** | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -102,3 +102,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>) { | ||
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BSTOptions<K>) { | ||
super([], options); | ||
@@ -108,5 +108,3 @@ | ||
const { variant } = options; | ||
if (variant) { | ||
this._variant = variant; | ||
} | ||
if (variant) this._variant = variant; | ||
} | ||
@@ -116,3 +114,3 @@ | ||
if (nodes) this.addMany(nodes); | ||
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -119,0 +117,0 @@ |
@@ -56,3 +56,3 @@ /** | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
@@ -65,7 +65,7 @@ * construction. If this parameter is provided, the `addMany` method is called to add all the | ||
*/ | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>) { | ||
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: RBTreeOptions<K>) { | ||
super([], options); | ||
this._root = this.Sentinel; | ||
if (nodes) super.addMany(nodes); | ||
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -72,0 +72,0 @@ |
@@ -55,5 +55,5 @@ /** | ||
implements IBinaryTree<K, V, N, TREE> { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) { | ||
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) { | ||
super([], options); | ||
if (nodes) this.addMany(nodes); | ||
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries); | ||
} | ||
@@ -60,0 +60,0 @@ |
@@ -8,3 +8,9 @@ /** | ||
*/ | ||
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; | ||
import type { | ||
EntryCallback, | ||
HashMapLinkedNode, | ||
HashMapOptions, | ||
HashMapStoreItem, | ||
LinkedHashMapOptions | ||
} from '../../types'; | ||
import { IterableEntryBase } from '../base'; | ||
@@ -15,5 +21,5 @@ import { isWeakKey, rangeCheck } from '../../utils'; | ||
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key. | ||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key. | ||
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time. | ||
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time. | ||
*/ | ||
@@ -25,4 +31,4 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, 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 | ||
* The constructor function initializes a new instance of a class with optional entries and options. | ||
* @param entries - The `entries` 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 | ||
@@ -33,8 +39,3 @@ * key-value pairs. | ||
*/ | ||
constructor( | ||
elements: Iterable<[K, V]> = [], | ||
options?: { | ||
hashFn: (key: K) => string; | ||
} | ||
) { | ||
constructor(entries: Iterable<[K, V]> = [], options?: HashMapOptions<K>) { | ||
super(); | ||
@@ -47,4 +48,4 @@ if (options) { | ||
} | ||
if (elements) { | ||
this.setMany(elements); | ||
if (entries) { | ||
this.setMany(entries); | ||
} | ||
@@ -96,8 +97,8 @@ } | ||
* 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. | ||
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each | ||
* key-value pair is represented as an array with two entries: the key and the value. | ||
*/ | ||
setMany(elements: Iterable<[K, V]>): boolean[] { | ||
setMany(entries: Iterable<[K, V]>): boolean[] { | ||
const results: boolean[] = []; | ||
for (const [key, value] of elements) results.push(this.set(key, value)); | ||
for (const [key, value] of entries) results.push(this.set(key, value)); | ||
return results; | ||
@@ -223,6 +224,2 @@ } | ||
print(): void { | ||
console.log([...this.entries()]); | ||
} | ||
put(key: K, value: V): boolean { | ||
@@ -271,4 +268,4 @@ return this.set(key, value); | ||
/** | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list. | ||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map. | ||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list. | ||
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases. | ||
@@ -282,12 +279,4 @@ */ | ||
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>; | ||
protected _hashFn: (key: K) => string; | ||
protected _objHashFn: (key: K) => object; | ||
constructor( | ||
elements?: Iterable<[K, V]>, | ||
options: HashMapOptions<K> = { | ||
hashFn: (key: K) => String(key), | ||
objHashFn: (key: K) => <object>key | ||
} | ||
) { | ||
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>) { | ||
super(); | ||
@@ -297,7 +286,10 @@ this._sentinel = <HashMapLinkedNode<K, V>>{}; | ||
const { hashFn, objHashFn } = options; | ||
this._hashFn = hashFn; | ||
this._objHashFn = objHashFn; | ||
if (elements) { | ||
for (const el of elements) { | ||
if (options) { | ||
const { hashFn, objHashFn } = options; | ||
if (hashFn) this._hashFn = hashFn; | ||
if (objHashFn) this._objHashFn = objHashFn; | ||
} | ||
if (entries) { | ||
for (const el of entries) { | ||
this.set(el[0], el[1]); | ||
@@ -560,3 +552,3 @@ } | ||
* | ||
* The `clear` function clears all the elements in a data structure and resets its properties. | ||
* The `clear` function clears all the entries in a data structure and resets its properties. | ||
*/ | ||
@@ -581,7 +573,2 @@ clear(): void { | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -614,7 +601,2 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -645,2 +627,7 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
put(key: K, value: V): boolean { | ||
@@ -651,3 +638,12 @@ return this.set(key, value); | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
protected _objHashFn: (key: K) => object = (key: K) => <object>key; | ||
/** | ||
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap. | ||
* Space Complexity: O(1) | ||
@@ -654,0 +650,0 @@ * |
@@ -1,2 +0,1 @@ | ||
export * from './hash-table'; | ||
export * from './hash-map'; |
@@ -24,19 +24,8 @@ /** | ||
export class Heap<E = any> extends IterableElementBase<E> { | ||
options: HeapOptions<E>; | ||
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) { | ||
super(); | ||
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) { | ||
super(); | ||
const defaultComparator = (a: E, b: E) => { | ||
if (!(typeof a === 'number' && typeof b === 'number')) { | ||
throw new Error('The a, b params of compare function must be number'); | ||
} else { | ||
return a - b; | ||
} | ||
}; | ||
if (options) { | ||
this.options = options; | ||
} else { | ||
this.options = { | ||
comparator: defaultComparator | ||
}; | ||
const { comparator } = options; | ||
if (comparator) this._comparator = comparator; | ||
} | ||
@@ -52,2 +41,14 @@ | ||
protected _comparator = (a: E, b: E) => { | ||
if (!(typeof a === 'number' && typeof b === 'number')) { | ||
throw new Error('The a, b params of compare function must be number'); | ||
} else { | ||
return a - b; | ||
} | ||
}; | ||
get comparator() { | ||
return this._comparator; | ||
} | ||
protected _elements: E[] = []; | ||
@@ -283,3 +284,3 @@ | ||
clone(): Heap<E> { | ||
const clonedHeap = new Heap<E>([], this.options); | ||
const clonedHeap = new Heap<E>([], { comparator: this.comparator }); | ||
clonedHeap._elements = [...this.elements]; | ||
@@ -419,3 +420,3 @@ return clonedHeap; | ||
const parentItem = this.elements[parent]; | ||
if (this.options.comparator(parentItem, element) <= 0) break; | ||
if (this.comparator(parentItem, element) <= 0) break; | ||
this.elements[index] = parentItem; | ||
@@ -442,7 +443,7 @@ index = parent; | ||
let minItem = this.elements[left]; | ||
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) { | ||
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) { | ||
left = right; | ||
minItem = this.elements[right]; | ||
} | ||
if (this.options.comparator(minItem, element) >= 0) break; | ||
if (this.comparator(minItem, element) >= 0) break; | ||
this.elements[index] = minItem; | ||
@@ -449,0 +450,0 @@ index = left; |
@@ -23,3 +23,3 @@ /** | ||
constructor( | ||
elements?: Iterable<E>, | ||
elements: Iterable<E> = [], | ||
options: HeapOptions<E> = { | ||
@@ -26,0 +26,0 @@ comparator: (a: E, b: E) => { |
@@ -23,3 +23,3 @@ /** | ||
constructor( | ||
elements?: Iterable<E>, | ||
elements: Iterable<E> = [], | ||
options: HeapOptions<E> = { | ||
@@ -26,0 +26,0 @@ comparator: (a: E, b: E) => { |
@@ -38,3 +38,3 @@ /** | ||
*/ | ||
constructor(elements?: Iterable<E>) { | ||
constructor(elements: Iterable<E> = []) { | ||
super(); | ||
@@ -41,0 +41,0 @@ this._head = undefined; |
@@ -30,7 +30,4 @@ /** | ||
*/ | ||
constructor(elements?: Iterable<E>) { | ||
constructor(elements: Iterable<E> = []) { | ||
super(); | ||
this._head = undefined; | ||
this._tail = undefined; | ||
this._size = 0; | ||
if (elements) { | ||
@@ -53,3 +50,3 @@ for (const el of elements) this.push(el); | ||
protected _size: number; | ||
protected _size: number = 0; | ||
@@ -56,0 +53,0 @@ get size(): number { |
@@ -8,2 +8,3 @@ /** | ||
*/ | ||
import type { SkipLinkedListOptions } from '../../types'; | ||
@@ -23,17 +24,15 @@ export class SkipListNode<K, V> { | ||
export class SkipList<K, V> { | ||
/** | ||
* The constructor initializes a SkipList with a specified maximum level and probability. | ||
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines | ||
* the maximum number of levels that can be created in the skip list. | ||
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher | ||
* level in the skip list. It is used to determine the height of each node in the skip list. | ||
*/ | ||
constructor(maxLevel = 16, probability = 0.5) { | ||
this._head = new SkipListNode<K, V>(undefined as any, undefined as any, maxLevel); | ||
this._level = 0; | ||
this._maxLevel = maxLevel; | ||
this._probability = probability; | ||
constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) { | ||
if (options) { | ||
const { maxLevel, probability } = options; | ||
if (typeof maxLevel === 'number') this._maxLevel = maxLevel; | ||
if (typeof probability === 'number') this._probability = probability; | ||
} | ||
if (elements) { | ||
for (const [key, value] of elements) this.add(key, value); | ||
} | ||
} | ||
protected _head: SkipListNode<K, V>; | ||
protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel); | ||
@@ -44,3 +43,3 @@ get head(): SkipListNode<K, V> { | ||
protected _level: number; | ||
protected _level: number = 0; | ||
@@ -51,3 +50,3 @@ get level(): number { | ||
protected _maxLevel: number; | ||
protected _maxLevel: number = 16; | ||
@@ -58,3 +57,3 @@ get maxLevel(): number { | ||
protected _probability: number; | ||
protected _probability: number = 0.5; | ||
@@ -61,0 +60,0 @@ get probability(): number { |
@@ -8,2 +8,3 @@ /** | ||
*/ | ||
import type { MatrixOptions } from '../../types'; | ||
@@ -18,12 +19,3 @@ export class Matrix { | ||
*/ | ||
constructor( | ||
data: number[][], | ||
options?: { | ||
rows?: number; | ||
cols?: number; | ||
addFn?: (a: number, b: number) => any; | ||
subtractFn?: (a: number, b: number) => any; | ||
multiplyFn?: (a: number, b: number) => any; | ||
} | ||
) { | ||
constructor(data: number[][], options?: MatrixOptions) { | ||
if (options) { | ||
@@ -30,0 +22,0 @@ const { rows, cols, addFn, subtractFn, multiplyFn } = options; |
@@ -13,3 +13,3 @@ /** | ||
constructor( | ||
elements?: Iterable<E>, | ||
elements: Iterable<E> = [], | ||
options: PriorityQueueOptions<E> = { | ||
@@ -16,0 +16,0 @@ comparator: (a: E, b: E) => { |
@@ -13,3 +13,3 @@ /** | ||
constructor( | ||
elements?: Iterable<E>, | ||
elements: Iterable<E> = [], | ||
options: PriorityQueueOptions<E> = { | ||
@@ -16,0 +16,0 @@ comparator: (a: E, b: E) => { |
@@ -20,5 +20,5 @@ /** | ||
export class PriorityQueue<E = any> extends Heap<E> { | ||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) { | ||
constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) { | ||
super(elements, options); | ||
} | ||
} |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { ElementCallback, IterableWithSizeOrLength } from '../../types'; | ||
import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types'; | ||
import { IterableElementBase } from '../base'; | ||
@@ -26,15 +26,12 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils'; | ||
protected _bucketCount = 0; | ||
protected readonly _bucketSize: number; | ||
protected readonly _bucketSize: number = 1 << 12; | ||
/** | ||
* The constructor initializes a data structure with a specified bucket size and populates it with | ||
* elements from an iterable. | ||
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that | ||
* contains the initial elements to be stored in the data structure. It can also be an object with a | ||
* `length` property or a `size` property, which represents the number of elements in the iterable. | ||
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be | ||
* stored in each bucket. It determines the size of each bucket in the data structure. | ||
*/ | ||
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = 1 << 12) { | ||
constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) { | ||
super(); | ||
if (options) { | ||
const { bucketSize } = options; | ||
if (typeof bucketSize === 'number') this._bucketSize = bucketSize; | ||
} | ||
let _size: number; | ||
@@ -49,3 +46,2 @@ if ('length' in elements) { | ||
this._bucketSize = bucketSize; | ||
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1; | ||
@@ -643,3 +639,3 @@ for (let i = 0; i < this._bucketCount; ++i) { | ||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> { | ||
const newDeque = new Deque<E>([], this._bucketSize); | ||
const newDeque = new Deque<E>([], { bucketSize: this._bucketSize }); | ||
let index = 0; | ||
@@ -673,3 +669,3 @@ for (const el of this) { | ||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> { | ||
const newDeque = new Deque<T>([], this._bucketSize); | ||
const newDeque = new Deque<T>([], { bucketSize: this._bucketSize }); | ||
let index = 0; | ||
@@ -676,0 +672,0 @@ for (const el of this) { |
@@ -16,3 +16,3 @@ /** | ||
* 5. Data Buffering: Acting as a buffer for data packets in network communication. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited. | ||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited. | ||
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets. | ||
@@ -24,18 +24,19 @@ */ | ||
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it | ||
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be | ||
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be | ||
* initialized as an empty array. | ||
*/ | ||
constructor(elements?: E[]) { | ||
constructor(elements: Iterable<E> = []) { | ||
super(); | ||
this._nodes = elements || []; | ||
this._offset = 0; | ||
if (elements) { | ||
for (const el of elements) this.push(el); | ||
} | ||
} | ||
protected _nodes: E[]; | ||
protected _elements: E[] = []; | ||
get nodes(): E[] { | ||
return this._nodes; | ||
get elements(): E[] { | ||
return this._elements; | ||
} | ||
protected _offset: number; | ||
protected _offset: number = 0; | ||
@@ -51,3 +52,3 @@ get offset(): number { | ||
get size(): number { | ||
return this.nodes.length - this.offset; | ||
return this.elements.length - this.offset; | ||
} | ||
@@ -59,8 +60,8 @@ | ||
* | ||
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
*/ | ||
get first(): E | undefined { | ||
return this.size > 0 ? this.nodes[this.offset] : undefined; | ||
return this.size > 0 ? this.elements[this.offset] : undefined; | ||
} | ||
@@ -78,7 +79,7 @@ | ||
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
*/ | ||
get last(): E | undefined { | ||
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; | ||
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined; | ||
} | ||
@@ -117,3 +118,3 @@ | ||
push(element: E): boolean { | ||
this.nodes.push(element); | ||
this.elements.push(element); | ||
return true; | ||
@@ -141,7 +142,7 @@ } | ||
if (this.offset * 2 < this.nodes.length) return first; | ||
if (this.offset * 2 < this.elements.length) return first; | ||
// only delete dequeued elements when reaching half size | ||
// to decrease latency of shifting elements. | ||
this._nodes = this.nodes.slice(this.offset); | ||
this._elements = this.elements.slice(this.offset); | ||
this._offset = 0; | ||
@@ -160,4 +161,4 @@ return first; | ||
* | ||
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at | ||
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at | ||
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`. | ||
@@ -179,3 +180,3 @@ */ | ||
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the | ||
* array is empty, it returns `undefined`. | ||
@@ -231,3 +232,3 @@ */ | ||
getAt(index: number): E | undefined { | ||
return this.nodes[index]; | ||
return this.elements[index]; | ||
} | ||
@@ -260,14 +261,14 @@ | ||
* | ||
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array. | ||
* The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
* @returns An array of type E is being returned. | ||
*/ | ||
toArray(): E[] { | ||
return this.nodes.slice(this.offset); | ||
return this.elements.slice(this.offset); | ||
} | ||
/** | ||
* The clear function resets the nodes array and offset to their initial values. | ||
* The clear function resets the elements array and offset to their initial values. | ||
*/ | ||
clear(): void { | ||
this._nodes = []; | ||
this._elements = []; | ||
this._offset = 0; | ||
@@ -289,3 +290,3 @@ } | ||
clone(): Queue<E> { | ||
return new Queue(this.nodes.slice(this.offset)); | ||
return new Queue(this.elements.slice(this.offset)); | ||
} | ||
@@ -360,3 +361,3 @@ | ||
protected* _getIterator(): IterableIterator<E> { | ||
for (const item of this.nodes) { | ||
for (const item of this.elements) { | ||
yield item; | ||
@@ -363,0 +364,0 @@ } |
@@ -26,13 +26,10 @@ /** | ||
*/ | ||
constructor(elements?: Iterable<E>) { | ||
constructor(elements: Iterable<E> = []) { | ||
super(); | ||
this._elements = []; | ||
if (elements) { | ||
for (const el of elements) { | ||
this.push(el); | ||
} | ||
for (const el of elements) this.push(el); | ||
} | ||
} | ||
protected _elements: E[]; | ||
protected _elements: E[] = []; | ||
@@ -39,0 +36,0 @@ get elements(): E[] { |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { ElementCallback } from '../../types'; | ||
import type { ElementCallback, TrieOptions } from '../../types'; | ||
import { IterableElementBase } from '../base'; | ||
@@ -42,15 +42,14 @@ | ||
export class Trie extends IterableElementBase<string> { | ||
constructor(words?: string[], caseSensitive = true) { | ||
constructor(words: Iterable<string> = [], options?: TrieOptions) { | ||
super(); | ||
this._root = new TrieNode(''); | ||
this._caseSensitive = caseSensitive; | ||
this._size = 0; | ||
if (options) { | ||
const { caseSensitive } = options; | ||
if (caseSensitive !== undefined) this._caseSensitive = caseSensitive; | ||
} | ||
if (words) { | ||
for (const word of words) { | ||
this.add(word); | ||
} | ||
for (const word of words) this.add(word); | ||
} | ||
} | ||
protected _size: number; | ||
protected _size: number = 0; | ||
@@ -61,3 +60,3 @@ get size(): number { | ||
protected _caseSensitive: boolean; | ||
protected _caseSensitive: boolean = true; | ||
@@ -68,3 +67,3 @@ get caseSensitive(): boolean { | ||
protected _root: TrieNode; | ||
protected _root: TrieNode = new TrieNode(''); | ||
@@ -71,0 +70,0 @@ get root() { |
@@ -9,4 +9,4 @@ import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; | ||
export type BinaryTreeOptions<K> = { | ||
iterationType: IterationType, | ||
extractor: (key: K) => number | ||
iterationType?: IterationType, | ||
extractor?: (key: K) => number | ||
} |
@@ -10,3 +10,3 @@ import { BST, BSTNode } from '../../../data-structures'; | ||
export type BSTOptions<K> = BinaryTreeOptions<K> & { | ||
variant: BSTVariant | ||
variant?: BSTVariant | ||
} |
@@ -8,2 +8,2 @@ import { TreeMultimap, TreeMultimapNode } from '../../../data-structures'; | ||
export type TreeMultimapOptions<K> = Omit<AVLTreeOptions<K>, 'isMergeDuplicatedNodeByKey'> & {} | ||
export type TreeMultimapOptions<K> = AVLTreeOptions<K> & {} |
@@ -8,7 +8,11 @@ export type HashMapLinkedNode<K, V> = { | ||
export type LinkedHashMapOptions<K> = { | ||
hashFn?: (key: K) => string; | ||
objHashFn?: (key: K) => object; | ||
}; | ||
export type HashMapOptions<K> = { | ||
hashFn: (key: K) => string; | ||
objHashFn: (key: K) => object; | ||
hashFn?: (key: K) => string; | ||
}; | ||
export type HashMapStoreItem<K, V> = { key: K; value: V }; |
export * from './hash-map'; | ||
export * from './hash-table'; | ||
export type HashFunction<K> = (key: K) => number; |
import { Comparator } from '../../common'; | ||
export type HeapOptions<T> = { comparator: Comparator<T> }; | ||
export type HeapOptions<T> = { comparator?: Comparator<T> }; |
export * from './singly-linked-list'; | ||
export * from './doubly-linked-list'; | ||
export * from './skip-linked-list'; |
@@ -1,1 +0,1 @@ | ||
export {}; | ||
export type SkipLinkedListOptions = { maxLevel?: number; probability?: number }; |
export * from './navigator'; | ||
export * from './matrix'; |
@@ -1,1 +0,7 @@ | ||
export {}; | ||
export type MatrixOptions = { | ||
rows?: number; | ||
cols?: number; | ||
addFn?: (a: number, b: number) => any; | ||
subtractFn?: (a: number, b: number) => any; | ||
multiplyFn?: (a: number, b: number) => any; | ||
}; |
@@ -1,1 +0,1 @@ | ||
export {}; | ||
export type DequeOptions = { bucketSize?: number }; |
@@ -1,1 +0,1 @@ | ||
export {}; | ||
export type TrieOptions = { caseSensitive?: boolean }; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
670
7
1
76
1933948
338
35507
Updateddata-structure-typed@^1.49.7