Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Limit the depth of a Generic Stack?

Tags:

stack

c#

generics

Is there a built in way to limit the depth of a System.Collection.Generics.Stack? So that if you are at max capacity, pushing a new element would remove the bottom of the stack?

I know I can do it by converting to an array and rebuilding the stack, but I figured there's probably a method on it already.

EDIT: I wrote an extension method:

    public static void Trim<T> (this Stack<T> stack, int trimCount) 
    {
        if (stack.Count <= trimCount)
            return;

       stack = new 
            Stack<T>
            (
                stack
                    .ToArray()
                    .Take(trimCount)
            );           
    }

So, it returns a new stack upon trimming, but isn't immutability the functional way =)

The reason for this is that I'm storing undo steps for an app in a stack, and I only want to store a finite amount of steps.

like image 522
FlySwat Avatar asked Dec 21 '08 03:12

FlySwat


People also ask

What is the stack limit?

The stack size limit is the maximum size of the stack for a process, in units of 1024 bytes. The stack is a per-thread resource that has unlimited hard and soft limits.

Is stack a generic collection?

A Stack represents a last-in, first-out collection of objects. It is used when you need last-in, first-out access to items. It is both a generic and non-generic type of collection.

What is generic stack in C#?

The Generic Stack in C# is a collection class that works on the principle of Last In First Out (LIFO) and this class is present in System. Collections. Generic namespace. The Generic Stack Collection is used when we need Last In First Out (LIFO) access to items.

How many items can a list hold C#?

MaxValue or 2,147,483,647 is the most items you could stick in a list. Really got to question why on earth you would need that many, there is likely to be a more sensible approach to managing the data.


1 Answers

What you are looking for is called a dropout stack. AFAIK, the BCL does not contain one, although they are trivial to implement. Typically Undo and Redo functionality relies on such data structures.

They are basically an array, and when you push onto the stack the 'top' of the stack moves around the array. Eventually the top will wrap back to the beginning when the stack is full and replace the 'bottom' of the stack.

Google didn't provide much info on it. This is the best i could find:

(Warning PDF) http://courses.cs.vt.edu/~cs2704/spring04/projects/DropOutStack.pdf

Here's some boiler plate code, to get you started. I'll let you fill in the rest (sanity checking, count, indexer, etc.)

class DropOutStack<T>
{
    private T[] items;
    private int top = 0;
    public DropOutStack(int capacity)
    { 
        items = new T[capacity];
    }

    public void Push(T item)
    {
        items[top] = item;
        top = (top + 1) % items.Length;
    }
    public T Pop()
    {
        top = (items.Length + top - 1) % items.Length;
        return items[top];
    }
}
like image 79
Greg Dean Avatar answered Sep 28 '22 16:09

Greg Dean