Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Stack<T> Remove<T>(this Stack<T> stack, T item)
- {
- Stack<T> newStack = new Stack<T>();
- EqualityComparer<T> eqc = EqualityComparer<T>.Default;
- foreach( T newItem in stack.Reverse() )
- {
- if( !eqc.Equals(newItem, item) )
- {
- newStack.Push(newItem);
- }
- }
- return newStack;
- }
- /// <summary>
- /// Returns a new Stack{T} with one or more items removed, based on the given predicate.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="stack"></param>
- /// <param name="fnRemove"></param>
- /// <returns>The new stack.</returns>
- /// <remarks>
- /// We have to turn tricks in order to save the LIFO order of the pool
- /// since there is no built-in remove method since they implement a Stack internally
- /// as an array instead of a linked list. Maybe they have a good reason, I don't know...
- ///
- /// So, to fix this I'm just using a LINQ extension method to enumerate in reverse.
- /// </remarks>
- public static Stack<T> RemoveWhere<T>(this Stack<T> stack, Predicate<T> fnRemove)
- {
- Stack<T> newStack = new Stack<T>();
- foreach( T newItem in stack.Reverse() )
- {
- /// Check using the caller's method.
- if( fnRemove(newItem) )
- {
- /// It's not allowed in the new stack.
- continue;
- }
- newStack.Push(newItem);
- }
- return newStack;
- }
Add Comment
Please, Sign In to add comment