< Summary

Information
Class: MoreStructures.Stacks.ArrayBasedDataStructure<T>
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Stacks/ArrayBasedDataStructure.cs
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 70
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 Cyclomatic complexity Line coverage
get_Items()100%1100%
get_IncreasingFactor()100%1100%
.ctor(...)100%4100%
ResizeItems(...)100%1100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/Stacks/ArrayBasedDataStructure.cs

#LineLine coverage
 1namespace MoreStructures.Stacks;
 2
 3/// <summary>
 4/// Exposes properties shared by data structures based on a backing array of items of type <typeparamref name="T"/>.
 5/// </summary>
 6/// <typeparam name="T">The type of items, the data structure is composed of.</typeparam>
 7public abstract class ArrayBasedDataStructure<T>
 8{
 9    /// <summary>
 10    /// The default initial size of the array backing the data structure.
 11    /// </summary>
 12    /// <remarks>
 13    /// In an array initialized with capacity x, up to x insertions can be done in constant time, with no need for
 14    /// array resizing.
 15    /// </remarks>
 16    public const int DefaultInitialCapacity = 16;
 17
 18    /// <summary>
 19    /// The default value for <see cref="IncreasingFactor"/>.
 20    /// </summary>
 21    public const double DefaultIncreasingFactor = 2;
 22
 23    /// <summary>
 24    /// The array of items, backing this data structure.
 25    /// </summary>
 345326    protected T?[] Items { get; set; }
 27
 28    /// <summary>
 29    /// The multiplicative factor used to resize the underlying array, every time it gets full.
 30    /// </summary>
 31    /// <remarks>
 32    /// Required to be bigger than 1.0.
 33    /// </remarks>
 76534    public double IncreasingFactor { get; }
 35
 36    /// <summary>
 37    /// Initializes the data structure with an array list of initial capacity equals to the provided
 38    /// <paramref name="capacity"/>.
 39    /// </summary>
 40    /// <param name="capacity">
 41    /// The initial capacity of the backing array. If not specified, <see cref="DefaultInitialCapacity"/> is used.
 42    /// </param>
 43    /// <param name="increasingFactor">
 44    ///     <inheritdoc cref="IncreasingFactor" path="/summary"/>
 45    /// </param>
 2346    protected ArrayBasedDataStructure(
 2347        int capacity = DefaultInitialCapacity, double increasingFactor = DefaultIncreasingFactor)
 2348    {
 2349        if (capacity <= 0)
 250            throw new ArgumentOutOfRangeException(nameof(capacity), "Must be positive.");
 51
 2152        if (increasingFactor <= 1)
 353            throw new ArgumentOutOfRangeException(nameof(increasingFactor), "Must be strictly bigger than 1.");
 54
 1855        Items = new T[capacity];
 1856        IncreasingFactor = increasingFactor;
 1857    }
 58
 59    /// <summary>
 60    /// Resizes the <see cref="Items"/> array, applying the provided <paramref name="factor"/> to its length.
 61    /// </summary>
 62    /// <param name="factor">The multiplicative factor to be applied to the length of the array.</param>
 63    protected virtual void ResizeItems(double factor)
 2464    {
 2465        var oldItems = Items;
 2466        var newSize = (int)Math.Ceiling(oldItems.Length * factor);
 2467        Array.Resize(ref oldItems, newSize);
 2468        Items = oldItems;
 2469    }
 70}