> Zip <TOutItem, TFirstItem>(
            BuiltInEnumerable <TFirstItem> first,
            RangeEnumerable second,
            Func <TFirstItem, int, TOutItem> resultSelector
            )
        {
            var firstBridged = Bridge(first, nameof(first));

            if (second.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(second));
            }
            if (resultSelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(resultSelector));
            }

            return
                (CommonImplementation.ZipImpl <
                     TOutItem,
                     TFirstItem,
                     int,
                     BuiltInEnumerable <TFirstItem>,
                     BuiltInEnumerator <TFirstItem>,
                     RangeEnumerable,
                     RangeEnumerator
                     >(RefLocal(firstBridged), ref second, resultSelector));
        }
Пример #2
0
        // RangeEnumerable

        public double Average(RangeEnumerable source)
        {
            if (source.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(source));
            }
            if (source.InnerCount == 0)
            {
                throw CommonImplementation.SequenceEmpty();
            }

            // 4 5 6
            //   ^

            // 1 2 3 4
            //    ^

            var    leftIx = source.InnerCount / 2;
            double left   = source.Start + leftIx;

            if (source.InnerCount % 2 == 0)
            {
                // even
                double right = left + 1;

                return((left + right) / 2.0);
            }
            else
            {
                // odd
                return(left);
            }
        }
Пример #3
0
        // RangeEnumerable

        public int Sum(RangeEnumerable source)
        {
            if (source.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(source));
            }
            if (source.InnerCount == 0)
            {
                return(0);
            }

            long first  = source.Start;
            long last   = first + source.InnerCount - 1;
            var  length = source.InnerCount;

            // https://math.stackexchange.com/a/50487/111
            // sum of range [a, b] = (a + b) / 2 * ((b - a) + 1)
            // count = b - a + 1
            // 2 * sum = (a + b) * ((b - a) + 1)

            // 0 1 2 3
            //   a = 0
            //   b = 3
            //   (a + b) = 3
            //   (b - a) + 1 = 4
            //   (a + b) * ((b - a) + 1) = 12
            //   sum = 12 / 2 = 6

            // 1 2 3 4 5
            //   a = 1
            //   b = 5
            //   (a + b) = 6
            //   (b - a) + 1 = 5
            //   (a + b) * ((b - a) + 1) = 30
            //   sum = 30 / 2 = 15

            long twiceSum = (first + last);

            twiceSum *= length;
            long ret = twiceSum / 2;

            checked
            {
                return((int)ret);
            }
        }
            > Except(
            BuiltInEnumerable <int> first,
            RangeEnumerable second
            )
        {
            var firstBridge = Bridge(first, nameof(first));

            if (second.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(second));
            }

            return(CommonImplementation.ExceptImpl <
                       int,
                       BuiltInEnumerable <int>,
                       BuiltInEnumerator <int>,
                       RangeEnumerable,
                       RangeEnumerator
                       >(RefLocal(firstBridge), ref second));
        }
Пример #5
0
            > Join <TOutItem, TJoinKeyItem, TJoinLeftItem>(
            BuiltInEnumerable <TJoinLeftItem> outer,
            RangeEnumerable inner,
            Func <TJoinLeftItem, TJoinKeyItem> outerKeySelector,
            Func <int, TJoinKeyItem> innerKeySelector,
            Func <TJoinLeftItem, int, TOutItem> resultSelector,
            IEqualityComparer <TJoinKeyItem> comparer
            )
        {
            var outerBridge = Bridge(outer, nameof(outer));

            if (inner.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(inner));
            }

            if (outerKeySelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(outerKeySelector));
            }
            if (innerKeySelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(innerKeySelector));
            }
            if (resultSelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(resultSelector));
            }

            return
                (CommonImplementation.JoinImpl <
                     TOutItem,
                     TJoinKeyItem,
                     TJoinLeftItem,
                     BuiltInEnumerable <TJoinLeftItem>,
                     BuiltInEnumerator <TJoinLeftItem>,
                     int,
                     RangeEnumerable,
                     RangeEnumerator
                     >(RefLocal(outerBridge), ref inner, outerKeySelector, innerKeySelector, resultSelector, comparer));
        }
Пример #6
0
            > Intersect(
            BuiltInEnumerable <int> first,
            RangeEnumerable second,
            IEqualityComparer <int> comparer
            )
        {
            var firstBridge = Bridge(first, nameof(first));

            if (second.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(second));
            }

            return(CommonImplementation.IntersectImpl <
                       int,
                       BuiltInEnumerable <int>,
                       BuiltInEnumerator <int>,
                       RangeEnumerable,
                       RangeEnumerator
                       >(RefLocal(firstBridge), ref second, comparer));
        }
            > GroupJoin <TResult, TGroupJoinKey, TOuter>(
            BuiltInEnumerable <TOuter> outer,
            RangeEnumerable inner,
            Func <TOuter, TGroupJoinKey> outerKeySelector,
            Func <int, TGroupJoinKey> innerKeySelector,
            Func <TOuter, GroupedEnumerable <TGroupJoinKey, int>, TResult> resultSelector,
            IEqualityComparer <TGroupJoinKey> comparer
            )
        {
            var outerBridge = Bridge(outer, nameof(outer));

            if (inner.IsDefaultValue())
            {
                throw CommonImplementation.Uninitialized(nameof(inner));
            }
            if (outerKeySelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(outerKeySelector));
            }
            if (innerKeySelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(innerKeySelector));
            }
            if (resultSelector == null)
            {
                throw CommonImplementation.ArgumentNull(nameof(resultSelector));
            }

            return
                (CommonImplementation.GroupJoinImpl <
                     TResult,
                     TGroupJoinKey,
                     TOuter,
                     BuiltInEnumerable <TOuter>,
                     BuiltInEnumerator <TOuter>,
                     int,
                     RangeEnumerable,
                     RangeEnumerator
                     >(RefLocal(outerBridge), ref inner, outerKeySelector, innerKeySelector, resultSelector, comparer));
        }