-
-
Save digitalconceptvisuals/a27f0227fffd1e36ed468f8a1ab1e7d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We extend the map instead of composing it | |
// So that non-overridden functions are passed to the base | |
class MFUMap extends Map { | |
// Private properties | |
#frequencyMap = new Map(); | |
#maxSize = 10; | |
constructor(size = 10) { | |
super(); | |
this.#maxSize = size; | |
debug(`Initialized map with size=${size}`); | |
} | |
// Check if our map size is going beyond maxSize | |
#resizeMap = () => { | |
// Drop entries from the map | |
} | |
#updateFrequency = key => { | |
// Update frequency of the key here | |
} | |
// Override the get and set function of Map | |
set(key, value) { | |
let result = super.set(key, value); | |
// Update the frequency of the key | |
this.#updateFrequency(key); | |
return result; | |
} | |
get(key) { | |
let exists = super.get(key); | |
// Don't update non existing gets | |
if (exists) | |
this.#updateFrequency(key); | |
return exists; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment