forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
131 lines (116 loc) · 3.51 KB
/
queue.js
File metadata and controls
131 lines (116 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* A circular queue that automatically extends its capacity when full.
* This implementation uses a fixed-size array to store elements and
* supports efficient enqueue and dequeue operations.
* It is recommended to use `initialCapacity` that is close to **real-world** usage.
* @template T
*/
class Queue {
/**
* Create a new queue.
* @param {number} [initialCapacity] - The initial capacity of the queue.
*/
constructor(initialCapacity = 8) {
/**
* Underlying storage for the queue.
* @type {Array<T|undefined>}
* @private
*/
this._storage = new Array(initialCapacity);
/**
* The head (front) index.
* @type {number}
* @private
*/
this._head = 0;
/**
* The current number of elements in the queue.
* @type {number}
* @private
*/
this._length = 0;
}
/**
* The current number of elements in the queue.
* @type {number}
* @readonly
*/
get length() {
return this._length;
}
/**
* Change the capacity of the underlying storage.
* Does not shrink capacity if new capacity is less than or equal to the current length.
* @param {number} capacity - The new capacity for the queue.
*/
set capacity(capacity) {
if (capacity <= this._length) {
return;
}
const oldCapacity = this._storage.length;
this._storage.length = capacity;
// Handle wrap-around scenario by moving elements.
if (this._head + this._length > oldCapacity) {
const endLength = oldCapacity - this._head;
for (let i = 0; i < endLength; i++) {
this._storage[capacity - endLength + i] = this._storage[this._head + i];
}
this._head = capacity - endLength;
}
}
/**
* The capacity of the queue.
* @type {number}
* @readonly
*/
get capacity() {
return this._storage.length;
}
/**
* Enqueue (push) a value to the back of the queue.
* Automatically extends capacity if the queue is full.
* @param {T} value - The value to enqueue.
* @returns {number} The new length of the queue.
*/
enqueue(value) {
if (this._length === this._storage.length) {
this.capacity = this._storage.length * 2;
}
const tailIndex = (this._head + this._length) % this._storage.length;
this._storage[tailIndex] = value;
this._length++;
return this._length;
}
/**
* Dequeue (pop) a value from the front of the queue.
* @returns {T|undefined} The dequeued value, or `undefined` if the queue is empty.
*/
dequeue() {
if (this.isEmpty()) {
return undefined;
}
const value = this._storage[this._head];
this._storage[this._head] = undefined;
this._head = (this._head + 1) % this._storage.length;
this._length--;
return value;
}
/**
* Returns the value at the front of the queue without removing it.
* @returns {T|undefined} The front value, or `undefined` if the queue is empty.
*/
peek() {
if (this.isEmpty()) {
return undefined;
}
return this._storage[this._head];
}
/**
* Determines whether the queue is empty.
* @returns {boolean} True if the queue is empty, false otherwise.
*/
isEmpty() {
return this._length === 0;
}
}
export { Queue };