deque-typed 1.49.5 → 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 (100) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  2. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
  4. package/dist/data-structures/binary-tree/binary-tree.js +194 -153
  5. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  6. package/dist/data-structures/binary-tree/bst.js +114 -91
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  8. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
  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/interfaces/binary-tree.d.ts +3 -3
  36. package/dist/types/common.d.ts +3 -3
  37. package/dist/types/common.js +2 -2
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  40. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  41. package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
  42. package/dist/types/data-structures/hash/index.d.ts +0 -1
  43. package/dist/types/data-structures/hash/index.js +0 -1
  44. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  45. package/dist/types/data-structures/linked-list/index.d.ts +1 -0
  46. package/dist/types/data-structures/linked-list/index.js +1 -0
  47. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  48. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  49. package/dist/types/data-structures/matrix/index.js +1 -0
  50. package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
  51. package/dist/types/data-structures/queue/deque.d.ts +3 -1
  52. package/dist/types/data-structures/trie/trie.d.ts +3 -1
  53. package/package.json +2 -2
  54. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  55. package/src/data-structures/binary-tree/binary-tree.ts +255 -211
  56. package/src/data-structures/binary-tree/bst.ts +126 -107
  57. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  58. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  59. package/src/data-structures/hash/hash-map.ts +46 -50
  60. package/src/data-structures/hash/index.ts +0 -1
  61. package/src/data-structures/heap/heap.ts +20 -19
  62. package/src/data-structures/heap/max-heap.ts +1 -1
  63. package/src/data-structures/heap/min-heap.ts +1 -1
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  65. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  66. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  67. package/src/data-structures/matrix/matrix.ts +2 -10
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  70. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  71. package/src/data-structures/queue/deque.ts +11 -15
  72. package/src/data-structures/queue/queue.ts +29 -28
  73. package/src/data-structures/stack/stack.ts +3 -6
  74. package/src/data-structures/trie/trie.ts +10 -11
  75. package/src/interfaces/binary-tree.ts +3 -3
  76. package/src/types/common.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  80. package/src/types/data-structures/hash/hash-map.ts +6 -2
  81. package/src/types/data-structures/hash/index.ts +0 -1
  82. package/src/types/data-structures/heap/heap.ts +1 -1
  83. package/src/types/data-structures/linked-list/index.ts +1 -0
  84. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  85. package/src/types/data-structures/matrix/index.ts +1 -0
  86. package/src/types/data-structures/matrix/matrix.ts +7 -1
  87. package/src/types/data-structures/queue/deque.ts +1 -1
  88. package/src/types/data-structures/trie/trie.ts +1 -1
  89. package/dist/data-structures/hash/hash-table.d.ts +0 -108
  90. package/dist/data-structures/hash/hash-table.js +0 -281
  91. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  92. package/dist/types/data-structures/hash/hash-table.js +0 -2
  93. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
  94. package/dist/types/data-structures/matrix/matrix2d.js +0 -2
  95. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
  96. package/dist/types/data-structures/matrix/vector2d.js +0 -2
  97. package/src/data-structures/hash/hash-table.ts +0 -318
  98. package/src/types/data-structures/hash/hash-table.ts +0 -1
  99. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  100. package/src/types/data-structures/matrix/vector2d.ts +0 -1
@@ -24,11 +24,11 @@ exports.TreeMultimapNode = TreeMultimapNode;
24
24
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
25
25
  */
