| | | 1 | | namespace Towel.DataStructures; |
| | | 2 | | |
| | | 3 | | /// <summary>Polymorphism base for all data structures in Towel.</summary> |
| | | 4 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | | 5 | | public interface IDataStructure<T> : ISteppable<T>, System.Collections.Generic.IEnumerable<T> |
| | | 6 | | { |
| | | 7 | | #region Methods |
| | | 8 | | |
| | | 9 | | /// <summary>Constructs an array with the values of this data structure.</summary> |
| | | 10 | | /// <returns>An array with the values of this data structure.</returns> |
| | | 11 | | T[] ToArray(); |
| | | 12 | | |
| | | 13 | | #endregion |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <summary>Contains static members for <see cref="IDataStructure{T}"/>.</summary> |
| | | 17 | | public static class DataStructure |
| | | 18 | | { |
| | | 19 | | #region Interfaces |
| | | 20 | | |
| | | 21 | | /// <summary>Property of a data structure (does it have a contains method).</summary> |
| | | 22 | | /// <typeparam name="T">The type of value.</typeparam> |
| | | 23 | | public interface IAuditable<T> |
| | | 24 | | { |
| | | 25 | | /// <summary>Checks if the data structure contains a value.</summary> |
| | | 26 | | /// <param name="value">The value to look for in the data structure.</param> |
| | | 27 | | /// <returns>True if the value exists in the data structure. False if not.</returns> |
| | | 28 | | bool Contains(T value); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Represents a type that is hashing values of type <typeparamref name="T"/>.</summary> |
| | | 32 | | /// <typeparam name="T">The type of values this type is hashing.</typeparam> |
| | | 33 | | /// <typeparam name="THash">The type that is hashing <typeparamref name="T"/> values.</typeparam> |
| | | 34 | | public interface IHashing<T, THash> |
| | | 35 | | where THash : struct, IFunc<T, int> |
| | | 36 | | { |
| | | 37 | | /// <summary>Gets the value of the type that is hashing <typeparamref name="T"/> values.</summary> |
| | | 38 | | THash Hash { get; } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary>Represents a type that is comparing values of type <typeparamref name="T"/>.</summary> |
| | | 42 | | /// <typeparam name="T">The type of values this type is comparing.</typeparam> |
| | | 43 | | /// <typeparam name="TCompare">The type that is comparing <typeparamref name="T"/> values.</typeparam> |
| | | 44 | | public interface IComparing<T, TCompare> |
| | | 45 | | where TCompare : struct, IFunc<T, T, CompareResult> |
| | | 46 | | { |
| | | 47 | | /// <summary>Gets the value of the comparer that is comparing <typeparamref name="T"/> values.</summary> |
| | | 48 | | TCompare Compare { get; } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary>Property of a data structure (does it have a Add method).</summary> |
| | | 52 | | /// <typeparam name="T">The type of value.</typeparam> |
| | | 53 | | public interface IAddable<T> |
| | | 54 | | { |
| | | 55 | | /// <summary>Tries to add a value to a data structure.</summary> |
| | | 56 | | /// <param name="value">The value to add to the data structure.</param> |
| | | 57 | | /// <returns>True if the value was added or false if not.</returns> |
| | | 58 | | (bool Success, Exception? Exception) TryAdd(T value); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary>Property of a data structure (does it have a Romove method).</summary> |
| | | 62 | | /// <typeparam name="T">The type of value.</typeparam> |
| | | 63 | | public interface IRemovable<T> |
| | | 64 | | { |
| | | 65 | | /// <summary>Tries to remove a value.</summary> |
| | | 66 | | /// <param name="value">The value to remove.</param> |
| | | 67 | | /// <returns>True if the value was removed or false if not.</returns> |
| | | 68 | | (bool Success, Exception? Exception) TryRemove(T value); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary>Property of a data structure (does it have a Count method).</summary> |
| | | 72 | | public interface ICountable |
| | | 73 | | { |
| | | 74 | | /// <summary>Gets the current count of the data structure.</summary> |
| | | 75 | | int Count { get; } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary>Property of a data structure (does it have a Clear method).</summary> |
| | | 79 | | public interface IClearable |
| | | 80 | | { |
| | | 81 | | /// <summary>Returns the data structure to an empty state.</summary> |
| | | 82 | | void Clear(); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary>Represents a type that is equality checking values of type <typeparamref name="T"/>.</summary> |
| | | 86 | | /// <typeparam name="T">The type of values this type is equality checking.</typeparam> |
| | | 87 | | /// <typeparam name="TEquate">The type that is equality checking <typeparamref name="T"/> values.</typeparam> |
| | | 88 | | public interface IEquating<T, TEquate> |
| | | 89 | | where TEquate : struct, IFunc<T, T, bool> |
| | | 90 | | { |
| | | 91 | | /// <summary>Gets the value of the type that is checking <typeparamref name="T"/> values for equality.</summary> |
| | | 92 | | TEquate Equate { get; } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | #endregion |
| | | 96 | | |
| | | 97 | | #region Extension Methods |
| | | 98 | | |
| | | 99 | | /// <summary>Adds a value to a data structure.</summary> |
| | | 100 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | | 101 | | /// <param name="addable">The data structure to add the value to.</param> |
| | | 102 | | /// <param name="value">The value to add to the data structure.</param> |
| | | 103 | | public static void Add<T>(this IAddable<T> addable, T value) |
| | 1482288 | 104 | | { |
| | 1482288 | 105 | | var (success, exception) = addable.TryAdd(value); |
| | 1482288 | 106 | | if (!success) |
| | 8 | 107 | | { |
| | 8 | 108 | | throw exception ?? new ArgumentException($"{nameof(Add)} failed but the {nameof(exception)} is null"); |
| | | 109 | | } |
| | 1482280 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary>Removes a value from a data structure.</summary> |
| | | 113 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | | 114 | | /// <param name="removable">The data structure to removable the value from.</param> |
| | | 115 | | /// <param name="value">The value to remove from the data structure.</param> |
| | | 116 | | public static void Remove<T>(this IRemovable<T> removable, T value) |
| | 135879 | 117 | | { |
| | 135879 | 118 | | var (success, exception) = removable.TryRemove(value); |
| | 135879 | 119 | | if (!success) |
| | 2 | 120 | | { |
| | 2 | 121 | | throw exception ?? new ArgumentException($"{nameof(Remove)} failed but the {nameof(exception)} is null"); |
| | | 122 | | } |
| | 135877 | 123 | | } |
| | | 124 | | |
| | | 125 | | #endregion |
| | | 126 | | } |