예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        /// <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);
            }
        }
예제 #3
0
        /// <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);
            }
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        /// <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));
            }
        }
예제 #6
0
        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");
        }
예제 #7
0
        /// <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);
            }
        }
예제 #8
0
        public static PoolingList <T> AsPoolingList <T>(this IPoolingEnumerable <T> source)
        {
            var collection = Pool <PoolingList <T> > .Get().Init();

            collection.AddRange(source);
            return(collection);
        }
예제 #9
0
        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");
        }
예제 #10
0
 public AppendExprEnumerable <T> Init(IPoolingEnumerable <T> src, T element)
 {
     _src     = src;
     _count   = 0;
     _element = element;
     return(this);
 }
예제 #11
0
 public static void AddRange <T>(this PoolingList <T> target, IPoolingEnumerable <T> src)
 {
     foreach (var item in src)
     {
         target.Add(item);
     }
 }
예제 #12
0
 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));
 }
예제 #13
0
 public SelectClauseWithContextEnumerable <T, TR, TContext> Init(IPoolingEnumerable <T> src, TContext context, Func <TContext, T, TR> condition)
 {
     _src       = src;
     _context   = context;
     _condition = condition;
     return(this);
 }
예제 #14
0
        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));
        }
예제 #15
0
 public ConcatExprEnumerable <T> Init(IPoolingEnumerable <T> src, IPoolingEnumerable <T> second)
 {
     _src    = src;
     _count  = 0;
     _second = second;
     return(this);
 }
예제 #16
0
        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);
 }
예제 #18
0
 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);
     }
 }
예제 #19
0
        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);
        }
예제 #20
0
        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");
        }
예제 #21
0
 public WhereExprEnumerable <T> Init(IPoolingEnumerable <T> src, Func <T, bool> condition)
 {
     _count     = 0;
     _src       = src;
     _condition = condition;
     return(this);
 }
예제 #22
0
        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);
 }
예제 #24
0
        public static bool Any <T>(this IPoolingEnumerable <T> source)
        {
            var enumerator = source.GetEnumerator();
            var hasItems   = enumerator.MoveNext();

            enumerator.Dispose();
            return(hasItems);
        }
예제 #25
0
 public static IPoolingEnumerable <TR> OfType <TR>(this IPoolingEnumerable source)
 {
     if (source is IPoolingEnumerable <TR> res)
     {
         return(res);
     }
     return(Pool <OfTypeExprEnumerable <TR> > .Get().Init(source));
 }
예제 #26
0
 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);
 }
예제 #27
0
 public SkipTakeExprPoolingEnumerable <T> Init(IPoolingEnumerable <T> source, bool take, int count)
 {
     _count     = 0;
     _workCount = count;
     _source    = source;
     _take      = take;
     return(this);
 }
예제 #28
0
        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));
        }