deque-typed 1.19.3 → 1.19.5
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/README.md +10 -204
- package/dist/index.d.ts +8 -1
- package/dist/index.js +12 -15
- package/package.json +2 -2
- package/tsconfig.json +6 -4
- package/dist/deque.d.ts +0 -120
- package/dist/deque.js +0 -277
package/README.md
CHANGED
|
@@ -1,180 +1,30 @@
|
|
|
1
1
|
# What
|
|
2
|
-
|
|
3
2
|
## Brief
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
|
|
7
|
-
|
|
8
|
-
## Algorithms list only a few out, you can discover more in API docs
|
|
3
|
+
This is a standalone Deque data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete [data-structure-typed](https://www.npmjs.com/package/data-structure-typed) package
|
|
9
4
|
|
|
10
|
-
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
11
|
-
|
|
12
|
-
## Code design
|
|
13
|
-
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
14
5
|
|
|
15
6
|
# How
|
|
16
7
|
|
|
17
8
|
## install
|
|
18
|
-
###
|
|
19
|
-
|
|
9
|
+
### npm
|
|
20
10
|
```bash
|
|
21
|
-
|
|
11
|
+
npm i deque-typed
|
|
22
12
|
```
|
|
23
|
-
|
|
24
|
-
### npm
|
|
25
|
-
|
|
13
|
+
### yarn
|
|
26
14
|
```bash
|
|
27
|
-
|
|
15
|
+
yarn add deque-typed
|
|
28
16
|
```
|
|
29
17
|
|
|
30
|
-
###
|
|
31
|
-
|
|
18
|
+
### snippet
|
|
32
19
|
#### TS
|
|
33
20
|
```typescript
|
|
34
|
-
import {BST, BSTNode} from 'data-structure-typed';
|
|
35
|
-
|
|
36
|
-
const bst = new BST();
|
|
37
|
-
bst.add(11);
|
|
38
|
-
bst.add(3);
|
|
39
|
-
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
40
|
-
bst.size === 16; // true
|
|
41
|
-
bst.has(6); // true
|
|
42
|
-
const node6 = bst.get(6);
|
|
43
|
-
bst.getHeight(6) === 2; // true
|
|
44
|
-
bst.getHeight() === 5; // true
|
|
45
|
-
bst.getDepth(6) === 3; // true
|
|
46
|
-
const leftMost = bst.getLeftMost();
|
|
47
|
-
leftMost?.id === 1; // true
|
|
48
|
-
expect(leftMost?.id).toBe(1);
|
|
49
|
-
bst.remove(6);
|
|
50
|
-
bst.get(6); // null
|
|
51
|
-
bst.isAVLBalanced(); // true or false
|
|
52
|
-
const bfsIDs = bst.BFS();
|
|
53
|
-
bfsIDs[0] === 11; // true
|
|
54
|
-
expect(bfsIDs[0]).toBe(11);
|
|
55
|
-
|
|
56
|
-
const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
|
|
57
|
-
objBST.add(11, {id: 11, keyA: 11});
|
|
58
|
-
objBST.add(3, {id: 3, keyA: 3});
|
|
59
|
-
|
|
60
|
-
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
61
|
-
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
62
|
-
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
63
|
-
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
64
|
-
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
65
|
-
|
|
66
|
-
objBST.remove(11);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const avlTree = new AVLTree();
|
|
70
|
-
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
71
|
-
avlTree.isAVLBalanced(); // true
|
|
72
|
-
avlTree.remove(10);
|
|
73
|
-
avlTree.isAVLBalanced(); // true
|
|
74
21
|
|
|
75
22
|
```
|
|
76
23
|
#### JS
|
|
77
24
|
```javascript
|
|
78
|
-
const {BST, BSTNode} = require('data-structure-typed');
|
|
79
|
-
|
|
80
|
-
const bst = new BST();
|
|
81
|
-
bst.add(11);
|
|
82
|
-
bst.add(3);
|
|
83
|
-
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
84
|
-
bst.size === 16; // true
|
|
85
|
-
bst.has(6); // true
|
|
86
|
-
const node6 = bst.get(6);
|
|
87
|
-
bst.getHeight(6) === 2; // true
|
|
88
|
-
bst.getHeight() === 5; // true
|
|
89
|
-
bst.getDepth(6) === 3; // true
|
|
90
|
-
const leftMost = bst.getLeftMost();
|
|
91
|
-
leftMost?.id === 1; // true
|
|
92
|
-
expect(leftMost?.id).toBe(1);
|
|
93
|
-
bst.remove(6);
|
|
94
|
-
bst.get(6); // null
|
|
95
|
-
bst.isAVLBalanced(); // true or false
|
|
96
|
-
const bfsIDs = bst.BFS();
|
|
97
|
-
bfsIDs[0] === 11; // true
|
|
98
|
-
expect(bfsIDs[0]).toBe(11);
|
|
99
|
-
|
|
100
|
-
const objBST = new BST();
|
|
101
|
-
objBST.add(11, {id: 11, keyA: 11});
|
|
102
|
-
objBST.add(3, {id: 3, keyA: 3});
|
|
103
|
-
|
|
104
|
-
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
105
|
-
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
106
|
-
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
107
|
-
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
108
|
-
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
109
|
-
|
|
110
|
-
objBST.remove(11);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const avlTree = new AVLTree();
|
|
114
|
-
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
115
|
-
avlTree.isAVLBalanced(); // true
|
|
116
|
-
avlTree.remove(10);
|
|
117
|
-
avlTree.isAVLBalanced(); // true
|
|
118
|
-
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Directed Graph simple snippet
|
|
122
25
|
|
|
123
|
-
#### TS or JS
|
|
124
|
-
```typescript
|
|
125
|
-
import {DirectedGraph} from 'data-structure-typed';
|
|
126
|
-
|
|
127
|
-
const graph = new DirectedGraph();
|
|
128
|
-
|
|
129
|
-
graph.addVertex('A');
|
|
130
|
-
graph.addVertex('B');
|
|
131
|
-
|
|
132
|
-
graph.hasVertex('A'); // true
|
|
133
|
-
graph.hasVertex('B'); // true
|
|
134
|
-
graph.hasVertex('C'); // false
|
|
135
|
-
|
|
136
|
-
graph.addEdge('A', 'B');
|
|
137
|
-
graph.hasEdge('A', 'B'); // true
|
|
138
|
-
graph.hasEdge('B', 'A'); // false
|
|
139
|
-
|
|
140
|
-
graph.removeEdgeSrcToDest('A', 'B');
|
|
141
|
-
graph.hasEdge('A', 'B'); // false
|
|
142
|
-
|
|
143
|
-
graph.addVertex('C');
|
|
144
|
-
|
|
145
|
-
graph.addEdge('A', 'B');
|
|
146
|
-
graph.addEdge('B', 'C');
|
|
147
|
-
|
|
148
|
-
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Undirected Graph snippet
|
|
152
|
-
|
|
153
|
-
#### TS or JS
|
|
154
|
-
```typescript
|
|
155
|
-
import {UndirectedGraph} from 'data-structure-typed';
|
|
156
|
-
|
|
157
|
-
const graph = new UndirectedGraph();
|
|
158
|
-
graph.addVertex('A');
|
|
159
|
-
graph.addVertex('B');
|
|
160
|
-
graph.addVertex('C');
|
|
161
|
-
graph.addVertex('D');
|
|
162
|
-
graph.removeVertex('C');
|
|
163
|
-
graph.addEdge('A', 'B');
|
|
164
|
-
graph.addEdge('B', 'D');
|
|
165
|
-
|
|
166
|
-
const dijkstraResult = graph.dijkstra('A');
|
|
167
|
-
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
168
26
|
```
|
|
169
27
|
|
|
170
|
-

|
|
171
|
-
|
|
172
|
-

|
|
173
|
-
|
|
174
|
-

|
|
175
|
-
|
|
176
|
-

|
|
177
|
-
|
|
178
28
|
|
|
179
29
|
## API docs & Examples
|
|
180
30
|
|
|
@@ -182,10 +32,6 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
182
32
|
|
|
183
33
|
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
184
34
|
|
|
185
|
-
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
186
|
-
|
|
187
|
-
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
188
|
-
|
|
189
35
|
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
190
36
|
|
|
191
37
|
## Data Structures
|
|
@@ -203,10 +49,10 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
203
49
|
<tbody>
|
|
204
50
|
<tr>
|
|
205
51
|
<td>Binary Tree</td>
|
|
206
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
207
|
-
</
|
|
208
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
209
|
-
</
|
|
52
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
|
|
53
|
+
</td>
|
|
54
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
|
|
55
|
+
</td>
|
|
210
56
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
|
|
211
57
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
212
58
|
</tr>
|
|
@@ -653,46 +499,6 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
653
499
|
|
|
654
500
|

|
|
655
501
|
|
|
656
|
-
[//]: # ()
|
|
657
|
-
|
|
658
|
-
[//]: # ()
|
|
659
|
-
[//]: # ()
|
|
660
|
-
|
|
661
|
-
[//]: # ()
|
|
662
|
-
[//]: # ()
|
|
663
|
-
|
|
664
|
-
[//]: # ()
|
|
665
|
-
[//]: # ()
|
|
666
|
-
|
|
667
|
-
[//]: # ()
|
|
668
|
-
[//]: # ()
|
|
669
|
-
|
|
670
|
-
[//]: # ()
|
|
671
|
-
[//]: # ()
|
|
672
|
-
|
|
673
|
-
[//]: # ()
|
|
674
|
-
[//]: # ()
|
|
675
|
-
|
|
676
|
-
[//]: # ()
|
|
677
|
-
[//]: # ()
|
|
678
|
-
|
|
679
|
-
[//]: # ()
|
|
680
|
-
[//]: # ()
|
|
681
|
-
|
|
682
|
-
[//]: # ()
|
|
683
|
-
[//]: # ()
|
|
684
|
-
|
|
685
|
-
[//]: # ()
|
|
686
|
-
[//]: # ()
|
|
687
|
-
|
|
688
|
-
[//]: # ()
|
|
689
|
-
[//]: # ()
|
|
690
|
-
|
|
691
|
-
[//]: # ()
|
|
692
|
-
|
|
693
|
-
[//]: # ()
|
|
694
|
-
|
|
695
|
-
[//]: # ()
|
|
696
502
|
|
|
697
503
|
|
|
698
504
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.ArrayDeque = exports.ObjectDeque = exports.Deque = 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
|
+
var data_structure_typed_1 = require("data-structure-typed");
|
|
12
|
+
Object.defineProperty(exports, "Deque", { enumerable: true, get: function () { return data_structure_typed_1.Deque; } });
|
|
13
|
+
Object.defineProperty(exports, "ObjectDeque", { enumerable: true, get: function () { return data_structure_typed_1.ObjectDeque; } });
|
|
14
|
+
Object.defineProperty(exports, "ArrayDeque", { enumerable: true, get: function () { return data_structure_typed_1.ArrayDeque; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deque-typed",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.5",
|
|
4
4
|
"description": "Deque. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"typescript": "^4.9.5"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"data-structure-typed": "^1.19.
|
|
64
|
+
"data-structure-typed": "^1.19.5",
|
|
65
65
|
"zod": "^3.22.2"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/tsconfig.json
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
"declaration": true,
|
|
4
4
|
"outDir": "./dist",
|
|
5
5
|
"module": "commonjs",
|
|
6
|
-
"target": "
|
|
6
|
+
"target": "es6",
|
|
7
7
|
"lib": [
|
|
8
|
-
"
|
|
8
|
+
// "es2015",
|
|
9
|
+
"esnext"
|
|
9
10
|
],
|
|
10
11
|
"strict": true,
|
|
11
12
|
"esModuleInterop": true,
|
|
@@ -30,8 +31,9 @@
|
|
|
30
31
|
"src",
|
|
31
32
|
],
|
|
32
33
|
"exclude": [
|
|
33
|
-
// "node_modules/data-structure-typed",
|
|
34
|
+
// "node_modules/data-structure-typed",
|
|
34
35
|
"node_modules",
|
|
35
36
|
"dist"
|
|
36
37
|
]
|
|
37
|
-
}
|
|
38
|
+
}
|
|
39
|
+
|
package/dist/deque.d.ts
DELETED
|
@@ -1,120 +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
|
-
import { DoublyLinkedList } from 'data-structure-typed';
|
|
9
|
-
export declare class Deque<T> extends DoublyLinkedList<T> {
|
|
10
|
-
}
|
|
11
|
-
export declare class ObjectDeque<T = number> {
|
|
12
|
-
constructor(capacity?: number);
|
|
13
|
-
private _nodes;
|
|
14
|
-
get nodes(): {
|
|
15
|
-
[p: number]: T;
|
|
16
|
-
};
|
|
17
|
-
private _capacity;
|
|
18
|
-
get capacity(): number;
|
|
19
|
-
set capacity(value: number);
|
|
20
|
-
private _first;
|
|
21
|
-
get first(): number;
|
|
22
|
-
set first(value: number);
|
|
23
|
-
private _last;
|
|
24
|
-
get last(): number;
|
|
25
|
-
set last(value: number);
|
|
26
|
-
private _size;
|
|
27
|
-
get size(): number;
|
|
28
|
-
addFirst(value: T): void;
|
|
29
|
-
addLast(value: T): void;
|
|
30
|
-
pollFirst(): T | undefined;
|
|
31
|
-
peekFirst(): T | undefined;
|
|
32
|
-
pollLast(): T | undefined;
|
|
33
|
-
peekLast(): T | undefined;
|
|
34
|
-
get(index: number): NonNullable<T> | null;
|
|
35
|
-
isEmpty(): boolean;
|
|
36
|
-
protected _seNodes(value: {
|
|
37
|
-
[p: number]: T;
|
|
38
|
-
}): void;
|
|
39
|
-
protected _setSize(value: number): void;
|
|
40
|
-
}
|
|
41
|
-
export declare class ArrayDeque<T> {
|
|
42
|
-
protected _nodes: T[];
|
|
43
|
-
get size(): number;
|
|
44
|
-
/**
|
|
45
|
-
* The function "addLast" adds a value to the end of an array.
|
|
46
|
-
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
47
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
48
|
-
*/
|
|
49
|
-
addLast(value: T): number;
|
|
50
|
-
/**
|
|
51
|
-
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
52
|
-
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
53
|
-
*/
|
|
54
|
-
pollLast(): T | null;
|
|
55
|
-
/**
|
|
56
|
-
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
57
|
-
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
58
|
-
* empty.
|
|
59
|
-
*/
|
|
60
|
-
pollFirst(): T | null;
|
|
61
|
-
/**
|
|
62
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
63
|
-
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
64
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
65
|
-
* `value` at the beginning.
|
|
66
|
-
*/
|
|
67
|
-
addFirst(value: T): number;
|
|
68
|
-
/**
|
|
69
|
-
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
70
|
-
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
71
|
-
* empty, it will return `null`.
|
|
72
|
-
*/
|
|
73
|
-
peekFirst(): T | null;
|
|
74
|
-
/**
|
|
75
|
-
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
76
|
-
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
77
|
-
*/
|
|
78
|
-
peekLast(): T | null;
|
|
79
|
-
/**
|
|
80
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
81
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
82
|
-
* retrieve from the array.
|
|
83
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
84
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
85
|
-
*/
|
|
86
|
-
get(index: number): T | null;
|
|
87
|
-
/**
|
|
88
|
-
* The set function assigns a value to a specific index in an array.
|
|
89
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
90
|
-
* that you want to set a new value for.
|
|
91
|
-
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
92
|
-
* _nodes array.
|
|
93
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
94
|
-
*/
|
|
95
|
-
set(index: number, value: T): T;
|
|
96
|
-
/**
|
|
97
|
-
* The insert function adds a value at a specified index in an array.
|
|
98
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
99
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
100
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
101
|
-
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
102
|
-
* index.
|
|
103
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
104
|
-
* are being removed, an empty array will be returned.
|
|
105
|
-
*/
|
|
106
|
-
insert(index: number, value: T): T[];
|
|
107
|
-
/**
|
|
108
|
-
* The remove function removes an element from an array at a specified index.
|
|
109
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
110
|
-
* is a number that represents the index of the element to be removed.
|
|
111
|
-
* @returns The method is returning an array containing the removed element.
|
|
112
|
-
*/
|
|
113
|
-
remove(index: number): T[];
|
|
114
|
-
/**
|
|
115
|
-
* The function checks if an array called "_nodes" is empty.
|
|
116
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
117
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
118
|
-
*/
|
|
119
|
-
isEmpty(): boolean;
|
|
120
|
-
}
|
package/dist/deque.js
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
|
|
19
|
-
/**
|
|
20
|
-
* data-structure-typed
|
|
21
|
-
*
|
|
22
|
-
* @author Tyler Zeng
|
|
23
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
24
|
-
* @license MIT License
|
|
25
|
-
*/
|
|
26
|
-
var data_structure_typed_1 = require("data-structure-typed");
|
|
27
|
-
// O(n) time complexity of obtaining the value
|
|
28
|
-
// O(1) time complexity of adding at the beginning and the end
|
|
29
|
-
var Deque = /** @class */ (function (_super) {
|
|
30
|
-
__extends(Deque, _super);
|
|
31
|
-
function Deque() {
|
|
32
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
33
|
-
}
|
|
34
|
-
return Deque;
|
|
35
|
-
}(data_structure_typed_1.DoublyLinkedList));
|
|
36
|
-
exports.Deque = Deque;
|
|
37
|
-
// O(1) time complexity of obtaining the value
|
|
38
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
39
|
-
// todo tested slowest one
|
|
40
|
-
var ObjectDeque = /** @class */ (function () {
|
|
41
|
-
function ObjectDeque(capacity) {
|
|
42
|
-
this._nodes = {};
|
|
43
|
-
this._capacity = Number.MAX_SAFE_INTEGER;
|
|
44
|
-
this._first = -1;
|
|
45
|
-
this._last = -1;
|
|
46
|
-
this._size = 0;
|
|
47
|
-
if (capacity !== undefined)
|
|
48
|
-
this._capacity = capacity;
|
|
49
|
-
}
|
|
50
|
-
Object.defineProperty(ObjectDeque.prototype, "nodes", {
|
|
51
|
-
get: function () {
|
|
52
|
-
return this._nodes;
|
|
53
|
-
},
|
|
54
|
-
enumerable: false,
|
|
55
|
-
configurable: true
|
|
56
|
-
});
|
|
57
|
-
Object.defineProperty(ObjectDeque.prototype, "capacity", {
|
|
58
|
-
get: function () {
|
|
59
|
-
return this._capacity;
|
|
60
|
-
},
|
|
61
|
-
set: function (value) {
|
|
62
|
-
this._capacity = value;
|
|
63
|
-
},
|
|
64
|
-
enumerable: false,
|
|
65
|
-
configurable: true
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(ObjectDeque.prototype, "first", {
|
|
68
|
-
get: function () {
|
|
69
|
-
return this._first;
|
|
70
|
-
},
|
|
71
|
-
set: function (value) {
|
|
72
|
-
this._first = value;
|
|
73
|
-
},
|
|
74
|
-
enumerable: false,
|
|
75
|
-
configurable: true
|
|
76
|
-
});
|
|
77
|
-
Object.defineProperty(ObjectDeque.prototype, "last", {
|
|
78
|
-
get: function () {
|
|
79
|
-
return this._last;
|
|
80
|
-
},
|
|
81
|
-
set: function (value) {
|
|
82
|
-
this._last = value;
|
|
83
|
-
},
|
|
84
|
-
enumerable: false,
|
|
85
|
-
configurable: true
|
|
86
|
-
});
|
|
87
|
-
Object.defineProperty(ObjectDeque.prototype, "size", {
|
|
88
|
-
get: function () {
|
|
89
|
-
return this._size;
|
|
90
|
-
},
|
|
91
|
-
enumerable: false,
|
|
92
|
-
configurable: true
|
|
93
|
-
});
|
|
94
|
-
ObjectDeque.prototype.addFirst = function (value) {
|
|
95
|
-
if (this._size === 0) {
|
|
96
|
-
var mid = Math.floor(this._capacity / 2);
|
|
97
|
-
this._first = mid;
|
|
98
|
-
this._last = mid;
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
this._first--;
|
|
102
|
-
}
|
|
103
|
-
this._nodes[this._first] = value;
|
|
104
|
-
this._size++;
|
|
105
|
-
};
|
|
106
|
-
ObjectDeque.prototype.addLast = function (value) {
|
|
107
|
-
if (this._size === 0) {
|
|
108
|
-
var mid = Math.floor(this._capacity / 2);
|
|
109
|
-
this._first = mid;
|
|
110
|
-
this._last = mid;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
this._last++;
|
|
114
|
-
}
|
|
115
|
-
this._nodes[this._last] = value;
|
|
116
|
-
this._size++;
|
|
117
|
-
};
|
|
118
|
-
ObjectDeque.prototype.pollFirst = function () {
|
|
119
|
-
if (!this._size)
|
|
120
|
-
return;
|
|
121
|
-
var value = this.peekFirst();
|
|
122
|
-
delete this._nodes[this._first];
|
|
123
|
-
this._first++;
|
|
124
|
-
this._size--;
|
|
125
|
-
return value;
|
|
126
|
-
};
|
|
127
|
-
ObjectDeque.prototype.peekFirst = function () {
|
|
128
|
-
if (this._size)
|
|
129
|
-
return this._nodes[this._first];
|
|
130
|
-
};
|
|
131
|
-
ObjectDeque.prototype.pollLast = function () {
|
|
132
|
-
if (!this._size)
|
|
133
|
-
return;
|
|
134
|
-
var value = this.peekLast();
|
|
135
|
-
delete this._nodes[this._last];
|
|
136
|
-
this._last--;
|
|
137
|
-
this._size--;
|
|
138
|
-
return value;
|
|
139
|
-
};
|
|
140
|
-
ObjectDeque.prototype.peekLast = function () {
|
|
141
|
-
if (this._size)
|
|
142
|
-
return this._nodes[this._last];
|
|
143
|
-
};
|
|
144
|
-
ObjectDeque.prototype.get = function (index) {
|
|
145
|
-
return this._nodes[this._first + index] || null;
|
|
146
|
-
};
|
|
147
|
-
ObjectDeque.prototype.isEmpty = function () {
|
|
148
|
-
return this._size <= 0;
|
|
149
|
-
};
|
|
150
|
-
ObjectDeque.prototype._seNodes = function (value) {
|
|
151
|
-
this._nodes = value;
|
|
152
|
-
};
|
|
153
|
-
ObjectDeque.prototype._setSize = function (value) {
|
|
154
|
-
this._size = value;
|
|
155
|
-
};
|
|
156
|
-
return ObjectDeque;
|
|
157
|
-
}());
|
|
158
|
-
exports.ObjectDeque = ObjectDeque;
|
|
159
|
-
// O(1) time complexity of obtaining the value
|
|
160
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
161
|
-
var ArrayDeque = /** @class */ (function () {
|
|
162
|
-
function ArrayDeque() {
|
|
163
|
-
this._nodes = [];
|
|
164
|
-
}
|
|
165
|
-
Object.defineProperty(ArrayDeque.prototype, "size", {
|
|
166
|
-
get: function () {
|
|
167
|
-
return this._nodes.length;
|
|
168
|
-
},
|
|
169
|
-
enumerable: false,
|
|
170
|
-
configurable: true
|
|
171
|
-
});
|
|
172
|
-
/**
|
|
173
|
-
* The function "addLast" adds a value to the end of an array.
|
|
174
|
-
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
175
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
176
|
-
*/
|
|
177
|
-
ArrayDeque.prototype.addLast = function (value) {
|
|
178
|
-
return this._nodes.push(value);
|
|
179
|
-
};
|
|
180
|
-
/**
|
|
181
|
-
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
182
|
-
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
183
|
-
*/
|
|
184
|
-
ArrayDeque.prototype.pollLast = function () {
|
|
185
|
-
var _a;
|
|
186
|
-
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
190
|
-
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
191
|
-
* empty.
|
|
192
|
-
*/
|
|
193
|
-
ArrayDeque.prototype.pollFirst = function () {
|
|
194
|
-
var _a;
|
|
195
|
-
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
196
|
-
};
|
|
197
|
-
/**
|
|
198
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
199
|
-
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
200
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
201
|
-
* `value` at the beginning.
|
|
202
|
-
*/
|
|
203
|
-
ArrayDeque.prototype.addFirst = function (value) {
|
|
204
|
-
return this._nodes.unshift(value);
|
|
205
|
-
};
|
|
206
|
-
/**
|
|
207
|
-
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
208
|
-
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
209
|
-
* empty, it will return `null`.
|
|
210
|
-
*/
|
|
211
|
-
ArrayDeque.prototype.peekFirst = function () {
|
|
212
|
-
var _a;
|
|
213
|
-
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
214
|
-
};
|
|
215
|
-
/**
|
|
216
|
-
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
217
|
-
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
218
|
-
*/
|
|
219
|
-
ArrayDeque.prototype.peekLast = function () {
|
|
220
|
-
var _a;
|
|
221
|
-
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
222
|
-
};
|
|
223
|
-
/**
|
|
224
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
225
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
226
|
-
* retrieve from the array.
|
|
227
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
228
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
229
|
-
*/
|
|
230
|
-
ArrayDeque.prototype.get = function (index) {
|
|
231
|
-
var _a;
|
|
232
|
-
return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
233
|
-
};
|
|
234
|
-
/**
|
|
235
|
-
* The set function assigns a value to a specific index in an array.
|
|
236
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
237
|
-
* that you want to set a new value for.
|
|
238
|
-
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
239
|
-
* _nodes array.
|
|
240
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
241
|
-
*/
|
|
242
|
-
ArrayDeque.prototype.set = function (index, value) {
|
|
243
|
-
return this._nodes[index] = value;
|
|
244
|
-
};
|
|
245
|
-
/**
|
|
246
|
-
* The insert function adds a value at a specified index in an array.
|
|
247
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
248
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
249
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
250
|
-
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
251
|
-
* index.
|
|
252
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
253
|
-
* are being removed, an empty array will be returned.
|
|
254
|
-
*/
|
|
255
|
-
ArrayDeque.prototype.insert = function (index, value) {
|
|
256
|
-
return this._nodes.splice(index, 0, value);
|
|
257
|
-
};
|
|
258
|
-
/**
|
|
259
|
-
* The remove function removes an element from an array at a specified index.
|
|
260
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
261
|
-
* is a number that represents the index of the element to be removed.
|
|
262
|
-
* @returns The method is returning an array containing the removed element.
|
|
263
|
-
*/
|
|
264
|
-
ArrayDeque.prototype.remove = function (index) {
|
|
265
|
-
return this._nodes.splice(index, 1);
|
|
266
|
-
};
|
|
267
|
-
/**
|
|
268
|
-
* The function checks if an array called "_nodes" is empty.
|
|
269
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
270
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
271
|
-
*/
|
|
272
|
-
ArrayDeque.prototype.isEmpty = function () {
|
|
273
|
-
return this._nodes.length === 0;
|
|
274
|
-
};
|
|
275
|
-
return ArrayDeque;
|
|
276
|
-
}());
|
|
277
|
-
exports.ArrayDeque = ArrayDeque;
|