| 49 | } |
| 50 | |
| 51 | class LFUCache { |
| 52 | #capacity |
| 53 | #frequencyMap |
| 54 | |
| 55 | /** |
| 56 | * @param {number} capacity - The range of LFUCache |
| 57 | * @returns {LFUCache} - sealed |
| 58 | */ |
| 59 | constructor(capacity) { |
| 60 | this.#capacity = capacity |
| 61 | this.#frequencyMap = new FrequencyMap() |
| 62 | this.misses = 0 |
| 63 | this.hits = 0 |
| 64 | this.cache = new Map() |
| 65 | |
| 66 | return Object.seal(this) |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the capacity of the LFUCache |
| 71 | * @returns {number} |
| 72 | */ |
| 73 | get capacity() { |
| 74 | return this.#capacity |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the current size of LFUCache |
| 79 | * @returns {number} |
| 80 | */ |
| 81 | get size() { |
| 82 | return this.cache.size |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Set the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used |
| 87 | */ |
| 88 | set capacity(newCapacity) { |
| 89 | if (this.#capacity > newCapacity) { |
| 90 | let diff = this.#capacity - newCapacity // get the decrement number of capacity |
| 91 | |
| 92 | while (diff--) { |
| 93 | this.#removeCacheNode() |
| 94 | } |
| 95 | |
| 96 | this.cache.size === 0 && this.#frequencyMap.clear() |
| 97 | } |
| 98 | |
| 99 | this.#capacity = newCapacity |
| 100 | } |
| 101 | |
| 102 | get info() { |
| 103 | return Object.freeze({ |
| 104 | misses: this.misses, |
| 105 | hits: this.hits, |
| 106 | capacity: this.capacity, |
| 107 | currentSize: this.size, |
| 108 | leastFrequency: this.leastFrequency |
nothing calls this directly
no outgoing calls
no test coverage detected