/// <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)); } }
/// <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); } }
public SelectExprEnumerator Init(SelectExprEnumerable <T, TR> parent, IPoolingEnumerator <T> src, Func <T, TR> mutator) { _src = src; _parent = parent; _mutator = mutator; return(this); }
public bool MoveNext() { if (_finished) { return(false); } if (_currentEnumerator == default) { if (!_src.MoveNext()) { _finished = true; return(false); } _currentEnumerator = _mutator(_src.Current).GetEnumerator(); } do { var hasValue = _currentEnumerator.MoveNext(); if (hasValue) { return(true); } if (!_src.MoveNext()) { _finished = true; return(false); } _currentEnumerator?.Dispose(); _currentEnumerator = _mutator(_src.Current).GetEnumerator(); } while (true); }
/// <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); }
public WhereExprEnumerator Init(IPoolingEnumerator <T> src, WhereExprEnumerable <T> parent, Func <T, bool> mutator) { _src = src; _mutator = mutator; _parent = parent; return(this); }
public IntersectExprEnumerator Init(IntersectExprEnumerable <T> parent, IPoolingEnumerator <T> src, IEqualityComparer <T> comparer) { _src = src; _parent = parent; _alreadyDoneItems = Pool <PoolingDictionary <T, int> > .Get().Init(0, comparer); return(this); }
public void Dispose() { _parent?.Dispose(); _parent = null; _src?.Dispose(); _src = default; Pool <CastExprEnumerator> .Return(this); }
public void Dispose() { _parent?.Dispose(); _parent = default; _src?.Dispose(); _src = default; Pool <WhereExprEnumerator> .Return(this); }
public AppendExprEnumerator Init(IPoolingEnumerator src, AppendExprEnumerable <T> parent, T element) { _src = src; _parent = parent; _element = element; _overcount = 0; return(this); }
public DistinctExprEnumerable <T, TItem> Init(IPoolingEnumerator <T> parent, Func <T, TItem> selector, IEqualityComparer <TItem> comparer = default) { _parent = parent; _selector = selector; _comparer = comparer; _count = 0; return(this); }
public ConcatExprEnumerator Init( ConcatExprEnumerable <T> parent, IPoolingEnumerator <T> src, IPoolingEnumerator <T> second) { _parent = parent; _src = src; _second = second; _first = true; return(this); }
public void Dispose() { _parent?.Dispose(); _parent = null; _src?.Dispose(); _src = default; _first = _shouldReturnElement = false; Pool <PrependExprEnumerator> .Return(this); }
public void Dispose() { _parent?.Dispose(); _parent = default; _src?.Dispose(); _src = default; _context = default; Pool <SelectExprWithContextEnumerator> .Return(this); }
public SkipTakeExprPoolingEnumerator Init(SkipTakeExprPoolingEnumerable <T> parent, IPoolingEnumerator <T> source, bool take, int workCount) { _pos = 0; _take = take; _source = source; _parent = parent; _workCount = workCount; return(this); }
public GroupedResultEnumerator Init( GroupedResultEnumerable <TSource, TKey, TResult> parent, PoolingDictionary <TKey, PoolingGrouping> src) { _src = src; _parent = parent; _enumerator = _src.GetEnumerator(); return(this); }
public PrependExprEnumerator Init(IPoolingEnumerator src, PrependExprEnumerable <T> parent, T element) { _src = src; _parent = parent; _element = element; _first = true; _shouldReturnElement = true; return(this); }
public void Dispose() { _enumerator?.Dispose(); _enumerator = default; _parent?.Dispose(); _parent = default; Pool <EnumeratorRef> .Return(this); }
public void Dispose() { _parent?.Dispose(); _parent = default; _src?.Dispose(); _src = default; _second?.Dispose(); _second = default; Pool <ConcatExprEnumerator> .Return(this); }
public void Dispose() { _parent?.Dispose(); _parent = default; _source?.Dispose(); _source = default; Pool <SkipTakeExprPoolingEnumerator> .Return(this); }
public SelectManyExprEnumerator Init( SelectManyExprEnumerable <T, TR> parent, IPoolingEnumerator <T> src, Func <T, IPoolingEnumerable <TR> > mutator) { _src = src; _finished = false; _parent = parent; _mutator = mutator; _currentEnumerator = default; return(this); }
public void Dispose() { _parent?.Dispose(); _parent = default; _hashset?.Dispose(); _hashset = default; _src = default; _selector = default; Pool <DistinctExprEnumerator> .Return(this); }
public SelectExprWithContextEnumerator Init( SelectExprWithContextEnumerable <T, TR, TContext> parent, IPoolingEnumerator <T> src, TContext context, Func <TContext, T, TR> condition) { _src = src; _parent = parent; _context = context; _condition = condition; return(this); }
public void Dispose() { _currentEnumerator?.Dispose(); _currentEnumerator = default; _parent?.Dispose(); _parent = default; _src.Dispose(); _src = default; Pool <SelectManyExprEnumerator> .Return(this); }
public DistinctExprEnumerator Init( DistinctExprEnumerable <T, TItem> parent, IPoolingEnumerator <T> src, Func <T, TItem> selector, IEqualityComparer <TItem> comparer) { _src = src; _parent = parent; _selector = selector; _hashset = Pool <PoolingDictionary <TItem, int> > .Get().Init(0, comparer ?? EqualityComparer <TItem> .Default); return(this); }
public SelectManyExprWithContextEnumerator Init( SelectManyExprWithContextEnumerable <T, TR, TContext> parent, IPoolingEnumerator <T> src, Func <T, TContext, IPoolingEnumerable <TR> > mutator, TContext context) { _src = src; _finished = false; _parent = parent; _mutator = mutator; _context = context; _currentEnumerator = default; return(this); }
private void Dispose() { if (_count == 0) { return; } _count--; if (_count == 0) { _parent?.Dispose(); _parent = default; _selector = default; Pool <DistinctExprEnumerable <T, TItem> > .Return(this); } }
public void Dispose() { _src?.Dispose(); _src = null; _alreadyDoneItems?.Dispose(); Pool <PoolingDictionary <T, int> > .Return(_alreadyDoneItems); _alreadyDoneItems = default; _parent?.Dispose(); _parent = default; Pool <IntersectExprEnumerator> .Return(this); }
/// <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()) { while (e.MoveNext()) { double?v = selector(e.Current); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (e.MoveNext()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return(sum / count); } } } return(null); }