min-heap-typed 1.49.6 → 1.49.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
  2. package/dist/data-structures/binary-tree/avl-tree.js +5 -5
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
  4. package/dist/data-structures/binary-tree/binary-tree.js +7 -9
  5. package/dist/data-structures/binary-tree/bst.d.ts +3 -3
  6. package/dist/data-structures/binary-tree/bst.js +6 -7
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
  8. package/dist/data-structures/binary-tree/rb-tree.js +4 -4
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  10. package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
  11. package/dist/data-structures/hash/hash-map.d.ts +24 -27
  12. package/dist/data-structures/hash/hash-map.js +35 -35
  13. package/dist/data-structures/hash/index.d.ts +0 -1
  14. package/dist/data-structures/hash/index.js +0 -1
  15. package/dist/data-structures/heap/heap.d.ts +2 -1
  16. package/dist/data-structures/heap/heap.js +13 -13
  17. package/dist/data-structures/heap/max-heap.js +1 -1
  18. package/dist/data-structures/heap/min-heap.js +1 -1
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
  20. package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
  21. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  22. package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
  23. package/dist/data-structures/matrix/matrix.d.ts +2 -7
  24. package/dist/data-structures/matrix/matrix.js +0 -7
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
  26. package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
  27. package/dist/data-structures/priority-queue/priority-queue.js +1 -1
  28. package/dist/data-structures/queue/deque.d.ts +2 -11
  29. package/dist/data-structures/queue/deque.js +9 -13
  30. package/dist/data-structures/queue/queue.d.ts +13 -13
  31. package/dist/data-structures/queue/queue.js +29 -25
  32. package/dist/data-structures/stack/stack.js +2 -3
  33. package/dist/data-structures/trie/trie.d.ts +2 -2
  34. package/dist/data-structures/trie/trie.js +9 -5
  35. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  36. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  37. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  38. package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
  39. package/dist/types/data-structures/hash/index.d.ts +0 -1
  40. package/dist/types/data-structures/hash/index.js +0 -1
  41. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  42. package/dist/types/data-structures/linked-list/index.d.ts +1 -0
  43. package/dist/types/data-structures/linked-list/index.js +1 -0
  44. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  45. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  46. package/dist/types/data-structures/matrix/index.js +1 -0
  47. package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
  48. package/dist/types/data-structures/queue/deque.d.ts +3 -1
  49. package/dist/types/data-structures/trie/trie.d.ts +3 -1
  50. package/package.json +2 -2
  51. package/src/data-structures/binary-tree/avl-tree.ts +4 -4
  52. package/src/data-structures/binary-tree/binary-tree.ts +6 -10
  53. package/src/data-structures/binary-tree/bst.ts +5 -7
  54. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  55. package/src/data-structures/binary-tree/tree-multimap.ts +2 -2
  56. package/src/data-structures/hash/hash-map.ts +46 -50
  57. package/src/data-structures/hash/index.ts +0 -1
  58. package/src/data-structures/heap/heap.ts +20 -19
  59. package/src/data-structures/heap/max-heap.ts +1 -1
  60. package/src/data-structures/heap/min-heap.ts +1 -1
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  62. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  63. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  64. package/src/data-structures/matrix/matrix.ts +2 -10
  65. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  66. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  67. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  68. package/src/data-structures/queue/deque.ts +11 -15
  69. package/src/data-structures/queue/queue.ts +29 -28
  70. package/src/data-structures/stack/stack.ts +3 -6
  71. package/src/data-structures/trie/trie.ts +10 -11
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  73. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  74. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  75. package/src/types/data-structures/hash/hash-map.ts +6 -2
  76. package/src/types/data-structures/hash/index.ts +0 -1
  77. package/src/types/data-structures/heap/heap.ts +1 -1
  78. package/src/types/data-structures/linked-list/index.ts +1 -0
  79. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  80. package/src/types/data-structures/matrix/index.ts +1 -0
  81. package/src/types/data-structures/matrix/matrix.ts +7 -1
  82. package/src/types/data-structures/queue/deque.ts +1 -1
  83. package/src/types/data-structures/trie/trie.ts +1 -1
  84. package/dist/data-structures/hash/hash-table.d.ts +0 -108
  85. package/dist/data-structures/hash/hash-table.js +0 -281
  86. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  87. package/dist/types/data-structures/hash/hash-table.js +0 -2
  88. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
  89. package/dist/types/data-structures/matrix/matrix2d.js +0 -2
  90. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
  91. package/dist/types/data-structures/matrix/vector2d.js +0 -2
  92. package/src/data-structures/hash/hash-table.ts +0 -318
  93. package/src/types/data-structures/hash/hash-table.ts +0 -1
  94. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  95. package/src/types/data-structures/matrix/vector2d.ts +0 -1
