data-structure-typed 1.49.0 → 1.49.2

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 (195) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/README_zh-CN.md +2 -2
  4. package/benchmark/report.html +46 -1
  5. package/benchmark/report.json +457 -22
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
  7. package/dist/cjs/data-structures/base/iterable-base.js +21 -0
  8. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js +3 -4
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js +6 -6
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -15
  16. package/dist/cjs/data-structures/binary-tree/bst.js +3 -3
  17. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js +5 -6
  20. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  22. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +3 -3
  23. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  24. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +9 -3
  25. package/dist/cjs/data-structures/graph/abstract-graph.js +27 -31
  26. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/directed-graph.d.ts +8 -1
  28. package/dist/cjs/data-structures/graph/directed-graph.js +1 -8
  29. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  30. package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
  31. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +8 -1
  32. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -8
  33. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  34. package/dist/cjs/data-structures/hash/hash-map.d.ts +22 -10
  35. package/dist/cjs/data-structures/hash/hash-map.js +28 -16
  36. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  37. package/dist/cjs/data-structures/hash/hash-table.d.ts +2 -2
  38. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  39. package/dist/cjs/data-structures/heap/heap.d.ts +20 -38
  40. package/dist/cjs/data-structures/heap/heap.js +22 -42
  41. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -1
  43. package/dist/cjs/data-structures/heap/max-heap.js +10 -7
  44. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  45. package/dist/cjs/data-structures/heap/min-heap.d.ts +11 -1
  46. package/dist/cjs/data-structures/heap/min-heap.js +10 -7
  47. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +132 -136
  50. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -49
  53. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  55. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
  56. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  58. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -7
  59. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  60. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  61. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -7
  62. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
  64. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -7
  65. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/queue/deque.d.ts +76 -80
  67. package/dist/cjs/data-structures/queue/deque.js +106 -122
  68. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  69. package/dist/cjs/data-structures/queue/queue.d.ts +30 -16
  70. package/dist/cjs/data-structures/queue/queue.js +31 -24
  71. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  72. package/dist/cjs/data-structures/stack/stack.d.ts +17 -8
  73. package/dist/cjs/data-structures/stack/stack.js +9 -9
  74. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  75. package/dist/cjs/data-structures/trie/trie.d.ts +14 -5
  76. package/dist/cjs/data-structures/trie/trie.js +13 -13
  77. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  78. package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
  79. package/dist/cjs/types/common.d.ts +32 -8
  80. package/dist/cjs/types/common.js +22 -1
  81. package/dist/cjs/types/common.js.map +1 -1
  82. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  83. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -22
  84. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  85. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  86. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  87. package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
  88. package/dist/mjs/data-structures/base/iterable-base.js +21 -0
  89. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
  90. package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -4
  91. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
  92. package/dist/mjs/data-structures/binary-tree/binary-tree.js +6 -6
  93. package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -15
  94. package/dist/mjs/data-structures/binary-tree/bst.js +3 -3
  95. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
  96. package/dist/mjs/data-structures/binary-tree/rb-tree.js +5 -6
  97. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  98. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -3
  99. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +9 -3
  100. package/dist/mjs/data-structures/graph/abstract-graph.js +27 -31
  101. package/dist/mjs/data-structures/graph/directed-graph.d.ts +8 -1
  102. package/dist/mjs/data-structures/graph/directed-graph.js +1 -8
  103. package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
  104. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +8 -1
  105. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -8
  106. package/dist/mjs/data-structures/hash/hash-map.d.ts +22 -10
  107. package/dist/mjs/data-structures/hash/hash-map.js +27 -15
  108. package/dist/mjs/data-structures/hash/hash-table.d.ts +2 -2
  109. package/dist/mjs/data-structures/heap/heap.d.ts +20 -38
  110. package/dist/mjs/data-structures/heap/heap.js +23 -43
  111. package/dist/mjs/data-structures/heap/max-heap.d.ts +11 -1
  112. package/dist/mjs/data-structures/heap/max-heap.js +9 -6
  113. package/dist/mjs/data-structures/heap/min-heap.d.ts +11 -1
  114. package/dist/mjs/data-structures/heap/min-heap.js +9 -6
  115. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  116. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +132 -136
  117. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  118. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -50
  119. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  120. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
  121. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  122. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +0 -7
  123. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  124. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +0 -7
  125. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
  126. package/dist/mjs/data-structures/priority-queue/priority-queue.js +7 -6
  127. package/dist/mjs/data-structures/queue/deque.d.ts +76 -80
  128. package/dist/mjs/data-structures/queue/deque.js +106 -122
  129. package/dist/mjs/data-structures/queue/queue.d.ts +30 -16
  130. package/dist/mjs/data-structures/queue/queue.js +31 -24
  131. package/dist/mjs/data-structures/stack/stack.d.ts +17 -8
  132. package/dist/mjs/data-structures/stack/stack.js +10 -10
  133. package/dist/mjs/data-structures/trie/trie.d.ts +14 -5
  134. package/dist/mjs/data-structures/trie/trie.js +14 -14
  135. package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
  136. package/dist/mjs/types/common.d.ts +32 -8
  137. package/dist/mjs/types/common.js +21 -0
  138. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  139. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -21
  140. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  141. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  142. package/dist/umd/data-structure-typed.js +533 -558
  143. package/dist/umd/data-structure-typed.min.js +2 -2
  144. package/dist/umd/data-structure-typed.min.js.map +1 -1
  145. package/package.json +1 -1
  146. package/src/data-structures/base/iterable-base.ts +24 -0
  147. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  148. package/src/data-structures/binary-tree/binary-tree.ts +74 -77
  149. package/src/data-structures/binary-tree/bst.ts +18 -17
  150. package/src/data-structures/binary-tree/rb-tree.ts +17 -18
  151. package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
  152. package/src/data-structures/graph/abstract-graph.ts +35 -25
  153. package/src/data-structures/graph/directed-graph.ts +2 -2
  154. package/src/data-structures/graph/map-graph.ts +1 -1
  155. package/src/data-structures/graph/undirected-graph.ts +2 -2
  156. package/src/data-structures/hash/hash-map.ts +40 -24
  157. package/src/data-structures/hash/hash-table.ts +3 -3
  158. package/src/data-structures/heap/heap.ts +33 -60
  159. package/src/data-structures/heap/max-heap.ts +11 -2
  160. package/src/data-structures/heap/min-heap.ts +11 -2
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
  162. package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
  163. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  164. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  165. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  166. package/src/data-structures/priority-queue/priority-queue.ts +9 -2
  167. package/src/data-structures/queue/deque.ts +129 -144
  168. package/src/data-structures/queue/queue.ts +37 -26
  169. package/src/data-structures/stack/stack.ts +20 -14
  170. package/src/data-structures/trie/trie.ts +18 -13
  171. package/src/interfaces/binary-tree.ts +5 -5
  172. package/src/types/common.ts +37 -12
  173. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
  174. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
  175. package/src/types/data-structures/binary-tree/bst.ts +0 -1
  176. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  177. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  178. package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
  179. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  180. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  181. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  182. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
  183. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  184. package/test/unit/data-structures/graph/directed-graph.test.ts +21 -1
  185. package/test/unit/data-structures/graph/undirected-graph.test.ts +39 -1
  186. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  187. package/test/unit/data-structures/heap/heap.test.ts +1 -1
  188. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  189. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
  190. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  193. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +4 -4
  195. package/test/unit/data-structures/trie/trie.test.ts +1 -1
