コード例 #1
0
ファイル: Average.cs プロジェクト: sidristij/memory-pools
        /// <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));
            }
        }
コード例 #2
0
ファイル: Average.cs プロジェクト: sidristij/memory-pools
        /// <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);
            }
        }
コード例 #3
0
 public SelectExprEnumerator Init(SelectExprEnumerable <T, TR> parent, IPoolingEnumerator <T> src, Func <T, TR> mutator)
 {
     _src     = src;
     _parent  = parent;
     _mutator = mutator;
     return(this);
 }
コード例 #4
0
            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);
            }
コード例 #5
0
ファイル: Average.cs プロジェクト: sidristij/memory-pools
        /// <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);
            }
        }
コード例 #6
0
ファイル: Average.cs プロジェクト: sidristij/memory-pools
        /// <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);
        }
コード例 #7
0
 public WhereExprEnumerator Init(IPoolingEnumerator <T> src, WhereExprEnumerable <T> parent, Func <T, bool> mutator)
 {
     _src     = src;
     _mutator = mutator;
     _parent  = parent;
     return(this);
 }
コード例 #8
0
            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);
            }
コード例 #9
0
 public void Dispose()
 {
     _parent?.Dispose();
     _parent = null;
     _src?.Dispose();
     _src = default;
     Pool <CastExprEnumerator> .Return(this);
 }
コード例 #10
0
 public void Dispose()
 {
     _parent?.Dispose();
     _parent = default;
     _src?.Dispose();
     _src = default;
     Pool <WhereExprEnumerator> .Return(this);
 }
コード例 #11
0
 public AppendExprEnumerator Init(IPoolingEnumerator src, AppendExprEnumerable <T> parent, T element)
 {
     _src       = src;
     _parent    = parent;
     _element   = element;
     _overcount = 0;
     return(this);
 }
コード例 #12
0
 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);
 }
コード例 #13
0
 public ConcatExprEnumerator Init(
     ConcatExprEnumerable <T> parent, IPoolingEnumerator <T> src, IPoolingEnumerator <T> second)
 {
     _parent = parent;
     _src    = src;
     _second = second;
     _first  = true;
     return(this);
 }
コード例 #14
0
 public void Dispose()
 {
     _parent?.Dispose();
     _parent = null;
     _src?.Dispose();
     _src   = default;
     _first = _shouldReturnElement = false;
     Pool <PrependExprEnumerator> .Return(this);
 }
コード例 #15
0
 public void Dispose()
 {
     _parent?.Dispose();
     _parent = default;
     _src?.Dispose();
     _src     = default;
     _context = default;
     Pool <SelectExprWithContextEnumerator> .Return(this);
 }
コード例 #16
0
 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);
 }
コード例 #17
0
 public GroupedResultEnumerator Init(
     GroupedResultEnumerable <TSource, TKey, TResult> parent,
     PoolingDictionary <TKey, PoolingGrouping> src)
 {
     _src        = src;
     _parent     = parent;
     _enumerator = _src.GetEnumerator();
     return(this);
 }
コード例 #18
0
 public PrependExprEnumerator Init(IPoolingEnumerator src, PrependExprEnumerable <T> parent, T element)
 {
     _src                 = src;
     _parent              = parent;
     _element             = element;
     _first               = true;
     _shouldReturnElement = true;
     return(this);
 }
コード例 #19
0
                public void Dispose()
                {
                    _enumerator?.Dispose();
                    _enumerator = default;

                    _parent?.Dispose();
                    _parent = default;

                    Pool <EnumeratorRef> .Return(this);
                }
コード例 #20
0
 public void Dispose()
 {
     _parent?.Dispose();
     _parent = default;
     _src?.Dispose();
     _src = default;
     _second?.Dispose();
     _second = default;
     Pool <ConcatExprEnumerator> .Return(this);
 }
コード例 #21
0
            public void Dispose()
            {
                _parent?.Dispose();
                _parent = default;

                _source?.Dispose();
                _source = default;

                Pool <SkipTakeExprPoolingEnumerator> .Return(this);
            }
コード例 #22
0
 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);
 }
コード例 #23
0
            public void Dispose()
            {
                _parent?.Dispose();
                _parent = default;

                _hashset?.Dispose();
                _hashset = default;

                _src      = default;
                _selector = default;
                Pool <DistinctExprEnumerator> .Return(this);
            }
コード例 #24
0
 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);
 }
コード例 #25
0
            public void Dispose()
            {
                _currentEnumerator?.Dispose();
                _currentEnumerator = default;

                _parent?.Dispose();
                _parent = default;

                _src.Dispose();
                _src = default;

                Pool <SelectManyExprEnumerator> .Return(this);
            }
コード例 #26
0
            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);
            }
コード例 #27
0
 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);
 }
コード例 #28
0
 private void Dispose()
 {
     if (_count == 0)
     {
         return;
     }
     _count--;
     if (_count == 0)
     {
         _parent?.Dispose();
         _parent   = default;
         _selector = default;
         Pool <DistinctExprEnumerable <T, TItem> > .Return(this);
     }
 }
コード例 #29
0
            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);
            }
コード例 #30
0
ファイル: Average.cs プロジェクト: sidristij/memory-pools
        /// <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);
        }