UNPKG

max-heap-typed

Version:
184 lines (183 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IterableEntryBase = void 0; /** * Iterable view over key-value entries. * @template K - Key type. * @template V - Value type. * @remarks Time O(1), Space O(1) */ class IterableEntryBase { /** * Default iterator yielding `[key, value]` entries. * @returns Iterator of `[K, V]`. * @remarks Time O(n) to iterate, Space O(1) */ *[Symbol.iterator](...args) { yield* this._getIterator(...args); } /** * Iterate over `[key, value]` pairs (may yield `undefined` values). * @returns Iterator of `[K, V | undefined]`. * @remarks Time O(n), Space O(1) */ *entries() { for (const item of this) { yield item; } } /** * Iterate over keys only. * @returns Iterator of keys. * @remarks Time O(n), Space O(1) */ *keys() { for (const item of this) { yield item[0]; } } /** * Iterate over values only. * @returns Iterator of values. * @remarks Time O(n), Space O(1) */ *values() { for (const item of this) { yield item[1]; } } /** * Test whether all entries satisfy the predicate. * @param predicate - `(key, value, index, self) => boolean`. * @param thisArg - Optional `this` for callback. * @returns `true` if all pass; otherwise `false`. * @remarks Time O(n), Space O(1) */ every(predicate, thisArg) { let index = 0; for (const item of this) { if (!predicate.call(thisArg, item[0], item[1], index++, this)) { return false; } } return true; } /** * Test whether any entry satisfies the predicate. * @param predicate - `(key, value, index, self) => boolean`. * @param thisArg - Optional `this` for callback. * @returns `true` if any passes; otherwise `false`. * @remarks Time O(n), Space O(1) */ some(predicate, thisArg) { let index = 0; for (const item of this) { if (predicate.call(thisArg, item[0], item[1], index++, this)) { return true; } } return false; } /** * Visit each entry, left-to-right. * @param callbackfn - `(key, value, index, self) => void`. * @param thisArg - Optional `this` for callback. * @remarks Time O(n), Space O(1) */ forEach(callbackfn, thisArg) { let index = 0; for (const item of this) { const [key, value] = item; callbackfn.call(thisArg, key, value, index++, this); } } /** * Find the first entry that matches a predicate. * @param callbackfn - `(key, value, index, self) => boolean`. * @param thisArg - Optional `this` for callback. * @returns Matching `[key, value]` or `undefined`. * @remarks Time O(n), Space O(1) */ find(callbackfn, thisArg) { let index = 0; for (const item of this) { const [key, value] = item; if (callbackfn.call(thisArg, key, value, index++, this)) return item; } return; } /** * Whether the given key exists. * @param key - Key to test. * @returns `true` if found; otherwise `false`. * @remarks Time O(n) generic, Space O(1) */ has(key) { for (const item of this) { const [itemKey] = item; if (itemKey === key) return true; } return false; } /** * Whether there exists an entry with the given value. * @param value - Value to test. * @returns `true` if found; otherwise `false`. * @remarks Time O(n), Space O(1) */ hasValue(value) { for (const [, elementValue] of this) { if (elementValue === value) return true; } return false; } /** * Get the value under a key. * @param key - Key to look up. * @returns Value or `undefined`. * @remarks Time O(n) generic, Space O(1) */ get(key) { for (const item of this) { const [itemKey, value] = item; if (itemKey === key) return value; } return; } /** * Reduce entries into a single accumulator. * @param callbackfn - `(acc, value, key, index, self) => acc`. * @param initialValue - Initial accumulator. * @returns Final accumulator. * @remarks Time O(n), Space O(1) */ reduce(callbackfn, initialValue) { let accumulator = initialValue; let index = 0; for (const item of this) { const [key, value] = item; accumulator = callbackfn(accumulator, value, key, index++, this); } return accumulator; } /** * Visualize the iterable as an array of `[key, value]` pairs (or a custom string). * @returns Array of entries (default) or a string. * @remarks Time O(n), Space O(n) */ toVisual() { return [...this]; } /** * Print a human-friendly representation to the console. * @remarks Time O(n), Space O(n) */ print() { console.log(this.toVisual()); } } exports.IterableEntryBase = IterableEntryBase;