| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EnumerableExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Collections; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class EnumerableExtensions |
| | | 18 | | { |
| | | 19 | | extension<T>(IEnumerable<T> source) |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Filters out null elements from the sequence and returns a non-nullable enumerable of the remaining items. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <returns>A non-nullable enumerable containing the non-null elements.</returns> |
| | 3 | 25 | | public IEnumerable<T> NotNull() => source.Where(e => e is not null).Select(e => e!); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Converts an <see cref="IEnumerable{T}"/> to an <see cref="ObservableCollection{T}"/>, reusing the source whe |
| | | 29 | | /// </summary> |
| | | 30 | | /// <returns>An observable collection containing the sequence elements.</returns> |
| | | 31 | | public ObservableCollection<T> ToObservableCollection() |
| | | 32 | | { |
| | 6 | 33 | | ArgumentNullException.ThrowIfNull(source); |
| | | 34 | | |
| | 6 | 35 | | return source as ObservableCollection<T> ?? new(source); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Iterates the sequence and invokes the provided action for each element. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="action">The action to invoke for each element.</param> |
| | | 42 | | public void ForEach(Action<T> action) |
| | | 43 | | { |
| | 3 | 44 | | ArgumentNullException.ThrowIfNull(source); |
| | 3 | 45 | | ArgumentNullException.ThrowIfNull(action); |
| | | 46 | | |
| | 24 | 47 | | foreach (var item in source) |
| | | 48 | | { |
| | 9 | 49 | | action(item); |
| | | 50 | | } |
| | 3 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Iterates the sequence and invokes the provided action for each element including the element index. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="action">The action to invoke for each element receiving the element and its zero-based index.</ |
| | | 57 | | public void ForEach(Action<T, int> action) |
| | | 58 | | { |
| | 3 | 59 | | ArgumentNullException.ThrowIfNull(source); |
| | 3 | 60 | | ArgumentNullException.ThrowIfNull(action); |
| | | 61 | | |
| | 3 | 62 | | var i = 0; |
| | 24 | 63 | | foreach (var item in source) |
| | | 64 | | { |
| | 9 | 65 | | action(item, i); |
| | 9 | 66 | | i++; |
| | | 67 | | } |
| | 3 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Sums a sequence of <see cref="TimeSpan"/> values projected from the source. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="selector">A selector that returns a <see cref="TimeSpan"/> for each element.</param> |
| | | 74 | | /// <returns>The aggregated <see cref="TimeSpan"/>.</returns> |
| | | 75 | | public TimeSpan Sum(Func<T, TimeSpan> selector) |
| | | 76 | | { |
| | 3 | 77 | | ArgumentNullException.ThrowIfNull(source); |
| | 3 | 78 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 79 | | |
| | 3 | 80 | | return source.Aggregate(TimeSpan.Zero, (current, item) => current + selector(item)); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Rotates the sequence by the specified offset, moving the first <paramref name="offset"/> elements to the end |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="offset">The number of elements to skip from the start.</param> |
| | | 87 | | /// <returns>A rotated sequence.</returns> |
| | | 88 | | public IEnumerable<T> Rotate(int offset) |
| | | 89 | | { |
| | 9 | 90 | | var list = source as IList<T> ?? [.. source]; |
| | | 91 | | |
| | 9 | 92 | | var count = list.Count; |
| | 9 | 93 | | if (count == 0) |
| | 3 | 94 | | yield break; |
| | | 95 | | |
| | 6 | 96 | | offset %= count; |
| | 6 | 97 | | if (offset < 0) |
| | 3 | 98 | | offset += count; |
| | | 99 | | |
| | 60 | 100 | | for (var i = 0; i < count; i++) |
| | 24 | 101 | | yield return list[(i + offset) % count]; |
| | 6 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Returns the average of a projected integer sequence or <paramref name="defaultValue"/> when the sequence is |
| | | 106 | | /// </summary> |
| | | 107 | | public double AverageOrDefault(Func<T, int> selector, int defaultValue = 0) |
| | 9 | 108 | | => source.AverageOrDefault(x => (double)selector(x), defaultValue); |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Returns the average of a projected double sequence or <paramref name="defaultValue"/> when the sequence is e |
| | | 112 | | /// </summary> |
| | | 113 | | public double AverageOrDefault(Func<T, double> selector, double defaultValue = 0) |
| | | 114 | | { |
| | 15 | 115 | | ArgumentNullException.ThrowIfNull(source); |
| | 15 | 116 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 117 | | |
| | 15 | 118 | | var sum = 0.0d; |
| | 15 | 119 | | var count = 0; |
| | | 120 | | |
| | 84 | 121 | | foreach (var item in source) |
| | | 122 | | { |
| | 27 | 123 | | sum += selector(item); |
| | 27 | 124 | | count++; |
| | | 125 | | } |
| | | 126 | | |
| | 15 | 127 | | return count == 0 ? defaultValue : sum / count; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Returns the average of a projected decimal sequence or <paramref name="defaultValue"/> when the sequence is |
| | | 132 | | /// </summary> |
| | | 133 | | public decimal AverageOrDefault(Func<T, decimal> selector, decimal defaultValue = 0) |
| | | 134 | | { |
| | 6 | 135 | | ArgumentNullException.ThrowIfNull(source); |
| | 6 | 136 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 137 | | |
| | 6 | 138 | | var sum = 0.0m; |
| | 6 | 139 | | var count = 0; |
| | | 140 | | |
| | 30 | 141 | | foreach (var item in source) |
| | | 142 | | { |
| | 9 | 143 | | sum += selector(item); |
| | 9 | 144 | | count++; |
| | | 145 | | } |
| | | 146 | | |
| | 6 | 147 | | return count == 0 ? defaultValue : sum / count; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Returns the maximum projected value from the sequence or <paramref name="defaultValue"/> when the sequence i |
| | | 152 | | /// </summary> |
| | | 153 | | public TResult MaxOrDefault<TResult>(Func<T, TResult> selector, TResult defaultValue = default) |
| | | 154 | | where TResult : struct, IComparable |
| | | 155 | | { |
| | 6 | 156 | | ArgumentNullException.ThrowIfNull(source); |
| | | 157 | | |
| | 6 | 158 | | using var e = source.GetEnumerator(); |
| | 6 | 159 | | if (!e.MoveNext()) |
| | 3 | 160 | | return defaultValue; |
| | | 161 | | |
| | 3 | 162 | | var max = selector(e.Current); |
| | | 163 | | |
| | 9 | 164 | | while (e.MoveNext()) |
| | | 165 | | { |
| | 6 | 166 | | var current = selector(e.Current); |
| | 6 | 167 | | if (current.CompareTo(max) > 0) |
| | 3 | 168 | | max = current; |
| | | 169 | | } |
| | | 170 | | |
| | 3 | 171 | | return max; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Returns the minimum projected value from the sequence or <paramref name="defaultValue"/> when the sequence i |
| | | 176 | | /// </summary> |
| | | 177 | | public TResult MinOrDefault<TResult>(Func<T, TResult> selector, TResult defaultValue = default) |
| | | 178 | | where TResult : struct, IComparable |
| | | 179 | | { |
| | 6 | 180 | | ArgumentNullException.ThrowIfNull(source); |
| | | 181 | | |
| | 6 | 182 | | using var e = source.GetEnumerator(); |
| | 6 | 183 | | if (!e.MoveNext()) |
| | 3 | 184 | | return defaultValue; |
| | | 185 | | |
| | 3 | 186 | | var min = selector(e.Current); |
| | | 187 | | |
| | 9 | 188 | | while (e.MoveNext()) |
| | | 189 | | { |
| | 6 | 190 | | var current = selector(e.Current); |
| | 6 | 191 | | if (current.CompareTo(min) < 0) |
| | 0 | 192 | | min = current; |
| | | 193 | | } |
| | | 194 | | |
| | 3 | 195 | | return min; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Generates round-robin pairings for the sequence items. If the number of items is odd, a default placeholder |
| | | 200 | | /// </summary> |
| | | 201 | | /// <returns>A collection of rounds, each round being a sequence of pairs.</returns> |
| | | 202 | | public IEnumerable<IEnumerable<(T Item1, T Item2)>> RoundRobin() |
| | | 203 | | { |
| | 3 | 204 | | var list = source.ToList(); |
| | | 205 | | |
| | 3 | 206 | | if (list.Count % 2 != 0) |
| | 0 | 207 | | list.Add(default!); |
| | | 208 | | |
| | 3 | 209 | | var n = list.Count; |
| | 3 | 210 | | var rounds = n - 1; |
| | | 211 | | |
| | 24 | 212 | | for (var r = 0; r < rounds; r++) |
| | | 213 | | { |
| | 9 | 214 | | var round = new List<(T, T)>(n / 2); |
| | | 215 | | |
| | 54 | 216 | | for (var i = 0; i < n / 2; i++) |
| | | 217 | | { |
| | 18 | 218 | | var a = list[i]; |
| | 18 | 219 | | var b = list[n - 1 - i]; |
| | | 220 | | |
| | 18 | 221 | | if (a is not null && b is not null) |
| | 18 | 222 | | round.Add((a, b)); |
| | | 223 | | } |
| | | 224 | | |
| | 9 | 225 | | yield return round; |
| | | 226 | | |
| | | 227 | | // rotate (circle method) |
| | 9 | 228 | | var last = list[^1]; |
| | | 229 | | |
| | 9 | 230 | | list.RemoveAt(list.Count - 1); |
| | 9 | 231 | | list.Insert(1, last); |
| | | 232 | | } |
| | 3 | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | extension<T, TId>(IEnumerable<T> source) |
| | | 237 | | where T : IIdentifiable<TId> |
| | | 238 | | { |
| | | 239 | | /// <summary> |
| | | 240 | | /// Finds an item by its identifier or returns the default when not found. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="id">The identifier to search for.</param> |
| | | 243 | | /// <returns>The matching element or <c>null</c> if not found.</returns> |
| | | 244 | | public T? GetByIdOrDefault(TId? id) |
| | | 245 | | { |
| | 12 | 246 | | ArgumentNullException.ThrowIfNull(source); |
| | | 247 | | |
| | 48 | 248 | | foreach (var item in source) |
| | | 249 | | { |
| | 15 | 250 | | if (Equals(item.Id, id)) |
| | 6 | 251 | | return item; |
| | | 252 | | } |
| | | 253 | | |
| | 6 | 254 | | return default; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Finds an item by its identifier or throws when not found. |
| | | 259 | | /// </summary> |
| | | 260 | | public T GetById(TId id) |
| | | 261 | | { |
| | 6 | 262 | | var result = source.GetByIdOrDefault(id); |
| | | 263 | | |
| | 6 | 264 | | return result ?? throw new KeyNotFoundException($"Id '{id}' not found."); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Determines whether any element in the sequence has the specified identifier. |
| | | 269 | | /// </summary> |
| | | 270 | | public bool HasId(TId id) |
| | | 271 | | { |
| | 6 | 272 | | ArgumentNullException.ThrowIfNull(source); |
| | | 273 | | |
| | 6 | 274 | | return source.Any(item => Equals(item.Id, id)); |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | extension(IEnumerable<string> source) |
| | | 279 | | { |
| | | 280 | | /// <summary> |
| | | 281 | | /// Filters out null or empty string elements from the sequence and returns a non-nullable enumerable of the rem |
| | | 282 | | /// </summary> |
| | | 283 | | /// <returns>A non-nullable enumerable containing the non-null and non-empty string elements.</returns> |
| | 3 | 284 | | public IEnumerable<string> NotNullOrEmpty() => source.Where(e => !string.IsNullOrWhiteSpace(e)).Select(e => e); |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Returns the average of a sequence of integers or 0 when the sequence is empty. |
| | | 289 | | /// </summary> |
| | 6 | 290 | | public static double AverageOrDefault(this IEnumerable<int> source) => source.AverageOrDefault(x => x); |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Returns the average of a sequence of doubles or 0 when the sequence is empty. |
| | | 294 | | /// </summary> |
| | 3 | 295 | | public static double AverageOrDefault(this IEnumerable<double> source) => source.AverageOrDefault(x => x); |
| | | 296 | | |
| | | 297 | | /// <summary> |
| | | 298 | | /// Returns the average of a sequence of decimals or 0 when the sequence is empty. |
| | | 299 | | /// </summary> |
| | 3 | 300 | | public static decimal AverageOrDefault(this IEnumerable<decimal> source) => source.AverageOrDefault(x => x); |
| | | 301 | | } |
| | | 302 | | |