UNPKG

@figliolia/data-structures

Version:

Efficient data structures for every day programming

69 lines (68 loc) 1.41 kB
import { AutoIncrementingID } from "@figliolia/event-emitter"; /** * Quick List * * The base construct for `QuickStacks` and `QuickQueues`, * designed to mimick Array-like interfaces while providing * access to and deletions of items at O(1) */ export class QuickList { storage = new Map(); IDs = new AutoIncrementingID(); /** * Push * * Adds a new item onto the stack, returns a unique * ID for the item */ push(item) { const ID = this.IDs.get(); this.storage.set(ID, item); return ID; } /** * Length * * Returns the total number of items on the stack */ get length() { return this.storage.size; } /** * Is Empty * * Returns true if the list contains no items */ get isEmpty() { return this.storage.size === 0; } /** * Get * * Returns an item on the stack by unique ID */ get(ID) { return this.storage.get(ID); } /** * Delete * * Removes an entry from the stack by unique ID */ delete(ID) { return this.storage.delete(ID); } /** * Clear * * Removes all items from the queue */ clear() { return this.storage.clear(); } *[Symbol.iterator]() { for (const [_, value] of this.storage) { yield value; } } }