@@ -5,8 +5,14 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
9
- import { IterableEntryBase } from "../base";
8
+ import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
9
+ import { IterableEntryBase } from '../base';
10
+ /**
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.
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.
15
+ */
10
16
  export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
11
17
  protected _store: {
12
18
  [key: string]: HashMapStoreItem<K, V>;
@@ -36,13 +42,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
36
42
  * @param {V} value - The value parameter represents the value that you want to associate with the
37
43
  * key in the data structure.
38
44
  */
39
- set(key: K, value: V): void;
45
+ set(key: K, value: V): boolean;
40
46
  /**
41
47
  * The function "setMany" sets multiple key-value pairs in a map.
42
48
  * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
43
49
  * key-value pair is represented as an array with two elements: the key and the value.
44
50
  */
45
- setMany(elements: Iterable<[K, V]>): void;
51
+ setMany(elements: Iterable<[K, V]>): boolean[];
46
52
  /**
47
53
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
48
54
  * a string map.
@@ -108,6 +114,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
108
114
  */
109
115
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
110
116
  print(): void;
117
+ put(key: K, value: V): boolean;
111
118
  /**
112
119
  * The function returns an iterator that yields key-value pairs from both an object store and an
113
120
  * object map.
@@ -117,6 +124,11 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
117
124
  protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
118
125
  protected _getNoObjKey(key: K): string;
119
126
  }
127
+ /**
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.
130
+ * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
131
+ */
120
132
  export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
121
133
  protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
122
134
  protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
@@ -167,9 +179,9 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
167
179
  * value associated with the key being set in the data structure.
168
180
  * @returns the size of the data structure after the key-value pair has been set.
169
181
  */
170
- set(key: K, value?: V): number;
182
+ set(key: K, value?: V): boolean;
171
183
  has(key: K): boolean;
172
- setMany(entries: Iterable<[K, V]>): void;
184
+ setMany(entries: Iterable<[K, V]>): boolean[];
173
185
  /**
174
186
  * Time Complexity: O(1)
175
187
  * Space Complexity: O(1)
@@ -195,7 +207,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
195
207
  * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
196
208
  * where `K` is the key and `V` is the value.
197
209
  */
198
- getAt(index: number): [K, V];
210
+ getAt(index: number): V | undefined;
199
211
  /**
200
212
  * Time Complexity: O(1)
201
213
  * Space Complexity: O(1)
@@ -216,7 +228,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
216
228
  * deleted in the linked list.
217
229
  * @returns The size of the list after deleting the element at the specified index.
218
230
  */
219
- deleteAt(index: number): number;
231
+ deleteAt(index: number): boolean;
220
232
  /**
221
233
  * Time Complexity: O(1)
222
234
  * Space Complexity: O(1)
@@ -276,7 +288,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
276
288
  * function.
277
289
  */
278
290
  map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
279
- print(): void;
291
+ put(key: K, value: V): boolean;
280
292
  /**
281
293
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
282
294
  * Space Complexity: O(1)
@@ -294,5 +306,5 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
294
306
  * represents a node in a linked list. It contains a key-value pair and references to the previous
295
307
  * and next nodes in the list.
296
308
  */
297
- protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
309
+ protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean;
298
310
  }
