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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +5 -5
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.js +7 -9
- package/dist/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/data-structures/binary-tree/bst.js +6 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +6 -10
- package/src/data-structures/binary-tree/bst.ts +5 -7
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.49.
|
|
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.
|
|
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
|
|
52
|
-
* @param [
|
|
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(
|
|
59
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: AVLTreeOptions<K>) {
|
|
60
60
|
super([], options);
|
|
61
|
-
if (
|
|
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
|
|
115
|
-
* @param [
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
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
|
|
96
|
-
* @param [
|
|
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(
|
|
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 (
|
|
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 [
|
|
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(
|
|
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 (
|
|
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(
|
|
55
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) {
|
|
56
56
|
super([], options);
|
|
57
|
-
if (
|
|
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 {
|
|
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
|
|
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
|
|
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
|
|
24
|
-
* @param
|
|
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 (
|
|
44
|
-
this.setMany(
|
|
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
|
|
92
|
-
* key-value pair is represented as an array with two
|
|
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(
|
|
95
|
+
setMany(entries: Iterable<[K, V]>): boolean[] {
|
|
95
96
|
const results: boolean[] = [];
|
|
96
|
-
for (const [key, value] of
|
|
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
|
|
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
|
|
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
|
-
|
|
275
|
-
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
|
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)
|
|
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.
|
|
@@ -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
|
-
|
|
25
|
-
|
|
26
|
-
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
|
|
24
|
+
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
|
|
27
25
|
super();
|
|
28
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
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
|
|
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
|
|
20
|
+
constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) {
|
|
21
21
|
super(elements, options);
|
|
22
22
|
}
|
|
23
23
|
}
|