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
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
import { IterableWithSizeOrLength
|
|
11
|
-
import { calcMinUnitsRequired, rangeCheck
|
|
10
|
+
import { IterableWithSizeOrLength } from "../../types";
|
|
11
|
+
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Deque can provide random access with O(1) time complexity
|
|
@@ -17,89 +17,6 @@ import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
|
|
|
17
17
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
export class DequeIterator<E> {
|
|
21
|
-
iterateDirection: IterateDirection;
|
|
22
|
-
|
|
23
|
-
index: number;
|
|
24
|
-
readonly deque: Deque<E>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The constructor initializes the index, iterate direction, and prev/next functions for a
|
|
28
|
-
* DequeIterator object.
|
|
29
|
-
* @param {number} index - The index parameter represents the current index position of the iterator
|
|
30
|
-
* within the deque. It is a number that indicates the position of the element that the iterator is
|
|
31
|
-
* currently pointing to.
|
|
32
|
-
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
|
|
33
|
-
* double-ended queue data structure, which allows elements to be added or removed from both ends.
|
|
34
|
-
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
|
|
35
|
-
* the direction in which the iterator should iterate over the elements of the `deque`. It has a
|
|
36
|
-
* default value of `IterateDirection.DEFAULT`.
|
|
37
|
-
* @returns The constructor is not returning anything. It is used to initialize the properties of the
|
|
38
|
-
* object being created.
|
|
39
|
-
*/
|
|
40
|
-
constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
|
|
41
|
-
this.index = index;
|
|
42
|
-
this.iterateDirection = iterateDirection;
|
|
43
|
-
if (this.iterateDirection === IterateDirection.DEFAULT) {
|
|
44
|
-
this.prev = function () {
|
|
45
|
-
if (this.index === 0) {
|
|
46
|
-
throwRangeError();
|
|
47
|
-
}
|
|
48
|
-
this.index -= 1;
|
|
49
|
-
return this;
|
|
50
|
-
};
|
|
51
|
-
this.next = function () {
|
|
52
|
-
if (this.index === this.deque.size) {
|
|
53
|
-
throwRangeError();
|
|
54
|
-
}
|
|
55
|
-
this.index += 1;
|
|
56
|
-
return this;
|
|
57
|
-
};
|
|
58
|
-
} else {
|
|
59
|
-
this.prev = function () {
|
|
60
|
-
if (this.index === this.deque.size - 1) {
|
|
61
|
-
throwRangeError();
|
|
62
|
-
}
|
|
63
|
-
this.index += 1;
|
|
64
|
-
return this;
|
|
65
|
-
};
|
|
66
|
-
this.next = function () {
|
|
67
|
-
if (this.index === -1) {
|
|
68
|
-
throwRangeError();
|
|
69
|
-
}
|
|
70
|
-
this.index -= 1;
|
|
71
|
-
return this;
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
this.deque = deque;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get current() {
|
|
78
|
-
return this.deque.getAt(this.index);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
set current(newElement: E) {
|
|
82
|
-
this.deque.setAt(this.index, newElement);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
isAccessible() {
|
|
86
|
-
return this.index !== this.deque.size;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
prev(): DequeIterator<E> {
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
next(): DequeIterator<E> {
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
clone() {
|
|
98
|
-
return new DequeIterator<E>(this.index, this.deque, this.iterateDirection);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
20
|
export class Deque<E> {
|
|
104
21
|
protected _bucketFirst = 0;
|
|
105
22
|
protected _firstInBucket = 0;
|
|
@@ -123,7 +40,7 @@ export class Deque<E> {
|
|
|
123
40
|
if ('length' in elements) {
|
|
124
41
|
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
|
|
125
42
|
} else {
|
|
126
|
-
if (elements.size instanceof Function) _size = elements.size();else _size = elements.size;
|
|
43
|
+
if (elements.size instanceof Function) _size = elements.size(); else _size = elements.size;
|
|
127
44
|
}
|
|
128
45
|
|
|
129
46
|
this._bucketSize = bucketSize;
|
|
@@ -245,38 +162,26 @@ export class Deque<E> {
|
|
|
245
162
|
}
|
|
246
163
|
|
|
247
164
|
/**
|
|
248
|
-
* The
|
|
249
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
250
|
-
*/
|
|
251
|
-
begin() {
|
|
252
|
-
return new DequeIterator<E>(0, this);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* The `end()` function returns a new `DequeIterator` object with the size and reference to the
|
|
257
|
-
* current deque.
|
|
258
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
165
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
259
166
|
*/
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
* the deque and iterates in reverse direction.
|
|
267
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
268
|
-
*/
|
|
269
|
-
reverseBegin() {
|
|
270
|
-
return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
|
|
167
|
+
* begin(): Generator<E> {
|
|
168
|
+
let index = 0;
|
|
169
|
+
while (index < this.size) {
|
|
170
|
+
yield this.getAt(index);
|
|
171
|
+
index++;
|
|
172
|
+
}
|
|
271
173
|
}
|
|
272
174
|
|
|
273
175
|
/**
|
|
274
|
-
* The
|
|
275
|
-
*
|
|
276
|
-
* @returns A new instance of the DequeIterator class is being returned.
|
|
176
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
177
|
+
* the last element.
|
|
277
178
|
*/
|
|
278
|
-
|
|
279
|
-
|
|
179
|
+
* reverseBegin(): Generator<E> {
|
|
180
|
+
let index = this.size - 1;
|
|
181
|
+
while (index >= 0) {
|
|
182
|
+
yield this.getAt(index);
|
|
183
|
+
index--;
|
|
184
|
+
}
|
|
280
185
|
}
|
|
281
186
|
|
|
282
187
|
/**
|
|
@@ -431,7 +336,7 @@ export class Deque<E> {
|
|
|
431
336
|
* @returns The element at the specified position in the data structure is being returned.
|
|
432
337
|
*/
|
|
433
338
|
getAt(pos: number): E {
|
|
434
|
-
rangeCheck
|
|
339
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
435
340
|
const {
|
|
436
341
|
bucketIndex,
|
|
437
342
|
indexInBucket
|
|
@@ -456,7 +361,7 @@ export class Deque<E> {
|
|
|
456
361
|
* position in the data structure.
|
|
457
362
|
*/
|
|
458
363
|
setAt(pos: number, element: E) {
|
|
459
|
-
rangeCheck
|
|
364
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
460
365
|
const {
|
|
461
366
|
bucketIndex,
|
|
462
367
|
indexInBucket
|
|
@@ -486,7 +391,7 @@ export class Deque<E> {
|
|
|
486
391
|
*/
|
|
487
392
|
insertAt(pos: number, element: E, num = 1) {
|
|
488
393
|
const length = this.size;
|
|
489
|
-
rangeCheck
|
|
394
|
+
rangeCheck(pos, 0, length);
|
|
490
395
|
if (pos === 0) {
|
|
491
396
|
while (num--) this.unshift(element);
|
|
492
397
|
} else if (pos === this.size) {
|
|
@@ -550,7 +455,7 @@ export class Deque<E> {
|
|
|
550
455
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
551
456
|
*/
|
|
552
457
|
deleteAt(pos: number) {
|
|
553
|
-
rangeCheck
|
|
458
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
554
459
|
if (pos === 0) this.shift();
|
|
555
460
|
else if (pos === this.size - 1) this.pop();
|
|
556
461
|
else {
|
|
@@ -605,50 +510,6 @@ export class Deque<E> {
|
|
|
605
510
|
return this.size;
|
|
606
511
|
}
|
|
607
512
|
|
|
608
|
-
/**
|
|
609
|
-
* Time Complexity: O(n)
|
|
610
|
-
* Space Complexity: O(1)
|
|
611
|
-
*/
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
* Time Complexity: O(n)
|
|
615
|
-
* Space Complexity: O(1)
|
|
616
|
-
*
|
|
617
|
-
* The function deletes an element from a deque using an iterator and returns the next iterator.
|
|
618
|
-
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
|
|
619
|
-
* that is used to iterate over elements in a deque (double-ended queue).
|
|
620
|
-
* @returns the updated iterator after deleting an element from the deque.
|
|
621
|
-
*/
|
|
622
|
-
deleteByIterator(iter: DequeIterator<E>) {
|
|
623
|
-
const index = iter.index;
|
|
624
|
-
this.deleteAt(index);
|
|
625
|
-
iter = iter.next();
|
|
626
|
-
return iter;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
* Time Complexity: O(n)
|
|
631
|
-
* Space Complexity: O(1)
|
|
632
|
-
*/
|
|
633
|
-
|
|
634
|
-
/**
|
|
635
|
-
* Time Complexity: O(n)
|
|
636
|
-
* Space Complexity: O(1)
|
|
637
|
-
*
|
|
638
|
-
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to
|
|
639
|
-
* the element if found, otherwise it returns an iterator pointing to the end of the deque.
|
|
640
|
-
* @param {E} element - The `element` parameter is the element that you want to find in the deque.
|
|
641
|
-
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
|
|
642
|
-
*/
|
|
643
|
-
findIterator(element: E) {
|
|
644
|
-
for (let i = 0; i < this.size; ++i) {
|
|
645
|
-
if (this.getAt(i) === element) {
|
|
646
|
-
return new DequeIterator<E>(i, this);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
return this.end();
|
|
650
|
-
}
|
|
651
|
-
|
|
652
513
|
/**
|
|
653
514
|
* Time Complexity: O(n)
|
|
654
515
|
* Space Complexity: O(1)
|
package/src/utils/utils.ts
CHANGED
|
@@ -93,7 +93,7 @@ export const throwRangeError = (message = 'The value is off-limits.'): void => {
|
|
|
93
93
|
throw new RangeError(message);
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
export const
|
|
96
|
+
export const isWeakKey = (input: unknown): input is WeakKey => {
|
|
97
97
|
const inputType = typeof input;
|
|
98
98
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
99
99
|
};
|
|
@@ -5,44 +5,119 @@ import { magnitude } from '../../../utils';
|
|
|
5
5
|
import { isCompetitor } from '../../../config';
|
|
6
6
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
|
-
const {
|
|
8
|
+
const { MILLION } = magnitude;
|
|
9
9
|
|
|
10
|
-
suite.add(`${
|
|
10
|
+
suite.add(`${MILLION.toLocaleString()} set`, () => {
|
|
11
11
|
const hm = new HashMap<number, number>();
|
|
12
12
|
|
|
13
|
-
for (let i = 0; i <
|
|
13
|
+
for (let i = 0; i < MILLION; i++) {
|
|
14
14
|
hm.set(i, i);
|
|
15
15
|
}
|
|
16
16
|
});
|
|
17
|
+
|
|
17
18
|
if (isCompetitor) {
|
|
18
|
-
suite.add(`${
|
|
19
|
+
suite.add(`${MILLION.toLocaleString()} CPT set`, () => {
|
|
19
20
|
const hm = new CHashMap<number, number>();
|
|
20
21
|
|
|
21
|
-
for (let i = 0; i <
|
|
22
|
+
for (let i = 0; i < MILLION; i++) {
|
|
22
23
|
hm.setElement(i, i);
|
|
23
24
|
}
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
+
|
|
28
|
+
suite.add(`${MILLION.toLocaleString()} Map set`, () => {
|
|
29
|
+
const hm = new Map<number, number>();
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < MILLION; i++) hm.set(i, i);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
suite.add(`${MILLION.toLocaleString()} Set add`, () => {
|
|
35
|
+
const hs = new Set<number>();
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
suite.add(`${MILLION.toLocaleString()} set & get`, () => {
|
|
27
41
|
const hm = new HashMap<number, number>();
|
|
28
42
|
|
|
29
|
-
for (let i = 0; i <
|
|
43
|
+
for (let i = 0; i < MILLION; i++) {
|
|
30
44
|
hm.set(i, i);
|
|
31
45
|
}
|
|
32
|
-
for (let i = 0; i <
|
|
46
|
+
for (let i = 0; i < MILLION; i++) {
|
|
33
47
|
hm.get(i);
|
|
34
48
|
}
|
|
35
49
|
});
|
|
50
|
+
|
|
36
51
|
if (isCompetitor) {
|
|
37
|
-
suite.add(`${
|
|
52
|
+
suite.add(`${MILLION.toLocaleString()} CPT set & get`, () => {
|
|
38
53
|
const hm = new CHashMap<number, number>();
|
|
39
54
|
|
|
40
|
-
for (let i = 0; i <
|
|
55
|
+
for (let i = 0; i < MILLION; i++) {
|
|
41
56
|
hm.setElement(i, i);
|
|
42
57
|
}
|
|
43
|
-
for (let i = 0; i <
|
|
58
|
+
for (let i = 0; i < MILLION; i++) {
|
|
44
59
|
hm.getElementByKey(i);
|
|
45
60
|
}
|
|
46
61
|
});
|
|
47
62
|
}
|
|
63
|
+
|
|
64
|
+
suite.add(`${MILLION.toLocaleString()} Map set & get`, () => {
|
|
65
|
+
const hm = new Map<number, number>();
|
|
66
|
+
|
|
67
|
+
for (let i = 0; i < MILLION; i++) {
|
|
68
|
+
hm.set(i, i);
|
|
69
|
+
}
|
|
70
|
+
for (let i = 0; i < MILLION; i++) {
|
|
71
|
+
hm.get(i);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
suite.add(`${MILLION.toLocaleString()} Set add & has`, () => {
|
|
76
|
+
const hs = new Set<number>();
|
|
77
|
+
|
|
78
|
+
for (let i = 0; i < MILLION; i++) hs.add(i);
|
|
79
|
+
|
|
80
|
+
for (let i = 0; i < MILLION; i++) hs.has(i);
|
|
81
|
+
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
|
|
85
|
+
const hm = new HashMap<[number, number], number>();
|
|
86
|
+
const objKeys:[number, number][] = [];
|
|
87
|
+
for (let i = 0; i < MILLION; i++) {
|
|
88
|
+
const obj: [number, number] = [i, i];
|
|
89
|
+
objKeys.push(obj)
|
|
90
|
+
hm.set(obj, i);
|
|
91
|
+
}
|
|
92
|
+
for (let i = 0; i < MILLION; i++) {
|
|
93
|
+
hm.get(objKeys[i]);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
|
|
98
|
+
const hm = new Map<[number, number], number>();
|
|
99
|
+
const objs:[number, number][] = [];
|
|
100
|
+
for (let i = 0; i < MILLION; i++) {
|
|
101
|
+
const obj: [number, number] = [i, i];
|
|
102
|
+
objs.push(obj)
|
|
103
|
+
hm.set(obj, i);
|
|
104
|
+
}
|
|
105
|
+
for (let i = 0; i < MILLION; i++) {
|
|
106
|
+
hm.get(objs[i]);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
suite.add(`${MILLION.toLocaleString()} Set ObjKey add & has`, () => {
|
|
111
|
+
const hs = new Set<[number, number]>();
|
|
112
|
+
const objs:[number, number][] = [];
|
|
113
|
+
for (let i = 0; i < MILLION; i++) {
|
|
114
|
+
const obj: [number, number] = [i, i];
|
|
115
|
+
objs.push(obj)
|
|
116
|
+
hs.add(obj);
|
|
117
|
+
}
|
|
118
|
+
for (let i = 0; i < MILLION; i++) {
|
|
119
|
+
hs.has(objs[i]);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
48
123
|
export { suite };
|
|
@@ -25,17 +25,20 @@ const reportDistPath = path.join(parentDirectory, 'benchmark');
|
|
|
25
25
|
|
|
26
26
|
const testDir = path.join(__dirname, 'data-structures');
|
|
27
27
|
const allFiles = fastGlob.sync(path.join(testDir, '**', '*.test.ts'));
|
|
28
|
-
let testFiles: string[]
|
|
28
|
+
let testFiles: string[];
|
|
29
|
+
|
|
30
|
+
let isIndividual = false;
|
|
29
31
|
if (args.length > 0) {
|
|
30
32
|
console.log(`arguments: ${args.join(' ')}`)
|
|
31
33
|
|
|
32
34
|
testFiles = allFiles.filter(file =>
|
|
33
35
|
args.every(word => file.includes(word))
|
|
34
36
|
);
|
|
35
|
-
|
|
37
|
+
isIndividual = true;
|
|
36
38
|
console.log(`${testFiles.map(file => coloredLabeled('Matched', file)).join(`
|
|
37
39
|
`)}`);
|
|
38
40
|
} else {
|
|
41
|
+
isIndividual = false;
|
|
39
42
|
testFiles = allFiles;
|
|
40
43
|
}
|
|
41
44
|
|
|
@@ -129,7 +132,7 @@ const composeReport = () => {
|
|
|
129
132
|
html += `</div>
|
|
130
133
|
</body>
|
|
131
134
|
</html>`;
|
|
132
|
-
replaceMarkdownContent(
|
|
135
|
+
if (!isIndividual) replaceMarkdownContent(
|
|
133
136
|
'[//]: # (No deletion!!! Start of Replace Section)', // Start tag
|
|
134
137
|
'[//]: # (No deletion!!! End of Replace Section)', // end identifier
|
|
135
138
|
htmlTables // New content to be inserted
|
|
@@ -172,15 +172,14 @@ describe('HashMap', () => {
|
|
|
172
172
|
stdMap.forEach((value, key) => {
|
|
173
173
|
if (index === 0) {
|
|
174
174
|
expect(hashMap.first).toEqual([key, value]);
|
|
175
|
-
expect(hashMap.begin.
|
|
175
|
+
expect(hashMap.begin().next().value).toEqual([key, value]);
|
|
176
176
|
} else if (index === hashMap.size - 1) {
|
|
177
177
|
expect(hashMap.last).toEqual([key, value]);
|
|
178
|
-
expect(hashMap.reverseBegin.
|
|
178
|
+
expect(hashMap.reverseBegin().next().value).toEqual([key, value]);
|
|
179
179
|
} else if (index <= 1000) {
|
|
180
180
|
expect(hashMap.getAt(index)).toEqual([key, value]);
|
|
181
181
|
}
|
|
182
182
|
expect(hashMap.get(key)).toEqual(value);
|
|
183
|
-
expect(hashMap.getIterator(key).current[1]).toEqual(value);
|
|
184
183
|
index++;
|
|
185
184
|
});
|
|
186
185
|
}
|
|
@@ -210,8 +209,8 @@ describe('HashMap', () => {
|
|
|
210
209
|
test('should iterate correctly with reverse iterators', () => {
|
|
211
210
|
hashMap.set('key1', 'value1');
|
|
212
211
|
hashMap.set('key2', 'value2');
|
|
213
|
-
const iterator = hashMap.reverseBegin;
|
|
214
|
-
expect(iterator.next().
|
|
212
|
+
const iterator = hashMap.reverseBegin();
|
|
213
|
+
expect(iterator.next().value).toEqual(['key2', 'value2']);
|
|
215
214
|
});
|
|
216
215
|
|
|
217
216
|
test('should return the last element', () => {
|
|
@@ -230,3 +229,35 @@ describe('HashMap', () => {
|
|
|
230
229
|
expect(hashMap.getAt(1)).toEqual(['key2', 'value2']);
|
|
231
230
|
});
|
|
232
231
|
});
|
|
232
|
+
|
|
233
|
+
describe('HashMap for coordinate object keys', () => {
|
|
234
|
+
const hashMap: HashMap<[number, number], number> = new HashMap();
|
|
235
|
+
const codObjs: [number, number][] = [];
|
|
236
|
+
|
|
237
|
+
test('set elements in hash map', () => {
|
|
238
|
+
for (let i = 0; i < 1000; i++) {
|
|
239
|
+
const codObj: [number, number] = [getRandomInt(-10000, 10000), i];
|
|
240
|
+
codObjs.push(codObj);
|
|
241
|
+
hashMap.set(codObj, i);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('get elements in hash map', () => {
|
|
246
|
+
for (let i = 0; i < 1000; i++) {
|
|
247
|
+
const codObj = codObjs[i];
|
|
248
|
+
if (codObj) {
|
|
249
|
+
expect(hashMap.get(codObj)).toBe(i);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('delete elements in hash map', () => {
|
|
255
|
+
for (let i = 0; i < 1000; i++) {
|
|
256
|
+
if (i === 500) expect(hashMap.size).toBe(500)
|
|
257
|
+
const codObj = codObjs[i];
|
|
258
|
+
if (codObj) hashMap.delete(codObj);
|
|
259
|
+
}
|
|
260
|
+
expect(hashMap.size).toBe(0);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
});
|
|
@@ -419,4 +419,32 @@ describe('Deque', () => {
|
|
|
419
419
|
expect(deque.indexOf(2)).toBe(1);
|
|
420
420
|
expect(deque.indexOf(4)).toBe(-1);
|
|
421
421
|
});
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
//Test begin method
|
|
425
|
+
describe('begin()', () => {
|
|
426
|
+
it('should return an iterator at the beginning of the deque', () => {
|
|
427
|
+
deque.push(1);
|
|
428
|
+
deque.push(2);
|
|
429
|
+
deque.push(3);
|
|
430
|
+
|
|
431
|
+
const iterator = deque.begin();
|
|
432
|
+
|
|
433
|
+
expect(iterator.next().value).toBe(1);
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
// Test the reverse Begin method
|
|
438
|
+
describe('reverseBegin()', () => {
|
|
439
|
+
it('should return a reverse iterator at the beginning of the deque', () => {
|
|
440
|
+
deque.push(1);
|
|
441
|
+
deque.push(2);
|
|
442
|
+
deque.push(3);
|
|
443
|
+
|
|
444
|
+
const iterator = deque.reverseBegin();
|
|
445
|
+
|
|
446
|
+
expect(iterator.next().value).toBe(3);
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
422
450
|
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export declare class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
-
constructor(joint?: string);
|
|
10
|
-
protected _joint: string;
|
|
11
|
-
get joint(): string;
|
|
12
|
-
/**
|
|
13
|
-
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
14
|
-
* key array with a specified delimiter.
|
|
15
|
-
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
16
|
-
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
17
|
-
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
18
|
-
*/
|
|
19
|
-
has(key: number[]): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
22
|
-
* delimiter before calling the original set method.
|
|
23
|
-
* @param {number[]} key - The key parameter is an array of numbers.
|
|
24
|
-
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
25
|
-
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
26
|
-
* (`super.set(key.join(this._joint), value)`).
|
|
27
|
-
*/
|
|
28
|
-
set(key: number[], value: V): this;
|
|
29
|
-
/**
|
|
30
|
-
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
31
|
-
* method.
|
|
32
|
-
* @param {number[]} key - An array of numbers
|
|
33
|
-
* @returns The code is returning the value associated with the specified key in the map.
|
|
34
|
-
*/
|
|
35
|
-
get(key: number[]): V | undefined;
|
|
36
|
-
/**
|
|
37
|
-
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
38
|
-
* the super delete method.
|
|
39
|
-
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
40
|
-
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
41
|
-
* `key` array joined together using the `_joint` property.
|
|
42
|
-
*/
|
|
43
|
-
delete(key: number[]): boolean;
|
|
44
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CoordinateMap = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* data-structure-typed
|
|
6
|
-
*
|
|
7
|
-
* @author Tyler Zeng
|
|
8
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
-
* @license MIT License
|
|
10
|
-
*/
|
|
11
|
-
class CoordinateMap extends Map {
|
|
12
|
-
constructor(joint) {
|
|
13
|
-
super();
|
|
14
|
-
this._joint = '_';
|
|
15
|
-
if (joint !== undefined)
|
|
16
|
-
this._joint = joint;
|
|
17
|
-
}
|
|
18
|
-
get joint() {
|
|
19
|
-
return this._joint;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
23
|
-
* key array with a specified delimiter.
|
|
24
|
-
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
25
|
-
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
26
|
-
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
27
|
-
*/
|
|
28
|
-
has(key) {
|
|
29
|
-
return super.has(key.join(this._joint));
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
33
|
-
* delimiter before calling the original set method.
|
|
34
|
-
* @param {number[]} key - The key parameter is an array of numbers.
|
|
35
|
-
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
36
|
-
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
37
|
-
* (`super.set(key.join(this._joint), value)`).
|
|
38
|
-
*/
|
|
39
|
-
set(key, value) {
|
|
40
|
-
return super.set(key.join(this._joint), value);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
44
|
-
* method.
|
|
45
|
-
* @param {number[]} key - An array of numbers
|
|
46
|
-
* @returns The code is returning the value associated with the specified key in the map.
|
|
47
|
-
*/
|
|
48
|
-
get(key) {
|
|
49
|
-
return super.get(key.join(this._joint));
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
53
|
-
* the super delete method.
|
|
54
|
-
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
55
|
-
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
56
|
-
* `key` array joined together using the `_joint` property.
|
|
57
|
-
*/
|
|
58
|
-
delete(key) {
|
|
59
|
-
return super.delete(key.join(this._joint));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.CoordinateMap = CoordinateMap;
|
|
63
|
-
//# sourceMappingURL=coordinate-map.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"coordinate-map.js","sourceRoot":"","sources":["../../../../src/data-structures/hash/coordinate-map.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,aAAiB,SAAQ,GAAW;IAC/C,YAAY,KAAc;QACxB,KAAK,EAAE,CAAC;QAIA,WAAM,GAAG,GAAG,CAAC;QAHrB,IAAI,KAAK,KAAK,SAAS;YAAE,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IAC/C,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACM,GAAG,CAAC,GAAa;QACxB,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;;OAOG;IACM,GAAG,CAAC,GAAa,EAAE,KAAQ;QAClC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;IAED;;;;;OAKG;IACM,GAAG,CAAC,GAAa;QACxB,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACM,MAAM,CAAC,GAAa;QAC3B,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7C,CAAC;CACF;AAvDD,sCAuDC"}
|