| 10 | |
| 11 | // This frequency map class will act like javascript Map DS with more two custom method refresh & insert |
| 12 | class FrequencyMap extends Map { |
| 13 | static get [Symbol.species]() { |
| 14 | return Map |
| 15 | } // for using Symbol.species we can access Map constructor @see -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@species |
| 16 | get [Symbol.toStringTag]() { |
| 17 | return '' |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @method refresh |
| 22 | * @description - It's revive a CacheNode, increment of this nodes frequency and refresh the frequencyMap via new incremented nodes frequency |
| 23 | * @param {CacheNode} node |
| 24 | */ |
| 25 | refresh(node) { |
| 26 | const { frequency } = node |
| 27 | const freqSet = this.get(frequency) |
| 28 | freqSet.delete(node) |
| 29 | |
| 30 | node.frequency++ |
| 31 | |
| 32 | this.insert(node) |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @method insert |
| 37 | * @description - Add new CacheNode into HashSet by the frequency |
| 38 | * @param {CacheNode} node |
| 39 | */ |
| 40 | insert(node) { |
| 41 | const { frequency } = node |
| 42 | |
| 43 | if (!this.has(frequency)) { |
| 44 | this.set(frequency, new Set()) |
| 45 | } |
| 46 | |
| 47 | this.get(frequency).add(node) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | class LFUCache { |
| 52 | #capacity |
nothing calls this directly
no outgoing calls
no test coverage detected