[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 28
  • Last Modified:

Create a List<sometype> of a certain size

Hi, I am writing to ask if anyone knows of an existing .Net framework 4.5 class that allows a list of a certain size, and when the size is exceeded, it just drops off/loses one off the end keeping the most recent items.

For example

  SomeListClass<int> slc = new SomeListClass<int>(3);  //can contain only 3 items
  slc.Add(1);     //list contains 1
  slc.Add(2);     //list contains 1,2
  slc.Add(3);     //list contains 1, 2, 3
  slc.Add(4);     //list contains 2, 3, 4
  slc.Add(100);//list contains 3, 4, 100

Open in new window


I could easily roll my own, but I don't want to do this if it is already provided by .Net
0
John Bolter
Asked:
John Bolter
3 Solutions
 
Jacques Bourgeois (James Burger)Commented:
The Queue<T> generic class enables you to easily remove the oldest element in the collection, but won't automatically do it for you.

You will either have to check if it is "full" each time you enqueue (add) something, or inherit from it and see if it is possible to build the mechanism Inside of you own class. Unfortunately, most of its methods are not overridable (virtual), so it might not be a straightforward thing to do.
0
 
käµfm³d 👽Commented:
I'm 99% sure that you'll have to roll your own.
0
 
Fernando SotoCommented:
Hi John;

Yes as the others have said there is no collection that will do that automatically. As James has stated you could use a Queue object and modify it to fit your needs. Below is a class which Inheritance from the queue Generic class which will do as you stated in your question.

using System.Collections.Generic;

namespace YourNameSpace
{
    class QueueMaxSize<T> : Queue<T>
    {
    	  // The maxium size of your queue
        public int MaxSize { get; set; }

        // Default constructor to set default size
        public QueueMaxSize()
        {
            // Set a default size
            MaxSize = 10;
        }

        // Constructor to accept the max size of the queue
        public QueueMaxSize(int maxsize)
        {
            MaxSize = maxsize;
        }

        // This hides the base classes Enqueue method to implement
        // some modification in the process of adding to the queue
        public new void Enqueue(T item)
        {
            // If the queue has reached the max size remove the first entry
            if(this.Count >= MaxSize)
                this.Dequeue();

            // Add the new item to the queue
            base.Enqueue(item);
        }
    }
}

Open in new window

Then in your code you could do the following.

QueueMaxSize<int> slc = new QueueMaxSize<int>(3);  //can contain only 3 items

private void button4_Click(object sender, EventArgs e)
{
    slc.Enqueue(1);     //list contains 1
    slc.Enqueue(2);     //list contains 1,2
    slc.Enqueue(3);     //list contains 1, 2, 3
    slc.Enqueue(4);     //list contains 2, 3, 4
    slc.Enqueue(100);   //list contains 3, 4, 100
}

Open in new window

0
 
käµfm³d 👽Commented:
Aww... no fair! He said he'd roll his own. Well, since I was bored, I thought I'd throw out an old-skool approach using arrays.

public class CircularBuffer<T> : IEnumerable<T>
{
    private T[] buffer;
    private int currentHead;

    public CircularBuffer(int size)
    {
        if (size <= 0) throw new ArgumentException("Buffer size must be at least 1.");

        this.buffer = new T[size];
        this.currentHead = 0;
    }

    public void Add(T item)
    {
        this.buffer[this.currentHead++] = item;

        this.currentHead = this.currentHead % this.buffer.Length;   // Wrap around if necessary
    }

    public IEnumerator<T> GetEnumerator()
    {
        for (int i = this.currentHead; i < this.buffer.Length; i++)
        {
            yield return this.buffer[i];
        }

        for (int i = 0; i < this.currentHead; i++)
        {
            yield return this.buffer[i];
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public T this[int index]
    {
        get
        {
            return this.buffer[(this.currentHead + index) % this.buffer.Length];
        }

        set
        {
            this.buffer[(this.currentHead + index) % this.buffer.Length] = value;
        }
    }
}

Open in new window


You can index and iterate the above data structure. I didn't do exhaustive testing, so there could be bugs.
0
 
John BolterAuthor Commented:
Thanks everyone. This has all been so helpful.
I have a lot to learn !!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now