MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / FrequencyMap

Class FrequencyMap

Cache/LFUCache.js:12–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11// This frequency map class will act like javascript Map DS with more two custom method refresh & insert
12class 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
51class LFUCache {
52 #capacity

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected