Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote an LRU Cache and some test cases, feel free to use it.</p> <p>You can read through the source on <a href="http://samsaffron.com/archive/2009/08/21/Behaviour+driven+design+using+rspec+IronRuby+and+C" rel="noreferrer">my blog</a>.</p> <p>For the lazy (here it is minus the test cases): </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LRUCache { public class IndexedLinkedList&lt;T&gt; { LinkedList&lt;T&gt; data = new LinkedList&lt;T&gt;(); Dictionary&lt;T, LinkedListNode&lt;T&gt;&gt; index = new Dictionary&lt;T, LinkedListNode&lt;T&gt;&gt;(); public void Add(T value) { index[value] = data.AddLast(value); } public void RemoveFirst() { index.Remove(data.First.Value); data.RemoveFirst(); } public void Remove(T value) { LinkedListNode&lt;T&gt; node; if (index.TryGetValue(value, out node)) { data.Remove(node); index.Remove(value); } } public int Count { get { return data.Count; } } public void Clear() { data.Clear(); index.Clear(); } public T First { get { return data.First.Value; } } } } </code></pre> <p>LRUCache</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LRUCache { public class LRUCache&lt;TKey, TValue&gt; : IDictionary&lt;TKey, TValue&gt; { object sync = new object(); Dictionary&lt;TKey, TValue&gt; data; IndexedLinkedList&lt;TKey&gt; lruList = new IndexedLinkedList&lt;TKey&gt;(); ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; dataAsCollection; int capacity; public LRUCache(int capacity) { if (capacity &lt;= 0) { throw new ArgumentException("capacity should always be bigger than 0"); } data = new Dictionary&lt;TKey, TValue&gt;(capacity); dataAsCollection = data; this.capacity = capacity; } public void Add(TKey key, TValue value) { if (!ContainsKey(key)) { this[key] = value; } else { throw new ArgumentException("An attempt was made to insert a duplicate key in the cache."); } } public bool ContainsKey(TKey key) { return data.ContainsKey(key); } public ICollection&lt;TKey&gt; Keys { get { return data.Keys; } } public bool Remove(TKey key) { bool existed = data.Remove(key); lruList.Remove(key); return existed; } public bool TryGetValue(TKey key, out TValue value) { return data.TryGetValue(key, out value); } public ICollection&lt;TValue&gt; Values { get { return data.Values; } } public TValue this[TKey key] { get { var value = data[key]; lruList.Remove(key); lruList.Add(key); return value; } set { data[key] = value; lruList.Remove(key); lruList.Add(key); if (data.Count &gt; capacity) { data.Remove(lruList.First); lruList.RemoveFirst(); } } } public void Add(KeyValuePair&lt;TKey, TValue&gt; item) { Add(item.Key, item.Value); } public void Clear() { data.Clear(); lruList.Clear(); } public bool Contains(KeyValuePair&lt;TKey, TValue&gt; item) { return dataAsCollection.Contains(item); } public void CopyTo(KeyValuePair&lt;TKey, TValue&gt;[] array, int arrayIndex) { dataAsCollection.CopyTo(array, arrayIndex); } public int Count { get { return data.Count; } } public bool IsReadOnly { get { return false; } } public bool Remove(KeyValuePair&lt;TKey, TValue&gt; item) { bool removed = dataAsCollection.Remove(item); if (removed) { lruList.Remove(item.Key); } return removed; } public IEnumerator&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; GetEnumerator() { return dataAsCollection.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return ((System.Collections.IEnumerable)data).GetEnumerator(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload