UNPKG

@figliolia/data-structures

Version:

Efficient data structures for every day programming

75 lines (74 loc) 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuickList = void 0; const event_emitter_1 = require("@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) */ class QuickList { constructor() { this.storage = new Map(); this.IDs = new event_emitter_1.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; } } } exports.QuickList = QuickList;