< Summary

Information
Class: SwiftCollections.SwiftIntStack
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Collection/Support/SwiftIntStack.cs
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 134
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Array()100%11100%
get_Count()100%11100%
.ctor()100%11100%
.ctor(...)100%22100%
.ctor(...)100%11100%
Push(...)100%11100%
Pop()100%11100%
Peek()100%11100%
Reset()100%11100%
Clear()100%11100%
EnsureCapacity(...)100%22100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Collection/Support/SwiftIntStack.cs

#LineLine coverage
 1using System;
 2using System.Runtime.CompilerServices;
 3
 4namespace 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]
 11internal 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>
 24144727    public int[] Array { get; private set; }
 28
 29    /// <summary>
 30    /// The current count of elements in the stack.
 31    /// </summary>
 58684832    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>
 18341    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>
 18247    public SwiftIntStack(int capacity)
 18248    {
 18249        Array = capacity == 0 ? new int[DefaultCapacity] : new int[capacity];
 18250        Count = 0;
 18251    }
 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>
 163    public SwiftIntStack(int[] array, int count)
 164    {
 165        Array = array;
 166        Count = count;
 167    }
 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)
 9823480    {
 9823481        EnsureCapacity(Count + 1);
 9823482        Array[Count++] = value;
 9823483    }
 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)]
 4456691    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)]
 199    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()
 9106    {
 9107        Array = new int[DefaultCapacity];
 9108        Count = 0;
 9109    }
 110
 111    /// <summary>
 112    /// Removes all elements from the stack.
 113    /// </summary>
 114    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 36115    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)
 98270124    {
 98270125        if (capacity >= Array.Length)
 60126        {
 60127            int[] newArray = new int[Array.Length * 2];
 60128            System.Array.Copy(Array, 0, newArray, 0, Count);
 60129            Array = newArray;
 60130        }
 98270131    }
 132
 133    #endregion
 134}