/// <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); } }
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); } }
/// <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)); } }
/// <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); } }
/// <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 <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 IEnumerator <T> GetEnumerator() { var enumerator = _enumerable.GetEnumerator(); _enumerable = default; Heap.Return(this); return(Heap.Get <GenericEnumerator <T> >().Init(enumerator)); }
public static bool Any <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); var hasItems = enumerator.MoveNext(); enumerator.Dispose(); return(hasItems); }
public IEnumerator <T> GetEnumerator() { var enumerator = _enumerable.GetEnumerator(); _enumerable = default; Pool <GenericEnumerable <T> > .Return(this); return(Pool <GenericEnumerator <T> > .Get().Init(enumerator)); }
/// <summary> /// Gets first element from sequence. Complexity = O(1) /// </summary> public static T FirstOrDefault <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); var hasItem = enumerator.MoveNext(); var item = hasItem ? enumerator.Current : default; enumerator.Dispose(); return(item); }
public static T SingleOrDefault <T>(this IPoolingEnumerable <T> source) { var wasFound = false; var element = default(T); var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (wasFound) { enumerator.Dispose(); return(default);
public static T LastOrDefault <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); T element = default; var hasItems = false; while (enumerator.MoveNext()) { element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return(hasItems ? element : default);
public static T Last <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); T element = default; var hasItems = false; while (enumerator.MoveNext()) { element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return(hasItems ? element : throw new InvalidOperationException("Sequence is empty")); }
/// <summary> /// Gets first element from sequence. Complexity = O(1) /// </summary> public static T First <T>(this IPoolingEnumerable <T> source) { var enumerator = source.GetEnumerator(); var hasItems = enumerator.MoveNext(); if (!hasItems) { throw new InvalidOperationException("Sequence is empty"); } var element = enumerator.Current; enumerator.Dispose(); return(element); }
public static bool All <T, TContext>(this IPoolingEnumerable <T> source, TContext context, Func <TContext, T, bool> condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) { enumerator.Dispose(); return(false); } } enumerator.Dispose(); return(true); }
/// <summary> /// Gets first element from sequence by given <paramref name="condition"/>. Complexity = O(1) - O(N) /// </summary> public static T First <T>(this IPoolingEnumerable <T> source, Func <T, bool> condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (condition(enumerator.Current)) { var item = enumerator.Current; enumerator.Dispose(); return(item); } } enumerator.Dispose(); throw new InvalidOperationException("Sequence is empty"); }
/// <summary> /// Gets first element from sequence by given <paramref name="condition"/>. Complexity = O(1) - O(N) /// </summary> public static T FirstOrDefault <T>(this IPoolingEnumerable <T> source, Func <T, bool> condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (!condition(enumerator.Current)) { continue; } var elem = enumerator.Current; enumerator.Dispose(); return(elem); } enumerator.Dispose(); return(default);
public static T Last <T, TContext>(this IPoolingEnumerable <T> source, TContext context, Func <TContext, T, bool> condition) { var enumerator = source.GetEnumerator(); T element = default; var hasItems = false; while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) { continue; } element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return(hasItems ? element : throw new InvalidOperationException("Sequence is empty")); }
public static T Single <T>(this IPoolingEnumerable <T> source) { var wasFound = false; var element = default(T); var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (wasFound) { enumerator.Dispose(); throw new InvalidOperationException("Sequence should contain only one element"); } wasFound = true; element = enumerator.Current; } enumerator.Dispose(); return(element); }
/// <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); }
/// <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 e = source.GetEnumerator()) { while (e.MoveNext()) { int?v = e.Current; if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (e.MoveNext()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return((double)sum / count); } } } return(null); }
public static bool SequenceEqual <T>(this IPoolingEnumerable <T> self, IPoolingEnumerable <T> other) { var comparer = EqualityComparer <T> .Default; using (var left = self.GetEnumerator()) using (var right = other.GetEnumerator()) { bool equals, leftHas, rightHas; do { leftHas = left.MoveNext(); rightHas = right.MoveNext(); equals = comparer.Equals(left.Current, right.Current); if (leftHas != rightHas || !equals) { return(false); } } while (leftHas && rightHas); return(!leftHas && !rightHas); } }
public IPoolingEnumerator <T> GetEnumerator() { _count++; return(Pool <ExceptExprEnumerator> .Get().Init(this, _src.GetEnumerator())); }
public IPoolingEnumerator <T> GetEnumerator() { _count++; return(Pool <CastExprEnumerator> .Get().Init(_src.GetEnumerator(), this)); }
public IPoolingEnumerator <TR> GetEnumerator() { _count++; return(Pool <SelectExprWithContextEnumerator> .Get().Init(this, _src.GetEnumerator(), _context, _condition)); }
public IPoolingEnumerator <TR> GetEnumerator() { _count++; return(Pool <SelectManyExprEnumerator> .Get().Init(this, _src.GetEnumerator(), _mutator)); }
public IPoolingEnumerator <T> GetEnumerator() { _count++; return(Pool <WhereExprWithContextEnumerator> .Get().Init(_src.GetEnumerator(), this, _context, _condition)); }
public IPoolingEnumerator <T> GetEnumerator() { _count++; return(Pool <ConcatExprEnumerator> .Get().Init(this, _src.GetEnumerator(), _second.GetEnumerator())); }
public IPoolingEnumerator <T> GetEnumerator() { _count++; return(Pool <AppendExprEnumerator> .Get().Init(_src.GetEnumerator(), this, _element)); }