File size: 1,297 Bytes
eee16fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export default class Queue<T = any> {
    private maxSize: number;
    private items: { [key: number]: T };
    private frontPos: number;
    private rearPos: number;
    private length: number;

    constructor(size = Number.MAX_SAFE_INTEGER) {
        this.maxSize = size;
        this.items = {} as { [key: number]: T };
        this.frontPos = 0;
        this.rearPos = -1;
        this.length = 0;
    }

    dequeue(): T | null {
        if (this.isEmpty()) return null;
        const dequeued = this.front();
        delete this.items[(this.frontPos %= this.maxSize)];
        this.frontPos++;
        this.length--;
        return dequeued;
    }

    *dequeueIterator() {
        while (!this.isEmpty()) {
            yield this.dequeue();
        }
    }

    enqueue(item: T) {
        if (this.isFull()) return;
        this.rearPos++;
        this.length++;
        this.items[(this.rearPos %= this.maxSize)] = item;
    }

    isFull() {
        return this.length >= this.maxSize;
    }

    isEmpty() {
        return this.length < 1;
    }

    rear(): T | null {
        return this.items[this.rearPos];
    }

    front() {
        if (this.isEmpty()) return null;
        return this.items[this.frontPos % this.maxSize];
    }

    size() {
        return this.length;
    }
}