data-structure-typed 1.52.6 → 1.52.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/CHANGELOG.md +1 -1
- package/README.md +33 -30
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +156 -156
- package/dist/cjs/constants/index.d.ts +4 -0
- package/dist/cjs/constants/index.js +9 -0
- package/dist/cjs/constants/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +668 -597
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +3 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.js +4 -2
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/cjs/data-structures/queue/deque.js +29 -29
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/constants/index.d.ts +4 -0
- package/dist/mjs/constants/index.js +5 -0
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +667 -591
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.js +1 -1
- package/dist/mjs/data-structures/heap/heap.js +3 -3
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.js +4 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/mjs/data-structures/queue/deque.js +29 -29
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +1343 -1615
- package/dist/umd/data-structure-typed.min.js +10 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/eslint.config.mjs +69 -0
- package/package.json +30 -28
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +697 -725
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +1 -0
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
- package/test/config.ts +1 -7
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +17 -16
- package/test/integration/heap.test.js +6 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +51 -26
- package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/test/unit/data-structures/stack/stack.test.ts +6 -0
- package/test/unit/unrestricted-interconversion.test.ts +24 -24
- package/test/utils/big-o.ts +5 -4
- package/.eslintrc.js +0 -64
|
@@ -242,7 +242,7 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
242
242
|
* or the linked list is empty, it will return undefined.
|
|
243
243
|
*/
|
|
244
244
|
at(index) {
|
|
245
|
-
if (index < 0 || index >= this.
|
|
245
|
+
if (index < 0 || index >= this._size)
|
|
246
246
|
return undefined;
|
|
247
247
|
let current = this.head;
|
|
248
248
|
for (let i = 0; i < index; i++) {
|
|
@@ -262,7 +262,7 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
262
262
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
263
263
|
*/
|
|
264
264
|
getNodeAt(index) {
|
|
265
|
-
if (index < 0 || index >= this.
|
|
265
|
+
if (index < 0 || index >= this._size)
|
|
266
266
|
return undefined;
|
|
267
267
|
let current = this.head;
|
|
268
268
|
for (let i = 0; i < index; i++) {
|
|
@@ -303,13 +303,13 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
303
303
|
* if the index is out of bounds.
|
|
304
304
|
*/
|
|
305
305
|
addAt(index, value) {
|
|
306
|
-
if (index < 0 || index > this.
|
|
306
|
+
if (index < 0 || index > this._size)
|
|
307
307
|
return false;
|
|
308
308
|
if (index === 0) {
|
|
309
309
|
this.unshift(value);
|
|
310
310
|
return true;
|
|
311
311
|
}
|
|
312
|
-
if (index === this.
|
|
312
|
+
if (index === this._size) {
|
|
313
313
|
this.push(value);
|
|
314
314
|
return true;
|
|
315
315
|
}
|
|
@@ -407,13 +407,13 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
407
407
|
* bounds.
|
|
408
408
|
*/
|
|
409
409
|
deleteAt(index) {
|
|
410
|
-
if (index < 0 || index >= this.
|
|
410
|
+
if (index < 0 || index >= this._size)
|
|
411
411
|
return false;
|
|
412
412
|
if (index === 0) {
|
|
413
413
|
this.shift();
|
|
414
414
|
return true;
|
|
415
415
|
}
|
|
416
|
-
if (index === this.
|
|
416
|
+
if (index === this._size - 1) {
|
|
417
417
|
this.pop();
|
|
418
418
|
return true;
|
|
419
419
|
}
|
|
@@ -469,7 +469,7 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
469
469
|
* @returns A boolean value is being returned.
|
|
470
470
|
*/
|
|
471
471
|
isEmpty() {
|
|
472
|
-
return this.
|
|
472
|
+
return this._size === 0;
|
|
473
473
|
}
|
|
474
474
|
/**
|
|
475
475
|
* Time Complexity: O(1)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
|
@@ -211,7 +211,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
211
211
|
* `undefined` if the index is out of bounds.
|
|
212
212
|
*/
|
|
213
213
|
at(index) {
|
|
214
|
-
if (index < 0 || index >= this.
|
|
214
|
+
if (index < 0 || index >= this._size)
|
|
215
215
|
return undefined;
|
|
216
216
|
let current = this.head;
|
|
217
217
|
for (let i = 0; i < index; i++) {
|
|
@@ -247,13 +247,13 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
247
247
|
* bounds.
|
|
248
248
|
*/
|
|
249
249
|
deleteAt(index) {
|
|
250
|
-
if (index < 0 || index >= this.
|
|
250
|
+
if (index < 0 || index >= this._size)
|
|
251
251
|
return false;
|
|
252
252
|
if (index === 0) {
|
|
253
253
|
this.shift();
|
|
254
254
|
return true;
|
|
255
255
|
}
|
|
256
|
-
if (index === this.
|
|
256
|
+
if (index === this._size - 1) {
|
|
257
257
|
this.pop();
|
|
258
258
|
return true;
|
|
259
259
|
}
|
|
@@ -319,13 +319,13 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
319
319
|
* if the index is out of bounds.
|
|
320
320
|
*/
|
|
321
321
|
addAt(index, value) {
|
|
322
|
-
if (index < 0 || index > this.
|
|
322
|
+
if (index < 0 || index > this._size)
|
|
323
323
|
return false;
|
|
324
324
|
if (index === 0) {
|
|
325
325
|
this.unshift(value);
|
|
326
326
|
return true;
|
|
327
327
|
}
|
|
328
|
-
if (index === this.
|
|
328
|
+
if (index === this._size) {
|
|
329
329
|
this.push(value);
|
|
330
330
|
return true;
|
|
331
331
|
}
|
|
@@ -342,7 +342,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
342
342
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
343
343
|
*/
|
|
344
344
|
isEmpty() {
|
|
345
|
-
return this.
|
|
345
|
+
return this._size === 0;
|
|
346
346
|
}
|
|
347
347
|
/**
|
|
348
348
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { SkipLinkedListOptions } from '../../types';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { MatrixOptions } from '../../types';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { Direction, NavigatorParams, Turning } from '../../types';
|
|
@@ -30,7 +30,8 @@ export class Navigator {
|
|
|
30
30
|
this._cur = cur;
|
|
31
31
|
this._character = new Character(charDir, turning);
|
|
32
32
|
this.onMove = onMove;
|
|
33
|
-
|
|
33
|
+
if (this.onMove)
|
|
34
|
+
this.onMove(this._cur);
|
|
34
35
|
this._VISITED = VISITED;
|
|
35
36
|
this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
|
|
36
37
|
}
|
|
@@ -103,6 +104,7 @@ export class Navigator {
|
|
|
103
104
|
}
|
|
104
105
|
const [i, j] = this._cur;
|
|
105
106
|
this._matrix[i][j] = this._VISITED;
|
|
106
|
-
|
|
107
|
+
if (this.onMove)
|
|
108
|
+
this.onMove(this._cur);
|
|
107
109
|
}
|
|
108
110
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types';
|
|
@@ -287,7 +287,7 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
287
287
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
288
288
|
* memory usage.
|
|
289
289
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
290
|
-
* `this.
|
|
290
|
+
* `this._size` is 0, but it does not return any value.
|
|
291
291
|
*/
|
|
292
292
|
shrinkToFit(): void;
|
|
293
293
|
/**
|
|
@@ -139,7 +139,7 @@ export class Deque extends IterableElementBase {
|
|
|
139
139
|
* @returns The first element of the collection, of type E, is being returned.
|
|
140
140
|
*/
|
|
141
141
|
get first() {
|
|
142
|
-
if (this.
|
|
142
|
+
if (this._size === 0)
|
|
143
143
|
return;
|
|
144
144
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
145
145
|
}
|
|
@@ -148,7 +148,7 @@ export class Deque extends IterableElementBase {
|
|
|
148
148
|
* @return The last element in the array
|
|
149
149
|
*/
|
|
150
150
|
get last() {
|
|
151
|
-
if (this.
|
|
151
|
+
if (this._size === 0)
|
|
152
152
|
return;
|
|
153
153
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
154
154
|
}
|
|
@@ -162,7 +162,7 @@ export class Deque extends IterableElementBase {
|
|
|
162
162
|
* @returns The size of the data structure after the element has been pushed.
|
|
163
163
|
*/
|
|
164
164
|
push(element) {
|
|
165
|
-
if (this.
|
|
165
|
+
if (this._size) {
|
|
166
166
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
167
167
|
this._lastInBucket += 1;
|
|
168
168
|
}
|
|
@@ -192,10 +192,10 @@ export class Deque extends IterableElementBase {
|
|
|
192
192
|
* @returns The element that was removed from the data structure is being returned.
|
|
193
193
|
*/
|
|
194
194
|
pop() {
|
|
195
|
-
if (this.
|
|
195
|
+
if (this._size === 0)
|
|
196
196
|
return;
|
|
197
197
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
198
|
-
if (this.
|
|
198
|
+
if (this._size !== 1) {
|
|
199
199
|
if (this._lastInBucket > 0) {
|
|
200
200
|
this._lastInBucket -= 1;
|
|
201
201
|
}
|
|
@@ -222,7 +222,7 @@ export class Deque extends IterableElementBase {
|
|
|
222
222
|
* @returns The size of the data structure after the element has been added.
|
|
223
223
|
*/
|
|
224
224
|
unshift(element) {
|
|
225
|
-
if (this.
|
|
225
|
+
if (this._size) {
|
|
226
226
|
if (this._firstInBucket > 0) {
|
|
227
227
|
this._firstInBucket -= 1;
|
|
228
228
|
}
|
|
@@ -253,10 +253,10 @@ export class Deque extends IterableElementBase {
|
|
|
253
253
|
* returned.
|
|
254
254
|
*/
|
|
255
255
|
shift() {
|
|
256
|
-
if (this.
|
|
256
|
+
if (this._size === 0)
|
|
257
257
|
return;
|
|
258
258
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
259
|
-
if (this.
|
|
259
|
+
if (this._size !== 1) {
|
|
260
260
|
if (this._firstInBucket < this._bucketSize - 1) {
|
|
261
261
|
this._firstInBucket += 1;
|
|
262
262
|
}
|
|
@@ -280,7 +280,7 @@ export class Deque extends IterableElementBase {
|
|
|
280
280
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
281
281
|
*/
|
|
282
282
|
isEmpty() {
|
|
283
|
-
return this.
|
|
283
|
+
return this._size === 0;
|
|
284
284
|
}
|
|
285
285
|
/**
|
|
286
286
|
* Time Complexity: O(1)
|
|
@@ -300,7 +300,7 @@ export class Deque extends IterableElementBase {
|
|
|
300
300
|
*/
|
|
301
301
|
*begin() {
|
|
302
302
|
let index = 0;
|
|
303
|
-
while (index < this.
|
|
303
|
+
while (index < this._size) {
|
|
304
304
|
yield this.at(index);
|
|
305
305
|
index++;
|
|
306
306
|
}
|
|
@@ -310,7 +310,7 @@ export class Deque extends IterableElementBase {
|
|
|
310
310
|
* the last element.
|
|
311
311
|
*/
|
|
312
312
|
*reverseBegin() {
|
|
313
|
-
let index = this.
|
|
313
|
+
let index = this._size - 1;
|
|
314
314
|
while (index >= 0) {
|
|
315
315
|
yield this.at(index);
|
|
316
316
|
index--;
|
|
@@ -327,7 +327,7 @@ export class Deque extends IterableElementBase {
|
|
|
327
327
|
* @returns The element at the specified position in the data structure is being returned.
|
|
328
328
|
*/
|
|
329
329
|
at(pos) {
|
|
330
|
-
rangeCheck(pos, 0, this.
|
|
330
|
+
rangeCheck(pos, 0, this._size - 1);
|
|
331
331
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
332
332
|
return this._buckets[bucketIndex][indexInBucket];
|
|
333
333
|
}
|
|
@@ -342,7 +342,7 @@ export class Deque extends IterableElementBase {
|
|
|
342
342
|
* position in the data structure.
|
|
343
343
|
*/
|
|
344
344
|
setAt(pos, element) {
|
|
345
|
-
rangeCheck(pos, 0, this.
|
|
345
|
+
rangeCheck(pos, 0, this._size - 1);
|
|
346
346
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
347
347
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
348
348
|
return true;
|
|
@@ -363,19 +363,19 @@ export class Deque extends IterableElementBase {
|
|
|
363
363
|
* @returns The size of the array after the insertion is being returned.
|
|
364
364
|
*/
|
|
365
365
|
addAt(pos, element, num = 1) {
|
|
366
|
-
const length = this.
|
|
366
|
+
const length = this._size;
|
|
367
367
|
rangeCheck(pos, 0, length);
|
|
368
368
|
if (pos === 0) {
|
|
369
369
|
while (num--)
|
|
370
370
|
this.unshift(element);
|
|
371
371
|
}
|
|
372
|
-
else if (pos === this.
|
|
372
|
+
else if (pos === this._size) {
|
|
373
373
|
while (num--)
|
|
374
374
|
this.push(element);
|
|
375
375
|
}
|
|
376
376
|
else {
|
|
377
377
|
const arr = [];
|
|
378
|
-
for (let i = pos; i < this.
|
|
378
|
+
for (let i = pos; i < this._size; ++i) {
|
|
379
379
|
arr.push(this.at(i));
|
|
380
380
|
}
|
|
381
381
|
this.cut(pos - 1, true);
|
|
@@ -447,7 +447,7 @@ export class Deque extends IterableElementBase {
|
|
|
447
447
|
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
448
448
|
if (pos < 0)
|
|
449
449
|
pos = 0;
|
|
450
|
-
for (let i = pos; i < this.
|
|
450
|
+
for (let i = pos; i < this._size; i++) {
|
|
451
451
|
newDeque.push(this.at(i));
|
|
452
452
|
}
|
|
453
453
|
return newDeque;
|
|
@@ -465,13 +465,13 @@ export class Deque extends IterableElementBase {
|
|
|
465
465
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
466
466
|
*/
|
|
467
467
|
deleteAt(pos) {
|
|
468
|
-
rangeCheck(pos, 0, this.
|
|
468
|
+
rangeCheck(pos, 0, this._size - 1);
|
|
469
469
|
if (pos === 0)
|
|
470
470
|
this.shift();
|
|
471
|
-
else if (pos === this.
|
|
471
|
+
else if (pos === this._size - 1)
|
|
472
472
|
this.pop();
|
|
473
473
|
else {
|
|
474
|
-
const length = this.
|
|
474
|
+
const length = this._size - 1;
|
|
475
475
|
let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
|
|
476
476
|
for (let i = pos; i < length; ++i) {
|
|
477
477
|
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
|
|
@@ -494,7 +494,7 @@ export class Deque extends IterableElementBase {
|
|
|
494
494
|
* @returns The size of the data structure after the element has been deleted.
|
|
495
495
|
*/
|
|
496
496
|
delete(element) {
|
|
497
|
-
const size = this.
|
|
497
|
+
const size = this._size;
|
|
498
498
|
if (size === 0)
|
|
499
499
|
return false;
|
|
500
500
|
let i = 0;
|
|
@@ -539,12 +539,12 @@ export class Deque extends IterableElementBase {
|
|
|
539
539
|
* @returns The size of the modified array is being returned.
|
|
540
540
|
*/
|
|
541
541
|
unique() {
|
|
542
|
-
if (this.
|
|
542
|
+
if (this._size <= 1) {
|
|
543
543
|
return this;
|
|
544
544
|
}
|
|
545
545
|
let index = 1;
|
|
546
546
|
let prev = this.at(0);
|
|
547
|
-
for (let i = 1; i < this.
|
|
547
|
+
for (let i = 1; i < this._size; ++i) {
|
|
548
548
|
const cur = this.at(i);
|
|
549
549
|
if (cur !== prev) {
|
|
550
550
|
prev = cur;
|
|
@@ -566,11 +566,11 @@ export class Deque extends IterableElementBase {
|
|
|
566
566
|
*/
|
|
567
567
|
sort(comparator) {
|
|
568
568
|
const arr = [];
|
|
569
|
-
for (let i = 0; i < this.
|
|
569
|
+
for (let i = 0; i < this._size; ++i) {
|
|
570
570
|
arr.push(this.at(i));
|
|
571
571
|
}
|
|
572
572
|
arr.sort(comparator);
|
|
573
|
-
for (let i = 0; i < this.
|
|
573
|
+
for (let i = 0; i < this._size; ++i) {
|
|
574
574
|
this.setAt(i, arr[i]);
|
|
575
575
|
}
|
|
576
576
|
return this;
|
|
@@ -582,10 +582,10 @@ export class Deque extends IterableElementBase {
|
|
|
582
582
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
583
583
|
* memory usage.
|
|
584
584
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
585
|
-
* `this.
|
|
585
|
+
* `this._size` is 0, but it does not return any value.
|
|
586
586
|
*/
|
|
587
587
|
shrinkToFit() {
|
|
588
|
-
if (this.
|
|
588
|
+
if (this._size === 0)
|
|
589
589
|
return;
|
|
590
590
|
const newBuckets = [];
|
|
591
591
|
if (this._bucketFirst === this._bucketLast)
|
|
@@ -619,7 +619,7 @@ export class Deque extends IterableElementBase {
|
|
|
619
619
|
* in the data structure. If the element is not found, it returns -1.
|
|
620
620
|
*/
|
|
621
621
|
indexOf(element) {
|
|
622
|
-
for (let i = 0; i < this.
|
|
622
|
+
for (let i = 0; i < this._size; ++i) {
|
|
623
623
|
if (this.at(i) === element) {
|
|
624
624
|
return i;
|
|
625
625
|
}
|
|
@@ -710,7 +710,7 @@ export class Deque extends IterableElementBase {
|
|
|
710
710
|
* object to be iterated over using a for...of loop.
|
|
711
711
|
*/
|
|
712
712
|
*_getIterator() {
|
|
713
|
-
for (let i = 0; i < this.
|
|
713
|
+
for (let i = 0; i < this._size; ++i) {
|
|
714
714
|
yield this.at(i);
|
|
715
715
|
}
|
|
716
716
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { ElementCallback, StackOptions } from '../../types';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { ElementCallback, TrieOptions } from '../../types';
|
package/dist/mjs/index.d.ts
CHANGED
package/dist/mjs/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions,
|
|
2
|
+
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNKeyOrNodeOrEntry, BTNPredicate } from '../types';
|
|
3
3
|
export interface IBinaryTree<K = any, V = any, R = [K, V], NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>> {
|
|
4
4
|
createNode(key: K, value?: NODE['value']): NODE;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
|
|
6
6
|
add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
|
|
7
7
|
addMany(nodes: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
|
|
8
|
-
delete
|
|
8
|
+
delete(predicate: R | BTNKeyOrNodeOrEntry<K, V, NODE> | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
|
|
9
9
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
|
+
import { DFSOperation } from "bst-typed";
|
|
3
4
|
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
5
|
export type BinaryTreeNested<K, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
6
|
export type BinaryTreeOptions<K, V, R> = {
|
|
@@ -23,10 +24,7 @@ export type BinaryTreeDeleteResult<NODE> = {
|
|
|
23
24
|
needBalanced: OptBTNOrNull<NODE>;
|
|
24
25
|
};
|
|
25
26
|
export type BTNCallback<NODE, D = any> = (node: NODE) => D;
|
|
26
|
-
export
|
|
27
|
-
VISIT = 0,
|
|
28
|
-
PROCESS = 1
|
|
29
|
-
}
|
|
27
|
+
export type BTNPredicate<NODE> = (node: NODE) => boolean;
|
|
30
28
|
export type DFSStackItem<NODE> = {
|
|
31
29
|
opt: DFSOperation;
|
|
32
30
|
node: OptBTNOrNull<NODE>;
|