| | | 1 | | using System; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A minimal and efficient stack implementation for integers. |
| | | 8 | | /// Optimized for internal use in within the SwiftCollections library. |
| | | 9 | | /// </summary> |
| | | 10 | | [Serializable] |
| | | 11 | | internal class SwiftIntStack |
| | | 12 | | { |
| | | 13 | | #region Constants |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The default initial capacity of the stack. |
| | | 17 | | /// </summary> |
| | | 18 | | public const int DefaultCapacity = 8; |
| | | 19 | | |
| | | 20 | | #endregion |
| | | 21 | | |
| | | 22 | | #region Fields |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The internal array holding stack elements. |
| | | 26 | | /// </summary> |
| | 241447 | 27 | | public int[] Array { get; private set; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The current count of elements in the stack. |
| | | 31 | | /// </summary> |
| | 586848 | 32 | | public int Count { get; private set; } |
| | | 33 | | |
| | | 34 | | #endregion |
| | | 35 | | |
| | | 36 | | #region Constructors |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="SwiftIntStack"/> class with the default capacity. |
| | | 40 | | /// </summary> |
| | 183 | 41 | | public SwiftIntStack() : this(DefaultCapacity) { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="SwiftIntStack"/> class with a specified initial capacity. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="capacity">The initial capacity of the stack.</param> |
| | 182 | 47 | | public SwiftIntStack(int capacity) |
| | 182 | 48 | | { |
| | 182 | 49 | | Array = capacity == 0 ? new int[DefaultCapacity] : new int[capacity]; |
| | 182 | 50 | | Count = 0; |
| | 182 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Initializes a new instance of the IntStack class using the specified array as the underlying storage and sets |
| | | 55 | | /// the initial number of elements in the stack. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <remarks>This constructor allows advanced scenarios where the stack is initialized with a pre-existing |
| | | 58 | | /// array and a specific element count. The caller is responsible for ensuring that the array and count accurately |
| | | 59 | | /// represent the intended stack state.</remarks> |
| | | 60 | | /// <param name="array">The array that provides the underlying storage for the stack. This parameter cannot be null. |
| | | 61 | | /// <param name="count">The number of elements initially contained in the stack. Must be a non-negative integer and |
| | | 62 | | /// length of the array.</param> |
| | 1 | 63 | | public SwiftIntStack(int[] array, int count) |
| | 1 | 64 | | { |
| | 1 | 65 | | Array = array; |
| | 1 | 66 | | Count = count; |
| | 1 | 67 | | } |
| | | 68 | | |
| | | 69 | | #endregion |
| | | 70 | | |
| | | 71 | | #region Public Methods |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Pushes an integer onto the stack. |
| | | 75 | | /// Expands the stack's capacity if necessary. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="value">The integer value to push.</param> |
| | | 78 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 79 | | public void Push(int value) |
| | 98234 | 80 | | { |
| | 98234 | 81 | | EnsureCapacity(Count + 1); |
| | 98234 | 82 | | Array[Count++] = value; |
| | 98234 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Removes and returns the top integer from the stack. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <returns>The top integer from the stack.</returns> |
| | | 89 | | /// <exception cref="InvalidOperationException">Thrown when attempting to pop from an empty stack.</exception> |
| | | 90 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 44566 | 91 | | public int Pop() => Array[--Count]; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Returns the top integer from the stack without removing it. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <returns>The top integer from the stack.</returns> |
| | | 97 | | /// <exception cref="InvalidOperationException">Thrown when attempting to peek an empty stack.</exception> |
| | | 98 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 99 | | public int Peek() => Array[Count - 1]; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Resets the stack to it's initial state. |
| | | 103 | | /// </summary> |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public void Reset() |
| | 9 | 106 | | { |
| | 9 | 107 | | Array = new int[DefaultCapacity]; |
| | 9 | 108 | | Count = 0; |
| | 9 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Removes all elements from the stack. |
| | | 113 | | /// </summary> |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 36 | 115 | | public void Clear() => Count = 0; |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Ensures the stack has at least the specified capacity. |
| | | 119 | | /// Expands the internal array if necessary. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="capacity">The minimum capacity to ensure.</param> |
| | | 122 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 123 | | public void EnsureCapacity(int capacity) |
| | 98270 | 124 | | { |
| | 98270 | 125 | | if (capacity >= Array.Length) |
| | 60 | 126 | | { |
| | 60 | 127 | | int[] newArray = new int[Array.Length * 2]; |
| | 60 | 128 | | System.Array.Copy(Array, 0, newArray, 0, Count); |
| | 60 | 129 | | Array = newArray; |
| | 60 | 130 | | } |
| | 98270 | 131 | | } |
| | | 132 | | |
| | | 133 | | #endregion |
| | | 134 | | } |