Skip to content

Instantly share code, notes, and snippets.

@PeteGabriel
Created June 10, 2018 22:04
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/c131ce122a167bbf5034c6116e868bbd to your computer and use it in GitHub Desktop.
Save PeteGabriel/c131ce122a167bbf5034c6116e868bbd to your computer and use it in GitHub Desktop.
Insert operation
def insert(key: UUID, value : Int) = {
if (this.byKey.contains(key)){
throw new Exception("Key already exists")
}
val freqNode = this.frequencyHead.next
if (freqNode.value != 1){
this.frequencyHead.next = LfuCache.getNewNode(this.frequencyHead, freqNode)
this.byKey(key) = LfuItem(this.frequencyHead.next, value, key)
this.frequencyHead.next.items += this.byKey(key)
}else{
this.byKey(key) = LfuItem(freqNode, value, key)
freqNode.items += this.byKey(key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment