UNPKG

@figliolia/data-structures

Version:

Efficient data structures for every day programming

57 lines (56 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuickQueue = void 0; const QuickList_1 = require("./QuickList"); /** * Quick Queue * * A wrapper around the native Map that assigns an * auto-incrementing ID to each value added. It provides * a Queue-like interface with the ability to access and * remove items in 0(1) time * * ```typescript * const queue = new QuickQueue<() => void>(); * const uniqueID = queue.enqueue(() => {}); * * const FN = queue.dequeue() // Remove and return the first item on the queue * const FN = queue.get(uniqueID); // Get an item by ID * queue.delete(uniqueID) // Delete an item by ID * ``` */ class QuickQueue extends QuickList_1.QuickList { /** * Enqueue * * Adds an item to the queue and returns it's * unique ID */ enqueue(item) { return super.push(item); } /** * Dequeue * * Removes the first item from the Bucket and * returns it */ dequeue() { for (const [ID, item] of this.storage) { this.storage.delete(ID); return item; } } /** * Peek * * Returns the first item from the Bucket or undefined * if the queue is empty */ peek() { for (const entry of this.storage) { return entry; } } } exports.QuickQueue = QuickQueue;