|
1 | 1 | export class Queue<T extends defined> { |
2 | | - private readonly cache: Map<number, T>; // cause roblox-ts doesnt let u set specific array elements |
3 | 2 | private first = 0; |
4 | 3 | private last: number; |
5 | 4 |
|
6 | | - public constructor(cache: T[] = []) { |
7 | | - this.cache = cache as never; |
| 5 | + public constructor(private readonly cache: T[] = []) { |
8 | 6 | this.last = cache.size(); |
9 | 7 | } |
10 | 8 |
|
11 | 9 | public enqueue(value: T): void { |
12 | | - const last = this.last++; |
13 | | - this.cache.set(last + 1, value); |
| 10 | + this.cache[this.last++] = value; |
14 | 11 | } |
15 | 12 |
|
16 | 13 | public dequeue(): T | undefined { |
17 | 14 | if (this.isEmpty()) return; |
18 | 15 |
|
19 | 16 | const first = this.first++; |
20 | | - const value = this.cache.get(first + 1); |
21 | | - this.cache.delete(first); |
| 17 | + const value = this.cache[first]; |
| 18 | + this.cache[first] = undefined!; |
22 | 19 |
|
23 | 20 | return value; |
24 | 21 | } |
25 | 22 |
|
26 | 23 | public toArray(): T[] { |
27 | | - const result: T[] = []; |
28 | | - for (let i = this.first; i < this.last; i++) { |
29 | | - const value = this.cache.get(i + 1); |
30 | | - if (value === undefined) continue; |
31 | | - result.push(value); |
32 | | - } |
33 | | - |
34 | | - return result; |
| 24 | + return this.cache.filterUndefined(); |
35 | 25 | } |
36 | 26 |
|
37 | 27 | public at(index: number): T | undefined { |
38 | 28 | if (index < 0 || index >= this.size()) return; |
39 | | - return this.cache.get(this.first + index + 1); |
| 29 | + return this.cache[this.first + index]; |
40 | 30 | } |
41 | 31 |
|
42 | 32 | public isEmpty(): boolean { |
|
0 commit comments