@@ -1 +1,7 @@
1
- export {};
1
+ export type MatrixOptions = {
2
+ rows?: number;
3
+ cols?: number;
4
+ addFn?: (a: number, b: number) => any;
5
+ subtractFn?: (a: number, b: number) => any;
6
+ multiplyFn?: (a: number, b: number) => any;
7
+ };
@@ -1 +1,3 @@
1
- export {};
1
+ export type DequeOptions = {
2
+ bucketSize?: number;
3
+ };
@@ -1 +1,3 @@
1
- export {};
1
+ export type TrieOptions = {
2
+ caseSensitive?: boolean;
3
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.49.6",
3
+ "version": "1.49.7",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.49.5"
135
+ "data-structure-typed": "^1.49.7"
136
136
  }
137
137
  }
@@ -48,17 +48,17 @@ export class AVLTree<
48
48
  extends BST<K, V, N, TREE>
49
49
  implements IBinaryTree<K, V, N, TREE> {
50
50
  /**
51
- * The constructor function initializes an AVLTree object with optional nodes and options.
52
- * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
51
+ * The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
52
+ * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
53
53
  * objects. It represents a collection of nodes that will be added to the AVL tree during
54
54
  * initialization.
55
55
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
56
56
  * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
57
57
  * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
58
58
  */
59
- constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
59
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: AVLTreeOptions<K>) {
60
60
  super([], options);
61
- if (nodes) super.addMany(nodes);
61
+ if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
62
62
  }
63
63
 
64
64
  /**
@@ -111,29 +111,25 @@ export class BinaryTree<
111
111
  iterationType = IterationType.ITERATIVE;
112
112
 
113
113
  /**
114
- * The constructor function initializes a binary tree object with optional nodes and options.
115
- * @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
114
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
115
+ * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
116
116
  * nodes to be added to the binary tree.
117
117
  * @param [options] - The `options` parameter is an optional object that can contain additional
118
118
  * configuration options for the binary tree. In this case, it is of type
119
119
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
120
120
  * required.
121
121
  */
122
- constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
122
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BinaryTreeOptions<K>) {
123
123
  super();
124
124
  if (options) {
125
125
  const { iterationType, extractor } = options;
126
- if (iterationType) {
127
- this.iterationType = iterationType;
128
- }
129
- if (extractor) {
130
- this._extractor = extractor;
131
- }
126
+ if (iterationType) this.iterationType = iterationType;
127
+ if (extractor) this._extractor = extractor;
132
128
  }
133
129
 
134
130
  this._size = 0;
135
131
 
136
- if (nodes) this.addMany(nodes);
132
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
137
133
  }
138
134
 
139
135
  protected _extractor = (key: K) => Number(key);
@@ -92,25 +92,23 @@ export class BST<
92
92
  implements IBinaryTree<K, V, N, TREE> {
93
93
  /**
94
94
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
95
- * the tree with optional nodes and options.
96
- * @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
95
+ * the tree with optional keysOrNodesOrEntries and options.
96
+ * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
97
97
  * binary search tree.
98
98
  * @param [options] - The `options` parameter is an optional object that can contain additional
99
99
  * configuration options for the binary search tree. It can have the following properties:
100
100
  */
101
- constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>) {
101
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BSTOptions<K>) {
102
102
  super([], options);
103
103
 
104
104
  if (options) {
105
105
  const { variant } = options;
106
- if (variant) {
107
- this._variant = variant;
108
- }
106
+ if (variant) this._variant = variant;
109
107
  }
110
108
 
111
109
  this._root = undefined;
112
110
 
113
- if (nodes) this.addMany(nodes);
111
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
114
112
  }
115
113
 
116
114
  protected override _root?: N;
@@ -53,7 +53,7 @@ export class RedBlackTree<
53
53
  /**
54
54
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
55
55
  * initializes the tree with optional nodes and options.
56
- * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
56
+ * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
57
57
  * objects. It represents the initial nodes that will be added to the RBTree during its
58
58
  * construction. If this parameter is provided, the `addMany` method is called to add all the
59
59
  * nodes to the
@@ -61,11 +61,11 @@ export class RedBlackTree<
61
61
  * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
62
62
  * only a subset of the properties defined in the `RBTreeOptions` interface.
63
63
  */
64
- constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
64
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: RBTreeOptions<K>) {
65
65
  super([], options);
66
66
 
67
67
  this._root = this.Sentinel;
68
- if (nodes) super.addMany(nodes);
68
+ if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
69
69
  }
70
70
 
71
71
  protected _root: N;
@@ -52,9 +52,9 @@ export class TreeMultimap<
52
52
  >
53
53
  extends AVLTree<K, V, N, TREE>
54
54
  implements IBinaryTree<K, V, N, TREE> {
55
- constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
55
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) {
56
56
  super([], options);
57
- if (nodes) this.addMany(nodes);
57
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
58
58
  }
59
59
 
60
60
  private _count = 0;
@@ -5,34 +5,35 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
8
+ import type {
9
+ EntryCallback,
10
+ HashMapLinkedNode,
11
+ HashMapOptions,
12
+ HashMapStoreItem,
13
+ LinkedHashMapOptions
14
+ } from '../../types';
9
15
  import { IterableEntryBase } from '../base';
10
16
  import { isWeakKey, rangeCheck } from '../../utils';
11
17
 
12
18
  /**
13
19
  * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
14
- * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
20
+ * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
15
21
  * 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.
16
- * 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
22
+ * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
17
23
  */
18
24
  export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
19
25
  protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
20
26
  protected _objMap: Map<object, V> = new Map();
21
27
 
22
28
  /**
23
- * The constructor function initializes a new instance of a class with optional elements and options.
24
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
29
+ * The constructor function initializes a new instance of a class with optional entries and options.
30
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
25
31
  * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
26
32
  * key-value pairs.
27
33
  * @param [options] - The `options` parameter is an optional object that can contain additional
28
34
  * configuration options for the constructor. In this case, it has one property:
29
35
  */
30
- constructor(
31
- elements: Iterable<[K, V]> = [],
32
- options?: {
33
- hashFn: (key: K) => string;
34
- }
35
- ) {
36
+ constructor(entries: Iterable<[K, V]> = [], options?: HashMapOptions<K>) {
36
37
  super();
37
38
  if (options) {
38
39
  const { hashFn } = options;
@@ -40,8 +41,8 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
40
41
  this._hashFn = hashFn;
41
42
  }
42
43
  }
43
- if (elements) {
44
- this.setMany(elements);
44
+ if (entries) {
45
+ this.setMany(entries);
45
46
  }
46
47
  }
47
48
 
@@ -88,12 +89,12 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
88
89
 
89
90
  /**
90
91
  * The function "setMany" sets multiple key-value pairs in a map.
91
- * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
92
- * key-value pair is represented as an array with two elements: the key and the value.
92
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
93
+ * key-value pair is represented as an array with two entries: the key and the value.
93
94
  */
94
- setMany(elements: Iterable<[K, V]>): boolean[] {
95
+ setMany(entries: Iterable<[K, V]>): boolean[] {
95
96
  const results: boolean[] = [];
96
- for (const [key, value] of elements) results.push(this.set(key, value));
97
+ for (const [key, value] of entries) results.push(this.set(key, value));
97
98
  return results;
98
99
  }
99
100
 
@@ -214,10 +215,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
214
215
  return filteredMap;
215
216
  }
216
217
 
217
- print(): void {
218
- console.log([...this.entries()]);
219
- }
220
-
221
218
  put(key: K, value: V): boolean {
222
219
  return this.set(key, value);
223
220
  }
@@ -261,8 +258,8 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
261
258
  }
