namespace InnovEnergy.SysTools.Utils; internal static class EnumerableUtils { public static IEnumerable Pad(this IEnumerable src, Int32 length, T padding) { using (var enumerator = src.GetEnumerator()) { while (enumerator.MoveNext() && length-- > 0) yield return enumerator.Current; while (length-- > 0) yield return padding; } } public static Dictionary> IndexColumn(this IEnumerable> src, UInt16 index) { var d = new Dictionary>(); foreach (var outer in src) { var inner = outer.ToList(); var key = inner[index]; d.Add(key, inner); } return d; } public static IEnumerable<(TLeft left, TRight right)> Zip(IEnumerable left, IEnumerable right) { using (var l = left.GetEnumerator()) using (var r = right.GetEnumerator()) while (l.MoveNext() && r.MoveNext()) yield return (l.Current, r.Current); } public static IEnumerator Enumerator(this T t) { yield return t; } public static IEnumerable Enumerable(this T t) { yield return t; } public static IEnumerable Flatten(this IEnumerable> src) => src.SelectMany(s => s); public static void ForEach(this IEnumerable enumerable, Action action) { foreach (var e in enumerable) action(e); } public static IEnumerable Do(this IEnumerable enumerable, Action action) { return enumerable.Select(e => { action(e); return e; }); } public static void ForEach(this IEnumerable enumerable, Func func) { foreach (var e in enumerable) func(e); } public static IEnumerable WhereNot(this IEnumerable enumerable, Func predicate) { return enumerable.Where(e => !predicate(e)); } public static IEnumerable EmptyIfNull(this IEnumerable enumerable) { return enumerable ?? new T[0]; } // // https://stackoverflow.com/a/34006336/141397 // public static Int32 CombineHashes(this ITuple tupled, Int32 seed = 1009, Int32 factor = 9176) // { // var hash = seed; // // for (var i = 0; i < tupled.Length; i++) // { // unchecked // { // hash = hash * factor + tupled[i].GetHashCode(); // } // } // // return hash; // } }