import { type Maybe } from '../value/maybe.type'; /** * Which entry a size-limited collection treats as the OLDEST, and so evicts first. * * - `'recent'` — least-recently-used: a read of a present entry and a re-insert of an existing one move it to the end. * - `'insertion'` — first-in, first-out: nothing refreshes an entry, so the first inserted is the first evicted. */ export type LimitedCollectionOrder = 'recent' | 'insertion'; /** * The {@link LimitedCollectionOrder} a {@link LimitedMap} / `LimitedSet` uses when its config names none. */ export declare const DEFAULT_LIMITED_COLLECTION_ORDER: LimitedCollectionOrder; /** * Asserts a size-limited collection's limit is a positive integer. * * @param limit - The limit to check. * @returns The limit, unchanged. * @throws {RangeError} When the limit is not a positive integer. */ export declare function assertLimitedCollectionLimit(limit: number): number; /** * Called with each entry a size-limited map evicts, after it was deleted. */ export type LimitedMapEvictFunction = (key: K, value: V) => void; /** * Configuration for {@link limitMap}. */ export interface LimitMapConfig { /** * The most entries the map keeps. A limit below 0 empties it. */ readonly limit: number; /** * Called with each evicted entry, oldest first, after it was deleted. */ readonly onEvict?: Maybe>; } /** * Trims a map to its limit, ONCE: deletes its oldest entries, in iteration order, until `size <= limit`. * * The one-shot sibling of {@link limitArray}, and the eviction loop {@link LimitedMap} runs after every insert. * * @param map - The map to trim, in place. * @param config - The limit, and the callback that receives each evicted entry. * @returns The same map. * * @dbxUtil * @dbxUtilCategory map * @dbxUtilKind function * @dbxUtilTags map, limit, cap, trim, evict, truncate, size, oldest * @dbxUtilRelated limited-map, limit-set, limit-array * * @example * ```ts * const map = new Map([['a', 1], ['b', 2], ['c', 3]]); * limitMap(map, { limit: 2 }); // Map { 'b' => 2, 'c' => 3 } * ``` */ export declare function limitMap = Map>(map: M, config: LimitMapConfig): M; /** * Configuration for a {@link LimitedMap}. */ export interface LimitedMapConfig extends LimitMapConfig { /** * The most entries the map keeps. Must be a positive integer. */ readonly limit: number; /** * Which entry is the oldest. Defaults to {@link DEFAULT_LIMITED_COLLECTION_ORDER} (`'recent'`, least-recently-used). */ readonly order?: Maybe; } /** * A `Map` that never holds more than `limit` entries: an insert that takes it past its limit evicts the oldest entry. * * With `order: 'recent'` (the default) it is an LRU cache — {@link LimitedMap.get} of a present key and a re-`set` of an * existing key move it to the end. With `order: 'insertion'` nothing refreshes an entry (FIFO). `has` never changes * the order. * * Only `set` and `get` are overridden; `delete`, `clear`, `size`, `forEach` and the iterators are the native ones. * * @dbxUtil * @dbxUtilCategory map * @dbxUtilKind class * @dbxUtilTags map, limit, cap, lru, cache, fifo, evict, bounded, size * @dbxUtilRelated limit-map, limited-set * * @example * ```ts * const cache = new LimitedMap({ limit: 2 }); * cache.set('a', 1).set('b', 2); * cache.get('a'); // refreshes 'a' * cache.set('c', 3); // evicts 'b', the least recently used * ``` */ export declare class LimitedMap extends Map { /** * The most entries the map keeps. */ readonly limit: number; /** * Which entry is the oldest. */ readonly order: LimitedCollectionOrder; private readonly _onEvict; /** * @param config - The limit, the order and the eviction callback. * @param entries - Initial entries, inserted in order — the map is trimmed as they go. * @throws {RangeError} When the limit is not a positive integer. */ constructor(config: LimitedMapConfig, entries?: Maybe>); /** * Reads a key's value. Under `'recent'` order a present key becomes the most recent. * * @param key - The key to read. * @returns The value, or undefined when the key is absent. */ get(key: K): V | undefined; /** * Sets a key's value, then evicts the oldest entries past the limit. Under `'recent'` order an existing key becomes * the most recent. * * @param key - The key to set. * @param value - Its value. * @returns This map. */ set(key: K, value: V): this; }