UNPKG

@figliolia/data-structures

Version:

Efficient data structures for every day programming

53 lines (52 loc) 1.25 kB
import { QuickList } from "./QuickList.js"; /** * 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 * ``` */ export class QuickQueue extends 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; } } }