| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for collection manipulation and utility functions. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class SwiftExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Populates an array with values generated by a specified provider function. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The type of the elements in the array.</typeparam> |
| | | 16 | | /// <param name="array">The array to populate.</param> |
| | | 17 | | /// <param name="provider">A function that generates a value for each element in the array.</param> |
| | | 18 | | /// <returns>The populated array.</returns> |
| | | 19 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 20 | | public static T[] Populate<T>(this T[] array, Func<T> provider) |
| | 181 | 21 | | { |
| | 456040 | 22 | | for (int i = 0; i < array.Length; i++) |
| | 227839 | 23 | | array[i] = provider(); |
| | 181 | 24 | | return array; |
| | 181 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Populates an array with values generated by a provider function that accepts the current index. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <typeparam name="T">The type of the elements in the array.</typeparam> |
| | | 31 | | /// <param name="array">The array to populate.</param> |
| | | 32 | | /// <param name="provider">A function that generates a value for each element based on its index.</param> |
| | | 33 | | /// <returns>The populated array.</returns> |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public static T[] Populate<T>(this T[] array, Func<int, T> provider) |
| | 1 | 36 | | { |
| | 8 | 37 | | for (int i = 0; i < array.Length; i++) |
| | 3 | 38 | | array[i] = provider(i); |
| | 1 | 39 | | return array; |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Populates an array with new instances of the specified type. |
| | | 44 | | /// The type must have a parameterless constructor. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <typeparam name="T">The type of the elements in the array.</typeparam> |
| | | 47 | | /// <param name="array">The array to populate.</param> |
| | | 48 | | /// <returns>The populated array.</returns> |
| | | 49 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 50 | | public static T[] Populate<T>(this T[] array) where T : new() |
| | 1 | 51 | | { |
| | 6 | 52 | | for (int i = 0; i < array.Length; i++) |
| | 2 | 53 | | array[i] = new T(); |
| | 1 | 54 | | return array; |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Attempts to retrieve an element from the array at the specified index. |
| | | 59 | | /// Returns true if the index is valid and the element is retrieved; otherwise, returns false. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <typeparam name="T">The type of elements in the array.</typeparam> |
| | | 62 | | /// <param name="array">The array from which to retrieve the element.</param> |
| | | 63 | | /// <param name="index">The index of the element to retrieve.</param> |
| | | 64 | | /// <param name="result"> |
| | | 65 | | /// When this method returns, contains the element at the specified index if the index is valid; |
| | | 66 | | /// otherwise, the default value for the type of the element. |
| | | 67 | | /// </param> |
| | | 68 | | /// <returns> |
| | | 69 | | /// True if the element at the specified index was retrieved successfully; otherwise, false. |
| | | 70 | | /// </returns> |
| | | 71 | | public static bool TryIndex<T>(this T[] array, int index, out T result) |
| | 5 | 72 | | { |
| | 5 | 73 | | if (array != null) |
| | 4 | 74 | | { |
| | 4 | 75 | | if (index < 0) |
| | 2 | 76 | | { |
| | | 77 | | // Support negative indices to access elements from the end |
| | 2 | 78 | | index = array.Length + index; |
| | 2 | 79 | | } |
| | 4 | 80 | | if (index >= 0 && index < array.Length) |
| | 2 | 81 | | { |
| | 2 | 82 | | result = array[index]; |
| | 2 | 83 | | return true; |
| | | 84 | | } |
| | 2 | 85 | | } |
| | 3 | 86 | | result = default; |
| | 3 | 87 | | return false; |
| | 5 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// An iterator that yields the elements of the source collection in a random order using the specified random n |
| | | 92 | | /// </summary> |
| | | 93 | | /// <typeparam name="T">The type of elements in the collection.</typeparam> |
| | | 94 | | /// <param name="source">The collection to shuffle.</param> |
| | | 95 | | /// <param name="rng">The random number generator to use for shuffling.</param> |
| | | 96 | | /// <returns>An iterator that yields the shuffled elements.</returns> |
| | | 97 | | public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng) |
| | 3 | 98 | | { |
| | 3 | 99 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 2 | 100 | | SwiftThrowHelper.ThrowIfNull(rng, nameof(rng)); |
| | | 101 | | |
| | 1 | 102 | | SwiftList<T> buffer = new SwiftList<T>(source); |
| | 1 | 103 | | int n = buffer.Count; |
| | 7 | 104 | | while (n > 0) |
| | 6 | 105 | | { |
| | 6 | 106 | | int k = rng.Next(n); |
| | 6 | 107 | | n--; |
| | | 108 | | // Swap the selected element with the last unshuffled element |
| | 6 | 109 | | (buffer[k], buffer[n]) = (buffer[n], buffer[k]); |
| | 6 | 110 | | yield return buffer[n]; |
| | 6 | 111 | | } |
| | 1 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Shuffles the elements of the list in place using the specified random number generator. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 118 | | /// <param name="list">The list to shuffle.</param> |
| | | 119 | | /// <param name="rng">The random number generator to use for shuffling.</param> |
| | | 120 | | public static void ShuffleInPlace<T>(this IList<T> list, Random rng) |
| | 3 | 121 | | { |
| | 3 | 122 | | SwiftThrowHelper.ThrowIfNull(list, nameof(list)); |
| | 2 | 123 | | SwiftThrowHelper.ThrowIfNull(rng, nameof(rng)); |
| | | 124 | | |
| | 1 | 125 | | int n = list.Count; |
| | 6 | 126 | | while (n > 1) |
| | 5 | 127 | | { |
| | 5 | 128 | | n--; |
| | 5 | 129 | | int k = rng.Next(n + 1); |
| | 5 | 130 | | (list[n], list[k]) = (list[k], list[n]); |
| | 5 | 131 | | } |
| | 1 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Determines whether a sequence contains any elements. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <typeparam name="T"></typeparam> |
| | | 138 | | /// <param name="source"> The <see cref="IEnumerable{T}"/> to check for emptiness.</param> |
| | | 139 | | /// <returns> |
| | | 140 | | /// true if the source sequence contains any elements; otherwise, false. |
| | | 141 | | /// </returns> |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 143 | | public static bool IsPopulated<T>(this IEnumerable<T> source) |
| | 4 | 144 | | { |
| | 4 | 145 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 3 | 146 | | using IEnumerator<T> enumerator = source.GetEnumerator(); |
| | 3 | 147 | | return enumerator.MoveNext(); |
| | 3 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Determines whether the collection is not null and contains any elements. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <typeparam name="T">The type of elements in the collection.</typeparam> |
| | | 154 | | /// <param name="source">The collection to check.</param> |
| | | 155 | | /// <returns> |
| | | 156 | | /// True if the collection is not null and contains at least one element; otherwise, false. |
| | | 157 | | /// </returns> |
| | | 158 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 159 | | public static bool IsPopulatedSafe<T>(this IEnumerable<T> source) => source != null && source.IsPopulated(); |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the element at the specified index from the end (1-based). |
| | | 163 | | /// For example, FromEnd(1) returns the last item, FromEnd(2) returns second-to-last. |
| | | 164 | | /// </summary> |
| | | 165 | | public static T FromEnd<T>(this IEnumerable<T> source, int reverseIndex) |
| | 3 | 166 | | { |
| | 3 | 167 | | if (source is SwiftList<T> swift) |
| | 0 | 168 | | return swift.FromEnd(reverseIndex); |
| | | 169 | | |
| | | 170 | | // fallback for generic IEnumerable |
| | 3 | 171 | | var buffer = new SwiftList<T>(source); |
| | 3 | 172 | | return buffer.FromEnd(reverseIndex); |
| | 3 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Gets the element at the specified index from the end (1-based) from SwiftList. |
| | | 177 | | /// </summary> |
| | | 178 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 179 | | public static T FromEnd<T>(this SwiftList<T> list, int reverseIndex) |
| | 6 | 180 | | { |
| | 6 | 181 | | if (reverseIndex <= 0 || reverseIndex > list.Count) |
| | 2 | 182 | | throw new ArgumentOutOfRangeException(nameof(reverseIndex)); |
| | 4 | 183 | | return list[^reverseIndex]; |
| | 4 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Returns the last item in the sequence. |
| | | 188 | | /// </summary> |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 190 | | public static T PopLast<T>(this IEnumerable<T> source) => source.FromEnd(1); |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Bypasses a specified number of elements from the end and then returns the remaining elements. |
| | | 194 | | /// </summary> |
| | | 195 | | public static IEnumerable<T> SkipFromEnd<T>(this IEnumerable<T> source, int count) |
| | 5 | 196 | | { |
| | 5 | 197 | | if (source == null) |
| | 1 | 198 | | throw new ArgumentNullException(nameof(source)); |
| | 4 | 199 | | if (count < 0) |
| | 1 | 200 | | throw new ArgumentOutOfRangeException(nameof(count)); |
| | | 201 | | |
| | 3 | 202 | | SwiftQueue<T> buffer = new(); |
| | | 203 | | |
| | 29 | 204 | | foreach (T item in source) |
| | 10 | 205 | | { |
| | 10 | 206 | | buffer.Enqueue(item); |
| | 10 | 207 | | if (buffer.Count > count) |
| | 6 | 208 | | yield return buffer.Dequeue(); |
| | 10 | 209 | | } |
| | 3 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Returns the second-to-last item in the sequence. |
| | | 214 | | /// </summary> |
| | 1 | 215 | | public static T SecondToLast<T>(this IEnumerable<T> source) => source.FromEnd(2); |
| | | 216 | | } |
| | | 217 | | } |