Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Dictionary with Behaviour of a Ring List?

I need a ring list dictionary that can store key and item. Capacity = 50 and when I add #51 the first item must be removed. Basically it must be a dictionary that behaves like a ring list.

Is there something in .NET Framework that can do that ? Or do I have to write it by myself ?

like image 843
Bitterblue Avatar asked Aug 06 '13 10:08

Bitterblue


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


3 Answers

You won't find anything built-in I think but you can easily implement one using OrderedDictionary

OrderedDictionary maintains items in order which they are inserted. Whenever you reach the limit/capacity you can remove the first item.

like image 115
Haris Hasan Avatar answered Oct 02 '22 07:10

Haris Hasan


or use an extension method :

EDIT :

because

latest added entry ends up being returned first.

so u can remove the first item like :

dictionary.Remove(dictionary.Last().Key);  

& so your extension method is :

addExtension(this Dictionary<string, object> dictionary, string key, object value)
   {
        if(dictionary.Count == 50)
                dictionary.Remove(dictionary.Last().Key);  

        dictionary.Add(key, value);   
   }
like image 21
Ashok Damani Avatar answered Oct 02 '22 05:10

Ashok Damani


Try this:

class Program
{
    static void Main(string[] args)
    {
        var rD = new RingDictionary(50);
        for (int i = 0; i < 75; i++)
        {
            rD.Add(i, i);
        }
        foreach (var item in rD.Keys)
        {
            Console.WriteLine("{0} {1}", item, rD[item]);
        }
    }
}

class RingDictionary : OrderedDictionary
{
    int indexKey;

    int _capacity = 0;
    public int Capacity
    {
        get { return _capacity; }
        set
        {
            if (value <= 0)
            {
                var errorMessage = typeof(Environment)
                    .GetMethod(
                        "GetResourceString",
                        System.Reflection.BindingFlags.Static |
                        System.Reflection.BindingFlags.NonPublic,
                        null,
                        new Type[] { typeof(string) },
                        null)
                    .Invoke(null, new object[] { 
                        "ArgumentOutOfRange_NegativeCapacity" 
                    }).ToString();
                throw new ArgumentException(errorMessage);
            }
            _capacity = value;
        }
    }

    public RingDictionary(int capacity)
    {
        indexKey = -1;
        Capacity = capacity;
    }

    public new void Add(object key, object value)
    {
        indexKey++;

        if (base.Keys.Count > _capacity)
        {
            for (int i = base.Keys.Count-1; i >Capacity-1 ; i--)
            {
                base.RemoveAt(i);
            }
        }

        if (base.Keys.Count == _capacity)
        {
            base.RemoveAt(indexKey % _capacity);
            base.Insert(indexKey % _capacity, key, value);
        }
        else
        {
            base.Add(key, value);
        }
    }
}
like image 36
Alex Filipovici Avatar answered Oct 02 '22 07:10

Alex Filipovici