Example #1
0
    public void Put(int key, int value)
    {
        if (capacity == 0)
        {
            return;
        }
        Node node;

        if (nodeDict.ContainsKey(key))
        {
            node       = nodeDict[key];
            node.value = value;
            Update(node);
        }
        else
        {
            node = new Node(key, value);
            nodeDict.Add(key, node);
            if (size == capacity)
            {
                DLList lastList = countDict[min];
                nodeDict.Remove(lastList.RemoveLast().key);
                size--;
            }
            size++;
            min = 1;

            DLList newList = null;
            if (countDict.ContainsKey(node.cnt))
            {
                newList = countDict[node.cnt];
            }
            else
            {
                newList = new DLList();
            }

            newList.Add(node);
            if (countDict.ContainsKey(node.cnt))
            {
                countDict[node.cnt] = newList;
            }
            else
            {
                countDict.Add(node.cnt, newList);
            }
        }
    }