min-heap-typed 1.45.0 → 1.45.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.
- package/dist/data-structures/hash/hash-map.d.ts +58 -58
- package/dist/data-structures/hash/hash-map.js +73 -73
- package/dist/data-structures/heap/heap.js +21 -12
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +103 -103
- package/src/data-structures/hash/hash-table.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +30 -17
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +18 -4
- package/src/utils/utils.ts +6 -6
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {isObjOrFunc, rangeCheck, throwRangeError} from
|
|
10
|
-
import {HashMapLinkedNode, HashMapOptions, IterateDirection} from
|
|
9
|
+
import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils';
|
|
10
|
+
import { HashMapLinkedNode, HashMapOptions, IterateDirection } from '../../types';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
@@ -16,9 +16,8 @@ import {HashMapLinkedNode, HashMapOptions, IterateDirection} from "../../types";
|
|
|
16
16
|
*/
|
|
17
17
|
export class HashMapIterator<K, V> {
|
|
18
18
|
readonly hashMap: HashMap<K, V>;
|
|
19
|
-
|
|
20
|
-
protected _node: HashMapLinkedNode<K, V>;
|
|
21
19
|
readonly iterateDirection: IterateDirection;
|
|
20
|
+
protected _node: HashMapLinkedNode<K, V>;
|
|
22
21
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
23
22
|
|
|
24
23
|
/**
|
|
@@ -36,13 +35,16 @@ export class HashMapIterator<K, V> {
|
|
|
36
35
|
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
37
36
|
* methods of the object being created.
|
|
38
37
|
*/
|
|
39
|
-
constructor(
|
|
40
|
-
|
|
38
|
+
constructor(
|
|
39
|
+
node: HashMapLinkedNode<K, V>,
|
|
40
|
+
sentinel: HashMapLinkedNode<K, V>,
|
|
41
|
+
hashMap: HashMap<K, V>,
|
|
42
|
+
iterateDirection: IterateDirection = IterateDirection.DEFAULT
|
|
43
|
+
) {
|
|
41
44
|
this._node = node;
|
|
42
45
|
this._sentinel = sentinel;
|
|
43
46
|
this.iterateDirection = iterateDirection;
|
|
44
47
|
|
|
45
|
-
|
|
46
48
|
if (this.iterateDirection === IterateDirection.DEFAULT) {
|
|
47
49
|
this.prev = function () {
|
|
48
50
|
if (this._node.prev === this._sentinel) {
|
|
@@ -87,7 +89,7 @@ export class HashMapIterator<K, V> {
|
|
|
87
89
|
throwRangeError();
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
return new Proxy(<[K, V]
|
|
92
|
+
return new Proxy(<[K, V]>(<unknown>[]), {
|
|
91
93
|
get: (target, prop: '0' | '1') => {
|
|
92
94
|
if (prop === '0') return this._node.key;
|
|
93
95
|
else if (prop === '1') return this._node.value;
|
|
@@ -113,7 +115,6 @@ export class HashMapIterator<K, V> {
|
|
|
113
115
|
return this._node !== this._sentinel;
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
|
|
117
118
|
prev() {
|
|
118
119
|
return this;
|
|
119
120
|
}
|
|
@@ -124,18 +125,12 @@ export class HashMapIterator<K, V> {
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
export class HashMap<K = any, V = any> {
|
|
128
|
+
readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
127
129
|
protected _nodes: HashMapLinkedNode<K, V>[] = [];
|
|
128
130
|
protected _orgMap: Record<string, HashMapLinkedNode<K, V>> = {};
|
|
129
131
|
protected _head: HashMapLinkedNode<K, V>;
|
|
130
132
|
protected _tail: HashMapLinkedNode<K, V>;
|
|
131
133
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
132
|
-
readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
133
|
-
|
|
134
|
-
protected _size = 0;
|
|
135
|
-
|
|
136
|
-
get size() {
|
|
137
|
-
return this._size;
|
|
138
|
-
}
|
|
139
134
|
|
|
140
135
|
/**
|
|
141
136
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
@@ -151,7 +146,85 @@ export class HashMap<K = any, V = any> {
|
|
|
151
146
|
hashMap.forEach(el => {
|
|
152
147
|
this.set(el[0], el[1]);
|
|
153
148
|
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected _size = 0;
|
|
152
|
+
|
|
153
|
+
get size() {
|
|
154
|
+
return this._size;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(1)
|
|
159
|
+
* Space Complexity: O(1)
|
|
160
|
+
*
|
|
161
|
+
* The function returns a new iterator object for a HashMap.
|
|
162
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
163
|
+
*/
|
|
164
|
+
get begin() {
|
|
165
|
+
return new HashMapIterator<K, V>(this._head, this._sentinel, this);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(1)
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*
|
|
172
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
173
|
+
* end values.
|
|
174
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
175
|
+
*/
|
|
176
|
+
get end() {
|
|
177
|
+
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this);
|
|
178
|
+
}
|
|
154
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(1)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
184
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
185
|
+
* a HashMap in reverse order.
|
|
186
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
187
|
+
*/
|
|
188
|
+
get reverseBegin() {
|
|
189
|
+
return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(1)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
196
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
197
|
+
* HashMap in reverse order.
|
|
198
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
199
|
+
*/
|
|
200
|
+
get reverseEnd() {
|
|
201
|
+
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(1)
|
|
206
|
+
* Space Complexity: O(1)
|
|
207
|
+
*
|
|
208
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
209
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
210
|
+
* value (V).
|
|
211
|
+
*/
|
|
212
|
+
get front() {
|
|
213
|
+
if (this._size === 0) return;
|
|
214
|
+
return <[K, V]>[this._head.key, this._head.value];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Time Complexity: O(1)
|
|
219
|
+
* Space Complexity: O(1)
|
|
220
|
+
*
|
|
221
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
222
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
223
|
+
* data structure.
|
|
224
|
+
*/
|
|
225
|
+
get back() {
|
|
226
|
+
if (this._size === 0) return;
|
|
227
|
+
return <[K, V]>[this._tail.key, this._tail.value];
|
|
155
228
|
}
|
|
156
229
|
|
|
157
230
|
/**
|
|
@@ -170,7 +243,7 @@ export class HashMap<K = any, V = any> {
|
|
|
170
243
|
set(key: K, value?: V, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
171
244
|
let newTail;
|
|
172
245
|
if (isObjectKey) {
|
|
173
|
-
const index = (<Record<symbol, number
|
|
246
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
174
247
|
if (index !== undefined) {
|
|
175
248
|
this._nodes[<number>index].value = <V>value;
|
|
176
249
|
return this._size;
|
|
@@ -187,12 +260,12 @@ export class HashMap<K = any, V = any> {
|
|
|
187
260
|
};
|
|
188
261
|
this._nodes.push(newTail);
|
|
189
262
|
} else {
|
|
190
|
-
const node = this._orgMap[<string
|
|
263
|
+
const node = this._orgMap[<string>(<unknown>key)];
|
|
191
264
|
if (node) {
|
|
192
265
|
node.value = <V>value;
|
|
193
266
|
return this._size;
|
|
194
267
|
}
|
|
195
|
-
this._orgMap[<string
|
|
268
|
+
this._orgMap[<string>(<unknown>key)] = newTail = {
|
|
196
269
|
key: key,
|
|
197
270
|
value: <V>value,
|
|
198
271
|
prev: this._tail,
|
|
@@ -228,10 +301,10 @@ export class HashMap<K = any, V = any> {
|
|
|
228
301
|
*/
|
|
229
302
|
get(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
230
303
|
if (isObjectKey) {
|
|
231
|
-
const index = (<Record<symbol, number
|
|
304
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
232
305
|
return index !== undefined ? this._nodes[index].value : undefined;
|
|
233
306
|
}
|
|
234
|
-
const node = this._orgMap[<string
|
|
307
|
+
const node = this._orgMap[<string>(<unknown>key)];
|
|
235
308
|
return node ? node.value : undefined;
|
|
236
309
|
}
|
|
237
310
|
|
|
@@ -269,16 +342,16 @@ export class HashMap<K = any, V = any> {
|
|
|
269
342
|
* @returns a new instance of the `HashMapIterator` class.
|
|
270
343
|
*/
|
|
271
344
|
getIterator(key: K, isObjectKey?: boolean) {
|
|
272
|
-
let node: HashMapLinkedNode<K, V
|
|
345
|
+
let node: HashMapLinkedNode<K, V>;
|
|
273
346
|
if (isObjectKey) {
|
|
274
|
-
const index = (<Record<symbol, number
|
|
347
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
275
348
|
if (index === undefined) {
|
|
276
|
-
node = this._sentinel
|
|
349
|
+
node = this._sentinel;
|
|
277
350
|
} else {
|
|
278
351
|
node = this._nodes[index];
|
|
279
352
|
}
|
|
280
353
|
} else {
|
|
281
|
-
node = this._orgMap[<string
|
|
354
|
+
node = this._orgMap[<string>(<unknown>key)] || this._sentinel;
|
|
282
355
|
}
|
|
283
356
|
return new HashMapIterator<K, V>(node, this._sentinel, this);
|
|
284
357
|
}
|
|
@@ -299,15 +372,15 @@ export class HashMap<K = any, V = any> {
|
|
|
299
372
|
delete(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
300
373
|
let node;
|
|
301
374
|
if (isObjectKey) {
|
|
302
|
-
const index = (<Record<symbol, number
|
|
375
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
303
376
|
if (index === undefined) return false;
|
|
304
|
-
delete (<Record<symbol, number
|
|
377
|
+
delete (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
305
378
|
node = this._nodes[index];
|
|
306
379
|
delete this._nodes[index];
|
|
307
380
|
} else {
|
|
308
|
-
node = this._orgMap[<string
|
|
381
|
+
node = this._orgMap[<string>(<unknown>key)];
|
|
309
382
|
if (node === undefined) return false;
|
|
310
|
-
delete this._orgMap[<string
|
|
383
|
+
delete this._orgMap[<string>(<unknown>key)];
|
|
311
384
|
}
|
|
312
385
|
this._deleteNode(node);
|
|
313
386
|
return true;
|
|
@@ -362,79 +435,6 @@ export class HashMap<K = any, V = any> {
|
|
|
362
435
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
363
436
|
}
|
|
364
437
|
|
|
365
|
-
/**
|
|
366
|
-
* Time Complexity: O(1)
|
|
367
|
-
* Space Complexity: O(1)
|
|
368
|
-
*
|
|
369
|
-
* The function returns a new iterator object for a HashMap.
|
|
370
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
371
|
-
*/
|
|
372
|
-
get begin() {
|
|
373
|
-
return new HashMapIterator<K, V>(this._head, this._sentinel, this);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Time Complexity: O(1)
|
|
378
|
-
* Space Complexity: O(1)
|
|
379
|
-
*
|
|
380
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
381
|
-
* end values.
|
|
382
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
383
|
-
*/
|
|
384
|
-
get end() {
|
|
385
|
-
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Time Complexity: O(1)
|
|
390
|
-
* Space Complexity: O(1)
|
|
391
|
-
*
|
|
392
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
393
|
-
* a HashMap in reverse order.
|
|
394
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
395
|
-
*/
|
|
396
|
-
get reverseBegin() {
|
|
397
|
-
return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Time Complexity: O(1)
|
|
402
|
-
* Space Complexity: O(1)
|
|
403
|
-
*
|
|
404
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
405
|
-
* HashMap in reverse order.
|
|
406
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
407
|
-
*/
|
|
408
|
-
get reverseEnd() {
|
|
409
|
-
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Time Complexity: O(1)
|
|
414
|
-
* Space Complexity: O(1)
|
|
415
|
-
*
|
|
416
|
-
* The function returns the key-value pair at the front of a data structure.
|
|
417
|
-
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
418
|
-
* value (V).
|
|
419
|
-
*/
|
|
420
|
-
get front() {
|
|
421
|
-
if (this._size === 0) return;
|
|
422
|
-
return <[K, V]>[this._head.key, this._head.value];
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Time Complexity: O(1)
|
|
427
|
-
* Space Complexity: O(1)
|
|
428
|
-
*
|
|
429
|
-
* The function returns the key-value pair at the end of a data structure.
|
|
430
|
-
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
431
|
-
* data structure.
|
|
432
|
-
*/
|
|
433
|
-
get back() {
|
|
434
|
-
if (this._size === 0) return;
|
|
435
|
-
return <[K, V]>[this._tail.key, this._tail.value];
|
|
436
|
-
}
|
|
437
|
-
|
|
438
438
|
/**
|
|
439
439
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
440
440
|
* Space Complexity: O(1)
|
|
@@ -478,7 +478,7 @@ export class HashMap<K = any, V = any> {
|
|
|
478
478
|
* and next nodes in the list.
|
|
479
479
|
*/
|
|
480
480
|
protected _deleteNode(node: HashMapLinkedNode<K, V>) {
|
|
481
|
-
const {prev, next} = node;
|
|
481
|
+
const { prev, next } = node;
|
|
482
482
|
prev.next = next;
|
|
483
483
|
next.prev = prev;
|
|
484
484
|
if (node === this._head) {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeMap {
|
|
1
|
+
export class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeSet {
|
|
1
|
+
export class TreeSet {
|
|
2
|
+
}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
8
|
+
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Heap<E = any> {
|
|
11
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
11
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
12
12
|
this._comparator = options.comparator;
|
|
13
13
|
if (options.nodes && options.nodes.length > 0) {
|
|
14
14
|
this._nodes = options.nodes;
|
|
@@ -48,7 +48,7 @@ export class Heap<E = any> {
|
|
|
48
48
|
* @returns A new Heap instance.
|
|
49
49
|
* @param options
|
|
50
50
|
*/
|
|
51
|
-
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
|
51
|
+
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
|
|
52
52
|
return new Heap<E>(options);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -265,7 +265,7 @@ export class Heap<E = any> {
|
|
|
265
265
|
* @returns A new Heap instance containing the same elements.
|
|
266
266
|
*/
|
|
267
267
|
clone(): Heap<E> {
|
|
268
|
-
const clonedHeap = new Heap<E>({comparator: this.comparator});
|
|
268
|
+
const clonedHeap = new Heap<E>({ comparator: this.comparator });
|
|
269
269
|
clonedHeap._nodes = [...this.nodes];
|
|
270
270
|
return clonedHeap;
|
|
271
271
|
}
|
|
@@ -305,18 +305,28 @@ export class Heap<E = any> {
|
|
|
305
305
|
* @param index - The index of the newly added element.
|
|
306
306
|
*/
|
|
307
307
|
protected bubbleUp(index: number): void {
|
|
308
|
-
const element = this.nodes[index];
|
|
308
|
+
// const element = this.nodes[index];
|
|
309
|
+
// while (index > 0) {
|
|
310
|
+
// const parentIndex = (index - 1) >> 1;
|
|
311
|
+
// const parent = this.nodes[parentIndex];
|
|
312
|
+
// if (this.comparator(element, parent) < 0) {
|
|
313
|
+
// this.nodes[index] = parent;
|
|
314
|
+
// this.nodes[parentIndex] = element;
|
|
315
|
+
// index = parentIndex;
|
|
316
|
+
// } else {
|
|
317
|
+
// break;
|
|
318
|
+
// }
|
|
319
|
+
// }
|
|
320
|
+
|
|
321
|
+
const item = this.nodes[index];
|
|
309
322
|
while (index > 0) {
|
|
310
|
-
const
|
|
311
|
-
const
|
|
312
|
-
if (this.comparator(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
index = parentIndex;
|
|
316
|
-
} else {
|
|
317
|
-
break;
|
|
318
|
-
}
|
|
323
|
+
const parent = (index - 1) >> 1;
|
|
324
|
+
const parentItem = this.nodes[parent];
|
|
325
|
+
if (this.comparator(parentItem, item) <= 0) break;
|
|
326
|
+
this.nodes[index] = parentItem;
|
|
327
|
+
index = parent;
|
|
319
328
|
}
|
|
329
|
+
this.nodes[index] = item;
|
|
320
330
|
}
|
|
321
331
|
|
|
322
332
|
/**
|
|
@@ -332,8 +342,8 @@ export class Heap<E = any> {
|
|
|
332
342
|
* @param index - The index from which to start sinking.
|
|
333
343
|
*/
|
|
334
344
|
protected sinkDown(index: number): void {
|
|
335
|
-
const leftChildIndex =
|
|
336
|
-
const rightChildIndex =
|
|
345
|
+
const leftChildIndex = index << 1 | 1;
|
|
346
|
+
const rightChildIndex = leftChildIndex + 1;
|
|
337
347
|
const length = this.nodes.length;
|
|
338
348
|
let targetIndex = index;
|
|
339
349
|
|
|
@@ -741,7 +751,10 @@ export class FibonacciHeap<E> {
|
|
|
741
751
|
protected consolidate(): void {
|
|
742
752
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
743
753
|
const nodes = this.consumeLinkedList(this.root);
|
|
744
|
-
let x: FibonacciHeapNode<E> | undefined,
|
|
754
|
+
let x: FibonacciHeapNode<E> | undefined,
|
|
755
|
+
y: FibonacciHeapNode<E> | undefined,
|
|
756
|
+
d: number,
|
|
757
|
+
t: FibonacciHeapNode<E> | undefined;
|
|
745
758
|
|
|
746
759
|
for (const node of nodes) {
|
|
747
760
|
x = node;
|
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {Heap} from './heap';
|
|
10
|
-
import type {Comparator} from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
10
|
+
import type { Comparator } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {Heap} from './heap';
|
|
10
|
-
import type {Comparator} from '../../types';
|
|
9
|
+
import { Heap } from './heap';
|
|
10
|
+
import type { Comparator } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -826,7 +826,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
826
826
|
/**
|
|
827
827
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
828
828
|
*/
|
|
829
|
-
*[Symbol.iterator]() {
|
|
829
|
+
* [Symbol.iterator]() {
|
|
830
830
|
let current = this.head;
|
|
831
831
|
|
|
832
832
|
while (current) {
|
|
@@ -773,7 +773,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
773
773
|
/**
|
|
774
774
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
775
775
|
*/
|
|
776
|
-
*[Symbol.iterator]() {
|
|
776
|
+
* [Symbol.iterator]() {
|
|
777
777
|
let current = this.head;
|
|
778
778
|
|
|
779
779
|
while (current) {
|
|
@@ -14,8 +14,8 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
18
|
-
const {row, col, initialVal} = options;
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
|
+
const { row, col, initialVal } = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {Direction, NavigatorParams, Turning} from '../../types';
|
|
8
|
+
import type { Direction, NavigatorParams, Turning } from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Character {
|
|
11
11
|
direction: Direction;
|
|
@@ -37,7 +37,7 @@ export class Navigator<T = number> {
|
|
|
37
37
|
* in the matrix.
|
|
38
38
|
* @param - - `matrix`: a 2D array representing the grid or map
|
|
39
39
|
*/
|
|
40
|
-
constructor({matrix, turning, onMove, init: {cur, charDir, VISITED}}: NavigatorParams<T>) {
|
|
40
|
+
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }: NavigatorParams<T>) {
|
|
41
41
|
this._matrix = matrix;
|
|
42
42
|
this._cur = cur;
|
|
43
43
|
this._character = new Character(charDir, turning);
|
|
@@ -53,7 +53,7 @@ export class Navigator<T = number> {
|
|
|
53
53
|
*/
|
|
54
54
|
start() {
|
|
55
55
|
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
|
|
56
|
-
const {direction} = this._character;
|
|
56
|
+
const { direction } = this._character;
|
|
57
57
|
if (this.check(direction)) {
|
|
58
58
|
this.move(direction);
|
|
59
59
|
} else if (this.check(this._character.turn().direction)) {
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {Comparator} from '../../types';
|
|
8
|
+
import { PriorityQueue } from './priority-queue';
|
|
9
|
+
import type { Comparator } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {Comparator} from '../../types';
|
|
8
|
+
import { PriorityQueue } from './priority-queue';
|
|
9
|
+
import type { Comparator } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {Heap} from '../heap';
|
|
10
|
-
import {Comparator} from '../../types';
|
|
9
|
+
import { Heap } from '../heap';
|
|
10
|
+
import { Comparator } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {DoublyLinkedList} from '../linked-list';
|
|
8
|
+
import { DoublyLinkedList } from '../linked-list';
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
protected _nodes: {[key: number]: E} = {};
|
|
23
|
+
protected _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
|
-
import {SinglyLinkedList} from '../linked-list';
|
|
6
|
+
import { SinglyLinkedList } from '../linked-list';
|
|
7
7
|
|
|
8
8
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
9
|
/**
|
|
@@ -300,7 +300,7 @@ export class Queue<E = any> {
|
|
|
300
300
|
return new Queue(this.nodes.slice(this.offset));
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
*[Symbol.iterator]() {
|
|
303
|
+
* [Symbol.iterator]() {
|
|
304
304
|
for (const item of this.nodes) {
|
|
305
305
|
yield item;
|
|
306
306
|
}
|