package main
import "container/list"
type Solution struct {
cap int
lst *list.List
cache map[int]*list.Element
}
type entry struct {
key,value int
}
func Constructor(capacity int) Solution {
return Solution{
capacity,
list.New(),
map[int]*list.Element{},
}
}
func (this *Solution) get(key int) int {
e := this.cache[key]
if e == nil {
return -1
}
this.lst.MoveToFront(e)
return e.Value.(entry).value
}
func (this *Solution) set(key, value int) {
// write code here
e:=this.cache[key]
if e != nil {
e.Value=entry{key,value}
this.lst.MoveToFront(e)
return
}
this.cache[key]=this.lst.PushFront(entry{key,value})
if len(this.cache) > this.cap {
delete(this.cache, this.lst.Remove(this.lst.Back()).(entry).key)
}
}
/**
* Your Solution object will be instantiated and called as such:
* solution := Constructor(capacity);
* output := solution.get(key);
* solution.set(key,value);
*/