Created
April 20, 2017 16:54
-
-
Save twalk4821/c5f2a3d34d5f7d8f97d02fa5659d69ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const LFUcache = (capacity) => { | |
let box = {} | |
box.cache = {} | |
for (var i = 0; i<capacity; i++) { | |
box[i] = []; | |
} | |
box.put = function (k, v) { | |
if (box.cache.keys.length <capacity) { | |
if (!(k in cache)) { | |
var item = [k,v, 0] | |
this[0].push(item); | |
this.cache[k] = item; | |
} | |
} else { | |
let LFU = box[0].splice(box[0].length-1) | |
delete box[LFU[0]] | |
} | |
} | |
box.get = function(k) { | |
let item = box.cache[k] | |
if (item) { | |
box[item[2]].splice(box[item[2]].indexOf(item)) | |
item[2] += 1; | |
box[item[2]].unshift(item) | |
return item | |
} else { | |
return -1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment