Skip to content

Instantly share code, notes, and snippets.

@PeteGabriel
Created June 10, 2018 22:07
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 PeteGabriel/9a213f883357896b0b6007c007bd3668 to your computer and use it in GitHub Desktop.
Save PeteGabriel/9a213f883357896b0b6007c007bd3668 to your computer and use it in GitHub Desktop.
Access operation
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