import { type LimitedCollectionOrder } from '../map/map.limit'; import { type Maybe } from '../value/maybe.type'; /** * Called with each value a size-limited set evicts, after it was deleted. */ export type LimitedSetEvictFunction = (value: T) => void; /** * Configuration for {@link limitSet}. */ export interface LimitSetConfig { /** * The most values the set keeps. A limit below 0 empties it. */ readonly limit: number; /** * Called with each evicted value, oldest first, after it was deleted. */ readonly onEvict?: Maybe>; } /** * Trims a set to its limit, ONCE: deletes its oldest values, in iteration order, until `size <= limit`. * * The one-shot sibling of {@link limitArray}, and the eviction loop {@link LimitedSet} runs after every insert. * * @param set - The set to trim, in place. * @param config - The limit, and the callback that receives each evicted value. * @returns The same set. * * @dbxUtil * @dbxUtilCategory set * @dbxUtilKind function * @dbxUtilTags set, limit, cap, trim, evict, truncate, size, oldest * @dbxUtilRelated limited-set, limit-map, limit-array * * @example * ```ts * const set = new Set(['a', 'b', 'c']); * limitSet(set, { limit: 2 }); // Set { 'b', 'c' } * ``` */ export declare function limitSet = Set>(set: S, config: LimitSetConfig): S; /** * Configuration for a {@link LimitedSet}. */ export interface LimitedSetConfig extends LimitSetConfig { /** * The most values the set keeps. Must be a positive integer. */ readonly limit: number; /** * Which value is the oldest. Defaults to {@link DEFAULT_LIMITED_COLLECTION_ORDER} (`'recent'`, least-recently-used). */ readonly order?: Maybe; } /** * A `Set` that never holds more than `limit` values: an add that takes it past its limit evicts the oldest value. * * With `order: 'recent'` (the default) a re-`add` of an existing value moves it to the end (least-recently-used). With * `order: 'insertion'` nothing refreshes a value (FIFO). `has` never changes the order. * * Only `add` is overridden; `delete`, `clear`, `size`, `forEach` and the iterators are the native ones. * * @dbxUtil * @dbxUtilCategory set * @dbxUtilKind class * @dbxUtilTags set, limit, cap, lru, fifo, evict, bounded, size, seen * @dbxUtilRelated limit-set, limited-map * * @example * ```ts * const seen = new LimitedSet({ limit: 2 }); * seen.add('a').add('b'); * seen.add('a'); // refreshes 'a' * seen.add('c'); // evicts 'b', the least recently added * ``` */ export declare class LimitedSet extends Set { /** * The most values the set keeps. */ readonly limit: number; /** * Which value is the oldest. */ readonly order: LimitedCollectionOrder; private readonly _onEvict; /** * @param config - The limit, the order and the eviction callback. * @param values - Initial values, added in order — the set is trimmed as they go. * @throws {RangeError} When the limit is not a positive integer. */ constructor(config: LimitedSetConfig, values?: Maybe>); /** * Adds a value, then evicts the oldest values past the limit. Under `'recent'` order an existing value becomes the * most recent. * * @param value - The value to add. * @returns This set. */ add(value: T): this; }