1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| type Node struct { Key, Value int Next, Prev *Node }
type LinkedList struct { Head, Tail *Node Len int }
func NewList() LinkedList { head := new(Node) tail := new(Node) head.Next = tail tail.Prev = head return LinkedList{head, tail, 0} }
func (this *LinkedList) AddLast(x *Node) { x.Prev = this.Tail.Prev x.Next = this.Tail
this.Tail.Prev.Next = x this.Tail.Prev = x
this.Len++ }
func (this *LinkedList) Remove(x *Node) { x.Prev.Next = x.Next x.Next.Prev = x.Prev this.Len-- }
func (this *LinkedList) RemoveFirst() *Node { if this.Head.Next == this.Tail { return nil }
x := this.Head.Next this.Remove(x)
return x }
type LinkedHashSet struct { keyToNode map[int]*Node cache LinkedList }
func NewLinkedHashSet() *LinkedHashSet { return &LinkedHashSet{make(map[int]*Node), NewList()} }
func (this *LinkedHashSet) AddRecently(key, val int) { x := &Node{key, val, nil, nil} this.cache.AddLast(x) this.keyToNode[key] = x }
func (this *LinkedHashSet) DeleteKey(key int) { x := this.keyToNode[key] this.cache.Remove(x) delete(this.keyToNode, key) }
func (this *LinkedHashSet) DeleteLeastRecently() *Node { detetedNode := this.cache.RemoveFirst() delete(this.keyToNode, detetedNode.Key) return detetedNode }
func (this *LinkedHashSet) Len() int { return this.cache.Len }
type LFUCache struct { keyToVal map[int]int keyToFreq map[int]int freqToKeys map[int]*LinkedHashSet
minFreq int cap int }
func Constructor(capacity int) LFUCache { keyToVal := make(map[int]int) keyToFreq := make(map[int]int) freqToKeys := make(map[int]*LinkedHashSet)
return LFUCache{keyToVal, keyToFreq, freqToKeys, 0, capacity} }
func (this *LFUCache) Get(key int) int { if _, ok := this.keyToVal[key]; !ok { return -1 } this.increaseFreq(key) return this.keyToVal[key] }
func (this *LFUCache) Put(key int, value int) { if this.cap <= 0 { return }
if _, ok := this.keyToVal[key]; ok { this.keyToVal[key] = value this.increaseFreq(key) return }
if this.cap <= len(this.keyToVal) { this.removeMinFreqKey() }
this.keyToVal[key] = value this.keyToFreq[key] = 1 if _, ok := this.freqToKeys[1]; !ok { this.freqToKeys[1] = NewLinkedHashSet() } this.freqToKeys[1].AddRecently(key, value) this.minFreq = 1 }
func (this *LFUCache) removeMinFreqKey() { keyList := this.freqToKeys[this.minFreq]
deletedNode := keyList.DeleteLeastRecently() if keyList.Len() == 0 { delete(this.freqToKeys, this.minFreq) }
delete(this.keyToVal, deletedNode.Key) delete(this.keyToFreq, deletedNode.Key) }
func (this *LFUCache) increaseFreq(key int) { freq := this.keyToFreq[key] this.keyToFreq[key] = freq + 1
if _, ok := this.freqToKeys[freq + 1]; !ok { this.freqToKeys[freq + 1] = NewLinkedHashSet() } this.freqToKeys[freq + 1].AddRecently(key, this.keyToVal[key])
this.freqToKeys[freq].DeleteKey(key) if this.freqToKeys[freq].Len() == 0 { delete(this.freqToKeys, freq) if freq == this.minFreq { this.minFreq++ } } }
|