private void AddNode(LFUNode node) { if (Head == null) { Head = node; Tail = node; Count++; return; } if (Count == Capacity) { Set.Remove(Tail.Key); if (Head == Tail) { Head = node; Tail = node; return; } Tail.Left.Right = null; Tail = Tail.Left; } else { Count++; } Tail.Right = node; node.Left = Tail; Tail = node; }
public void Put(int key, int value) { if (Capacity <= 0) { return; } if (Set.TryGetValue(key, out LFUNode node)) { node.Value = value; } else { node = new LFUNode(key, value); if (Capacity == 1) { Set.Clear(); node = new LFUNode(key, value); Set.Add(node.Key, node); return; } else { AddNode(node); } Set.Add(node.Key, node); } Touch(node); }
private void Touch(LFUNode node) { node.Frequency++; LFUNode parent = node.Left; if (parent == null || parent.Frequency > node.Frequency) { return; } while (parent != null && parent.Frequency <= node.Frequency) { parent = parent.Left; } //cutoff node from its position node.Left.Right = node.Right; if (node.Right != null) { node.Right.Left = node.Left; } else { Tail = node.Left; } //insert node after parent node.Left = parent; if (parent != null) { node.Right = parent.Right; parent.Right.Left = node; parent.Right = node; } else { node.Right = Head; Head.Left = node; Head = node; } }