aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/utils/queue.ts
blob: 53afb7e1128e6c6cd97bcc4df6e3d62e0c06ceb0 (plain) (blame)
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
export class Queue<T> {
    private _store: T[] = [];

    public pushBack(val: T): void {
        this._store.push(val);
    }

    public pushFront(val: T): void {
        this._store.unshift(val);
    }

    public popFront(): T | undefined {
        return this._store.shift();
    }

    public popBack(): T | undefined {
        if (this._store.length === 0) {
            return undefined;
        }
        const backElement = this._store.splice(-1, 1)[0];
        return backElement;
    }

    public mergeBack(q: Queue<T>): void {
        this._store = this._store.concat(q._store);
    }

    public mergeFront(q: Queue<T>): void {
        this._store = q._store.concat(this._store);
    }

    public getStore(): T[] {
        return this._store;
    }

    public peekFront(): T | undefined {
        return this._store.length >= 0 ? this._store[0] : undefined;
    }
}