@@ -1,12 +1,11 @@
1
+ import { IterableEntryBase } from '../base';
2
+ import { isWeakKey, rangeCheck } from '../../utils';
1
3
  /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
4
+ * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
5
+ * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
6
+ * 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.
7
+ * 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
7
8
  */
8
- import { isWeakKey, rangeCheck } from '../../utils';
9
- import { IterableEntryBase } from "../base";
10
9
  export class HashMap extends IterableEntryBase {
11
10
  _store = {};
12
11
  _objMap = new Map();
@@ -65,6 +64,7 @@ export class HashMap extends IterableEntryBase {
65
64
  }
66
65
  this._store[strKey] = { key, value };
67
66
  }
67
+ return true;
68
68
  }
69
69
  /**
70
70
  * The function "setMany" sets multiple key-value pairs in a map.
@@ -72,8 +72,10 @@ export class HashMap extends IterableEntryBase {
72
72
  * key-value pair is represented as an array with two elements: the key and the value.
73
73
  */
74
74
  setMany(elements) {
75
+ const results = [];
75
76
  for (const [key, value] of elements)
76
- this.set(key, value);
77
+ results.push(this.set(key, value));
78
+ return results;
77
79
  }
78
80
  /**
79
81
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
@@ -190,6 +192,9 @@ export class HashMap extends IterableEntryBase {
190
192
  print() {
191
193
  console.log([...this.entries()]);
192
194
  }
195
+ put(key, value) {
196
+ return this.set(key, value);
197
+ }
193
198
  /**
194
199
  * The function returns an iterator that yields key-value pairs from both an object store and an
195
200
  * object map.
@@ -225,6 +230,11 @@ export class HashMap extends IterableEntryBase {
225
230
  return strKey;
226
231
  }
227
232
  }
233
+ /**
234
+ * 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.
235
+ * 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
+ * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
237
+ */
228
238
  export class LinkedHashMap extends IterableEntryBase {
229
239
  _noObjMap = {};
230
240
  _objMap = new WeakMap();
@@ -353,7 +363,7 @@ export class LinkedHashMap extends IterableEntryBase {
353
363
  this._sentinel.prev = node;
354
364
  this._size++;
355
365
  }
356
- return this._size;
366
+ return true;
357
367
  }
