|
@@ -1,25 +1,25 @@
|
|
|
-type biNode struct {
|
|
|
+type listNode struct {
|
|
|
key int
|
|
|
val int
|
|
|
cnt int
|
|
|
- prev *biNode
|
|
|
- next *biNode
|
|
|
+ prev *listNode
|
|
|
+ next *listNode
|
|
|
}
|
|
|
|
|
|
-type biList struct {
|
|
|
- head *biNode
|
|
|
- tail *biNode
|
|
|
+type linkedList struct {
|
|
|
+ head *listNode
|
|
|
+ tail *listNode
|
|
|
}
|
|
|
|
|
|
-func newBiList() *biList {
|
|
|
- head, tail := &biNode{}, &biNode{}
|
|
|
+func newLinkedList() *linkedList {
|
|
|
+ head, tail := &listNode{}, &listNode{}
|
|
|
head.next = tail
|
|
|
tail.prev = head
|
|
|
- li := &biList{head, tail}
|
|
|
+ li := &linkedList{head, tail}
|
|
|
return li
|
|
|
}
|
|
|
|
|
|
-func (li *biList) remove(node *biNode) {
|
|
|
+func (li *linkedList) remove(node *listNode) {
|
|
|
node.prev.next = node.next
|
|
|
node.next.prev = node.prev
|
|
|
}
|
|
@@ -28,15 +28,15 @@ type LFUCache struct {
|
|
|
capacity int
|
|
|
size int
|
|
|
min int
|
|
|
- cache map[int]*biNode
|
|
|
- freq map[int]*biList
|
|
|
+ cache map[int]*listNode
|
|
|
+ freq map[int]*linkedList
|
|
|
}
|
|
|
|
|
|
func Constructor(capacity int) LFUCache {
|
|
|
var lfu LFUCache
|
|
|
lfu.capacity = capacity
|
|
|
- lfu.cache = make(map[int]*biNode)
|
|
|
- lfu.freq = make(map[int]*biList)
|
|
|
+ lfu.cache = make(map[int]*listNode)
|
|
|
+ lfu.freq = make(map[int]*linkedList)
|
|
|
return lfu
|
|
|
}
|
|
|
|
|
@@ -66,17 +66,17 @@ func (this *LFUCache) Put(key int, value int) {
|
|
|
}
|
|
|
this.size--
|
|
|
} // If the cache is full, remove the last element in the least freq list.
|
|
|
- newNode := &biNode{key, value, 1, nil, nil}
|
|
|
+ newNode := &listNode{key, value, 1, nil, nil}
|
|
|
this.insert(newNode)
|
|
|
this.cache[key] = newNode
|
|
|
this.min = 1
|
|
|
this.size++ // Then, insert new node.
|
|
|
}
|
|
|
|
|
|
-func (this *LFUCache) insert(node *biNode) {
|
|
|
+func (this *LFUCache) insert(node *listNode) {
|
|
|
li := this.freq[node.cnt]
|
|
|
if li == nil {
|
|
|
- li = newBiList()
|
|
|
+ li = newLinkedList()
|
|
|
this.freq[node.cnt] = li
|
|
|
}
|
|
|
node.next = li.head.next
|
|
@@ -85,7 +85,7 @@ func (this *LFUCache) insert(node *biNode) {
|
|
|
node.next.prev = node
|
|
|
}
|
|
|
|
|
|
-func (this *LFUCache) touch(node *biNode) {
|
|
|
+func (this *LFUCache) touch(node *listNode) {
|
|
|
li := this.freq[node.cnt]
|
|
|
li.remove(node)
|
|
|
if li.head.next == li.tail {
|