Beispiel #1
0
        public void put(int key, int value)
        {
            var cur = new LN(key);

            if ((!dict.ContainsKey(key) && dict.Count < limit))
            {
                InsetAtFront(cur);
            }
            else if (!dict.ContainsKey(key) && dict.Count >= limit)
            {
                var v = back.next.val;
                dict.Remove(v);
                back.next      = back.next.next;
                back.next.prev = back;
                InsetAtFront(cur);
            }

            else if (dict.ContainsKey(key))
            {
                MoveNodeTOFront(key);
                dict[key] = value;
                return;
            }
            if (dict.ContainsKey(key))
            {
                dict[key] = value;
            }
            else
            {
                dict.Add(key, value);
            }
        }
Beispiel #2
0
 public LRUCache(int capacity)
 {
     front      = new LN(-1);
     back       = new LN(-1);
     front.prev = back;
     back.next  = front;
     limit      = capacity;
     dict       = new Dictionary <int, int>();
 }
Beispiel #3
0
 private void InsetAtFront(LN cur)
 {
     if (front.prev.val == -1)
     {
         cur.prev   = front.prev;
         cur.next   = front;
         back.next  = cur;
         front.prev = cur;
     }
     else
     {
         front.prev.next = cur;
         cur.prev        = front.prev;
         cur.next        = front;
         front.prev      = cur;
     }
 }