Is this algorithm an implementation of LRU or MRU?

I am working on implementing the MRU cache (most commonly used) in my project using C #.

I searched for some concepts and implementations about MRU, and, conversely, LRU (Least Recent Used), and found this article http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=626 that describes the implementation of the collection MRU in C #.

To confuse me, I believe this implementation is an LRU, not an MRU. Can someone help me confirm that this collection class is MRU or not?

The next code block is the entire MRUCollection class. Thank.

class MruDictionary<TKey, TValue>
{
    private LinkedList<MruItem> items;
    private Dictionary<TKey, LinkedListNode<MruItem>> itemIndex;
    private int maxCapacity;
    public MruDictionary(int cap)
    {
        maxCapacity = cap;
        items = new LinkedList<MruItem>();
        itemIndex = new Dictionary<TKey, LinkedListNode<MruItem>>(maxCapacity);
    }
    public void Add(TKey key, TValue value)
    {
        if (itemIndex.ContainsKey(key))
        {
            throw new ArgumentException("An item with the same key already exists.");
        }
        if (itemIndex.Count == maxCapacity)
        {
            LinkedListNode<MruItem> node = items.Last;
            items.RemoveLast(); //Why do we move the last than first here? The node accessed recently is moved to the front of list.
            itemIndex.Remove(node.Value.Key);
        }
        LinkedListNode<MruItem> newNode = new LinkedListNode<MruItem>(new MruItem(key, value));
        items.AddFirst(newNode);
        itemIndex.Add(key, newNode);
    }
    public bool TryGetValue(TKey key, out TValue value)
    {
        LinkedListNode<MruItem> node;
        if (itemIndex.TryGetValue(key, out node))
        {
            value = node.Value.Value;
            items.Remove(node);
            items.AddFirst(node);
            return true;
        }
        value = default(TValue);
        return false;
    }
}

class MruItem
{
    private TKey _key;
    private TValue _value;
    public MruItem(TKey k, TValue v)
    {
        _key = key;
        _value = v;
    }
    public TKey Key
    {
        get { return _key; }
    }
    public TValue Value
    {
        get { return _value; }
    }
}

</" > http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used
(MRU): , LRU, .

, , node , , node , .

+3
2

, MRU. , , , node, . Add() node AddFirst(), TryGetValue() node .

+2

, : http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used

LRU. , items "" .

"". , items.AddFirst(newNode);, . "", , :

items.Remove(node);
items.AddFirst(node);

, "" / " " , items.RemoveLast();

" " , .

+1

All Articles