CircularBuffer IDictionary in C #?

I need a CirculeBuffer IDictionary. Can anyone point me to a good open source implementation.

Thus, an IDictionary that has a maximum capacity, for example, is set to 100 elements, which, when element 101 is added, the original first element is extracted / removed from the dictionary, ensuring that the number of elements never exceeds 100.

thanks

+3
source share
6 answers

To save the installation of O (1) (with the oldest element removed for 100) and search for O (1), you need a class that implements IDictionary and saves an internal ordered list. If memory is more worrying, a BST implementation, for example, might be more appropriate SortedList. In any case, your class will contain both T[], and Dictionary<T,K>(or SortedList<T,K>). Make your own ring buffer indexing (easy) and save both collections in add, delete, etc. methods. You will have:

  • O (1) enqueue (back)
  • O (n), which violates the order of addition (since you must update the array); you'll probably never need it anyway
  • O (1) dequeue (front)
  • O (1) or O (log n) keyword search

IDictionary<T,K> IDictionary, , .

: ? , , :

  • ( , , - )
  • : Count , this[key]. , , , . , .
  • : , , .

, , , . , O (1) .

+12

- , . - , , , , . , Add, , . , -, , - , -. -, .

+3

# 3.0 CodePlex. BCL System.Collections.

, Dictionary<TKey, TValue> List<T>.

+2

:

    public class CircularDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        private Queue<TKey> m_ItemInsertList = new Queue<TKey>();
        private int m_ItemsToHold = 100;

        public int ItemsToHold
        {
            get { return m_ItemsToHold; }
            set {
                if (value < 1)
                    throw new ArgumentException("Items to hold must be at least 1");
                m_ItemsToHold = value; 
            }
        }

        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            if (m_ItemInsertList.Count == m_ItemsToHold)
                base.Remove(m_ItemInsertList.Dequeue());
            m_ItemInsertList.Enqueue(key);
        }
    }
0

- , SQLite System.Data.Sqlite(http://sqlite.phxsoftware.com/), . , . .

, , 100 , , . SQLite " ", . , , IDictionary, , .

0

Source: https://habr.com/ru/post/1714523/


All Articles