262
259
 
263
260
  /**
264
- * 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.
265
- * 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.
261
+ * 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.
262
+ * 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.
266
263
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
267
264
  */
268
265
  export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
@@ -271,25 +268,20 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
271
268
  protected _head: HashMapLinkedNode<K, V | undefined>;
272
269
  protected _tail: HashMapLinkedNode<K, V | undefined>;
273
270
  protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
274
- protected _hashFn: (key: K) => string;
275
- protected _objHashFn: (key: K) => object;
276
-
277
- constructor(
278
- elements?: Iterable<[K, V]>,
279
- options: HashMapOptions<K> = {
280
- hashFn: (key: K) => String(key),
281
- objHashFn: (key: K) => <object>key
282
- }
283
- ) {
271
+
272
+ constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>) {
284
273
  super();
285
274
  this._sentinel = <HashMapLinkedNode<K, V>>{};
286
275
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
287
276
 
288
- const { hashFn, objHashFn } = options;
289
- this._hashFn = hashFn;
290
- this._objHashFn = objHashFn;
291
- if (elements) {
292
- for (const el of elements) {
277
+ if (options) {
278
+ const { hashFn, objHashFn } = options;
279
+ if (hashFn) this._hashFn = hashFn;
280
+ if (objHashFn) this._objHashFn = objHashFn;
281
+ }
282
+
283
+ if (entries) {
284
+ for (const el of entries) {
293
285
  this.set(el[0], el[1]);
294
286
  }
295
287
  }
@@ -547,7 +539,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
547
539
  * Time Complexity: O(1)
548
540
  * Space Complexity: O(1)
549
541
  *
550
- * The `clear` function clears all the elements in a data structure and resets its properties.
542
+ * The `clear` function clears all the entries in a data structure and resets its properties.
551
543
  */
552
544
  clear(): void {
553
545
  this._noObjMap = {};
@@ -564,11 +556,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
564
556
  return cloned;
565
557
  }
566
558
 
567
- /**
568
- * Time Complexity: O(n)
569
- * Space Complexity: O(n)
570
- */
571
-
572
559
  /**
573
560
  * Time Complexity: O(n)
574
561
  * Space Complexity: O(n)
@@ -596,11 +583,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
596
583
  return filteredMap;
597
584
  }
598
585
 
599
- /**
600
- * Time Complexity: O(n)
601
- * Space Complexity: O(n)
602
- */
603
-
604
586
  /**
605
587
  * Time Complexity: O(n)
606
588
  * Space Complexity: O(n)
@@ -629,12 +611,26 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
629
611
  return mappedMap;
630
612
  }
631
613
 
614
+ /**
615
+ * Time Complexity: O(n)
616
+ * Space Complexity: O(n)
617
+ */
618
+
632
619
  put(key: K, value: V): boolean {
633
620
  return this.set(key, value);
634
621
  }
635
622
 
636
623
  /**
637
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
624
+ * Time Complexity: O(n)
625
+ * Space Complexity: O(n)
626
+ */
627
+
628
+ protected _hashFn: (key: K) => string = (key: K) => String(key);
629
+
630
+ protected _objHashFn: (key: K) => object = (key: K) => <object>key;
631
+
632
+ /**
633
+ * Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
638
634
  * Space Complexity: O(1)
639
635
  *
640
636
  * The above function is an iterator that yields key-value pairs from a linked list.
@@ -1,2 +1 @@
1
- export * from './hash-table';
2
1
  export * from './hash-map';
@@ -21,23 +21,12 @@ import { IterableElementBase } from '../base';
21
21
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
22
22
  */
23
23
  export class Heap<E = any> extends IterableElementBase<E> {
24
- options: HeapOptions<E>;
25
-
26
- constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
24
+ constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
27
25
  super();
28
- const defaultComparator = (a: E, b: E) => {
29
- if (!(typeof a === 'number' && typeof b === 'number')) {
30
- throw new Error('The a, b params of compare function must be number');
31
- } else {
32
- return a - b;
33
- }
34
- };
26
+
35
27
  if (options) {
36
- this.options = options;
37
- } else {
38
- this.options = {
39
- comparator: defaultComparator
40
- };
28
+ const { comparator } = options;
29
+ if (comparator) this._comparator = comparator;
41
30
  }
42
31
 
43
32
  if (elements) {
@@ -48,6 +37,18 @@ export class Heap<E = any> extends IterableElementBase<E> {
48
37
  }
49
38
  }
50
39
 
40
+ protected _comparator = (a: E, b: E) => {
41
+ if (!(typeof a === 'number' && typeof b === 'number')) {
42
+ throw new Error('The a, b params of compare function must be number');
43
+ } else {
44
+ return a - b;
45
+ }
46
+ };
47
+
48
+ get comparator() {
49
+ return this._comparator;
50
+ }
51
+
51
52
  protected _elements: E[] = [];
52
53
 
53
54
  get elements(): E[] {
@@ -278,7 +279,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
278
279
  * @returns A new Heap instance containing the same elements.
279
280
  */
280
281
  clone(): Heap<E> {
281
- const clonedHeap = new Heap<E>([], this.options);
282
+ const clonedHeap = new Heap<E>([], { comparator: this.comparator });
282
283
  clonedHeap._elements = [...this.elements];
283
284
  return clonedHeap;
284
285
  }
@@ -413,7 +414,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
413
414
  while (index > 0) {
414
415
  const parent = (index - 1) >> 1;
415
416
  const parentItem = this.elements[parent];
416
- if (this.options.comparator(parentItem, element) <= 0) break;
417
+ if (this.comparator(parentItem, element) <= 0) break;
417
418
  this.elements[index] = parentItem;
418
419
  index = parent;
419
420
  }
@@ -435,11 +436,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
435
436
  let left = (index << 1) | 1;
436
437
  const right = left + 1;
437
438
  let minItem = this.elements[left];
438
- if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
439
+ if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
439
440
  left = right;
440
441
  minItem = this.elements[right];
441
442
  }
442
- if (this.options.comparator(minItem, element) >= 0) break;
443
+ if (this.comparator(minItem, element) >= 0) break;
443
444
  this.elements[index] = minItem;
444
445
  index = left;
445
446
  }
@@ -20,7 +20,7 @@ import { Heap } from './heap';
20
20
  */
21
21
  export class MaxHeap<E = any> extends Heap<E> {
22
22
  constructor(
23
- elements?: Iterable<E>,
23
+ elements: Iterable<E> = [],
24
24
  options: HeapOptions<E> = {
25
25
  comparator: (a: E, b: E) => {
26
26
  if (!(typeof a === 'number' && typeof b === 'number')) {
@@ -20,7 +20,7 @@ import { Heap } from './heap';
20
20
  */
21
21
  export class MinHeap<E = any> extends Heap<E> {
22
22
  constructor(
23
- elements?: Iterable<E>,
23
+ elements: Iterable<E> = [],
24
24
  options: HeapOptions<E> = {
25
25
  comparator: (a: E, b: E) => {
26
26
  if (!(typeof a === 'number' && typeof b === 'number')) {
@@ -35,7 +35,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
35
35
  /**
36
36
  * The constructor initializes the linked list with an empty head, tail, and size.
37
37
  */
38
- constructor(elements?: Iterable<E>) {
38
+ constructor(elements: Iterable<E> = []) {
39
39
  super();
40
40
  this._head = undefined;
41
41
  this._tail = undefined;
@@ -27,11 +27,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
27
27
  /**
28
28
  * The constructor initializes the linked list with an empty head, tail, and length.
29
29
  */
30
- constructor(elements?: Iterable<E>) {
30
+ constructor(elements: Iterable<E> = []) {
31
31
  super();
32
- this._head = undefined;
33
- this._tail = undefined;
34
- this._size = 0;
35
32
  if (elements) {
36
33
  for (const el of elements) this.push(el);
37
34
  }
@@ -49,7 +46,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
49
46
  return this._tail;
50
47
  }
51
48
 
52
- protected _size: number;
49
+ protected _size: number = 0;
53
50
 
54
51
  get size(): number {
55
52
  return this._size;
@@ -5,6 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
+ import type { SkipLinkedListOptions } from '../../types';
8
9
 
9
10
  export class SkipListNode<K, V> {
10
11
  key: K;
@@ -19,39 +20,37 @@ export class SkipListNode<K, V> {
19
20
  }
20
21
 
21
22
  export class SkipList<K, V> {
22
- /**
23
- * The constructor initializes a SkipList with a specified maximum level and probability.
24
- * @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
25
- * the maximum number of levels that can be created in the skip list.
26
- * @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
27
- * level in the skip list. It is used to determine the height of each node in the skip list.
28
- */
29
- constructor(maxLevel = 16, probability = 0.5) {
30
- this._head = new SkipListNode<K, V>(undefined as any, undefined as any, maxLevel);
31
- this._level = 0;
32
- this._maxLevel = maxLevel;
33
- this._probability = probability;
23
+ constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
24
+ if (options) {
25
+ const { maxLevel, probability } = options;
26
+ if (typeof maxLevel === 'number') this._maxLevel = maxLevel;
27
+ if (typeof probability === 'number') this._probability = probability;
28
+ }
29
+
30
+ if (elements) {
31
+ for (const [key, value] of elements) this.add(key, value);
32
+ }
34
33
  }
35
34
 
36
- protected _head: SkipListNode<K, V>;
35
+ protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
37
36
 
38
37
  get head(): SkipListNode<K, V> {
39
38
  return this._head;
40
39
  }
41
40
 
42
- protected _level: number;
41
+ protected _level: number = 0;
43
42
 
44
43
  get level(): number {
45
44
  return this._level;
46
45
  }
47
46
 
48
- protected _maxLevel: number;
47
+ protected _maxLevel: number = 16;
49
48
 
50
49
  get maxLevel(): number {
51
50
  return this._maxLevel;
52
51
  }
53
52
 
54
- protected _probability: number;
53
+ protected _probability: number = 0.5;
55
54
 
56
55
  get probability(): number {
57
56
  return this._probability;
@@ -5,6 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
+ import type { MatrixOptions } from '../../types';
8
9
 
9
10
  export class Matrix {
10
11
  /**
@@ -14,16 +15,7 @@ export class Matrix {
14
15
  * @param [options] - The `options` parameter is an optional object that can contain the following
15
16
  * properties:
16
17
  */
17
- constructor(
18
- data: number[][],
19
- options?: {
20
- rows?: number;
21
- cols?: number;
22
- addFn?: (a: number, b: number) => any;
23
- subtractFn?: (a: number, b: number) => any;
24
- multiplyFn?: (a: number, b: number) => any;
25
- }
26
- ) {
18
+ constructor(data: number[][], options?: MatrixOptions) {
27
19
  if (options) {
28
20
  const { rows, cols, addFn, subtractFn, multiplyFn } = options;
29
21
  if (typeof rows === 'number' && rows > 0) this._rows = rows;
@@ -10,7 +10,7 @@ import { PriorityQueue } from './priority-queue';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- elements?: Iterable<E>,
13
+ elements: Iterable<E> = [],
14
14
  options: PriorityQueueOptions<E> = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
@@ -10,7 +10,7 @@ import { PriorityQueue } from './priority-queue';
10
10
 
11
11
  export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- elements?: Iterable<E>,
13
+ elements: Iterable<E> = [],
14
14
  options: PriorityQueueOptions<E> = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
@@ -17,7 +17,7 @@ import { Heap } from '../heap';
17
17
  * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
18
18
  */
19
19
  export class PriorityQueue<E = any> extends Heap<E> {
20
- constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
20
+ constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) {
21
21
  super(elements, options);
22
22
  }
23
23
  }