data-structure-typed 1.46.2 → 1.46.4
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/CHANGELOG.md +1 -1
- package/README.md +16 -16
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +8 -389
- package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -116
- package/dist/cjs/data-structures/hash/hash-map.js +127 -254
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/index.d.ts +0 -4
- package/dist/cjs/data-structures/hash/index.js +0 -4
- package/dist/cjs/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +6 -74
- package/dist/cjs/data-structures/queue/deque.js +20 -142
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/hash/index.d.ts +0 -4
- package/dist/cjs/types/data-structures/hash/index.js +0 -4
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -1
- package/dist/cjs/utils/utils.js +3 -3
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -116
- package/dist/mjs/data-structures/hash/hash-map.js +129 -257
- package/dist/mjs/data-structures/hash/index.d.ts +0 -4
- package/dist/mjs/data-structures/hash/index.js +0 -4
- package/dist/mjs/data-structures/queue/deque.d.ts +6 -74
- package/dist/mjs/data-structures/queue/deque.js +16 -140
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/hash/index.d.ts +0 -4
- package/dist/mjs/types/data-structures/hash/index.js +0 -4
- package/dist/mjs/utils/utils.d.ts +1 -1
- package/dist/mjs/utils/utils.js +1 -1
- package/dist/umd/data-structure-typed.js +169 -531
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/hash/hash-map.ts +154 -275
- package/src/data-structures/hash/index.ts +0 -4
- package/src/data-structures/queue/deque.ts +22 -161
- package/src/types/data-structures/hash/hash-map.ts +6 -0
- package/src/types/data-structures/hash/index.ts +0 -4
- package/src/utils/utils.ts +1 -1
- package/test/performance/data-structures/hash/hash-map.test.ts +86 -11
- package/test/performance/reportor.ts +6 -3
- package/test/unit/data-structures/hash/hash-map.test.ts +36 -5
- package/test/unit/data-structures/queue/deque.test.ts +28 -0
- package/dist/cjs/data-structures/hash/coordinate-map.d.ts +0 -44
- package/dist/cjs/data-structures/hash/coordinate-map.js +0 -63
- package/dist/cjs/data-structures/hash/coordinate-map.js.map +0 -1
- package/dist/cjs/data-structures/hash/coordinate-set.d.ts +0 -36
- package/dist/cjs/data-structures/hash/coordinate-set.js +0 -53
- package/dist/cjs/data-structures/hash/coordinate-set.js.map +0 -1
- package/dist/cjs/data-structures/hash/tree-map.d.ts +0 -2
- package/dist/cjs/data-structures/hash/tree-map.js +0 -7
- package/dist/cjs/data-structures/hash/tree-map.js.map +0 -1
- package/dist/cjs/data-structures/hash/tree-set.d.ts +0 -2
- package/dist/cjs/data-structures/hash/tree-set.js +0 -7
- package/dist/cjs/data-structures/hash/tree-set.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-map.js +0 -3
- package/dist/cjs/types/data-structures/hash/coordinate-map.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-set.js +0 -3
- package/dist/cjs/types/data-structures/hash/coordinate-set.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/tree-map.js +0 -3
- package/dist/cjs/types/data-structures/hash/tree-map.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/tree-set.js +0 -3
- package/dist/cjs/types/data-structures/hash/tree-set.js.map +0 -1
- package/dist/mjs/data-structures/hash/coordinate-map.d.ts +0 -44
- package/dist/mjs/data-structures/hash/coordinate-map.js +0 -58
- package/dist/mjs/data-structures/hash/coordinate-set.d.ts +0 -36
- package/dist/mjs/data-structures/hash/coordinate-set.js +0 -48
- package/dist/mjs/data-structures/hash/tree-map.d.ts +0 -2
- package/dist/mjs/data-structures/hash/tree-map.js +0 -2
- package/dist/mjs/data-structures/hash/tree-set.d.ts +0 -2
- package/dist/mjs/data-structures/hash/tree-set.js +0 -2
- package/dist/mjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-map.js +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-set.js +0 -1
- package/dist/mjs/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/tree-map.js +0 -1
- package/dist/mjs/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/tree-set.js +0 -1
- package/src/data-structures/hash/coordinate-map.ts +0 -63
- package/src/data-structures/hash/coordinate-set.ts +0 -52
- package/src/data-structures/hash/tree-map.ts +0 -2
- package/src/data-structures/hash/tree-set.ts +0 -2
- package/src/types/data-structures/hash/coordinate-map.ts +0 -1
- package/src/types/data-structures/hash/coordinate-set.ts +0 -1
- package/src/types/data-structures/hash/tree-map.ts +0 -1
- package/src/types/data-structures/hash/tree-set.ts +0 -1
- package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -74
- package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -66
|
@@ -5,87 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { calcMinUnitsRequired, rangeCheck
|
|
8
|
+
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
9
9
|
/**
|
|
10
10
|
* Deque can provide random access with O(1) time complexity
|
|
11
11
|
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
12
12
|
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
13
13
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
14
14
|
*/
|
|
15
|
-
export class DequeIterator {
|
|
16
|
-
iterateDirection;
|
|
17
|
-
index;
|
|
18
|
-
deque;
|
|
19
|
-
/**
|
|
20
|
-
* The constructor initializes the index, iterate direction, and prev/next functions for a
|
|
21
|
-
* DequeIterator object.
|
|
22
|
-
* @param {number} index - The index parameter represents the current index position of the iterator
|
|
23
|
-
* within the deque. It is a number that indicates the position of the element that the iterator is
|
|
24
|
-
* currently pointing to.
|
|
25
|
-
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
|
|
26
|
-
* double-ended queue data structure, which allows elements to be added or removed from both ends.
|
|
27
|
-
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
|
|
28
|
-
* the direction in which the iterator should iterate over the elements of the `deque`. It has a
|
|
29
|
-
* default value of `IterateDirection.DEFAULT`.
|
|
30
|
-
* @returns The constructor is not returning anything. It is used to initialize the properties of the
|
|
31
|
-
* object being created.
|
|
32
|
-
*/
|
|
33
|
-
constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
|
|
34
|
-
this.index = index;
|
|
35
|
-
this.iterateDirection = iterateDirection;
|
|
36
|
-
if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
|
|
37
|
-
this.prev = function () {
|
|
38
|
-
if (this.index === 0) {
|
|
39
|
-
throwRangeError();
|
|
40
|
-
}
|
|
41
|
-
this.index -= 1;
|
|
42
|
-
return this;
|
|
43
|
-
};
|
|
44
|
-
this.next = function () {
|
|
45
|
-
if (this.index === this.deque.size) {
|
|
46
|
-
throwRangeError();
|
|
47
|
-
}
|
|
48
|
-
this.index += 1;
|
|
49
|
-
return this;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
this.prev = function () {
|
|
54
|
-
if (this.index === this.deque.size - 1) {
|
|
55
|
-
throwRangeError();
|
|
56
|
-
}
|
|
57
|
-
this.index += 1;
|
|
58
|
-
return this;
|
|
59
|
-
};
|
|
60
|
-
this.next = function () {
|
|
61
|
-
if (this.index === -1) {
|
|
62
|
-
throwRangeError();
|
|
63
|
-
}
|
|
64
|
-
this.index -= 1;
|
|
65
|
-
return this;
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
this.deque = deque;
|
|
69
|
-
}
|
|
70
|
-
get current() {
|
|
71
|
-
return this.deque.getAt(this.index);
|
|
72
|
-
}
|
|
73
|
-
set current(newElement) {
|
|
74
|
-
this.deque.setAt(this.index, newElement);
|
|
75
|
-
}
|
|
76
|
-
isAccessible() {
|
|
77
|
-
return this.index !== this.deque.size;
|
|
78
|
-
}
|
|
79
|
-
prev() {
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
next() {
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
clone() {
|
|
86
|
-
return new DequeIterator(this.index, this.deque, this.iterateDirection);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
15
|
export class Deque {
|
|
90
16
|
_bucketFirst = 0;
|
|
91
17
|
_firstInBucket = 0;
|
|
@@ -220,35 +146,25 @@ export class Deque {
|
|
|
220
146
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
221
147
|
}
|
|
222
148
|
/**
|
|
223
|
-
* The
|
|
224
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
225
|
-
*/
|
|
226
|
-
begin() {
|
|
227
|
-
return new DequeIterator(0, this);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* The `end()` function returns a new `DequeIterator` object with the size and reference to the
|
|
231
|
-
* current deque.
|
|
232
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
149
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
233
150
|
*/
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
241
|
-
*/
|
|
242
|
-
reverseBegin() {
|
|
243
|
-
return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
|
|
151
|
+
*begin() {
|
|
152
|
+
let index = 0;
|
|
153
|
+
while (index < this.size) {
|
|
154
|
+
yield this.getAt(index);
|
|
155
|
+
index++;
|
|
156
|
+
}
|
|
244
157
|
}
|
|
245
158
|
/**
|
|
246
|
-
* The
|
|
247
|
-
*
|
|
248
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
159
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
160
|
+
* the last element.
|
|
249
161
|
*/
|
|
250
|
-
|
|
251
|
-
|
|
162
|
+
*reverseBegin() {
|
|
163
|
+
let index = this.size - 1;
|
|
164
|
+
while (index >= 0) {
|
|
165
|
+
yield this.getAt(index);
|
|
166
|
+
index--;
|
|
167
|
+
}
|
|
252
168
|
}
|
|
253
169
|
/**
|
|
254
170
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
@@ -555,46 +471,6 @@ export class Deque {
|
|
|
555
471
|
this.cut(index - 1);
|
|
556
472
|
return this.size;
|
|
557
473
|
}
|
|
558
|
-
/**
|
|
559
|
-
* Time Complexity: O(n)
|
|
560
|
-
* Space Complexity: O(1)
|
|
561
|
-
*/
|
|
562
|
-
/**
|
|
563
|
-
* Time Complexity: O(n)
|
|
564
|
-
* Space Complexity: O(1)
|
|
565
|
-
*
|
|
566
|
-
* The function deletes an element from a deque using an iterator and returns the next iterator.
|
|
567
|
-
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
|
|
568
|
-
* that is used to iterate over elements in a deque (double-ended queue).
|
|
569
|
-
* @returns the updated iterator after deleting an element from the deque.
|
|
570
|
-
*/
|
|
571
|
-
deleteByIterator(iter) {
|
|
572
|
-
const index = iter.index;
|
|
573
|
-
this.deleteAt(index);
|
|
574
|
-
iter = iter.next();
|
|
575
|
-
return iter;
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Time Complexity: O(n)
|
|
579
|
-
* Space Complexity: O(1)
|
|
580
|
-
*/
|
|
581
|
-
/**
|
|
582
|
-
* Time Complexity: O(n)
|
|
583
|
-
* Space Complexity: O(1)
|
|
584
|
-
*
|
|
585
|
-
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to
|
|
586
|
-
* the element if found, otherwise it returns an iterator pointing to the end of the deque.
|
|
587
|
-
* @param {E} element - The `element` parameter is the element that you want to find in the deque.
|
|
588
|
-
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
|
|
589
|
-
*/
|
|
590
|
-
findIterator(element) {
|
|
591
|
-
for (let i = 0; i < this.size; ++i) {
|
|
592
|
-
if (this.getAt(i) === element) {
|
|
593
|
-
return new DequeIterator(i, this);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
return this.end();
|
|
597
|
-
}
|
|
598
474
|
/**
|
|
599
475
|
* Time Complexity: O(n)
|
|
600
476
|
* Space Complexity: O(1)
|
|
@@ -20,5 +20,5 @@ export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Paramet
|
|
|
20
20
|
export declare const getMSB: (value: number) => number;
|
|
21
21
|
export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
|
|
22
22
|
export declare const throwRangeError: (message?: string) => void;
|
|
23
|
-
export declare const
|
|
23
|
+
export declare const isWeakKey: (input: unknown) => input is WeakKey;
|
|
24
24
|
export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
|
package/dist/mjs/utils/utils.js
CHANGED
|
@@ -59,7 +59,7 @@ export const rangeCheck = (index, min, max, message = 'Index out of bounds.') =>
|
|
|
59
59
|
export const throwRangeError = (message = 'The value is off-limits.') => {
|
|
60
60
|
throw new RangeError(message);
|
|
61
61
|
};
|
|
62
|
-
export const
|
|
62
|
+
export const isWeakKey = (input) => {
|
|
63
63
|
const inputType = typeof input;
|
|
64
64
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
65
65
|
};
|