| | 1 | | using System.Collections; |
| | 2 | |
|
| | 3 | | namespace MoreStructures.Utilities; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Extension methods for all <see cref="IEnumerable{T}"/> concretions. |
| | 7 | | /// </summary> |
| | 8 | | public static class EnumerableExtensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Optimized version of <see cref="Enumerable.Count{TSource}(IEnumerable{TSource})"/>, which runs in constant time |
| | 12 | | /// on <paramref name="source"/> of type <see cref="string"/>, <see cref="IDictionary"/>, <see cref="IList{T}"/> |
| | 13 | | /// and <see cref="IList"/>, and calls <see cref="Enumerable.Count{TSource}(IEnumerable{TSource})"/> for any |
| | 14 | | /// <paramref name="source"/> which cannot be assigned to either of these types. |
| | 15 | | /// </summary> |
| | 16 | | /// <typeparam name="TSource"> |
| | 17 | | /// <inheritdoc cref="Enumerable.Count{TSource}(IEnumerable{TSource})" path="/typeparam[@name='TSource']"/> |
| | 18 | | /// </typeparam> |
| | 19 | | /// <param name="source"> |
| | 20 | | /// <inheritdoc cref="Enumerable.Count{TSource}(IEnumerable{TSource})" path="/param[@name='source']"/> |
| | 21 | | /// </param> |
| | 22 | | /// <returns> |
| | 23 | | /// <inheritdoc cref="Enumerable.Count{TSource}(IEnumerable{TSource})" path="/returns"/> |
| | 24 | | /// </returns> |
| | 25 | | public static int CountO1<TSource>(this IEnumerable<TSource> source) |
| 12680 | 26 | | { |
| 12680 | 27 | | if (source is string str) |
| 507 | 28 | | return str.Length; |
| 12173 | 29 | | if (source is IDictionary nonGenericDict) |
| 3 | 30 | | return nonGenericDict.Count; |
| 12170 | 31 | | if (source is IList<TSource> genericList) |
| 1174 | 32 | | return genericList.Count; |
| 10996 | 33 | | if (source is IList nonGenericList) |
| 1 | 34 | | return nonGenericList.Count; |
| 10995 | 35 | | return source.Count(); |
| 12680 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)" path="//*[not(self::summary)]"/> |
| | 39 | | /// <summary> |
| | 40 | | /// Optimized version of <see cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)"/>, which runs in |
| | 41 | | /// constant time on <paramref name="source"/> of type <see cref="string"/>, <see cref="IList{T}"/> |
| | 42 | | /// and <see cref="IList"/>, and calls <see cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)"/> |
| | 43 | | /// for any <paramref name="source"/> which cannot be assigned to either of these types. |
| | 44 | | /// </summary> |
| | 45 | | public static TSource ElementAtO1<TSource>(this IEnumerable<TSource> source, int index) |
| 5494 | 46 | | { |
| 5494 | 47 | | if (source is string str) |
| 1614 | 48 | | { |
| 1614 | 49 | | if (index < 0 || index >= str.Length) |
| 2 | 50 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 1612 | 51 | | return (TSource)(str[index] as object); |
| | 52 | | } |
| | 53 | |
|
| 3880 | 54 | | if (source is IList<TSource> genericList) |
| 2702 | 55 | | { |
| 2702 | 56 | | if (index < 0 || index >= genericList.Count) |
| 6 | 57 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 2696 | 58 | | return genericList[index]; |
| | 59 | | } |
| | 60 | |
|
| 1178 | 61 | | if (source is IList nonGenericList) |
| 4 | 62 | | { |
| 4 | 63 | | if (index < 0 || index >= nonGenericList.Count) |
| 2 | 64 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 2 | 65 | | return (TSource)nonGenericList[index]!; |
| | 66 | | } |
| | 67 | |
|
| 1174 | 68 | | return source.ElementAt(index); |
| 5484 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <inheritdoc cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)" path="//*[not(self::summary)]"/> |
| | 72 | | /// <summary> |
| | 73 | | /// Optimized version of <see cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, Index)"/>, which runs in |
| | 74 | | /// constant time on <paramref name="source"/> of type <see cref="string"/>, <see cref="IList{T}"/> |
| | 75 | | /// and <see cref="IList"/>, and calls <see cref="Enumerable.ElementAt{TSource}(IEnumerable{TSource}, int)"/> |
| | 76 | | /// for any <paramref name="source"/> which cannot be assigned to either of these types. |
| | 77 | | /// </summary> |
| | 78 | | public static TSource ElementAtO1<TSource>(this IEnumerable<TSource> source, Index index) |
| 182215 | 79 | | { |
| 182215 | 80 | | if (source is string str) |
| 12 | 81 | | { |
| 12 | 82 | | var indexValue = index.GetOffset(str.Length); |
| 12 | 83 | | if (indexValue < 0 || indexValue >= str.Length) |
| 10 | 84 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 2 | 85 | | return (TSource)(str[index] as object); |
| | 86 | | } |
| | 87 | |
|
| 182203 | 88 | | if (source is IList<TSource> genericList) |
| 30 | 89 | | { |
| 30 | 90 | | var indexValue = index.GetOffset(genericList.Count); |
| 30 | 91 | | if (indexValue < 0 || indexValue >= genericList.Count) |
| 24 | 92 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 6 | 93 | | return genericList[index]; |
| | 94 | | } |
| | 95 | |
|
| 182173 | 96 | | if (source is IList nonGenericList) |
| 6 | 97 | | { |
| 6 | 98 | | var indexValue = index.GetOffset(nonGenericList.Count); |
| 6 | 99 | | if (indexValue < 0 || indexValue >= nonGenericList.Count) |
| 4 | 100 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| 2 | 101 | | return (TSource)nonGenericList[index]!; |
| | 102 | | } |
| | 103 | |
|
| 182167 | 104 | | return source.ElementAt(index); |
| 182177 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Optimized version of <see cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)"/>, which |
| | 109 | | /// runs in constant time on <paramref name="source"/> of type <see cref="string"/>, <see cref="IList{T}"/> |
| | 110 | | /// and <see cref="IList"/>, and calls |
| | 111 | | /// <see cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)"/> |
| | 112 | | /// for any <paramref name="source"/> which cannot be assigned to either of these types. |
| | 113 | | /// </summary> |
| | 114 | | /// <typeparam name="TSource"> |
| | 115 | | /// <inheritdoc cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)" |
| | 116 | | /// path="/typeparam[@name='TSource']"/> |
| | 117 | | /// </typeparam> |
| | 118 | | /// <param name="source"> |
| | 119 | | /// <inheritdoc cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)" |
| | 120 | | /// path="/param[@name='source']"/> |
| | 121 | | /// </param> |
| | 122 | | /// <param name="index"> |
| | 123 | | /// <inheritdoc cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)" |
| | 124 | | /// path="/param[@name='index']"/> |
| | 125 | | /// </param> |
| | 126 | | /// <returns> |
| | 127 | | /// <inheritdoc cref="Enumerable.ElementAtOrDefault{TSource}(IEnumerable{TSource}, int)" |
| | 128 | | /// path="/returns"/> |
| | 129 | | /// </returns> |
| | 130 | | public static TSource? ElementAtO1OrDefault<TSource>(this IEnumerable<TSource> source, int index) |
| 302 | 131 | | { |
| 302 | 132 | | if (index < 0) |
| 5 | 133 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(index)}: {index}"); |
| | 134 | |
|
| 297 | 135 | | if (source is string str) |
| 26 | 136 | | return index >= 0 && index < str.Length ? (TSource)(str[index] as object) : default; |
| 271 | 137 | | if (source is IList<TSource> genericList) |
| 48 | 138 | | return index >= 0 && index < genericList.Count ? genericList[index] : default; |
| 223 | 139 | | if (source is IList nonGenericList) |
| 3 | 140 | | return index >= 0 && index < nonGenericList.Count ? (TSource)nonGenericList[index]! : default; |
| 220 | 141 | | return source.ElementAtOrDefault(index); |
| 297 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Eagerly enumerates the first <paramref name="count"/> items of <paramref name="source"/>, returning an |
| | 146 | | /// <see cref="IList{T}"/> of them and the reminder, as a lazily evaluated <see cref="IEnumerable{T}"/>. |
| | 147 | | /// </summary> |
| | 148 | | /// <typeparam name="TSource">The type of elements of <paramref name="source"/>.</typeparam> |
| | 149 | | /// <param name="source">The <see cref="IEnumerable{T}"/> to split into two pieces.</param> |
| | 150 | | /// <param name="count">The number of items of <paramref name="source"/> to eagerly evaluate and return.</param> |
| | 151 | | /// <returns> |
| | 152 | | /// A couple of an <see cref="IList{T}"/> instance and an <see cref="IEnumerable{T}"/> instance. |
| | 153 | | /// </returns> |
| | 154 | | public static (IList<TSource> firstNItems, IEnumerable<TSource> reminder) EnumerateExactlyFirst<TSource>( |
| | 155 | | this IEnumerable<TSource> source, int count) |
| 121 | 156 | | { |
| 121 | 157 | | var (first, reminder) = source.EnumerateAtMostFirst(count); |
| | 158 | |
|
| 119 | 159 | | if (first.Count < count) |
| 4 | 160 | | throw new ArgumentException($"Sequence doesn't contain {count} elements.", nameof(source)); |
| | 161 | |
|
| 115 | 162 | | return (first, reminder); |
| 115 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Eagerly enumerates the first <paramref name="count"/> items of <paramref name="source"/>, or less if there |
| | 167 | | /// aren't enough, returning an <see cref="IList{T}"/> of them and the reminder, as a lazily evaluated |
| | 168 | | /// <see cref="IEnumerable{T}"/>. |
| | 169 | | /// </summary> |
| | 170 | | /// <typeparam name="TSource">The type of elements of <paramref name="source"/>.</typeparam> |
| | 171 | | /// <param name="source">The <see cref="IEnumerable{T}"/> to split into two pieces.</param> |
| | 172 | | /// <param name="count"> |
| | 173 | | /// The number of items of <paramref name="source"/> to eagerly evaluate and return (at most). |
| | 174 | | /// </param> |
| | 175 | | /// <returns> |
| | 176 | | /// A couple of an <see cref="IList{T}"/> instance and an <see cref="IEnumerable{T}"/> instance. |
| | 177 | | /// </returns> |
| | 178 | | public static (IList<TSource> firstNItems, IEnumerable<TSource> reminder) EnumerateAtMostFirst<TSource>( |
| | 179 | | this IEnumerable<TSource> source, int count) |
| 164 | 180 | | { |
| 164 | 181 | | if (count < 0) |
| 2 | 182 | | throw new ArgumentOutOfRangeException($"Invalid {nameof(count)}: {count}"); |
| | 183 | |
|
| 162 | 184 | | var enumerator = source.GetEnumerator(); |
| | 185 | |
|
| 162 | 186 | | var first = new List<TSource> { }; |
| 666 | 187 | | for (var i = 0; i < count; i++) |
| 185 | 188 | | { |
| 185 | 189 | | if (!enumerator.MoveNext()) |
| 14 | 190 | | break; |
| | 191 | |
|
| 171 | 192 | | first.Add(enumerator.Current); |
| 171 | 193 | | } |
| | 194 | |
|
| 162 | 195 | | return (first, ToEnumerable(enumerator)); |
| 162 | 196 | | } |
| | 197 | |
|
| | 198 | | private static IEnumerable<TSource> ToEnumerable<TSource>(IEnumerator<TSource> enumerator) |
| 109 | 199 | | { |
| 668 | 200 | | while (enumerator.MoveNext()) |
| 559 | 201 | | yield return enumerator.Current; |
| 109 | 202 | | } |
| | 203 | |
|
| | 204 | | /// <summary> |
| | 205 | | /// Optimized version of <see cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)"/>, which |
| | 206 | | /// runs in constant time on <paramref name="source"/> of type <see cref="string"/>, <see cref="IList{T}"/> |
| | 207 | | /// and <see cref="IList"/>, and calls |
| | 208 | | /// <see cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)"/> |
| | 209 | | /// for any <paramref name="source"/> which cannot be assigned to either of these types. |
| | 210 | | /// </summary> |
| | 211 | | /// <typeparam name="TSource"> |
| | 212 | | /// <inheritdoc cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)" |
| | 213 | | /// path="/typeparam[@name='TSource']"/> |
| | 214 | | /// </typeparam> |
| | 215 | | /// <param name="source"> |
| | 216 | | /// <inheritdoc cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)" |
| | 217 | | /// path="/param[@name='source']"/> |
| | 218 | | /// </param> |
| | 219 | | /// <param name="count"> |
| | 220 | | /// <inheritdoc cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)" |
| | 221 | | /// path="/param[@name='count']"/> |
| | 222 | | /// </param> |
| | 223 | | /// <returns> |
| | 224 | | /// <inheritdoc cref="Enumerable.Skip{TSource}(IEnumerable{TSource}, int)" |
| | 225 | | /// path="/returns"/> |
| | 226 | | /// </returns> |
| | 227 | | public static IEnumerable<TSource> SkipO1<TSource>(this IEnumerable<TSource> source, int count) |
| 637 | 228 | | { |
| 637 | 229 | | count = count >= 0 ? count : 0; |
| | 230 | |
|
| 637 | 231 | | if (source is string str) |
| 98 | 232 | | { |
| 1374 | 233 | | while (count < str.Length) |
| 1367 | 234 | | yield return (TSource)(str[count++] as object); |
| 7 | 235 | | yield break; |
| | 236 | | } |
| | 237 | |
|
| 539 | 238 | | if (source is IList<TSource> genericList) |
| 430 | 239 | | { |
| 2673 | 240 | | while (count < genericList.Count) |
| 2666 | 241 | | yield return genericList[count++]; |
| 7 | 242 | | yield break; |
| | 243 | | } |
| | 244 | |
|
| 109 | 245 | | if (source is IList nonGenericList) |
| 7 | 246 | | { |
| 18 | 247 | | while (count < nonGenericList.Count) |
| 11 | 248 | | yield return (TSource)nonGenericList[count++]!; |
| 7 | 249 | | yield break; |
| | 250 | | } |
| | 251 | |
|
| 1961 | 252 | | foreach (var item in source.Skip(count)) |
| 875 | 253 | | yield return item; |
| 7 | 254 | | } |
| | 255 | | } |