From ff9b4f5b2fe15604a2c9a6f2db428da8c96a362d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 16 Mar 2022 21:05:49 -0400 Subject: [PATCH] build: Upgrade dotnet core 5.0 -> 6.0 --- src/client/spark/Spark.csproj | 2 +- src/client/spark_api/spark_api.csproj | 2 +- .../Extensions/EnumerableExtensions.cs | 1716 ++++++++--------- .../TestRunner/TestResultReceiver.cs | 11 +- src/client/spark_core/spark_core.csproj | 2 +- src/client/spark_sdk/spark_sdk.csproj | 14 +- src/client/spark_test/spark_test.csproj | 2 +- src/client/sparkcmd/sparkcmd.csproj | 2 +- tools/cpp.build.props | 16 +- 9 files changed, 879 insertions(+), 888 deletions(-) diff --git a/src/client/spark/Spark.csproj b/src/client/spark/Spark.csproj index c4c35618d..4dde0b8da 100644 --- a/src/client/spark/Spark.csproj +++ b/src/client/spark/Spark.csproj @@ -1,6 +1,6 @@ - net5.0-windows + net6.0-windows true Blue Trading Systems Blue Trading Systems diff --git a/src/client/spark_api/spark_api.csproj b/src/client/spark_api/spark_api.csproj index 5e2ebe905..b342948e0 100644 --- a/src/client/spark_api/spark_api.csproj +++ b/src/client/spark_api/spark_api.csproj @@ -1,6 +1,6 @@ - net5.0 + net6.0 Blue Trading Systems Blue Trading Systems diff --git a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs index a7678d5b7..a2c3c6542 100644 --- a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs @@ -1,862 +1,854 @@ -namespace System.Collections.Generic { - using bts.utils; - - using mbts.utils; - - using System.Collections.Generic; - using System.Collections.ObjectModel; - using System.Diagnostics.CodeAnalysis; - using System.Linq; - using System.Reactive.Linq; - - public static class EnumerableExtensions { - /// - /// Determines whether all elements of a sequence are true. - /// - /// An that contains the elements to which to apply the predicate. - /// True if every element of the source sequence is true, or if the sequence is empty; otherwise false. - /// is null - public static bool All(this IEnumerable source) { - if (source is null) - throw new ArgumentNullException(nameof(source)); - foreach (var v in source) { - if (!v) - return false; - } - return true; - } - - /// - /// Similar to , but returns false if the is empty. - /// - /// The type of elements in the source. - /// The sequence of elements to which the predicate will be applied. - /// The predicate applied to each element in the specified sequence. - /// True if the source sequence is not empty, and every element within it passes the test in the specified predicate; otherwise, false. - public static bool AnyAndAll(this IEnumerable source, Func predicate) { - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - do { - if (!predicate(en.Current)) - return false; - } while (en.MoveNext()); - return true; - } - return false; - } - } - - #region Ordered - public static IOrderedEnumerable Ordered(this IEnumerable enumerable) => - enumerable.OrderBy(t => t); - - public static IEnumerable Ordered(this IEnumerable enumerable, Func compare) => - enumerable.Ordered(Comparer.Create(new Comparison(compare))); - - public static IEnumerable Ordered(this IEnumerable enumerable, IComparer comparer) => - enumerable.OrderBy(t => t, comparer); - #endregion - - public static IEnumerable DistinctOn(this IEnumerable enumerable, Func key_selector) => - enumerable.Distinct(EqualityComparer.Create((x, y) => key_selector(x).Equals(key_selector(y)), x => key_selector(x).GetHashCode())); - - public static IEnumerable Except(this IEnumerable source, params T[] values) => - source.Except(values.AsEnumerable()); - - public static IEnumerable Except(this IEnumerable enumerable, IEnumerable second, Func key_selector) => - enumerable.Where(t => !second.Contains(key_selector(t))); - - public static IEnumerable Intersect(this IEnumerable enumerable, IEnumerable second, Func key_selector) => - enumerable.Where(t => second.Contains(key_selector(t))); - - public static IEnumerable OfTypeSafe(this IEnumerable source) => - source == null ? Enumerable.Empty() : source.OfType(); - - public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate) => - AggregateWhile(source, seed, aggregate, predicate, (curr, el, next) => curr); - - public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate, TAccumulate early_break_rv) => - AggregateWhile(source, seed, aggregate, predicate, (curr, el, next) => early_break_rv); - - public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate, Func sanitize_on_early_break) { - TAccumulate rv = seed; - foreach (T el in source) { - var next = aggregate(rv, el); - if (predicate(next, el)) - rv = next; - else return sanitize_on_early_break(rv, el, next); - } - return rv; - } - - public static IEnumerable AggregateConsecutiveGroups( - this IEnumerable source, - Func seed_factory, - Func aggregate, - Func group_key) => - AggregateConsecutiveGroups(source, seed_factory, aggregate, group_key, EqualityComparer.Default); - - public static IEnumerable AggregateConsecutiveGroups( - this IEnumerable source, - Func seed_factory, - Func aggregate, - Func group_key, - IEqualityComparer group_key_comparer) { - // - if (source is null) - throw new ArgumentNullException(nameof(source)); - if (seed_factory is null) - throw new ArgumentNullException(nameof(seed_factory)); - if (aggregate is null) - throw new ArgumentNullException(nameof(aggregate)); - if (group_key is null) - throw new ArgumentNullException(nameof(group_key)); - if (group_key_comparer is null) - throw new ArgumentNullException(nameof(group_key_comparer)); - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - var curr_group = group_key(en.Current); - var agg = aggregate(seed_factory(), en.Current); - while (en.MoveNext()) { - var group = group_key(en.Current); - if (group_key_comparer.Equals(group, curr_group)) - agg = aggregate(agg, en.Current); - else { - yield return agg; - curr_group = group; - agg = aggregate(seed_factory(), en.Current); - } - } - yield return agg; - } - } - } - - public static bool CountAtLeast(this IEnumerable source, int count) { - if (count < 0) - throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count)); - if (count == 0) - return true; - foreach (var v in source) { - if (--count == 0) - return true; - } - return false; - } - - public static bool CountAtMost(this IEnumerable source, int count) { - if (count < 0) - throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count)); - foreach (var v in source) { - if (--count == -1) - return false; - } - return true; - } - - /// - /// Returns the subsequence of items, in the source sequence, that occur in between and . The relative order of said items is irrelevant. - /// - /// - /// The source sequence used to find a subsequence of items that are in between the specified items. - /// One of two items that will serve as a non-inclusive bookend for the resultant sequence. - /// One of two items that will serve as a non-inclusive bookend for the resultant sequence. - /// The sequence of elements between and . - public static IEnumerable Between(this IEnumerable source, T item1, T item2) { - using (var en = source.GetEnumerator()) { - while (en.MoveNext()) { - if (item1.Equals(en.Current)) { - while (en.MoveNext() && !item2.Equals(en.Current)) - yield return en.Current; - yield break; - } else if (item2.Equals(en.Current)) { - while (en.MoveNext() && !item1.Equals(en.Current)) - yield return en.Current; - yield break; - } - } - } - } - - public static IEnumerable BookEnd(this IEnumerable source, T front_end, T back_end) { - yield return front_end; - foreach (var v in source) - yield return v; - yield return back_end; - } - - public static IEnumerable Convert(this IEnumerable source) { - foreach (var v in source) - yield return ConvertUtils.try_convert(v); - } - - public static IEnumerable ExceptNull(this IEnumerable source) where T : class { - foreach (var v in source) { - if (v != null) - yield return v; - } - } - - public static IEnumerable KeyJoin(this IEnumerable outer_keys, IReadOnlyDictionary inner) { - foreach (var oval in outer_keys) { - if (inner.TryGetValue(oval, out var ival)) - yield return ival; - } - } - - /// - /// Checks if implements or . If so, the resultant Count is returned, otherwise null (will not enumerate through any items). - /// - /// The type of elements of source. - /// A sequence that contains elements to be counted, if possible without enumeration. - /// The number of elements in the input sequence, if the sequence implemented or , otherwise null. - public static int? SoftCount(this IEnumerable source) => - new EnumerableCount(source).Count; - - // use "new [] { item }" - avoid putting this on the intellisense for every item everywhere - //public static IEnumerable ToIEnumerable(this T item) { - // yield return item; - //} - - public static T SafeSingleOrDefault(this IEnumerable source) { - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - var rv_candidate = en.Current; - if (!en.MoveNext()) - return rv_candidate; - } - } - return default; - } - - public static T SafeSingleOrDefault(this IEnumerable source, T default_value) { - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - var rv_candidate = en.Current; - if (!en.MoveNext()) - return rv_candidate; - } - } - return default_value; - } - - public static IEnumerable TakeWhile(this IEnumerable source, TAccumulate seed, Func accumulate, Func predicate) { - TAccumulate a = seed; - foreach (T el in source) { - if (!predicate(a, el)) - break; - a = accumulate(a, el); - yield return el; - } - } - - public static IList ToListLazy(this IEnumerable source) => - new LazyList(source); - - public static bool TryGetFirst(this IEnumerable source, out T result) { - if (source == null) throw new ArgumentNullException(nameof(source)); - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - result = en.Current; - return true; - } - } - result = default; - return false; - } - - public static bool TryGetFirst(this IEnumerable source, Func predicate, out T result) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) { - result = element; - return true; - } - result = default; - return false; - } - - public static bool TryGetFirst(this IEnumerable source, Func predicate, out T result, out int index) { - if (source == null) throw new ArgumentNullException(nameof(source)); - index = 0; - foreach (T element in source) { - if (predicate(element)) { - result = element; - return true; - } - index++; - } - result = default; - return false; - } - - public static bool TryGetFirst(this IEnumerable source, Func predicate, Func select, out TResult result) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) { - result = select(element); - return true; - } - result = default; - return false; - } - - public static bool SafeTryGetFirst([AllowNull] this IEnumerable source, out T result) { - if (source is not null) { - foreach (var v in source) { - result = v; - return true; - } - } - result = default; - return false; - } - - public static bool TryGetSingle(this IEnumerable source, out T result) { - if (source == null) throw new ArgumentNullException(nameof(source)); - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - result = en.Current; - if (!en.MoveNext()) - return true; - } - } - result = default; - return false; - } - - public static bool TryGetSingle(this IEnumerable source, Func predicate, Func convert, out TResult result) { - if (TryGetSingle(source.Where(predicate), out T v)) { - result = convert(v); - return true; - } - result = default; - return false; - } - - public static T FirstOrDefault(this IEnumerable source, Func predicate, T default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) - return element; - return default_rv; - } - - public static T FirstOrDefault(this IEnumerable source, Func predicate, Func default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) - return element; - return default_rv(); - } - - public static TResult SelectFirstOrDefault(this IEnumerable source, Func predicate, Func select) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) - return select(element); - return default; - } - - public static TResult FirstOrDefault(this IEnumerable source, Func predicate, Func select, TResult default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - foreach (T element in source) - if (predicate(element)) - return select(element); - return default_rv; - } - - public static T FirstOrDefault(this IEnumerable source, params Func[] prioritized_predicates) { - if (prioritized_predicates.is_null_or_empty()) { - return source.FirstOrDefault(); - } - foreach (var p in prioritized_predicates) { - if (source.TryGetFirst(p, out var res)) { - return res; - } - } - return default; - } - - public static T LastOrDefault(this IEnumerable source, T default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - var list = source as IList; - if (list != null) { - int count = list.Count; - if (count > 0) return list[count - 1]; - } else { - using (var e = source.GetEnumerator()) - if (e.MoveNext()) { - T result; - do result = e.Current; - while (e.MoveNext()); - return result; - } - } - return default_rv; - } - - public static T LastOrDefault(this IEnumerable source, Func predicate, T default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (predicate == null) throw new ArgumentNullException(nameof(predicate)); - T rv = default_rv; - foreach (T element in source) - if (predicate(element)) rv = element; - return rv; - } - - public static TResult LastOrDefault(this IEnumerable source, Func select, Func predicate) => - LastOrDefault(source, select, predicate, default); - - public static TResult LastOrDefault(this IEnumerable source, Func select, Func predicate, TResult default_rv) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (select == null) throw new ArgumentNullException(nameof(select)); - if (predicate == null) throw new ArgumentNullException(nameof(predicate)); - TResult rv = default_rv; - foreach (T element in source) { - TResult r = select(element); - if (predicate(r)) rv = r; - } - return rv; - } - - public static bool AtLeast(this IEnumerable source, int count) { - if (count < 1) - throw new ArgumentException("Cannot be less than 1.", nameof(count)); - int n = 0; - foreach (T item in source) { - if (++n == count) - return true; - } - return false; - } - - public static bool AtLeast(this IEnumerable source, int count, Func where) { - if (count < 1) - throw new ArgumentException("Cannot be less than 1.", nameof(count)); - int n = 0; - foreach (T item in source) { - if (where(item)) { - if (++n == count) - return true; - } - } - return false; - } - - /// - /// Gets a bool indicating whether or not has - /// or fewer elements - /// - /// True if has - /// or fewer elements, otherwise false - /// - public static bool AtMost(this IEnumerable source, int count) { - foreach (var v in source) { - if (--count < 0) { - return false; - } - } - return true; - } - - public static List ToListSorted(this IEnumerable enumerable, Func key_selector) => - enumerable.ToListSorted(key_selector, Comparer.Default); - - public static List ToListSorted(this IEnumerable enumerable, Func key_selector, IComparer key_comparer) => - enumerable.ToListSorted(key_comparer.wrap(key_selector)); - - public static List ToListSorted(this IEnumerable enumerable, IComparer comparer) { - var list = new List(enumerable); - list.Sort(comparer); - return list; - } - - /// - /// Simply returns enumerable.ToList(), under the guise ofo IReadOnlyList<> - /// - public static IReadOnlyList ToReadOnlyList(this IEnumerable enumerable) => - enumerable.ToList(); - - public static Dictionary ToDictionary(this IEnumerable> key_values) => - key_values.ToDictionary(kv => kv.Key, kv => kv.Value); - - public static Dictionary ToDictionaryCrushed(this IEnumerable source, Func key_selector) { - var rv = new Dictionary(); - foreach (T v in source) - rv[key_selector(v)] = v; - return rv; - } - - public static Dictionary ToDictionaryCrushed(this IEnumerable> source) { - var rv = new Dictionary(); - foreach (var kv in source) { - rv[kv.Key] = kv.Value; - } - return rv; - } - - public static Dictionary ToDictionaryCrushed(this IEnumerable source, Func key_selector, Func element_selector) { - var rv = new Dictionary(); - foreach (T v in source) - rv[key_selector(v)] = element_selector(v); - return rv; - } - - public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector) => - new ReadOnlyDictionary(enumerable.ToDictionary(key_selector)); - - public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, IEqualityComparer comparer) => - new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, comparer)); - - public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, Func element_selector) => - new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, element_selector)); - - public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, Func element_selector, IEqualityComparer comparer) => - new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, element_selector, comparer)); - - public static ReadOnlyDictionary ToReadOnlyDictionaryWhere(this IEnumerable keys, TryGetValue value_selector) { - var mdict = new Dictionary(); - foreach (var k in keys) { - if (value_selector(k, out var val)) - mdict[k] = val; - } - return new ReadOnlyDictionary(mdict); - } - - public static ILookup ToFlatLookup(this IEnumerable source, Func key_selector, Func> selector) => - source.SelectMany(v => { - var k = key_selector(v); - return selector(v).Select(el => (k, el)); - }).ToLookup(t => t.k, t => t.el); - #region Min - public static T Min(this IEnumerable enumerable, Func select_value) where T : IComparable => - Min(enumerable, select_value, default(T)); - - public static T Min(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - T min = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (val.CompareTo(min) == -1) min = val; - } - return min; - } - - public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison) => - Min(enumerable, select_value, comparison, default); - - public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - T min = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (comparison(val, min) == -1) min = val; - } - return min; - } - #endregion - - #region MinOrDefault - public static T MinOrDefault(this IEnumerable source) where T : IComparable => - MinOrDefault(source, default); - - public static T MinOrDefault(this IEnumerable source, T default_value) where T : IComparable { - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - T min = en.Current; - while (en.MoveNext()) { - if (min.CompareTo(en.Current) > 0) - min = en.Current; - } - return min; - } - } - return default_value; - } - #endregion - - #region MinElement - public static TElement MinElement(this IEnumerable enumerable, Func select_value) where T : IComparable => - MinElement(enumerable, select_value, default(TElement)); - - public static TElement MinElement(this IEnumerable enumerable, Func select_value, TElement return_if_empty) where T : IComparable { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - TElement min_el = etr.Current; - T min = select_value(min_el); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (val.CompareTo(min) == -1) { - min_el = etr.Current; - min = val; - } - } - return min_el; - } - - public static TElement MinElement(this IEnumerable enumerable, Func select_value, Comparison comparison) => - MinElement(enumerable, select_value, comparison, default); - - public static TElement MinElement(this IEnumerable enumerable, Func select_value, Comparison comparison, TElement return_if_empty) { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - TElement min_el = etr.Current; - T min = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (comparison(val, min) == -1) { - min_el = etr.Current; - min = val; - } - } - return min_el; - } - #endregion - - #region Max - public static T Max(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - T max = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (val.CompareTo(max) == 1) max = val; - } - return max; - } - - public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison) => - Max(enumerable, select_value, comparison, default); - - - public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - T max = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (comparison(val, max) == 1) max = val; - } - return max; - } - #endregion - - #region MaxOrDefault - public static T MaxOrDefault(this IEnumerable source) where T : IComparable => - MaxOrDefault(source, default); - - public static T MaxOrDefault(this IEnumerable source, T default_value) where T : IComparable { - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - T max = en.Current; - while (en.MoveNext()) { - if (max.CompareTo(en.Current) < 0) - max = en.Current; - } - return max; - } - } - return default_value; - } - #endregion - - #region MaxElement - public static TElement MaxElement(this IEnumerable enumerable, Func select_value) where T : IComparable => - MaxElement(enumerable, select_value, default(TElement)); - - public static TElement MaxElement(this IEnumerable enumerable, Func select_value, TElement return_if_empty) where T : IComparable { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - TElement max_el = etr.Current; - T max = select_value(max_el); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (val.CompareTo(max) == 1) { - max_el = etr.Current; - max = val; - } - } - return max_el; - } - - public static TElement MaxElement(this IEnumerable enumerable, Func select_value, Comparison comparison) => - MaxElement(enumerable, select_value, comparison, default); - - public static TElement MaxElement(this IEnumerable enumerable, Func select_value, Comparison comparison, TElement return_if_empty) { - var etr = enumerable.GetEnumerator(); - if (!etr.MoveNext()) return return_if_empty; - TElement max_el = etr.Current; - T max = select_value(etr.Current); - while (etr.MoveNext()) { - T val = select_value(etr.Current); - if (comparison(val, max) == 1) { - max_el = etr.Current; - max = val; - } - } - return max_el; - } - #endregion - - public static bool MinMax(this IEnumerable source, out T min, out T max) => - source.MinMax(out min, out max, Comparer.Default); - - public static bool MinMax(this IEnumerable source, out T min, out T max, IComparer comparer) { - using var en = source.GetEnumerator(); - if (!en.MoveNext()) { - min = default; - max = default; - return false; - } - min = en.Current; - max = en.Current; - do { - int c = comparer.Compare(en.Current, min); - if (c == 0) { - continue; - } - if (c < 0) { - min = en.Current; - continue; - } - if (comparer.Compare(en.Current, max) > 0) { - max = en.Current; - } - } while (en.MoveNext()); - return true; - } - - public static (T min, T max)? MinMax(this IEnumerable source) => - source.MinMax(Comparer.Default); - - public static (T min, T max)? MinMax(this IEnumerable source, IComparer comparer) => - source.MinMax(out var min, out var max, comparer) ? (min, max) : null; - - #region Sum - public static uint Sum(this IEnumerable enumerable, Func select) => - Sum(enumerable.Select(select)); - - public static uint Sum(this IEnumerable enumerable) { - uint sum = 0; - foreach (uint val in enumerable) sum += val; - return sum; - } - #endregion - - public static bool is_null_or_empty(this IEnumerable source) => - source == null || !source.Any(); - - public static bool is_null_or_empty(this T[] array) => - array == null || array.Length == 0; - - public static IEnumerable empty_if_null(this IEnumerable source) => - source ?? Enumerable.Empty(); - - public static void DisposeAll(this IEnumerable source) { - foreach (IDisposable d in source) - d.Dispose(); - } - - public static IEnumerable return_if(bool condition, Func get_value) { - if (condition) - yield return get_value(); - } - - /// - /// Uses to convert the elements of an to the specified type. - /// - /// The type to convert the elements of source to. - /// The that contains the elements to be cast to type - /// An that contains each element of the source sequence cast to the specified type. - /// is null. - public static IEnumerable convert(this IEnumerable source) { - ExceptionAssert.Argument.NotNull(source, nameof(source)); - foreach (object obj in source) - yield return ConvertUtils.try_convert(obj); - } - - struct EnumerableCount { - readonly ICollection _collection; - readonly IReadOnlyCollection _ro_collection; - - public EnumerableCount(IEnumerable source) { - _collection = source as ICollection; - _ro_collection = _collection == null ? source as IReadOnlyCollection : null; - } - - public int? Count => _collection?.Count ?? _ro_collection?.Count; - } - - class LazyList : IList { - IEnumerable _source; - IList _list_backing; - - public LazyList(IEnumerable source) => - _source = source; - - public T this[int index] { get => _list[index]; set => _list[index] = value; } - - public int Count => _list.Count; - - public bool IsReadOnly => _list.IsReadOnly; - - IList _list { - get { - if (_list_backing == null) { - if (_source == null) { - _list_backing = new List(0); - } else { - _list_backing = new List(_source); - _source = null; - } - } - return _list_backing; - } - } - - public void Add(T item) => _list.Add(item); - public void Clear() => _list.Clear(); - public bool Contains(T item) => _list.Contains(item); - public void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex); - public IEnumerator GetEnumerator() => _list.GetEnumerator(); - public int IndexOf(T item) => _list.IndexOf(item); - public void Insert(int index, T item) => _list.Insert(index, item); - public bool Remove(T item) => _list.Remove(item); - public void RemoveAt(int index) => _list.RemoveAt(index); - IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator(); - } - - public static void AddRange(this ISet set, IEnumerable items) { - foreach (T t in items) - set.Add(t); - //items.ForEach(i => set.Add(i)); - } - public static void AddRange(this ISet set, params T[] items) { - foreach (T t in items) - set.Add(t); - } - - public static bool safe_sequence_equals(this IEnumerable source, IEnumerable other) => - source.empty_if_null().SequenceEqual(other.empty_if_null()); - - public static bool safe_sequence_equals(this IEnumerable source, IEnumerable other, IEqualityComparer comparer) => - source.empty_if_null().SequenceEqual(other.empty_if_null(), comparer); - - public static void for_each_of_type(this IEnumerable source, Action for_t1, Action for_t2, bool check_if_both_ts = false) { - foreach (var value in source) { - if (value is T1 t1) { - for_t1(t1); - if (!check_if_both_ts) { - continue; - } - } - if (value is T2 t2) { - for_t2(t2); - } - } - } - } -} +namespace System.Collections.Generic { + using bts.utils; + + using mbts.utils; + + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Diagnostics.CodeAnalysis; + using System.Linq; + using System.Reactive.Linq; + + public static class EnumerableExtensions { + /// + /// Determines whether all elements of a sequence are true. + /// + /// An that contains the elements to which to apply the predicate. + /// True if every element of the source sequence is true, or if the sequence is empty; otherwise false. + /// is null + public static bool All(this IEnumerable source) { + if (source is null) + throw new ArgumentNullException(nameof(source)); + foreach (var v in source) { + if (!v) + return false; + } + return true; + } + + /// + /// Similar to , but returns false if the is empty. + /// + /// The type of elements in the source. + /// The sequence of elements to which the predicate will be applied. + /// The predicate applied to each element in the specified sequence. + /// True if the source sequence is not empty, and every element within it passes the test in the specified predicate; otherwise, false. + public static bool AnyAndAll(this IEnumerable source, Func predicate) { + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + do { + if (!predicate(en.Current)) + return false; + } while (en.MoveNext()); + return true; + } + return false; + } + } + + #region Ordered + public static IOrderedEnumerable Ordered(this IEnumerable enumerable) => + enumerable.OrderBy(t => t); + + public static IEnumerable Ordered(this IEnumerable enumerable, Func compare) => + enumerable.Ordered(Comparer.Create(new Comparison(compare))); + + public static IEnumerable Ordered(this IEnumerable enumerable, IComparer comparer) => + enumerable.OrderBy(t => t, comparer); + #endregion + + public static IEnumerable DistinctOn(this IEnumerable enumerable, Func key_selector) => + enumerable.Distinct(EqualityComparer.Create((x, y) => key_selector(x).Equals(key_selector(y)), x => key_selector(x).GetHashCode())); + + public static IEnumerable Except(this IEnumerable source, params T[] values) => + source.Except(values.AsEnumerable()); + + public static IEnumerable Except(this IEnumerable enumerable, IEnumerable second, Func key_selector) => + enumerable.Where(t => !second.Contains(key_selector(t))); + + public static IEnumerable Intersect(this IEnumerable enumerable, IEnumerable second, Func key_selector) => + enumerable.Where(t => second.Contains(key_selector(t))); + + public static IEnumerable OfTypeSafe(this IEnumerable source) => + source == null ? Enumerable.Empty() : source.OfType(); + + public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate) => + AggregateWhile(source, seed, aggregate, predicate, (curr, el, next) => curr); + + public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate, TAccumulate early_break_rv) => + AggregateWhile(source, seed, aggregate, predicate, (curr, el, next) => early_break_rv); + + public static TAccumulate AggregateWhile(this IEnumerable source, TAccumulate seed, Func aggregate, Func predicate, Func sanitize_on_early_break) { + TAccumulate rv = seed; + foreach (T el in source) { + var next = aggregate(rv, el); + if (predicate(next, el)) + rv = next; + else return sanitize_on_early_break(rv, el, next); + } + return rv; + } + + public static IEnumerable AggregateConsecutiveGroups( + this IEnumerable source, + Func seed_factory, + Func aggregate, + Func group_key) => + AggregateConsecutiveGroups(source, seed_factory, aggregate, group_key, EqualityComparer.Default); + + public static IEnumerable AggregateConsecutiveGroups( + this IEnumerable source, + Func seed_factory, + Func aggregate, + Func group_key, + IEqualityComparer group_key_comparer) { + // + if (source is null) + throw new ArgumentNullException(nameof(source)); + if (seed_factory is null) + throw new ArgumentNullException(nameof(seed_factory)); + if (aggregate is null) + throw new ArgumentNullException(nameof(aggregate)); + if (group_key is null) + throw new ArgumentNullException(nameof(group_key)); + if (group_key_comparer is null) + throw new ArgumentNullException(nameof(group_key_comparer)); + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + var curr_group = group_key(en.Current); + var agg = aggregate(seed_factory(), en.Current); + while (en.MoveNext()) { + var group = group_key(en.Current); + if (group_key_comparer.Equals(group, curr_group)) + agg = aggregate(agg, en.Current); + else { + yield return agg; + curr_group = group; + agg = aggregate(seed_factory(), en.Current); + } + } + yield return agg; + } + } + } + + public static bool CountAtLeast(this IEnumerable source, int count) { + if (count < 0) + throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count)); + if (count == 0) + return true; + foreach (var v in source) { + if (--count == 0) + return true; + } + return false; + } + + public static bool CountAtMost(this IEnumerable source, int count) { + if (count < 0) + throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count)); + foreach (var v in source) { + if (--count == -1) + return false; + } + return true; + } + + /// + /// Returns the subsequence of items, in the source sequence, that occur in between and . The relative order of said items is irrelevant. + /// + /// + /// The source sequence used to find a subsequence of items that are in between the specified items. + /// One of two items that will serve as a non-inclusive bookend for the resultant sequence. + /// One of two items that will serve as a non-inclusive bookend for the resultant sequence. + /// The sequence of elements between and . + public static IEnumerable Between(this IEnumerable source, T item1, T item2) { + using (var en = source.GetEnumerator()) { + while (en.MoveNext()) { + if (item1.Equals(en.Current)) { + while (en.MoveNext() && !item2.Equals(en.Current)) + yield return en.Current; + yield break; + } else if (item2.Equals(en.Current)) { + while (en.MoveNext() && !item1.Equals(en.Current)) + yield return en.Current; + yield break; + } + } + } + } + + public static IEnumerable BookEnd(this IEnumerable source, T front_end, T back_end) { + yield return front_end; + foreach (var v in source) + yield return v; + yield return back_end; + } + + public static IEnumerable Convert(this IEnumerable source) { + foreach (var v in source) + yield return ConvertUtils.try_convert(v); + } + + public static IEnumerable ExceptNull(this IEnumerable source) where T : class { + foreach (var v in source) { + if (v != null) + yield return v; + } + } + + public static IEnumerable KeyJoin(this IEnumerable outer_keys, IReadOnlyDictionary inner) { + foreach (var oval in outer_keys) { + if (inner.TryGetValue(oval, out var ival)) + yield return ival; + } + } + + /// + /// Checks if implements or . If so, the resultant Count is returned, otherwise null (will not enumerate through any items). + /// + /// The type of elements of source. + /// A sequence that contains elements to be counted, if possible without enumeration. + /// The number of elements in the input sequence, if the sequence implemented or , otherwise null. + public static int? SoftCount(this IEnumerable source) => + new EnumerableCount(source).Count; + + // use "new [] { item }" - avoid putting this on the intellisense for every item everywhere + //public static IEnumerable ToIEnumerable(this T item) { + // yield return item; + //} + + public static T SafeSingleOrDefault(this IEnumerable source) { + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + var rv_candidate = en.Current; + if (!en.MoveNext()) + return rv_candidate; + } + } + return default; + } + + public static T SafeSingleOrDefault(this IEnumerable source, T default_value) { + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + var rv_candidate = en.Current; + if (!en.MoveNext()) + return rv_candidate; + } + } + return default_value; + } + + public static IEnumerable TakeWhile(this IEnumerable source, TAccumulate seed, Func accumulate, Func predicate) { + TAccumulate a = seed; + foreach (T el in source) { + if (!predicate(a, el)) + break; + a = accumulate(a, el); + yield return el; + } + } + + public static IList ToListLazy(this IEnumerable source) => + new LazyList(source); + + public static bool TryGetFirst(this IEnumerable source, out T result) { + if (source == null) throw new ArgumentNullException(nameof(source)); + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + result = en.Current; + return true; + } + } + result = default; + return false; + } + + public static bool TryGetFirst(this IEnumerable source, Func predicate, out T result) { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (T element in source) + if (predicate(element)) { + result = element; + return true; + } + result = default; + return false; + } + + public static bool TryGetFirst(this IEnumerable source, Func predicate, out T result, out int index) { + if (source == null) throw new ArgumentNullException(nameof(source)); + index = 0; + foreach (T element in source) { + if (predicate(element)) { + result = element; + return true; + } + index++; + } + result = default; + return false; + } + + public static bool TryGetFirst(this IEnumerable source, Func predicate, Func select, out TResult result) { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (T element in source) + if (predicate(element)) { + result = select(element); + return true; + } + result = default; + return false; + } + + public static bool SafeTryGetFirst([AllowNull] this IEnumerable source, out T result) { + if (source is not null) { + foreach (var v in source) { + result = v; + return true; + } + } + result = default; + return false; + } + + public static bool TryGetSingle(this IEnumerable source, out T result) { + if (source == null) throw new ArgumentNullException(nameof(source)); + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + result = en.Current; + if (!en.MoveNext()) + return true; + } + } + result = default; + return false; + } + + public static bool TryGetSingle(this IEnumerable source, Func predicate, Func convert, out TResult result) { + if (TryGetSingle(source.Where(predicate), out T v)) { + result = convert(v); + return true; + } + result = default; + return false; + } + + public static T FirstOrDefault(this IEnumerable source, Func predicate, Func default_rv) { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (T element in source) + if (predicate(element)) + return element; + return default_rv(); + } + + public static TResult SelectFirstOrDefault(this IEnumerable source, Func predicate, Func select) { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (T element in source) + if (predicate(element)) + return select(element); + return default; + } + + public static TResult FirstOrDefault(this IEnumerable source, Func predicate, Func select, TResult default_rv) { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (T element in source) + if (predicate(element)) + return select(element); + return default_rv; + } + + public static T FirstOrDefault(this IEnumerable source, params Func[] prioritized_predicates) { + if (prioritized_predicates.is_null_or_empty()) { + return source.FirstOrDefault(); + } + foreach (var p in prioritized_predicates) { + if (source.TryGetFirst(p, out var res)) { + return res; + } + } + return default; + } + + public static T LastOrDefault(this IEnumerable source, T default_rv) { + if (source == null) throw new ArgumentNullException(nameof(source)); + var list = source as IList; + if (list != null) { + int count = list.Count; + if (count > 0) return list[count - 1]; + } else { + using (var e = source.GetEnumerator()) + if (e.MoveNext()) { + T result; + do result = e.Current; + while (e.MoveNext()); + return result; + } + } + return default_rv; + } + + public static T LastOrDefault(this IEnumerable source, Func predicate, T default_rv) { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (predicate == null) throw new ArgumentNullException(nameof(predicate)); + T rv = default_rv; + foreach (T element in source) + if (predicate(element)) rv = element; + return rv; + } + + public static TResult LastOrDefault(this IEnumerable source, Func select, Func predicate) => + LastOrDefault(source, select, predicate, default); + + public static TResult LastOrDefault(this IEnumerable source, Func select, Func predicate, TResult default_rv) { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (select == null) throw new ArgumentNullException(nameof(select)); + if (predicate == null) throw new ArgumentNullException(nameof(predicate)); + TResult rv = default_rv; + foreach (T element in source) { + TResult r = select(element); + if (predicate(r)) rv = r; + } + return rv; + } + + public static bool AtLeast(this IEnumerable source, int count) { + if (count < 1) + throw new ArgumentException("Cannot be less than 1.", nameof(count)); + int n = 0; + foreach (T item in source) { + if (++n == count) + return true; + } + return false; + } + + public static bool AtLeast(this IEnumerable source, int count, Func where) { + if (count < 1) + throw new ArgumentException("Cannot be less than 1.", nameof(count)); + int n = 0; + foreach (T item in source) { + if (where(item)) { + if (++n == count) + return true; + } + } + return false; + } + + /// + /// Gets a bool indicating whether or not has + /// or fewer elements + /// + /// True if has + /// or fewer elements, otherwise false + /// + public static bool AtMost(this IEnumerable source, int count) { + foreach (var v in source) { + if (--count < 0) { + return false; + } + } + return true; + } + + public static List ToListSorted(this IEnumerable enumerable, Func key_selector) => + enumerable.ToListSorted(key_selector, Comparer.Default); + + public static List ToListSorted(this IEnumerable enumerable, Func key_selector, IComparer key_comparer) => + enumerable.ToListSorted(key_comparer.wrap(key_selector)); + + public static List ToListSorted(this IEnumerable enumerable, IComparer comparer) { + var list = new List(enumerable); + list.Sort(comparer); + return list; + } + + /// + /// Simply returns enumerable.ToList(), under the guise ofo IReadOnlyList<> + /// + public static IReadOnlyList ToReadOnlyList(this IEnumerable enumerable) => + enumerable.ToList(); + + public static Dictionary ToDictionary(this IEnumerable> key_values) => + key_values.ToDictionary(kv => kv.Key, kv => kv.Value); + + public static Dictionary ToDictionaryCrushed(this IEnumerable source, Func key_selector) { + var rv = new Dictionary(); + foreach (T v in source) + rv[key_selector(v)] = v; + return rv; + } + + public static Dictionary ToDictionaryCrushed(this IEnumerable> source) { + var rv = new Dictionary(); + foreach (var kv in source) { + rv[kv.Key] = kv.Value; + } + return rv; + } + + public static Dictionary ToDictionaryCrushed(this IEnumerable source, Func key_selector, Func element_selector) { + var rv = new Dictionary(); + foreach (T v in source) + rv[key_selector(v)] = element_selector(v); + return rv; + } + + public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector) => + new ReadOnlyDictionary(enumerable.ToDictionary(key_selector)); + + public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, IEqualityComparer comparer) => + new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, comparer)); + + public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, Func element_selector) => + new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, element_selector)); + + public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, Func element_selector, IEqualityComparer comparer) => + new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, element_selector, comparer)); + + public static ReadOnlyDictionary ToReadOnlyDictionaryWhere(this IEnumerable keys, TryGetValue value_selector) { + var mdict = new Dictionary(); + foreach (var k in keys) { + if (value_selector(k, out var val)) + mdict[k] = val; + } + return new ReadOnlyDictionary(mdict); + } + + public static ILookup ToFlatLookup(this IEnumerable source, Func key_selector, Func> selector) => + source.SelectMany(v => { + var k = key_selector(v); + return selector(v).Select(el => (k, el)); + }).ToLookup(t => t.k, t => t.el); + #region Min + public static T Min(this IEnumerable enumerable, Func select_value) where T : IComparable => + Min(enumerable, select_value, default(T)); + + public static T Min(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T min = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(min) == -1) min = val; + } + return min; + } + + public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison) => + Min(enumerable, select_value, comparison, default); + + public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T min = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, min) == -1) min = val; + } + return min; + } + #endregion + + #region MinOrDefault + public static T MinOrDefault(this IEnumerable source) where T : IComparable => + MinOrDefault(source, default); + + public static T MinOrDefault(this IEnumerable source, T default_value) where T : IComparable { + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + T min = en.Current; + while (en.MoveNext()) { + if (min.CompareTo(en.Current) > 0) + min = en.Current; + } + return min; + } + } + return default_value; + } + #endregion + + #region MinElement + public static TElement MinElement(this IEnumerable enumerable, Func select_value) where T : IComparable => + MinElement(enumerable, select_value, default(TElement)); + + public static TElement MinElement(this IEnumerable enumerable, Func select_value, TElement return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + TElement min_el = etr.Current; + T min = select_value(min_el); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(min) == -1) { + min_el = etr.Current; + min = val; + } + } + return min_el; + } + + public static TElement MinElement(this IEnumerable enumerable, Func select_value, Comparison comparison) => + MinElement(enumerable, select_value, comparison, default); + + public static TElement MinElement(this IEnumerable enumerable, Func select_value, Comparison comparison, TElement return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + TElement min_el = etr.Current; + T min = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, min) == -1) { + min_el = etr.Current; + min = val; + } + } + return min_el; + } + #endregion + + #region Max + public static T Max(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T max = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(max) == 1) max = val; + } + return max; + } + + public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison) => + Max(enumerable, select_value, comparison, default); + + + public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T max = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, max) == 1) max = val; + } + return max; + } + #endregion + + #region MaxOrDefault + public static T MaxOrDefault(this IEnumerable source) where T : IComparable => + MaxOrDefault(source, default); + + public static T MaxOrDefault(this IEnumerable source, T default_value) where T : IComparable { + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + T max = en.Current; + while (en.MoveNext()) { + if (max.CompareTo(en.Current) < 0) + max = en.Current; + } + return max; + } + } + return default_value; + } + #endregion + + #region MaxElement + public static TElement MaxElement(this IEnumerable enumerable, Func select_value) where T : IComparable => + MaxElement(enumerable, select_value, default(TElement)); + + public static TElement MaxElement(this IEnumerable enumerable, Func select_value, TElement return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + TElement max_el = etr.Current; + T max = select_value(max_el); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(max) == 1) { + max_el = etr.Current; + max = val; + } + } + return max_el; + } + + public static TElement MaxElement(this IEnumerable enumerable, Func select_value, Comparison comparison) => + MaxElement(enumerable, select_value, comparison, default); + + public static TElement MaxElement(this IEnumerable enumerable, Func select_value, Comparison comparison, TElement return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + TElement max_el = etr.Current; + T max = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, max) == 1) { + max_el = etr.Current; + max = val; + } + } + return max_el; + } + #endregion + + public static bool MinMax(this IEnumerable source, out T min, out T max) => + source.MinMax(out min, out max, Comparer.Default); + + public static bool MinMax(this IEnumerable source, out T min, out T max, IComparer comparer) { + using var en = source.GetEnumerator(); + if (!en.MoveNext()) { + min = default; + max = default; + return false; + } + min = en.Current; + max = en.Current; + do { + int c = comparer.Compare(en.Current, min); + if (c == 0) { + continue; + } + if (c < 0) { + min = en.Current; + continue; + } + if (comparer.Compare(en.Current, max) > 0) { + max = en.Current; + } + } while (en.MoveNext()); + return true; + } + + public static (T min, T max)? MinMax(this IEnumerable source) => + source.MinMax(Comparer.Default); + + public static (T min, T max)? MinMax(this IEnumerable source, IComparer comparer) => + source.MinMax(out var min, out var max, comparer) ? (min, max) : null; + + #region Sum + public static uint Sum(this IEnumerable enumerable, Func select) => + Sum(enumerable.Select(select)); + + public static uint Sum(this IEnumerable enumerable) { + uint sum = 0; + foreach (uint val in enumerable) sum += val; + return sum; + } + #endregion + + public static bool is_null_or_empty(this IEnumerable source) => + source == null || !source.Any(); + + public static bool is_null_or_empty(this T[] array) => + array == null || array.Length == 0; + + public static IEnumerable empty_if_null(this IEnumerable source) => + source ?? Enumerable.Empty(); + + public static void DisposeAll(this IEnumerable source) { + foreach (IDisposable d in source) + d.Dispose(); + } + + public static IEnumerable return_if(bool condition, Func get_value) { + if (condition) + yield return get_value(); + } + + /// + /// Uses to convert the elements of an to the specified type. + /// + /// The type to convert the elements of source to. + /// The that contains the elements to be cast to type + /// An that contains each element of the source sequence cast to the specified type. + /// is null. + public static IEnumerable convert(this IEnumerable source) { + ExceptionAssert.Argument.NotNull(source, nameof(source)); + foreach (object obj in source) + yield return ConvertUtils.try_convert(obj); + } + + struct EnumerableCount { + readonly ICollection _collection; + readonly IReadOnlyCollection _ro_collection; + + public EnumerableCount(IEnumerable source) { + _collection = source as ICollection; + _ro_collection = _collection == null ? source as IReadOnlyCollection : null; + } + + public int? Count => _collection?.Count ?? _ro_collection?.Count; + } + + class LazyList : IList { + IEnumerable _source; + IList _list_backing; + + public LazyList(IEnumerable source) => + _source = source; + + public T this[int index] { get => _list[index]; set => _list[index] = value; } + + public int Count => _list.Count; + + public bool IsReadOnly => _list.IsReadOnly; + + IList _list { + get { + if (_list_backing == null) { + if (_source == null) { + _list_backing = new List(0); + } else { + _list_backing = new List(_source); + _source = null; + } + } + return _list_backing; + } + } + + public void Add(T item) => _list.Add(item); + public void Clear() => _list.Clear(); + public bool Contains(T item) => _list.Contains(item); + public void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex); + public IEnumerator GetEnumerator() => _list.GetEnumerator(); + public int IndexOf(T item) => _list.IndexOf(item); + public void Insert(int index, T item) => _list.Insert(index, item); + public bool Remove(T item) => _list.Remove(item); + public void RemoveAt(int index) => _list.RemoveAt(index); + IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator(); + } + + public static void AddRange(this ISet set, IEnumerable items) { + foreach (T t in items) + set.Add(t); + //items.ForEach(i => set.Add(i)); + } + public static void AddRange(this ISet set, params T[] items) { + foreach (T t in items) + set.Add(t); + } + + public static bool safe_sequence_equals(this IEnumerable source, IEnumerable other) => + source.empty_if_null().SequenceEqual(other.empty_if_null()); + + public static bool safe_sequence_equals(this IEnumerable source, IEnumerable other, IEqualityComparer comparer) => + source.empty_if_null().SequenceEqual(other.empty_if_null(), comparer); + + public static void for_each_of_type(this IEnumerable source, Action for_t1, Action for_t2, bool check_if_both_ts = false) { + foreach (var value in source) { + if (value is T1 t1) { + for_t1(t1); + if (!check_if_both_ts) { + continue; + } + } + if (value is T2 t2) { + for_t2(t2); + } + } + } + } +} diff --git a/src/client/spark_core/TestRunner/TestResultReceiver.cs b/src/client/spark_core/TestRunner/TestResultReceiver.cs index 9063a0c59..2fe859126 100644 --- a/src/client/spark_core/TestRunner/TestResultReceiver.cs +++ b/src/client/spark_core/TestRunner/TestResultReceiver.cs @@ -43,11 +43,10 @@ namespace bts.test { return print_result; } private void post_result(uint port, TestResult result) { - var wr = WebRequest.Create($"http://localhost:{port}/"); - wr.Method = "POST"; - wr.ContentLength = result.CalculateSize(); - result.WriteTo(wr.GetRequestStream()); - var unused = wr.GetResponseAsync(); + var client = new HttpClient(); + using var ms = new MemoryStream(); + result.WriteTo(ms); + client.PostAsync($"http://localhost:{port}/", new ByteArrayContent(ms.ToArray())); } @@ -134,4 +133,4 @@ namespace bts.test { return port; } } -} \ No newline at end of file +} diff --git a/src/client/spark_core/spark_core.csproj b/src/client/spark_core/spark_core.csproj index 2fb1d282c..685601605 100644 --- a/src/client/spark_core/spark_core.csproj +++ b/src/client/spark_core/spark_core.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 true x64 x64 diff --git a/src/client/spark_sdk/spark_sdk.csproj b/src/client/spark_sdk/spark_sdk.csproj index e0b3de150..94eae22dc 100644 --- a/src/client/spark_sdk/spark_sdk.csproj +++ b/src/client/spark_sdk/spark_sdk.csproj @@ -1,9 +1,9 @@  - net5.0 + net6.0 Exe true - $(OutputPath)\net5.0 + $(OutputPath)\net6.0 true true $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage @@ -40,19 +40,19 @@ - + - - + + - ..\..\lib\net5.0\spark_api.dll + ..\..\lib\net6.0\spark_api.dll - + diff --git a/src/client/spark_test/spark_test.csproj b/src/client/spark_test/spark_test.csproj index 6cce394bf..e4101256d 100644 --- a/src/client/spark_test/spark_test.csproj +++ b/src/client/spark_test/spark_test.csproj @@ -1,7 +1,7 @@  - net5.0 + net6.0 x64 x64 diff --git a/src/client/sparkcmd/sparkcmd.csproj b/src/client/sparkcmd/sparkcmd.csproj index 9299ab8b4..9aea04fce 100644 --- a/src/client/sparkcmd/sparkcmd.csproj +++ b/src/client/sparkcmd/sparkcmd.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 win10-x64;ubuntu.16.04-x64 Blue Trading Systems Blue Trading Systems diff --git a/tools/cpp.build.props b/tools/cpp.build.props index 7111f852b..b35c7897b 100644 --- a/tools/cpp.build.props +++ b/tools/cpp.build.props @@ -2,44 +2,44 @@ $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory))) - $(BuildRoot)\$(Configuration)\bin\net5.0\ - $(BuildRoot)\$(Configuration)\bin\net5.0-windows\ + $(BuildRoot)\$(Configuration)\bin\net6.0\ + $(BuildRoot)\$(Configuration)\bin\net6.0-windows\ None