26
26
  class TreeMultimap extends avl_tree_1.AVLTree {
27
- constructor(elements, options) {
27
+ constructor(keysOrNodesOrEntries = [], options) {
28
28
  super([], options);
29
29
  this._count = 0;
30
- if (elements)
31
- this.addMany(elements);
30
+ if (keysOrNodesOrEntries)
31
+ this.addMany(keysOrNodesOrEntries);
32
32
  }
33
33
  // TODO the _count is not accurate after nodes count modified
34
34
  get count() {
@@ -52,26 +52,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
52
52
  return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
53
53
  }
54
54
  /**
55
- * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
57
- * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
- * class.
59
- */
60
- isNode(exemplar) {
61
- return exemplar instanceof TreeMultimapNode;
62
- }
63
- /**
64
- * The function "isNotNodeInstance" checks if a potential key is a K.
65
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
66
- * data type.
67
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
68
- */
69
- isNotNodeInstance(potentialKey) {
70
- return !(potentialKey instanceof TreeMultimapNode);
71
- }
72
- /**
73
- * The function `exemplarToNode` converts an exemplar object into a node object.
74
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
55
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
56
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
75
57
  * can be one of the following:
76
58
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
77
59
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
@@ -80,16 +62,16 @@ class TreeMultimap extends avl_tree_1.AVLTree {
80
62
  * times the value should be added to the node. If not provided, it defaults to 1.
81
63
  * @returns a node of type `N` or `undefined`.
82
64
  */
83
- exemplarToNode(exemplar, value, count = 1) {
65
+ exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
84
66
  let node;
85
- if (exemplar === undefined || exemplar === null) {
67
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
86
68
  return;
87
69
  }
88
- else if (this.isNode(exemplar)) {
89
- node = exemplar;
70
+ else if (this.isNode(keyOrNodeOrEntry)) {
71
+ node = keyOrNodeOrEntry;
90
72
  }
91
- else if (this.isEntry(exemplar)) {
92
- const [key, value] = exemplar;
73
+ else if (this.isEntry(keyOrNodeOrEntry)) {
74
+ const [key, value] = keyOrNodeOrEntry;
93
75
  if (key === undefined || key === null) {
94
76
  return;
95
77
  }
@@ -97,8 +79,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
97
79
  node = this.createNode(key, value, count);
98
80
  }
99
81
  }
100
- else if (this.isNotNodeInstance(exemplar)) {
101
- node = this.createNode(exemplar, value, count);
82
+ else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
83
+ node = this.createNode(keyOrNodeOrEntry, value, count);
102
84
  }
103
85
  else {
104
86
  return;
@@ -106,12 +88,31 @@ class TreeMultimap extends avl_tree_1.AVLTree {
106
88
  return node;
107
89
  }
108
90
  /**
109
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
110
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
91
+ * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
92
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
93
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
94
+ * class.
111
95
  */
96
+ isNode(keyOrNodeOrEntry) {
97
+ return keyOrNodeOrEntry instanceof TreeMultimapNode;
98
+ }
112
99
  /**
113
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
114
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
100
+ * The function "isNotNodeInstance" checks if a potential key is a K.
101
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
102
+ * data type.
103
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
104
+ */
105
+ isNotNodeInstance(potentialKey) {
106
+ return !(potentialKey instanceof TreeMultimapNode);
107
+ }
108
+ /**
109
+ * Time Complexity: O(log n)
110
+ * Space Complexity: O(1)
111
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
112
+ */
113
+ /**
114
+ * Time Complexity: O(log n)
115
+ * Space Complexity: O(1)
115
116
  *
116
117
  * The function overrides the add method of a binary tree node and adds a new node to the tree.
117
118
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -128,21 +129,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
128
129
  add(keyOrNodeOrEntry, value, count = 1) {
129
130
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
130
131
  if (newNode === undefined)
131
- return;
132
+ return false;
132
133
  const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
133
134
  const inserted = super.add(newNode);
134
135
  if (inserted) {
135
136
  this._count += orgNodeCount;
136
137
  }
137
- return inserted;
138
+ return true;
138
139
  }
139
140
  /**
140
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
141
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
141
+ * Time Complexity: O(k log n)
142
+ * Space Complexity: O(1)
143
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
142
144
  */
143
145
  /**
144
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
145
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
146
+ * Time Complexity: O(k log n)
147
+ * Space Complexity: O(1)
146
148
  *
147
149
  * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
148
150
  * structure.
@@ -154,12 +156,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
154
156
  return super.addMany(keysOrNodesOrEntries);
155
157
  }
156
158
  /**
157
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
158
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
159
+ * Time Complexity: O(n log n)
160
+ * Space Complexity: O(n)
161
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
159
162
  */
160
163
  /**
161
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
162
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
164
+ * Time Complexity: O(n log n)
165
+ * Space Complexity: O(n)
163
166
  *
164
167
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
165
168
  * tree using either a recursive or iterative approach.
@@ -205,12 +208,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
205
208
  }
206
209
  }
207
210
  /**
208
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
209
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
211
+ * Time Complexity: O(k log n)
212
+ * Space Complexity: O(1)
213
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
210
214
  */
211
215
  /**
212
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
213
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
216
+ * Time Complexity: O(k log n)
217
+ * Space Complexity: O(1)
214
218
  *
215
219
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
216
220
  * account the count of the node and balancing the tree if necessary.
@@ -286,10 +290,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
286
290
  return deletedResult;
287
291
  }
288
292
  /**
289
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
290
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
293
+ * Time Complexity: O(1)
294
+ * Space Complexity: O(1)
291
295
  */
292
296
  /**
297
+ * Time Complexity: O(1)
298
+ * Space Complexity: O(1)
299
+ *
293
300
  * The clear() function clears the contents of a data structure and sets the count to zero.
294
301
  */
295
302
  clear() {
@@ -5,13 +5,13 @@
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 { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types';
9
9
  import { IterableEntryBase } from '../base';
10
10
  /**
11
11
  * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
12
- * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
12
+ * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
13
13
  * 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.
14
- * 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
14
+ * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
15
15
  */
16
16
  export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
17
17
  protected _store: {
@@ -19,16 +19,14 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
19
19
  };
20
20
  protected _objMap: Map<object, V>;
21
21
  /**
22
- * The constructor function initializes a new instance of a class with optional elements and options.
23
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
22
+ * The constructor function initializes a new instance of a class with optional entries and options.
23
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
24
24
  * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
25
25
  * key-value pairs.
26
26
  * @param [options] - The `options` parameter is an optional object that can contain additional
27
27
  * configuration options for the constructor. In this case, it has one property:
28
28
  */
29
- constructor(elements?: Iterable<[K, V]>, options?: {
30
- hashFn: (key: K) => string;
31
- });
29
+ constructor(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
32
30
  protected _size: number;
33
31
  get size(): number;
34
32
  isEmpty(): boolean;
@@ -45,10 +43,10 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
45
43
  set(key: K, value: V): boolean;
46
44
  /**
47
45
  * The function "setMany" sets multiple key-value pairs in a map.
48
- * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
49
- * key-value pair is represented as an array with two elements: the key and the value.
46
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
47
+ * key-value pair is represented as an array with two entries: the key and the value.
50
48
  */
51
- setMany(elements: Iterable<[K, V]>): boolean[];
49
+ setMany(entries: Iterable<[K, V]>): boolean[];
52
50
  /**
53
51
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
54
52
  * a string map.
@@ -113,7 +111,6 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
113
111
  * from the original `HashMap` that pass the provided `predicate` function.
114
112
  */
115
113
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
116
- print(): void;
117
114
  put(key: K, value: V): boolean;
118
115
  /**
119
116
  * The function returns an iterator that yields key-value pairs from both an object store and an
@@ -125,8 +122,8 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
125
122
  protected _getNoObjKey(key: K): string;
126
123
  }
127
124
  /**
128
- * 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.
129
- * 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.
125
+ * 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.
126
+ * 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.
130
127
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
131
128
  */
132
129
  export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
@@ -135,9 +132,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
135
132
  protected _head: HashMapLinkedNode<K, V | undefined>;
136
133
  protected _tail: HashMapLinkedNode<K, V | undefined>;
137
134
  protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
138
- protected _hashFn: (key: K) => string;
139
- protected _objHashFn: (key: K) => object;
140
- constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
135
+ constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>);
141
136
  protected _size: number;
142
137
  get size(): number;
143
138
  /**
@@ -242,14 +237,10 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
242
237
  * Time Complexity: O(1)
243
238
  * Space Complexity: O(1)
244
239
  *
245
- * The `clear` function clears all the elements in a data structure and resets its properties.
240
+ * The `clear` function clears all the entries in a data structure and resets its properties.
246
241
  */
247
242
  clear(): void;
248
243
  clone(): LinkedHashMap<K, V>;
249
- /**
250
- * Time Complexity: O(n)
251
- * Space Complexity: O(n)
252
- */
253
244
  /**
254
245
  * Time Complexity: O(n)
255
246
  * Space Complexity: O(n)
@@ -266,10 +257,6 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
266
257
  * `LinkedHashMap` object that satisfy the given predicate function.
267
258
  */
268
259
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
269
- /**
270
- * Time Complexity: O(n)
271
- * Space Complexity: O(n)
272
- */
273
260
  /**
274
261
  * Time Complexity: O(n)
275
262
  * Space Complexity: O(n)
@@ -288,9 +275,19 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
288
275
  * function.
289
276
  */
290
277
  map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
278
+ /**
279
+ * Time Complexity: O(n)
280
+ * Space Complexity: O(n)
281
+ */
291
282
  put(key: K, value: V): boolean;
292
283
  /**
293
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
284
+ * Time Complexity: O(n)
285
+ * Space Complexity: O(n)
286
+ */
287
+ protected _hashFn: (key: K) => string;
288
+ protected _objHashFn: (key: K) => object;
289
+ /**
290
+ * Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
294
291
  * Space Complexity: O(1)
295
292
  *
296
293
  * The above function is an iterator that yields key-value pairs from a linked list.
@@ -5,20 +5,20 @@ const base_1 = require("../base");
5
5
  const utils_1 = require("../../utils");
6
6
  /**
7
7
  * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
8
- * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
8
+ * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
9
9
  * 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.
10
- * 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
10
+ * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
11
11
  */
12
12
  class HashMap extends base_1.IterableEntryBase {
13
13
  /**
14
- * The constructor function initializes a new instance of a class with optional elements and options.
15
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
14
+ * The constructor function initializes a new instance of a class with optional entries and options.
15
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
16
16
  * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
17
17
  * key-value pairs.
18
18
  * @param [options] - The `options` parameter is an optional object that can contain additional
19
19
  * configuration options for the constructor. In this case, it has one property:
20
20
  */
21
- constructor(elements = [], options) {
21
+ constructor(entries = [], options) {
22
22
  super();
23
23
  this._store = {};
24
24
  this._objMap = new Map();
@@ -30,8 +30,8 @@ class HashMap extends base_1.IterableEntryBase {
30
30
  this._hashFn = hashFn;
31
31
  }
32
32
  }
33
- if (elements) {
34
- this.setMany(elements);
33
+ if (entries) {
34
+ this.setMany(entries);
35
35
  }
36
36
  }
37
37
  get size() {
@@ -72,12 +72,12 @@ class HashMap extends base_1.IterableEntryBase {
72
72
  }
73
73
  /**
74
74
  * The function "setMany" sets multiple key-value pairs in a map.
75
- * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
76
- * key-value pair is represented as an array with two elements: the key and the value.
75
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
76
+ * key-value pair is represented as an array with two entries: the key and the value.
77
77
  */
78
- setMany(elements) {
78
+ setMany(entries) {
79
79
  const results = [];
80
- for (const [key, value] of elements)
80
+ for (const [key, value] of entries)
81
81
  results.push(this.set(key, value));
82
82
  return results;
83
83
  }
@@ -194,9 +194,6 @@ class HashMap extends base_1.IterableEntryBase {
194
194
  }
195
195
  return filteredMap;
196
196
  }
197
- print() {
198
- console.log([...this.entries()]);
199
- }
200
197
  put(key, value) {
201
198
  return this.set(key, value);
202
199
  }
@@ -236,26 +233,33 @@ class HashMap extends base_1.IterableEntryBase {
236
233
  }
237
234
  exports.HashMap = HashMap;
238
235
  /**
239
- * 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.
240
- * 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.
236
+ * 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.
237
+ * 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.
241
238
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
242
239
  */
243
240
  class LinkedHashMap extends base_1.IterableEntryBase {
244
- constructor(elements, options = {
245
- hashFn: (key) => String(key),
246
- objHashFn: (key) => key
247
- }) {
241
+ constructor(entries, options) {
248
242
  super();
249
243
  this._noObjMap = {};
250
244
  this._objMap = new WeakMap();
251
245
  this._size = 0;
246
+ /**
247
+ * Time Complexity: O(n)
248
+ * Space Complexity: O(n)
249
+ */
250
+ this._hashFn = (key) => String(key);
251
+ this._objHashFn = (key) => key;
252
252
  this._sentinel = {};
253
253
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
254
- const { hashFn, objHashFn } = options;
255
- this._hashFn = hashFn;
256
- this._objHashFn = objHashFn;
257
- if (elements) {
258
- for (const el of elements) {
254
+ if (options) {
255
+ const { hashFn, objHashFn } = options;
256
+ if (hashFn)
257
+ this._hashFn = hashFn;
258
+ if (objHashFn)
259
+ this._objHashFn = objHashFn;
260
+ }
261
+ if (entries) {
262
+ for (const el of entries) {
259
263
  this.set(el[0], el[1]);
260
264
  }
261
265
  }
@@ -495,7 +499,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
495
499
  * Time Complexity: O(1)
496
500
  * Space Complexity: O(1)
497
501
  *
498
- * The `clear` function clears all the elements in a data structure and resets its properties.
502
+ * The `clear` function clears all the entries in a data structure and resets its properties.
499
503
  */
500
504
  clear() {
501
505
  this._noObjMap = {};
@@ -510,10 +514,6 @@ class LinkedHashMap extends base_1.IterableEntryBase {
510
514
  }
511
515
  return cloned;
512
516
  }
513
- /**
514
- * Time Complexity: O(n)
515
- * Space Complexity: O(n)
516
- */
517
517
  /**
518
518
  * Time Complexity: O(n)
519
519
  * Space Complexity: O(n)
@@ -540,10 +540,6 @@ class LinkedHashMap extends base_1.IterableEntryBase {
540
540
  }
541
541
  return filteredMap;
542
542
  }
543
- /**
544
- * Time Complexity: O(n)
545
- * Space Complexity: O(n)
546
- */
547
543
  /**
548
544
  * Time Complexity: O(n)
549
545
  * Space Complexity: O(n)
@@ -571,11 +567,15 @@ class LinkedHashMap extends base_1.IterableEntryBase {
571
567
  }
572
568
  return mappedMap;
573
569
  }
570
+ /**
571
+ * Time Complexity: O(n)
572
+ * Space Complexity: O(n)
573
+ */
574
574
  put(key, value) {
575
575
  return this.set(key, value);
576
576
  }
577
577
  /**
578
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
578
+ * Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
579
579
  * Space Complexity: O(1)
580
580
  *
581
581
  * 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';
@@ -14,5 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./hash-table"), exports);
18
17
  __exportStar(require("./hash-map"), exports);
@@ -19,8 +19,9 @@ import { IterableElementBase } from '../base';
19
19
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
20
20
  */
21
21
  export declare class Heap<E = any> extends IterableElementBase<E> {
22
- options: HeapOptions<E>;
23
22
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
23
+ protected _comparator: (a: E, b: E) => number;
24
+ get comparator(): (a: E, b: E) => number;
24
25
  protected _elements: E[];
25
26
  get elements(): E[];
26
27
  /**
@@ -21,10 +21,9 @@ const base_1 = require("../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
  class Heap extends base_1.IterableElementBase {
24
- constructor(elements, options) {
24
+ constructor(elements = [], options) {
25
25
  super();
26
- this._elements = [];
27
- const defaultComparator = (a, b) => {
26
+ this._comparator = (a, b) => {
28
27
  if (!(typeof a === 'number' && typeof b === 'number')) {
29
28
  throw new Error('The a, b params of compare function must be number');
30
29
  }
@@ -32,13 +31,11 @@ class Heap extends base_1.IterableElementBase {
32
31
  return a - b;
33
32
  }
34
33
  };
34
+ this._elements = [];
35
35
  if (options) {
36
- this.options = options;
37
- }
38
- else {
39
- this.options = {
40
- comparator: defaultComparator
41
- };
36
+ const { comparator } = options;
37
+ if (comparator)
38
+ this._comparator = comparator;
42
39
  }
43
40
  if (elements) {
44
41
  for (const el of elements) {
@@ -47,6 +44,9 @@ class Heap extends base_1.IterableElementBase {
47
44
  // this.fix();
48
45
  }
49
46
  }
47
+ get comparator() {
48
+ return this._comparator;
49
+ }
50
50
  get elements() {
51
51
  return this._elements;
52
52
  }
@@ -256,7 +256,7 @@ class Heap extends base_1.IterableElementBase {
256
256
  * @returns A new Heap instance containing the same elements.
257
257
  */
258
258
  clone() {
259
- const clonedHeap = new Heap([], this.options);
259
+ const clonedHeap = new Heap([], { comparator: this.comparator });
260
260
  clonedHeap._elements = [...this.elements];
261
261
  return clonedHeap;
262
262
  }
@@ -382,7 +382,7 @@ class Heap extends base_1.IterableElementBase {
382
382
  while (index > 0) {
383
383
  const parent = (index - 1) >> 1;
384
384
  const parentItem = this.elements[parent];
385
- if (this.options.comparator(parentItem, element) <= 0)
385
+ if (this.comparator(parentItem, element) <= 0)
386
386
  break;
387
387
  this.elements[index] = parentItem;
388
388
  index = parent;
@@ -404,11 +404,11 @@ class Heap extends base_1.IterableElementBase {
404
404
  let left = (index << 1) | 1;
405
405
  const right = left + 1;
406
406
  let minItem = this.elements[left];
407
- if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
407
+ if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
408
408
  left = right;
409
409
  minItem = this.elements[right];
410
410
  }
411
- if (this.options.comparator(minItem, element) >= 0)
411
+ if (this.comparator(minItem, element) >= 0)
412
412
  break;
413
413
  this.elements[index] = minItem;
414
414
  index = left;
@@ -13,7 +13,7 @@ const heap_1 = require("./heap");
13
13
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
14
14
  */
15
15
  class MaxHeap extends heap_1.Heap {
16
- constructor(elements, options = {
16
+ constructor(elements = [], options = {
17
17
  comparator: (a, b) => {
18
18
  if (!(typeof a === 'number' && typeof b === 'number')) {
19
19
  throw new Error('The a, b params of compare function must be number');
@@ -13,7 +13,7 @@ const heap_1 = require("./heap");
13
13
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
14
14
  */
15
15
  class MinHeap extends heap_1.Heap {
16
- constructor(elements, options = {
16
+ constructor(elements = [], options = {
17
17
  comparator: (a, b) => {
18
18
  if (!(typeof a === 'number' && typeof b === 'number')) {
19
19
  throw new Error('The a, b params of compare function must be number');
@@ -25,7 +25,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
25
25
  /**
26
26
  * The constructor initializes the linked list with an empty head, tail, and size.
27
27
  */
28
- constructor(elements) {
28
+ constructor(elements = []) {
29
29
  super();
30
30
  this._head = undefined;
31
31
  this._tail = undefined;
@@ -18,10 +18,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
18
18
  /**
19
19
  * The constructor initializes the linked list with an empty head, tail, and length.
20
20
  */
21
- constructor(elements) {
21
+ constructor(elements = []) {
22
22
  super();
23
- this._head = undefined;
24
- this._tail = undefined;
25
23
  this._size = 0;
26
24
  if (elements) {
27
25
  for (const el of elements)
@@ -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
  export declare class SkipListNode<K, V> {
9
10
  key: K;
10
11
  value: V;
@@ -12,14 +13,7 @@ export declare class SkipListNode<K, V> {
12
13
  constructor(key: K, value: V, level: number);
13
14
  }
14
15
  export declare class SkipList<K, V> {
15
- /**
16
- * The constructor initializes a SkipList with a specified maximum level and probability.
17
- * @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
18
- * the maximum number of levels that can be created in the skip list.
19
- * @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
20
- * level in the skip list. It is used to determine the height of each node in the skip list.
21
- */
22
- constructor(maxLevel?: number, probability?: number);
16
+ constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
23
17
  protected _head: SkipListNode<K, V>;
24
18
  get head(): SkipListNode<K, V>;
25
19
  protected _level: number;