Created
June 10, 2018 22:07
-
-
Save PeteGabriel/9a213f883357896b0b6007c007bd3668 to your computer and use it in GitHub Desktop.
Access operation
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
def access(key: UUID): Int = { | |
val tmp = this.byKey(key) | |
if (tmp == null) { | |
throw new Exception("No such key") | |
} | |
val freq = tmp.parent | |
var nextFreq = freq.next | |
if (nextFreq == this.frequencyHead || nextFreq.value != (freq.value + 1)) { | |
nextFreq = LfuCache.getNewNode(freq, nextFreq, freq.value + 1) | |
} | |
nextFreq.items += this.byKey(key) | |
tmp.parent = nextFreq | |
nextFreq.prev.items -= nextFreq.prev.items.filter(freq => freq.key == key).head | |
if (nextFreq.prev.items.isEmpty){ | |
this.deleteNode(freq) | |
} | |
tmp.data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment