UNPKG

@tensorflow/tfjs-layers

Version:

TensorFlow layers API in JavaScript

69 lines 7.08 kB
/** * @license * Copyright 2022 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * LruCache: A mapping from the String to T. If the number of the entries is * exceeding the `maxEntries`, the LruCache will delete the least recently * used entry. */ export class LruCache { constructor(maxEntries) { this.maxEntries = maxEntries || 100; this.cache = new Map(); } /** * Get the entry for the key and mark it as used recently. */ get(key) { let entry; if (this.cache.has(key)) { entry = this.cache.get(key); this.cache.delete(key); this.cache.set(key, entry); } return entry; } /** * Put the entry into the cache. If the key already existed, mark the key as * used recently. */ put(key, value) { if (this.cache.has(key)) { this.cache.delete(key); } else if (this.cache.size >= this.maxEntries) { const keyToDelete = this.cache.keys().next().value; this.cache.delete(keyToDelete); } this.cache.set(key, value); } /** * Get the MaxEntries of the cache. */ getMaxEntries() { return this.maxEntries; } /** * Set the MaxEntries of the cache. If the maxEntries is decreased, reduce * entries in the cache. */ setMaxEntries(maxEntries) { if (maxEntries < 0) { throw new Error(`The maxEntries of LRU caches must be at least 0, but got ${maxEntries}.`); } if (this.maxEntries > maxEntries) { for (let i = 0; i < this.maxEntries - maxEntries; i++) { const keyToDelete = this.cache.keys().next().value; this.cache.delete(keyToDelete); } } this.maxEntries = maxEntries; } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhlY3V0b3JfdXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi90ZmpzLWxheWVycy9zcmMvdXRpbHMvZXhlY3V0b3JfdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7O0dBUUc7QUFDSDs7OztHQUlHO0FBRUgsTUFBTSxPQUFPLFFBQVE7SUFJbkIsWUFBWSxVQUFtQjtRQUM3QixJQUFJLENBQUMsVUFBV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