@itwin/core-bentley
Version:
Bentley JavaScript core components
130 lines • 5.03 kB
TypeScript
/** @packageDocumentation
* @module Collections
*/
import { OrderedComparator } from "./Compare";
/**
* Derived from:
* Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
* See README.md at https://github.com/rsms/js-lru for details.
*/
/** An entry holds the key and value, and pointers to any older and newer entries.
* @public
*/
export declare class Entry<K, V> {
key: K;
value: V;
newer?: Entry<K, V>;
older?: Entry<K, V>;
constructor(key: K, value: V);
}
/** The interface that must be satisfied by the underlying container type used by a LRUCache.
* Compatible with a [[Dictionary]] or a standard Map.
* @public
*/
export interface EntryContainer<K, V> {
readonly size: number;
clear(): void;
get(key: K): Entry<K, V> | undefined;
set(key: K, value: Entry<K, V>): void;
has(key: K): boolean;
delete(key: K): void;
}
/**
* A mapping of a key/value pairs, where the size of the cache can be limited.
*
* When entries are inserted, if the cache is "full", the
* least-recently-used (LRU) value is dropped. When entries are retrieved, they are moved to the front of the LRU list.
*
* Illustration of the design:
*
* ```
*
* entry entry entry entry
* ______ ______ ______ ______
* | head |.newer => | |.newer => | |.newer => | tail |
* | A | | B | | C | | D |
* |______| <= older.|______| <= older.|______| <= older.|______|
*
* removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
* ```
* @public
*/
export declare class LRUCache<K, V> {
private _container;
/** Current number of items */
size: number;
/** Maximum number of items this cache can hold */
limit: number;
/** Least recently-used entry. Invalidated when cache is modified. */
oldest?: Entry<K, V>;
/** Most recently-used entry. Invalidated when cache is modified. */
newest?: Entry<K, V>;
/**
* Construct a new LRUCache to hold up to `limit` entries.
*/
constructor(limit: number, container: EntryContainer<K, V>);
private markEntryAsUsed;
/** Replace all values in this cache with key-value pairs (2-element Arrays) from provided iterable. */
assign(entries: Iterable<[K, V]>): void;
/** Get and register recent use of <key>.
* Returns the value associated with <key> or undefined if not in cache.
*/
get(key: K): V | undefined;
/** Put <value> into the cache associated with <key>. Replaces any existing entry with the same key.
* @returns `this`.
*/
set(key: K, value: V): LRUCache<K, V>;
/** Purge the least recently used (oldest) entry from the cache.
* @returns The removed entry or undefined if the cache was empty.
*/
shift(): [K, V] | undefined;
/** Access value for `key` without registering recent use. Useful if you do not
* want to change the state of the cache, but only "peek" at it.
* @returns The value associated with `key` if found, or undefined if not found.
*/
find(key: K): V | undefined;
/** Check if there's a value for key in the cache without registering recent use. */
has(key: K): boolean;
/** Remove entry `key` from cache and return its value.
* @returns The removed value, or undefined if not found.
*/
delete(key: K): V | undefined;
/** Removes all entries */
clear(): void;
/** Returns an iterator over all keys, starting with the oldest. */
keys(): Iterator<K | undefined> | undefined;
/** Returns an iterator over all values, starting with the oldest. */
values(): Iterator<V | undefined> | undefined;
/** Returns an iterator over all entries, starting with the oldest. */
entries(): Iterator<[K, V] | undefined> | undefined;
/** Call `fun` for each entry, starting with the oldest entry. */
forEach(fun: (value: V, key: K, m: LRUCache<K, V>) => void, thisObj?: any): void;
/** Returns a JSON (array) representation */
toJSON(): Array<{
key: K;
value: V;
}>;
/** Returns a String representation */
toString(): string;
}
/** A [[LRUCache]] using a standard Map as its internal storage.
* @public
*/
export declare class LRUMap<K, V> extends LRUCache<K, V> {
/**
* Construct a new LRUMap to hold up to `limit` entries.
*/
constructor(limit: number);
}
/** A [[LRUCache]] using a [[Dictionary]] as its internal storage, permitting custom key comparison logic.
* @public
*/
export declare class LRUDictionary<K, V> extends LRUCache<K, V> {
/**
* Construct a new LRUDictionary to hold up to `limit` entries.
* @param limit The maximum number of entries permitted in the dictionary.
* @param compareKeys The function used to compare keys within the dictionary.
*/
constructor(limit: number, compareKeys: OrderedComparator<K>);
}
//# sourceMappingURL=LRUMap.d.ts.map