358
368
  has(key) {
359
369
  if (isWeakKey(key)) {
@@ -366,10 +376,12 @@ export class LinkedHashMap extends IterableEntryBase {
366
376
  }
367
377
  }
368
378
  setMany(entries) {
379
+ const results = [];
369
380
  for (const entry of entries) {
370
381
  const [key, value] = entry;
371
- this.set(key, value);
382
+ results.push(this.set(key, value));
372
383
  }
384
+ return results;
373
385
  }
374
386
  /**
375
387
  * Time Complexity: O(1)
@@ -413,7 +425,7 @@ export class LinkedHashMap extends IterableEntryBase {
413
425
  while (index--) {
414
426
  node = node.next;
415
427
  }
416
- return [node.key, node.value];
428
+ return node.value;
417
429
  }
418
430
  /**
419
431
  * Time Complexity: O(1)
@@ -466,8 +478,7 @@ export class LinkedHashMap extends IterableEntryBase {
466
478
  while (index--) {
467
479
  node = node.next;
468
480
  }
469
- this._deleteNode(node);
470
- return this._size;
481
+ return this._deleteNode(node);
471
482
  }
472
483
  /**
473
484
  * Time Complexity: O(1)
@@ -560,8 +571,8 @@ export class LinkedHashMap extends IterableEntryBase {
560
571
  }
561
572
  return mappedMap;
562
573
  }
563
- print() {
564
- console.log([...this]);
574
+ put(key, value) {
575
+ return this.set(key, value);
565
576
  }
566
577
  /**
567
578
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -597,5 +608,6 @@ export class LinkedHashMap extends IterableEntryBase {
597
608
  this._tail = prev;
598
609
  }
599
610
  this._size -= 1;
611
+ return true;
600
612
  }
601
613
  }
@@ -5,13 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- export declare class HashTableNode<K, V> {
8
+ import type { HashFunction } from '../../types';
9
+ export declare class HashTableNode<K = any, V = any> {
9
10
  key: K;
10
11
  value: V;
11
12
  next: HashTableNode<K, V> | undefined;
12
13
  constructor(key: K, value: V);
13
14
  }
14
- import { HashFunction } from '../../types';
15
15
  export declare class HashTable<K = any, V = any> {
16
16
  protected static readonly DEFAULT_CAPACITY = 16;
17
17
  protected static readonly LOAD_FACTOR = 0.75;
@@ -4,9 +4,20 @@
4
4
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
5
  * @license MIT License
6
6
  */
7
- import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
8
- import { HeapOptions } from "../../types";
9
- import { IterableElementBase } from "../base";
7
+ import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
8
+ import { IterableElementBase } from '../base';
9
+ /**
10
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
11
+ * 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
12
+ * Max Heap: The value of each parent node is greater than or equal to the value of its children.
13
+ * Min Heap: The value of each parent node is less than or equal to the value of its children.
14
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
15
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
16
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
17
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
18
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
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
+ */
10
21
  export declare class Heap<E = any> extends IterableElementBase<E> {
11
22
  options: HeapOptions<E>;
12
23
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
@@ -41,19 +52,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
41
52
  * Insert an element into the heap and maintain the heap properties.
42
53
  * @param element - The element to be inserted.
43
54
  */
44
- add(element: E): Heap<E>;
45
- /**
46
- * Time Complexity: O(log n), where n is the number of elements in the heap.
47
- * Space Complexity: O(1)
48
- */
49
- /**
50
- * Time Complexity: O(log n), where n is the number of elements in the heap.
51
- * Space Complexity: O(1)
52
- *
53
- * Insert an element into the heap and maintain the heap properties.
54
- * @param element - The element to be inserted.
55
- */
56
- push(element: E): Heap<E>;
55
+ add(element: E): boolean;
57
56
  /**
58
57
  * Time Complexity: O(log n), where n is the number of elements in the heap.
59
58
  * Space Complexity: O(1)
@@ -66,18 +65,6 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
66
65
  * @returns The top element or undefined if the heap is empty.
67
66
  */
68
67
  poll(): E | undefined;
69
- /**
70
- * Time Complexity: O(log n), where n is the number of elements in the heap.
71
- * Space Complexity: O(1)
72
- */
73
- /**
74
- * Time Complexity: O(log n), where n is the number of elements in the heap.
75
- * Space Complexity: O(1)
76
- *
77
- * Remove and return the top element (smallest or largest element) from the heap.
78
- * @returns The top element or undefined if the heap is empty.
79
- */
80
- pop(): E | undefined;
81
68
  /**
82
69
  * Peek at the top element of the heap without removing it.
83
70
  * @returns The top element or undefined if the heap is empty.
@@ -103,7 +90,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
103
90
  * Clear and add elements of the heap
104
91
  * @param elements
105
92
  */
106
- refill(elements: E[]): void;
93
+ refill(elements: E[]): boolean[];
107
94
  /**
108
95
  * Time Complexity: O(n), where n is the number of elements in the heap.
109
96
  * Space Complexity: O(1)
@@ -192,7 +179,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
192
179
  *
193
180
  * Fix the entire heap to maintain heap properties.
194
181
  */
195
- fix(): void;
182
+ fix(): boolean[];
196
183
  /**
197
184
  * Time Complexity: O(n)
198
185
  * Space Complexity: O(n)
@@ -239,12 +226,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
239
226
  * original Heap.
240
227
  */
241
228
  map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
242
- /**
243
- * Time Complexity: O(log n)
244
- * Space Complexity: O(1)
245
- */
246
- print(): void;
247
- protected _getIterator(): Generator<E, void, unknown>;
229
+ protected _getIterator(): IterableIterator<E>;
248
230
  /**
249
231
  * Time Complexity: O(n)
250
232
  * Space Complexity: O(1)
@@ -256,7 +238,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
256
238
  * Float operation to maintain heap properties after adding an element.
257
239
  * @param index - The index of the newly added element.
258
240
  */
259
- protected _bubbleUp(index: number): void;
241
+ protected _bubbleUp(index: number): boolean;
260
242
  /**
261
243
  * Time Complexity: O(log n)
262
244
  * Space Complexity: O(1)
@@ -265,7 +247,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
265
247
  * @param index - The index from which to start sinking.
266
248
  * @param halfLength
267
249
  */
268
- protected _sinkDown(index: number, halfLength: number): void;
250
+ protected _sinkDown(index: number, halfLength: number): boolean;
269
251
  }
270
252
  export declare class FibonacciHeapNode<E> {
271
253
  element: E;
@@ -4,7 +4,19 @@
4
4
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
5
  * @license MIT License
6
6
  */
7
- import { IterableElementBase } from "../base";
7
+ import { IterableElementBase } from '../base';
8
+ /**
9
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
10
+ * 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
11
+ * Max Heap: The value of each parent node is greater than or equal to the value of its children.
12
+ * Min Heap: The value of each parent node is less than or equal to the value of its children.
13
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
14
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
15
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
16
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
17
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
19
+ */
8
20
  export class Heap extends IterableElementBase {
9
21
  options;
10
22
  constructor(elements, options) {
@@ -27,7 +39,7 @@ export class Heap extends IterableElementBase {
27
39
  }
28
40
  if (elements) {
29
41
  for (const el of elements) {
30
- this.push(el);
42
+ this.add(el);
31
43
  }
32
44
  // this.fix();
33
45
  }
@@ -70,23 +82,8 @@ export class Heap extends IterableElementBase {
70
82
  * @param element - The element to be inserted.
71
83
  */
72
84
  add(element) {
73
- return this.push(element);
74
- }
75
- /**
76
- * Time Complexity: O(log n), where n is the number of elements in the heap.
77
- * Space Complexity: O(1)
78
- */
79
- /**
80
- * Time Complexity: O(log n), where n is the number of elements in the heap.
81
- * Space Complexity: O(1)
82
- *
83
- * Insert an element into the heap and maintain the heap properties.
84
- * @param element - The element to be inserted.
85
- */
86
- push(element) {
87
85
  this._elements.push(element);
88
- this._bubbleUp(this.elements.length - 1);
89
- return this;
86
+ return this._bubbleUp(this.elements.length - 1);
90
87
  }
91
88
  /**
92
89
  * Time Complexity: O(log n), where n is the number of elements in the heap.
@@ -110,20 +107,6 @@ export class Heap extends IterableElementBase {
110
107
  }
111
108
  return value;
112
109
  }
113
- /**
114
- * Time Complexity: O(log n), where n is the number of elements in the heap.
115
- * Space Complexity: O(1)
116
- */
117
- /**
118
- * Time Complexity: O(log n), where n is the number of elements in the heap.
119
- * Space Complexity: O(1)
120
- *
121
- * Remove and return the top element (smallest or largest element) from the heap.
122
- * @returns The top element or undefined if the heap is empty.
123
- */
124
- pop() {
125
- return this.poll();
126
- }
127
110
  /**
128
111
  * Peek at the top element of the heap without removing it.
129
112
  * @returns The top element or undefined if the heap is empty.
@@ -157,7 +140,7 @@ export class Heap extends IterableElementBase {
157
140
  */
158
141
  refill(elements) {
159
142
  this._elements = elements;
160
- this.fix();
143
+ return this.fix();
161
144
  }
162
145
  /**
163
146
  * Time Complexity: O(n), where n is the number of elements in the heap.
@@ -194,7 +177,7 @@ export class Heap extends IterableElementBase {
194
177
  if (index < 0)
195
178
  return false;
196
179
  if (index === 0) {
197
- this.pop();
180
+ this.poll();
198
181
  }
199
182
  else if (index === this.elements.length - 1) {
200
183
  this.elements.pop();
@@ -306,8 +289,10 @@ export class Heap extends IterableElementBase {
306
289
  * Fix the entire heap to maintain heap properties.
307
290
  */
308
291
  fix() {
292
+ const results = [];
309
293
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
310
- this._sinkDown(i, this.elements.length >> 1);
294
+ results.push(this._sinkDown(i, this.elements.length >> 1));
295
+ return results;
311
296
  }
312
297
  /**
313
298
  * Time Complexity: O(n)
@@ -334,7 +319,7 @@ export class Heap extends IterableElementBase {
334
319
  let index = 0;
335
320
  for (const current of this) {
336
321
  if (callback.call(thisArg, current, index, this)) {
337
- filteredList.push(current);
322
+ filteredList.add(current);
338
323
  }
339
324
  index++;
340
325
  }
@@ -373,13 +358,6 @@ export class Heap extends IterableElementBase {
373
358
  }
374
359
  return mappedHeap;
375
360
  }
376
- /**
377
- * Time Complexity: O(log n)
378
- * Space Complexity: O(1)
379
- */
380
- print() {
381
- console.log([...this]);
382
- }
383
361
  *_getIterator() {
384
362
  for (const element of this.elements) {
385
363
  yield element;
@@ -407,6 +385,7 @@ export class Heap extends IterableElementBase {
407
385
  index = parent;
408
386
  }
409
387
  this.elements[index] = element;
388
+ return true;
410
389
  }
411
390
  /**
412
391
  * Time Complexity: O(log n)
@@ -433,6 +412,7 @@ export class Heap extends IterableElementBase {
433
412
  index = left;
434
413
  }
435
414
  this.elements[index] = element;
415
+ return true;
436
416
  }
437
417
  }
438
418
  export class FibonacciHeapNode {
@@ -5,8 +5,18 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { Heap } from './heap';
9
8
  import type { HeapOptions } from '../../types';
9
+ import { Heap } from './heap';
10
+ /**
11
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
12
+ * 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
13
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
14
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
15
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
16
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
17
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
19
+ */
10
20
  export declare class MaxHeap<E = any> extends Heap<E> {
11
21
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
12
22
  }
@@ -1,11 +1,14 @@
1
+ import { Heap } from './heap';
1
2
  /**
2
- * data-structure-typed
3
- *
4
- * @author Kirk Qi
5
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
- * @license MIT License
3
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
4
+ * 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
5
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
6
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
7
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
8
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
9
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
10
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
7
11
  */
8
- import { Heap } from './heap';
9
12
  export class MaxHeap extends Heap {
10
13
  constructor(elements, options = {
11
14
  comparator: (a, b) => {
@@ -5,8 +5,18 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { Heap } from './heap';
9
8
  import type { HeapOptions } from '../../types';
9
+ import { Heap } from './heap';
10
+ /**
11
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
12
+ * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
13
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
14
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
15
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
16
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
17
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
19
+ */
10
20
  export declare class MinHeap<E = any> extends Heap<E> {
11
21
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
12
22
  }
@@ -1,11 +1,14 @@
1
+ import { Heap } from './heap';
1
2
  /**
2
- * data-structure-typed
3
- *
4
- * @author Kirk Qi
5
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
- * @license MIT License
3
+ * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
4
+ * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
5
+ * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
6
+ * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
7
+ * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
8
+ * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
9
+ * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
10
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
7
11
  */
8
- import { Heap } from './heap';
9
12
  export class MinHeap extends Heap {
10
13
  constructor(elements, options = {
11
14
  comparator: (a, b) => {