/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static decimal Average <TSource>(this IPoolingEnumerable <TSource> source, Func <TSource, decimal> selector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } using (IPoolingEnumerator <TSource> e = source.GetEnumerator()) { if (!e.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } decimal sum = selector(e.Current); long count = 1; while (e.MoveNext()) { sum += selector(e.Current); ++count; } return(sum / count); } }
/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static double Average <TSource>(this IPoolingEnumerable <TSource> source, Func <TSource, int> selector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } using (var e = source.GetEnumerator()) { if (!e.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } long sum = selector(e.Current); long count = 1; checked { while (e.MoveNext()) { sum += selector(e.Current); ++count; } } return((double)sum / count); } }
/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static double Average <TSource>(this IPoolingEnumerable <TSource> source, Func <TSource, double> selector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } using (IPoolingEnumerator <TSource> e = source.GetEnumerator()) { if (!e.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } double sum = selector(e.Current); long count = 1; while (e.MoveNext()) { // There is an opportunity to short-circuit here, in that if e.Current is // ever NaN then the result will always be NaN. Assuming that this case is // rare enough that not checking is the better approach generally. sum += selector(e.Current); ++count; } return(sum / count); } }
/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static decimal?Average(this IPoolingEnumerable <decimal?> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } using (IPoolingEnumerator <decimal?> e = source.GetEnumerator()) { while (e.MoveNext()) { decimal?v = e.Current; if (v.HasValue) { decimal sum = v.GetValueOrDefault(); long count = 1; while (e.MoveNext()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } return(sum / count); } } } return(null); }
/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static float Average(this IPoolingEnumerable <float> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } using (IPoolingEnumerator <float> e = source.GetEnumerator()) { if (!e.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } double sum = e.Current; long count = 1; while (e.MoveNext()) { sum += e.Current; ++count; } return((float)(sum / count)); } }
public static int Min(this IPoolingEnumerable <int> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } int value = 0; bool hasValue = false; foreach (int x in source) { if (hasValue) { if (x < value) { value = x; } } else { value = x; hasValue = true; } } if (hasValue) { return(value); } throw new InvalidOperationException("Sequence contains no elements"); }
/// <summary> /// Calculates avg of all given numbers. Complexity = O(N) /// </summary> public static double Average(this IPoolingEnumerable <int> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } using (var enumerator = source.GetEnumerator()) { if (!enumerator.MoveNext()) { // throw new InvalidOperationException("Sequence contains no elements"); } long sum = enumerator.Current; long count = 1; checked { while (enumerator.MoveNext()) { sum += enumerator.Current; ++count; } } return((double)sum / count); } }
public static PoolingList <T> AsPoolingList <T>(this IPoolingEnumerable <T> source) { var collection = Pool <PoolingList <T> > .Get().Init(); collection.AddRange(source); return(collection); }
public static float Max(this IPoolingEnumerable <float> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } float value = 0; bool hasValue = false; foreach (float x in source) { if (hasValue) { if (x > value || Double.IsNaN(value)) { value = x; } } else { value = x; hasValue = true; } } if (hasValue) { return(value); } throw new InvalidOperationException("Sequence contains no elements"); }
public AppendExprEnumerable <T> Init(IPoolingEnumerable <T> src, T element) { _src = src; _count = 0; _element = element; return(this); }
public static void AddRange <T>(this PoolingList <T> target, IPoolingEnumerable <T> src) { foreach (var item in src) { target.Add(item); } }
public static IPoolingEnumerable <TR> SelectMany <T, TR, TContext>( this IPoolingEnumerable <T> source, TContext context, Func <T, TContext, IPoolingEnumerable <TR> > mutator) { return(Pool <SelectManyExprWithContextEnumerable <T, TR, TContext> > .Get().Init(source, mutator, context)); }
public SelectClauseWithContextEnumerable <T, TR, TContext> Init(IPoolingEnumerable <T> src, TContext context, Func <TContext, T, TR> condition) { _src = src; _context = context; _condition = condition; return(this); }
public static TResult Aggregate <TSource, TAccumulate, TResult>(this IPoolingEnumerable <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, TAccumulate> func, Func <TAccumulate, TResult> resultSelector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (func == null) { throw new ArgumentNullException(nameof(func)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } var result = seed; foreach (var element in source) { result = func(result, element); } return(resultSelector(result)); }
public ConcatExprEnumerable <T> Init(IPoolingEnumerable <T> src, IPoolingEnumerable <T> second) { _src = src; _count = 0; _second = second; return(this); }
public static TSource Aggregate <TSource>(this IPoolingEnumerable <TSource> source, Func <TSource, TSource, TSource> func) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (func == null) { throw new ArgumentNullException(nameof(func)); } using (var enumerator = source.GetEnumerator()) { if (!enumerator.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } var result = enumerator.Current; while (enumerator.MoveNext()) { result = func(result, enumerator.Current); } return(result); } }
public SelectManyExprEnumerable <T, TR> Init(IPoolingEnumerable <T> src, Func <T, IPoolingEnumerable <TR> > mutator) { _src = src; _count = 0; _mutator = mutator; return(this); }
public static void AddRange <TK, TV>(this PoolingDictionary <TK, TV> target, IPoolingEnumerable <KeyValuePair <TK, TV> > src) { foreach (var item in src) { target.Add(item.Key, item.Value); } }
public static PoolingDictionary <TK, TV> AsPoolingDictionary <TK, TV>(this IPoolingEnumerable <KeyValuePair <TK, TV> > source) { var collection = Pool <PoolingDictionary <TK, TV> > .Get().Init(); collection.AddRange(source); return(collection); }
public static decimal Max(this IPoolingEnumerable <decimal> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } decimal value = 0; bool hasValue = false; foreach (decimal x in source) { if (hasValue) { if (x > value) { value = x; } } else { value = x; hasValue = true; } } if (hasValue) { return(value); } throw new InvalidOperationException("Sequence contains no elements"); }
public WhereExprEnumerable <T> Init(IPoolingEnumerable <T> src, Func <T, bool> condition) { _count = 0; _src = src; _condition = condition; return(this); }
public IEnumerator <T> GetEnumerator() { var enumerator = _enumerable.GetEnumerator(); _enumerable = default; Heap.Return(this); return(Heap.Get <GenericEnumerator <T> >().Init(enumerator)); }
public WhereExprWithContextEnumerable <T, TContext> Init(IPoolingEnumerable <T> src, TContext context, Func <TContext, T, bool> condition) { _count = 0; _src = src; _context = context; _condition = condition; return(this); }
public static bool Any <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); var hasItems = enumerator.MoveNext(); enumerator.Dispose(); return(hasItems); }
public static IPoolingEnumerable <TR> OfType <TR>(this IPoolingEnumerable source) { if (source is IPoolingEnumerable <TR> res) { return(res); } return(Pool <OfTypeExprEnumerable <TR> > .Get().Init(source)); }
public ExceptExprEnumerable <T> Init(IPoolingEnumerable <T> src, PoolingDictionary <T, int> except, IEqualityComparer <T> comparer = default) { _src = src; _except = except; _comparer = comparer; _count = 0; return(this); }
public SkipTakeExprPoolingEnumerable <T> Init(IPoolingEnumerable <T> source, bool take, int count) { _count = 0; _workCount = count; _source = source; _take = take; return(this); }
public IEnumerator <T> GetEnumerator() { var enumerator = _enumerable.GetEnumerator(); _enumerable = default; Pool <GenericEnumerable <T> > .Return(this); return(Pool <GenericEnumerator <T> > .Get().Init(enumerator)); }
public static IPoolingEnumerable <T> AsSingleEnumerableList <T>(this IPoolingEnumerable <T> src) { var list = Pool <PoolingList <T> > .Get().Init(); foreach (var item in src) { list.Add(item); } return(Pool <EnumerableTyped <T> > .Get().Init(list)); }
public static IPoolingEnumerable <T> AsSingleEnumerableSharedList <T>(this IPoolingEnumerable <T> src) where T : class { var list = Pool <PoolingListCanon <T> > .Get().Init(); foreach (var item in src) { list.Add(item); } return(Pool <EnumerableShared <T> > .Get().Init(list)); }