| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="StackExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Collections; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class StackExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Removes the first occurrence of the specified item from the stack. The order of the remaining items is preserved |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="stack">The stack from which to remove the item.</param> |
| | | 20 | | /// <param name="item">The item to remove.</param> |
| | | 21 | | /// <typeparam name="T">The type of elements in the stack.</typeparam> |
| | | 22 | | /// <exception cref="ArgumentNullException">Thrown if the stack is null.</exception> |
| | | 23 | | public static void Remove<T>(this Stack<T> stack, T item) |
| | | 24 | | { |
| | 15 | 25 | | ArgumentNullException.ThrowIfNull(stack); |
| | | 26 | | |
| | 12 | 27 | | var temp = new Stack<T>(); |
| | | 28 | | |
| | 48 | 29 | | while (stack.Count > 0) |
| | | 30 | | { |
| | 36 | 31 | | var current = stack.Pop(); |
| | | 32 | | |
| | 36 | 33 | | if (!Equals(current, item)) |
| | 27 | 34 | | temp.Push(current); |
| | | 35 | | } |
| | | 36 | | |
| | 39 | 37 | | while (temp.Count > 0) |
| | 27 | 38 | | stack.Push(temp.Pop()); |
| | 12 | 39 | | } |
| | | 40 | | } |
| | | 41 | | |