min-heap-typed 1.39.6 → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -33,46 +33,30 @@ export class SkipList<K, V> {
|
|
|
33
33
|
this._probability = probability;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
protected _head: SkipListNode<K, V>;
|
|
37
37
|
|
|
38
38
|
get head(): SkipListNode<K, V> {
|
|
39
39
|
return this._head;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
this._head = value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private _level: number;
|
|
42
|
+
protected _level: number;
|
|
47
43
|
|
|
48
44
|
get level(): number {
|
|
49
45
|
return this._level;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
this._level = value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _maxLevel: number;
|
|
48
|
+
protected _maxLevel: number;
|
|
57
49
|
|
|
58
50
|
get maxLevel(): number {
|
|
59
51
|
return this._maxLevel;
|
|
60
52
|
}
|
|
61
53
|
|
|
62
|
-
|
|
63
|
-
this._maxLevel = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private _probability: number;
|
|
54
|
+
protected _probability: number;
|
|
67
55
|
|
|
68
56
|
get probability(): number {
|
|
69
57
|
return this._probability;
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
set probability(value: number) {
|
|
73
|
-
this._probability = value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
60
|
/**
|
|
77
61
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
78
62
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -80,7 +64,7 @@ export class SkipList<K, V> {
|
|
|
80
64
|
* List.
|
|
81
65
|
*/
|
|
82
66
|
add(key: K, value: V): void {
|
|
83
|
-
const newNode = new SkipListNode(key, value, this.
|
|
67
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
84
68
|
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
|
|
85
69
|
let current = this.head;
|
|
86
70
|
|
|
@@ -97,7 +81,7 @@ export class SkipList<K, V> {
|
|
|
97
81
|
}
|
|
98
82
|
|
|
99
83
|
if (newNode.forward[0] !== null) {
|
|
100
|
-
this.
|
|
84
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
101
85
|
}
|
|
102
86
|
}
|
|
103
87
|
|
|
@@ -124,6 +108,10 @@ export class SkipList<K, V> {
|
|
|
124
108
|
return undefined;
|
|
125
109
|
}
|
|
126
110
|
|
|
111
|
+
has(key: K): boolean {
|
|
112
|
+
return this.get(key) !== undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
127
115
|
/**
|
|
128
116
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
129
117
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -151,7 +139,7 @@ export class SkipList<K, V> {
|
|
|
151
139
|
update[i].forward[i] = current.forward[i];
|
|
152
140
|
}
|
|
153
141
|
while (this.level > 0 && this.head.forward[this.level - 1] === null) {
|
|
154
|
-
this.
|
|
142
|
+
this._level--;
|
|
155
143
|
}
|
|
156
144
|
return true;
|
|
157
145
|
}
|
|
@@ -160,10 +148,70 @@ export class SkipList<K, V> {
|
|
|
160
148
|
}
|
|
161
149
|
|
|
162
150
|
/**
|
|
163
|
-
*
|
|
151
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
152
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
153
|
+
*/
|
|
154
|
+
getFirst(): V | undefined {
|
|
155
|
+
const firstNode = this.head.forward[0];
|
|
156
|
+
return firstNode ? firstNode.value : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
161
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
162
|
+
*/
|
|
163
|
+
getLast(): V | undefined {
|
|
164
|
+
let current = this.head;
|
|
165
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
166
|
+
while (current.forward[i]) {
|
|
167
|
+
current = current.forward[i];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return current.value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
175
|
+
* @param key - the given key.
|
|
176
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
177
|
+
*/
|
|
178
|
+
higher(key: K): V | undefined {
|
|
179
|
+
let current = this.head;
|
|
180
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
181
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
182
|
+
current = current.forward[i];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const nextNode = current.forward[0];
|
|
186
|
+
return nextNode ? nextNode.value : undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
191
|
+
* @param key - the given key.
|
|
192
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
193
|
+
*/
|
|
194
|
+
lower(key: K): V | undefined {
|
|
195
|
+
let current = this.head;
|
|
196
|
+
let lastLess = null;
|
|
197
|
+
|
|
198
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
199
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
200
|
+
current = current.forward[i];
|
|
201
|
+
}
|
|
202
|
+
if (current.key < key) {
|
|
203
|
+
lastLess = current;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return lastLess ? lastLess.value : undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
164
212
|
* @returns the level, which is a number.
|
|
165
213
|
*/
|
|
166
|
-
|
|
214
|
+
protected _randomLevel(): number {
|
|
167
215
|
let level = 1;
|
|
168
216
|
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
169
217
|
level++;
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
// todo need to be improved
|
|
9
9
|
export class MatrixNTI2D<V = any> {
|
|
10
|
-
|
|
10
|
+
protected readonly _matrix: Array<Array<V>>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {Vector2D} from './vector2d';
|
|
9
9
|
|
|
10
10
|
export class Matrix2D {
|
|
11
|
-
|
|
11
|
+
protected readonly _matrix: number[][];
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
@@ -27,10 +27,10 @@ export class Character {
|
|
|
27
27
|
|
|
28
28
|
export class Navigator<T = number> {
|
|
29
29
|
onMove: (cur: [number, number]) => void;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
protected readonly _matrix: T[][];
|
|
31
|
+
protected readonly _cur: [number, number];
|
|
32
|
+
protected _character: Character;
|
|
33
|
+
protected readonly _VISITED: T;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,43 +20,31 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
protected _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
29
30
|
|
|
30
31
|
get capacity(): number {
|
|
31
32
|
return this._capacity;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
this._capacity = value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private _first = -1;
|
|
35
|
+
protected _first = -1;
|
|
39
36
|
|
|
40
37
|
get first(): number {
|
|
41
38
|
return this._first;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
this._first = value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private _last = -1;
|
|
41
|
+
protected _last = -1;
|
|
49
42
|
|
|
50
43
|
get last(): number {
|
|
51
44
|
return this._last;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
|
|
55
|
-
this._last = value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private _size = 0;
|
|
47
|
+
protected _size = 0;
|
|
59
48
|
|
|
60
49
|
get size(): number {
|
|
61
50
|
return this._size;
|
|
@@ -67,14 +56,14 @@ export class ObjectDeque<E = number> {
|
|
|
67
56
|
* structure.
|
|
68
57
|
*/
|
|
69
58
|
addFirst(value: E) {
|
|
70
|
-
if (this.
|
|
71
|
-
const mid = Math.floor(this.
|
|
59
|
+
if (this.size === 0) {
|
|
60
|
+
const mid = Math.floor(this.capacity / 2);
|
|
72
61
|
this._first = mid;
|
|
73
62
|
this._last = mid;
|
|
74
63
|
} else {
|
|
75
64
|
this._first--;
|
|
76
65
|
}
|
|
77
|
-
this.
|
|
66
|
+
this.nodes[this.first] = value;
|
|
78
67
|
this._size++;
|
|
79
68
|
}
|
|
80
69
|
|
|
@@ -83,14 +72,14 @@ export class ObjectDeque<E = number> {
|
|
|
83
72
|
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
84
73
|
*/
|
|
85
74
|
addLast(value: E) {
|
|
86
|
-
if (this.
|
|
87
|
-
const mid = Math.floor(this.
|
|
75
|
+
if (this.size === 0) {
|
|
76
|
+
const mid = Math.floor(this.capacity / 2);
|
|
88
77
|
this._first = mid;
|
|
89
78
|
this._last = mid;
|
|
90
79
|
} else {
|
|
91
80
|
this._last++;
|
|
92
81
|
}
|
|
93
|
-
this.
|
|
82
|
+
this.nodes[this.last] = value;
|
|
94
83
|
this._size++;
|
|
95
84
|
}
|
|
96
85
|
|
|
@@ -99,9 +88,9 @@ export class ObjectDeque<E = number> {
|
|
|
99
88
|
* @returns The value of the first element in the data structure.
|
|
100
89
|
*/
|
|
101
90
|
popFirst() {
|
|
102
|
-
if (!this.
|
|
91
|
+
if (!this.size) return;
|
|
103
92
|
const value = this.getFirst();
|
|
104
|
-
delete this.
|
|
93
|
+
delete this.nodes[this.first];
|
|
105
94
|
this._first++;
|
|
106
95
|
this._size--;
|
|
107
96
|
return value;
|
|
@@ -112,7 +101,7 @@ export class ObjectDeque<E = number> {
|
|
|
112
101
|
* @returns The element at the first position of the `_nodes` array.
|
|
113
102
|
*/
|
|
114
103
|
getFirst() {
|
|
115
|
-
if (this.
|
|
104
|
+
if (this.size) return this.nodes[this.first];
|
|
116
105
|
}
|
|
117
106
|
|
|
118
107
|
/**
|
|
@@ -120,9 +109,9 @@ export class ObjectDeque<E = number> {
|
|
|
120
109
|
* @returns The value that was removed from the data structure.
|
|
121
110
|
*/
|
|
122
111
|
popLast() {
|
|
123
|
-
if (!this.
|
|
112
|
+
if (!this.size) return;
|
|
124
113
|
const value = this.getLast();
|
|
125
|
-
delete this.
|
|
114
|
+
delete this.nodes[this.last];
|
|
126
115
|
this._last--;
|
|
127
116
|
this._size--;
|
|
128
117
|
|
|
@@ -134,7 +123,7 @@ export class ObjectDeque<E = number> {
|
|
|
134
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
135
124
|
*/
|
|
136
125
|
getLast() {
|
|
137
|
-
if (this.
|
|
126
|
+
if (this.size) return this.nodes[this.last];
|
|
138
127
|
}
|
|
139
128
|
|
|
140
129
|
/**
|
|
@@ -145,7 +134,7 @@ export class ObjectDeque<E = number> {
|
|
|
145
134
|
* index, `null` is returned.
|
|
146
135
|
*/
|
|
147
136
|
get(index: number) {
|
|
148
|
-
return this.
|
|
137
|
+
return this.nodes[this.first + index] || null;
|
|
149
138
|
}
|
|
150
139
|
|
|
151
140
|
/**
|
|
@@ -153,15 +142,7 @@ export class ObjectDeque<E = number> {
|
|
|
153
142
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
154
143
|
*/
|
|
155
144
|
isEmpty() {
|
|
156
|
-
return this.
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
-
this._nodes = value;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
protected _setSize(value: number) {
|
|
164
|
-
this._size = value;
|
|
145
|
+
return this.size <= 0;
|
|
165
146
|
}
|
|
166
147
|
}
|
|
167
148
|
|
|
@@ -170,8 +151,12 @@ export class ObjectDeque<E = number> {
|
|
|
170
151
|
export class ArrayDeque<E> {
|
|
171
152
|
protected _nodes: E[] = [];
|
|
172
153
|
|
|
154
|
+
get nodes(): E[] {
|
|
155
|
+
return this._nodes;
|
|
156
|
+
}
|
|
157
|
+
|
|
173
158
|
get size() {
|
|
174
|
-
return this.
|
|
159
|
+
return this.nodes.length;
|
|
175
160
|
}
|
|
176
161
|
|
|
177
162
|
/**
|
|
@@ -184,7 +169,7 @@ export class ArrayDeque<E> {
|
|
|
184
169
|
* @returns The return value is the new length of the array after the value has been added.
|
|
185
170
|
*/
|
|
186
171
|
addLast(value: E) {
|
|
187
|
-
return this.
|
|
172
|
+
return this.nodes.push(value);
|
|
188
173
|
}
|
|
189
174
|
|
|
190
175
|
/**
|
|
@@ -192,7 +177,7 @@ export class ArrayDeque<E> {
|
|
|
192
177
|
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
193
178
|
*/
|
|
194
179
|
popLast(): E | null {
|
|
195
|
-
return this.
|
|
180
|
+
return this.nodes.pop() ?? null;
|
|
196
181
|
}
|
|
197
182
|
|
|
198
183
|
/**
|
|
@@ -201,7 +186,7 @@ export class ArrayDeque<E> {
|
|
|
201
186
|
* empty.
|
|
202
187
|
*/
|
|
203
188
|
popFirst(): E | null {
|
|
204
|
-
return this.
|
|
189
|
+
return this.nodes.shift() ?? null;
|
|
205
190
|
}
|
|
206
191
|
|
|
207
192
|
/**
|
|
@@ -215,7 +200,7 @@ export class ArrayDeque<E> {
|
|
|
215
200
|
* `value` at the beginning.
|
|
216
201
|
*/
|
|
217
202
|
addFirst(value: E) {
|
|
218
|
-
return this.
|
|
203
|
+
return this.nodes.unshift(value);
|
|
219
204
|
}
|
|
220
205
|
|
|
221
206
|
/**
|
|
@@ -224,7 +209,7 @@ export class ArrayDeque<E> {
|
|
|
224
209
|
* empty, it will return `null`.
|
|
225
210
|
*/
|
|
226
211
|
getFirst(): E | null {
|
|
227
|
-
return this.
|
|
212
|
+
return this.nodes[0] ?? null;
|
|
228
213
|
}
|
|
229
214
|
|
|
230
215
|
/**
|
|
@@ -232,7 +217,7 @@ export class ArrayDeque<E> {
|
|
|
232
217
|
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
233
218
|
*/
|
|
234
219
|
getLast(): E | null {
|
|
235
|
-
return this.
|
|
220
|
+
return this.nodes[this.nodes.length - 1] ?? null;
|
|
236
221
|
}
|
|
237
222
|
|
|
238
223
|
/**
|
|
@@ -247,7 +232,7 @@ export class ArrayDeque<E> {
|
|
|
247
232
|
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
248
233
|
*/
|
|
249
234
|
get(index: number): E | null {
|
|
250
|
-
return this.
|
|
235
|
+
return this.nodes[index] ?? null;
|
|
251
236
|
}
|
|
252
237
|
|
|
253
238
|
/**
|
|
@@ -259,7 +244,7 @@ export class ArrayDeque<E> {
|
|
|
259
244
|
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
260
245
|
*/
|
|
261
246
|
set(index: number, value: E) {
|
|
262
|
-
return (this.
|
|
247
|
+
return (this.nodes[index] = value);
|
|
263
248
|
}
|
|
264
249
|
|
|
265
250
|
/**
|
|
@@ -273,7 +258,7 @@ export class ArrayDeque<E> {
|
|
|
273
258
|
* are being removed, an empty array will be returned.
|
|
274
259
|
*/
|
|
275
260
|
insert(index: number, value: E) {
|
|
276
|
-
return this.
|
|
261
|
+
return this.nodes.splice(index, 0, value);
|
|
277
262
|
}
|
|
278
263
|
|
|
279
264
|
/**
|
|
@@ -283,7 +268,7 @@ export class ArrayDeque<E> {
|
|
|
283
268
|
* @returns The method is returning an array containing the removed element.
|
|
284
269
|
*/
|
|
285
270
|
delete(index: number) {
|
|
286
|
-
return this.
|
|
271
|
+
return this.nodes.splice(index, 1);
|
|
287
272
|
}
|
|
288
273
|
|
|
289
274
|
/**
|
|
@@ -292,6 +277,6 @@ export class ArrayDeque<E> {
|
|
|
292
277
|
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
293
278
|
*/
|
|
294
279
|
isEmpty() {
|
|
295
|
-
return this.
|
|
280
|
+
return this.nodes.length === 0;
|
|
296
281
|
}
|
|
297
282
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import {SinglyLinkedList} from '../linked-list';
|
|
7
7
|
|
|
8
|
-
export class
|
|
8
|
+
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
9
|
/**
|
|
10
10
|
* The enqueue function adds a value to the end of an array.
|
|
11
11
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -22,12 +22,20 @@ export class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
22
22
|
return this.shift();
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
+
*/
|
|
29
|
+
getFirst(): E | undefined {
|
|
30
|
+
return this.head?.value;
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
/**
|
|
26
34
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
35
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
36
|
*/
|
|
29
37
|
peek(): E | undefined {
|
|
30
|
-
return this.
|
|
38
|
+
return this.getFirst();
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
|
|
@@ -43,26 +51,18 @@ export class Queue<E = any> {
|
|
|
43
51
|
this._offset = 0;
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
protected _nodes: E[];
|
|
47
55
|
|
|
48
56
|
get nodes(): E[] {
|
|
49
57
|
return this._nodes;
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
this._nodes = value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _offset: number;
|
|
60
|
+
protected _offset: number;
|
|
57
61
|
|
|
58
62
|
get offset(): number {
|
|
59
63
|
return this._offset;
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
set offset(value: number) {
|
|
63
|
-
this._offset = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
66
|
/**
|
|
67
67
|
* The size function returns the number of elements in an array.
|
|
68
68
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -101,25 +101,34 @@ export class Queue<E = any> {
|
|
|
101
101
|
shift(): E | undefined {
|
|
102
102
|
if (this.size === 0) return undefined;
|
|
103
103
|
|
|
104
|
-
const first = this.
|
|
105
|
-
this.
|
|
104
|
+
const first = this.getFirst();
|
|
105
|
+
this._offset += 1;
|
|
106
106
|
|
|
107
107
|
if (this.offset * 2 < this.nodes.length) return first;
|
|
108
108
|
|
|
109
109
|
// only delete dequeued elements when reaching half size
|
|
110
110
|
// to decrease latency of shifting elements.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
111
|
+
this._nodes = this.nodes.slice(this.offset);
|
|
112
|
+
this._offset = 0;
|
|
113
113
|
return first;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
118
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
119
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
120
|
+
*/
|
|
121
|
+
getFirst(): E | undefined {
|
|
122
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
116
125
|
/**
|
|
117
126
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
118
127
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
119
128
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
120
129
|
*/
|
|
121
130
|
peek(): E | undefined {
|
|
122
|
-
return this.
|
|
131
|
+
return this.getFirst();
|
|
123
132
|
}
|
|
124
133
|
|
|
125
134
|
/**
|
|
@@ -131,6 +140,15 @@ export class Queue<E = any> {
|
|
|
131
140
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
132
141
|
}
|
|
133
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
145
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
146
|
+
* array is empty, it returns `null`.
|
|
147
|
+
*/
|
|
148
|
+
peekLast(): E | undefined {
|
|
149
|
+
return this.getLast();
|
|
150
|
+
}
|
|
151
|
+
|
|
134
152
|
/**
|
|
135
153
|
* The enqueue function adds a value to the end of a queue.
|
|
136
154
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -171,8 +189,8 @@ export class Queue<E = any> {
|
|
|
171
189
|
* The clear function resets the nodes array and offset to their initial values.
|
|
172
190
|
*/
|
|
173
191
|
clear(): void {
|
|
174
|
-
this.
|
|
175
|
-
this.
|
|
192
|
+
this._nodes = [];
|
|
193
|
+
this._offset = 0;
|
|
176
194
|
}
|
|
177
195
|
|
|
178
196
|
/**
|
|
@@ -183,7 +201,7 @@ export class Queue<E = any> {
|
|
|
183
201
|
return new Queue(this.nodes.slice(this.offset));
|
|
184
202
|
}
|
|
185
203
|
|
|
186
|
-
*[Symbol.iterator]() {
|
|
204
|
+
* [Symbol.iterator]() {
|
|
187
205
|
for (const item of this.nodes) {
|
|
188
206
|
yield item;
|
|
189
207
|
}
|