func(this *LFUCache) Get(key int) int { node, exists := this.cache[key] if !exists { return-1 } node.freq++ node.index = this.index this.index++ heap.Fix(this.pq, node.pos) // for i := range *this.pq { // fmt.Printf("%v|%v|%v,", (*this.pq)[i].key, (*this.pq)[i].freq, (*this.pq)[i].index) // } // fmt.Printf("\n") return node.value }
func(this *LFUCache) Put(key int, value int) { if this.capacity == 0 { return } node, exists := this.cache[key] if exists { node.value = value node.freq++ node.index = this.index this.index++ heap.Fix(this.pq, node.pos) } else { if this.size == this.capacity { node := heap.Pop(this.pq).(*Node) // fmt.Printf("delete %v\n", node.key) delete(this.cache, node.key) this.size-- } node := NewNode(key, value, this.index) this.index++ heap.Push(this.pq, node) this.cache[key] = node this.size++ } // for i := range *this.pq { // fmt.Printf("%v|%v|%v,", (*this.pq)[i].key, (*this.pq)[i].freq, (*this.pq)[i].index) // } // fmt.Printf("\n") }
/** * Your LFUCache object will be instantiated and called as such: * obj := Constructor(capacity); * param_1 := obj.Get(key); * obj.Put(key,value); */