< Summary

Class:Towel.DataStructures.Array<T>
Assembly:Towel
File(s):File 1: /home/runner/work/Towel/Towel/Sources/Towel/DataStructures/Array.cs
Covered lines:14
Uncovered lines:14
Coverable lines:28
Total lines:112
Line coverage:50% (14 of 28)
Covered branches:5
Total branches:12
Branch coverage:41.6% (5 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
File 1: .ctor(...)50%271.42%
File 1: .ctor(...)100%10%
File 1: get_Item(...)50%466.66%
File 1: set_Item(...)50%466.66%
File 1: get_Length()100%1100%
File 1: op_Implicit(...)100%10%
File 1: op_Implicit(...)100%10%
File 1: Clone()100%10%
File 1: StepperBreak(...)100%10%
File 1: System.Collections.IEnumerable.GetEnumerator()100%10%
File 1: GetEnumerator()100%10%
File 1: ToArray()0%20%

File(s)

/home/runner/work/Towel/Towel/Sources/Towel/DataStructures/Array.cs

#LineLine coverage
 1namespace Towel.DataStructures;
 2
 3/// <summary>An indexed fixed-sized data structure.</summary>
 4/// <typeparam name="T">The generic type within the structure.</typeparam>
 5/// <typeparam name="TIndex">The generic type of the indexing.</typeparam>
 6public interface IArray<T, TIndex> : IDataStructure<T>
 7{
 8  #region Properties
 9
 10  /// <summary>Allows indexed access of the array.</summary>
 11  /// <param name="index">The index of the array to get/set.</param>
 12  /// <returns>The value at the desired index.</returns>
 13  T this[TIndex index] { get; set; }
 14  /// <summary>The length of the array.</summary>
 15  TIndex Length { get; }
 16
 17  #endregion
 18}
 19
 20/// <summary>An indexed fixed-sized data structure.</summary>
 21/// <typeparam name="T">The generic type within the structure.</typeparam>
 22public interface IArray<T> : IArray<T, int>
 23{
 24
 25}
 26
 27/// <summary>Contiguous fixed-sized data structure.</summary>
 28/// <typeparam name="T">The generic type within the structure.</typeparam>
 29public class Array<T> : IArray<T>, ICloneable<Array<T>>
 30{
 31  internal T[] _array;
 32
 33  #region Constructors
 34
 35  /// <summary>Constructs an array that implements a traversal delegate function
 36  /// which is an optimized "foreach" implementation.</summary>
 37  /// <param name="size">The length of the array in memory.</param>
 238  public Array(int size)
 239  {
 240    if (size < 1)
 041    {
 042      throw new ArgumentOutOfRangeException(nameof(size), "size of the array must be at least 1.");
 43    }
 244    _array = new T[size];
 245  }
 46
 47  /// <summary>Constructs by wrapping an existing array.</summary>
 48  /// <param name="array">The array to be wrapped.</param>
 049  public Array(params T[] array) => _array = array;
 50
 51  #endregion
 52
 53  #region Properties
 54
 55  /// <inheritdoc/>
 56  public T this[int index]
 57  {
 58    get
 959    {
 960      if (!(0 <= index || index < _array.Length))
 061      {
 062        throw new ArgumentOutOfRangeException(nameof(index), $"!(0 <= {nameof(index)} < this.{nameof(_array.Length)})");
 63      }
 964      return _array[index];
 965    }
 66    set
 967    {
 968      if (!(0 <= index || index < _array.Length))
 069      {
 070        throw new ArgumentOutOfRangeException(nameof(index), $"!(0 <= {nameof(index)} < this.{nameof(_array.Length)})");
 71      }
 972      _array[index] = value;
 973    }
 74  }
 75
 76  /// <summary>The length of the array.</summary>
 177  public int Length => _array.Length;
 78
 79  #endregion
 80
 81  #region Operators
 82
 83  /// <summary>Implicitly converts a C# System array into a Towel array.</summary>
 84  /// <param name="array">The array to be represented as a Towel array.</param>
 085  public static implicit operator Array<T>(T[] array) => new(array);
 86
 87  /// <summary>Implicitly converts a Towel array into a C# System array.</summary>
 88  /// <param name="array">The array to be represented as a C# System array.</param>
 089  public static implicit operator T[](Array<T> array) => array.ToArray();
 90
 91  #endregion
 92
 93  #region Methods
 94
 95  /// <inheritdoc/>
 096  public Array<T> Clone() => (T[])_array.Clone();
 97
 98  /// <inheritdoc/>
 99  public StepStatus StepperBreak<TStep>(TStep step)
 100    where TStep : struct, IFunc<T, StepStatus> =>
 0101    _array.StepperBreak(step);
 102
 0103  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
 104
 105  /// <inheritdoc/>
 0106  public System.Collections.Generic.IEnumerator<T> GetEnumerator() => ((System.Collections.Generic.IEnumerable<T>)_array
 107
 108  /// <inheritdoc/>
 0109  public T[] ToArray() => Length is 0 ? Array.Empty<T>() : _array[..];
 110
 111  #endregion
 112}