[.net] Stacks FTL
creating a stack:
Stack<MyArray[,,]> my_stack;
my_stack = new Stack<MyArray[,,]>(10); // supposedly to limit the stack to
// 10 MyArray objects
once done this if have an if statement that goes something like this
if ( my_stack.count > 10 )
{
my_stack.TrimExcess() // supposedly to reduce the stack's size to its
// limit , previously defined in constructor
}
im just amazed that after 200 pushes my stack is well above the 10 limit and the function TrimExcess() does absolutely nothing.
why is that?
why specifying an initial limit for the stack does not prevent it from keep pushin data into it?
Did you read the MSDN entry for the Stack constructor?
Quote:
The capacity of a Stack is the number of elements that the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required by reallocating the internal array.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Stack.
The MSDN page says:
You'll be at over 90% capacity if you keep pushing. Why are you pushing 200 times if you only want ten? Or do you just want the last ten pushed?
Quote:This method can be used to minimize a collection's memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large Stack<(Of <(T>)>) can be considerable, however, so the TrimExcess method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain.
You'll be at over 90% capacity if you keep pushing. Why are you pushing 200 times if you only want ten? Or do you just want the last ten pushed?
I guess he want the lower ones to automatically get dropped or something. Stack doesn't do that.
you could override the push method for a LimitedStack class, where you automatically pop the lowest as needed, or throw an exception if too much gets pushed on the stack, or what ever is you need.
you could override the push method for a LimitedStack class, where you automatically pop the lowest as needed, or throw an exception if too much gets pushed on the stack, or what ever is you need.
Im implementing an UNDO/REDO feature
im using stacks so i can revert to the last modification
since im working with possible BIG data structures i want to keep the undo history to a reasonable minimum.
right now i can apply modifications and the app quickly rises above 200mb of ram usage , it is only a matter of time before the app eats the whole machine's memory
im using stacks so i can revert to the last modification
since im working with possible BIG data structures i want to keep the undo history to a reasonable minimum.
right now i can apply modifications and the app quickly rises above 200mb of ram usage , it is only a matter of time before the app eats the whole machine's memory
You're looking for a deque. I haven't looked at his code, but it's trivial to make one out of a List<>. Or, if you're really hard up, go grab the C5 collections library.
Thank you
it worked beautifully!
Man this forums are such a help , its unbelievable
thanks again!
it worked beautifully!
Man this forums are such a help , its unbelievable
thanks again!
Actually, a ring-buffer would be a better data structure, as it will avoid all the extraneous object construction/destruction that a push onto a "full" deque will. I'm not sure if .net provides one, but it would be pretty trivial to implement one on top of an array.
I just implemented the deque and im not seeing any "extraneous" behavior
what should I look for , maybe I can spot something
what should I look for , maybe I can spot something
Quote:Original post by RavyneHow do you figure? You still have to create the objects to place into a ring buffer and destroy (or, rather, garbage-collect) the old ones. You're just shifting how they're being stored. I don't see any significant performance benefits coming from using a ring buffer, so long as you set the deque's capacity to be more than [history size + 1].
Actually, a ring-buffer would be a better data structure, as it will avoid all the extraneous object construction/destruction that a push onto a "full" deque will. I'm not sure if .net provides one, but it would be pretty trivial to implement one on top of an array.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement