| | 1 | | namespace 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> |
| | 6 | | public 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> |
| | 22 | | public 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> |
| | 29 | | public 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> |
| 2 | 38 | | public Array(int size) |
| 2 | 39 | | { |
| 2 | 40 | | if (size < 1) |
| 0 | 41 | | { |
| 0 | 42 | | throw new ArgumentOutOfRangeException(nameof(size), "size of the array must be at least 1."); |
| | 43 | | } |
| 2 | 44 | | _array = new T[size]; |
| 2 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary>Constructs by wrapping an existing array.</summary> |
| | 48 | | /// <param name="array">The array to be wrapped.</param> |
| 0 | 49 | | 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 |
| 9 | 59 | | { |
| 9 | 60 | | if (!(0 <= index || index < _array.Length)) |
| 0 | 61 | | { |
| 0 | 62 | | throw new ArgumentOutOfRangeException(nameof(index), $"!(0 <= {nameof(index)} < this.{nameof(_array.Length)})"); |
| | 63 | | } |
| 9 | 64 | | return _array[index]; |
| 9 | 65 | | } |
| | 66 | | set |
| 9 | 67 | | { |
| 9 | 68 | | if (!(0 <= index || index < _array.Length)) |
| 0 | 69 | | { |
| 0 | 70 | | throw new ArgumentOutOfRangeException(nameof(index), $"!(0 <= {nameof(index)} < this.{nameof(_array.Length)})"); |
| | 71 | | } |
| 9 | 72 | | _array[index] = value; |
| 9 | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary>The length of the array.</summary> |
| 1 | 77 | | 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> |
| 0 | 85 | | 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> |
| 0 | 89 | | public static implicit operator T[](Array<T> array) => array.ToArray(); |
| | 90 | |
|
| | 91 | | #endregion |
| | 92 | |
|
| | 93 | | #region Methods |
| | 94 | |
|
| | 95 | | /// <inheritdoc/> |
| 0 | 96 | | public Array<T> Clone() => (T[])_array.Clone(); |
| | 97 | |
|
| | 98 | | /// <inheritdoc/> |
| | 99 | | public StepStatus StepperBreak<TStep>(TStep step) |
| | 100 | | where TStep : struct, IFunc<T, StepStatus> => |
| 0 | 101 | | _array.StepperBreak(step); |
| | 102 | |
|
| 0 | 103 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 104 | |
|
| | 105 | | /// <inheritdoc/> |
| 0 | 106 | | public System.Collections.Generic.IEnumerator<T> GetEnumerator() => ((System.Collections.Generic.IEnumerable<T>)_array |
| | 107 | |
|
| | 108 | | /// <inheritdoc/> |
| 0 | 109 | | public T[] ToArray() => Length is 0 ? Array.Empty<T>() : _array[..]; |
| | 110 | |
|
| | 111 | | #endregion |
| | 112 | | } |