|
| 1 | +public class LFUCache { |
| 2 | + Dictionary<int ,(int,int)> keyValueFrequencyPairs; |
| 3 | + FrequencyMaintaner frequencyMaintaner; |
| 4 | + int capacity; |
| 5 | + int Count; |
| 6 | + |
| 7 | + |
| 8 | + public LFUCache(int capacity) { |
| 9 | + Count = 0; |
| 10 | + this.capacity = capacity; |
| 11 | + frequencyMaintaner = new FrequencyMaintaner(); |
| 12 | + keyValueFrequencyPairs = new Dictionary<int ,(int,int)>(); |
| 13 | + |
| 14 | + } |
| 15 | + |
| 16 | + public int Get(int key) { |
| 17 | + if (keyValueFrequencyPairs.ContainsKey(key)){ |
| 18 | + int value; |
| 19 | + int freq; |
| 20 | + (value,freq) = keyValueFrequencyPairs[key]; |
| 21 | + frequencyMaintaner.Incrament(key,freq); |
| 22 | + keyValueFrequencyPairs[key] = (value,freq+1); |
| 23 | + return value; |
| 24 | + } |
| 25 | + return -1; |
| 26 | + |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + public void Put(int key, int value) { |
| 31 | + if (keyValueFrequencyPairs.ContainsKey(key)){ |
| 32 | + int oldValue; |
| 33 | + int freq; |
| 34 | + (oldValue,freq) = keyValueFrequencyPairs[key]; |
| 35 | + frequencyMaintaner.Incrament(key,freq); |
| 36 | + keyValueFrequencyPairs[key] = (value,freq+1); |
| 37 | + } |
| 38 | + else{ |
| 39 | + if (capacity>Count){ |
| 40 | + keyValueFrequencyPairs.Add(key,(value,1)); |
| 41 | + frequencyMaintaner.Add(key); |
| 42 | + Count++; |
| 43 | + } |
| 44 | + else{ |
| 45 | + int depKey = frequencyMaintaner.popLowestKey(); |
| 46 | + keyValueFrequencyPairs.Remove(depKey); |
| 47 | + keyValueFrequencyPairs.Add(key,(value,1)); |
| 48 | + frequencyMaintaner.Add(key); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | +} |
| 54 | +public class FrequencyMaintaner{ |
| 55 | + Dictionary<int ,SpecialSet> FrequencyKeySetPairs ; |
| 56 | + int LowestFreq; |
| 57 | + Dictionary<int, Node> nodeMap; |
| 58 | + |
| 59 | + public FrequencyMaintaner(){ |
| 60 | + FrequencyKeySetPairs = new Dictionary<int , SpecialSet>(); |
| 61 | + LowestFreq = 0; |
| 62 | + nodeMap = new Dictionary<int , Node>(); |
| 63 | + } |
| 64 | + |
| 65 | + //adds the (new) key to the 1th frequency |
| 66 | + internal void Add(int key) |
| 67 | + { |
| 68 | + if (!nodeMap.ContainsKey(key)) |
| 69 | + { |
| 70 | + nodeMap.Add(key, new Node(key)); |
| 71 | + } |
| 72 | + Node KeyNode = nodeMap[key]; |
| 73 | + LowestFreq = 1; |
| 74 | + if (FrequencyKeySetPairs.ContainsKey(1)) |
| 75 | + { |
| 76 | + FrequencyKeySetPairs[1].add(KeyNode); |
| 77 | + } |
| 78 | + else{ |
| 79 | + FrequencyKeySetPairs.Add(1, new SpecialSet()); |
| 80 | + FrequencyKeySetPairs[1].add(KeyNode); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // given the current frequency of the key it removes it from the current special set and adds it to the next |
| 85 | + internal void Incrament(int key, int freq) |
| 86 | + { |
| 87 | + Node KeyNode = nodeMap[key]; |
| 88 | + FrequencyKeySetPairs[freq].remove(KeyNode); |
| 89 | + if(FrequencyKeySetPairs[freq].count() ==0){ |
| 90 | + FrequencyKeySetPairs.Remove(freq); |
| 91 | + if (LowestFreq == freq){ |
| 92 | + LowestFreq ++; |
| 93 | + } |
| 94 | + } |
| 95 | + if (FrequencyKeySetPairs.ContainsKey(freq+1)){ |
| 96 | + FrequencyKeySetPairs[freq+1].add(KeyNode); |
| 97 | + } |
| 98 | + else{ |
| 99 | + FrequencyKeySetPairs.Add(freq+1,new SpecialSet()); |
| 100 | + FrequencyKeySetPairs[freq+1].add(KeyNode); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //finds the lowest frequency specialset and pops the oldest value a temporal assumption |
| 105 | + // is made that the next thing that will be called is add on a new value to fix the lowest frequency |
| 106 | + internal int popLowestKey() |
| 107 | + { |
| 108 | + int key = FrequencyKeySetPairs[LowestFreq].popOldest(); |
| 109 | + Node KeyNode = nodeMap[key]; |
| 110 | + FrequencyKeySetPairs[LowestFreq].remove(KeyNode); |
| 111 | + nodeMap.Remove(key); |
| 112 | + if (FrequencyKeySetPairs[LowestFreq].count()==0){FrequencyKeySetPairs.Remove(LowestFreq); } |
| 113 | + return key; |
| 114 | + } |
| 115 | +} |
| 116 | +// spcialSet that uses a LinkedList to maintain O(1) for insertion deletion and removing the first to be inserted |
| 117 | +// deletion is done by letting refrences of nodes delete themselves |
| 118 | +public class SpecialSet{ |
| 119 | + public int Count; |
| 120 | + public Node left; |
| 121 | + public Node right; |
| 122 | + public SpecialSet(){ |
| 123 | + left = new Node(0); |
| 124 | + right = new Node(0); |
| 125 | + left.next = right; |
| 126 | + right.prev = left; |
| 127 | + |
| 128 | + } |
| 129 | + public void add (Node val) |
| 130 | + { |
| 131 | + val.AddSelfAfter(left); |
| 132 | + Count++; |
| 133 | + } |
| 134 | + |
| 135 | + public void remove (Node val) |
| 136 | + { |
| 137 | + val.removeSelf(); |
| 138 | + Count--; |
| 139 | + } |
| 140 | + public int popOldest(){ |
| 141 | + Node toPop = right.prev; |
| 142 | + toPop.removeSelf(); |
| 143 | + return toPop.Key; |
| 144 | + |
| 145 | + } |
| 146 | + public int count ()=> Count; |
| 147 | + |
| 148 | +} |
| 149 | +public class Node { |
| 150 | + public int Key; |
| 151 | + public Node next; |
| 152 | + public Node prev; |
| 153 | + public Node(int Key){ |
| 154 | + this.Key = Key; |
| 155 | + } |
| 156 | + |
| 157 | + internal void AddSelfAfter(Node val) |
| 158 | + { |
| 159 | + Node next = val.next; |
| 160 | + val.next = this; |
| 161 | + this.prev = val; |
| 162 | + this.next = next; |
| 163 | + next.prev = this; |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + internal void removeSelf() |
| 168 | + { |
| 169 | + this.prev.next = this.next; |
| 170 | + this.next.prev = this.prev; |
| 171 | + } |
| 172 | +} |
0 commit comments