Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created July 29, 2020 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalconceptvisuals/a27f0227fffd1e36ed468f8a1ab1e7d8 to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/a27f0227fffd1e36ed468f8a1ab1e7d8 to your computer and use it in GitHub Desktop.
// 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