12

Is there any way to limit the size of a generic collection?

I have a Stack of WriteableBitmap which I am using to store a clone of a WriteableBitmap on each change, meaning that I can undo easily by simply Popping the most recent WriteableBitmap off the stack.

The problem is the memory usage, I want to limit this stack to hold 10 objects, but I can't see a property allowing me to easily do this. Is there a way, or am I going to have to check the stack size on each change, and copy the last 10 objects into a new stack whenever I hit 10, and on each subsequent change? I know how to do this, but was hoping that there was an easier way, is there?

JMK
  • 27,273
  • 52
  • 163
  • 280
  • 4
    You can create your own collection (e.g derive from an existing one) and you can override the add method and implement your size checking logic there... – nemesv Dec 31 '12 at 12:01

4 Answers4

25

To elaborate on Tilak's answer here is some example code:

  public class LimitedSizeStack<T> : LinkedList<T>
  {
    private readonly int _maxSize;
    public LimitedSizeStack(int maxSize)
    {
      _maxSize = maxSize;
    }

    public void Push(T item)
    {
      this.AddFirst(item);

      if(this.Count > _maxSize)
        this.RemoveLast();
    }

    public T Pop()
    {
      var item = this.First.Value;
      this.RemoveFirst();
      return item;
    }
  }
Oliver
  • 35,233
  • 12
  • 66
  • 78
  • Would not recommend using inheritance in this case. Original methods still exist and through their use, the maximum size may be exceeded. – Palec Oct 02 '20 at 09:59
10

You have to implement your own wrapper to achieve that. There is no direct option available.

class FixedSizeStack : Stack
{
    private int MaxNumber;
    public FixedSizeStack(int Limit)
        : base()
    {
        MaxNumber = Limit;
    }

    public override void Push(object obj)
    {
        if (this.Count < MaxNumber)
            base.Push(obj);
    }

}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • That is the initial capacity I think, it increases as you need it to, which I am trying to avoid! – JMK Dec 31 '12 at 12:03
  • 1
    Would not recommend using inheritance in this case. This answer violates LSP, it changes the contract for the Push method. Using composition is fine though, just needs writing more code to expose the members of Stack required to be present in FixedSizeStack. – Palec Oct 02 '20 at 10:02
5

You can use LinkedList which represents doubly link list to simulate Circular Stack.

You can you AddFirst() corresponding to Push(). If Count is 10, you can use RemoveLast().

For Pop() you can use RemoveFirst()

Tilak
  • 30,108
  • 19
  • 83
  • 131
1

You will have to check the size (You will get an exception I believe if you set a limit, and don't check if it's full).

Edit

You don't get an exception if the size has been set, but the size just increases so you do have to check the size (via http://msdn.microsoft.com/en-us/library/6335ax0f.aspx):-

If Count already equals the capacity, the capacity of the Stack is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

Ross Dargan
  • 5,876
  • 4
  • 40
  • 53