예제 #1
0
        public static T2 WhereSumF <T, T2>(this ReadOnlySpan <T> source, Func <T, bool> predicate, Func <T, T2> selector)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (predicate == null)
            {
                throw Error.ArgumentNull(nameof(predicate));
            }
            if (selector == null)
            {
                throw Error.ArgumentNull(nameof(selector));
            }

            T2 a = default(T2);

            checked
            {
                for (int index = 0; index < source.Length; index++)
                {
                    if (predicate(source[index]))
                    {
                        a = GenericOperators.Add(a, selector(source[index]));
                    }
                }
            }

            return(a);
        }
예제 #2
0
        public static T2 WhereSumF <T, T2>(this T[] source, Func <T, bool> predicate, Func <T, T2> selector)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (predicate == null)
            {
                throw Error.ArgumentNull(nameof(predicate));
            }
            if (selector == null)
            {
                throw Error.ArgumentNull(nameof(selector));
            }

            T2 a = default(T2);

            checked
            {
                foreach (T b in source)
                {
                    if (predicate(b))
                    {
                        a = GenericOperators.Add(a, selector(b));
                    }
                }
            }

            return(a);
        }
예제 #3
0
        /// <summary>
        /// Determines whether an array contains a specified element
        /// using SIMD.
        /// </summary>
        /// <param name="source">An array in which to locate a value.</param>
        /// <param name="value">The value to locate.</param>
        /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false.</returns>
        public static bool ContainsS <T>(this T[] source, T value)
            where T : struct
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }

            var count       = Vector <T> .Count;
            var vectorValue = new Vector <T>(value);

            for (int i = 0; i < source.Length - count; i += count)
            {
                if (Vector.EqualsAny(new Vector <T>(source, i), vectorValue))
                {
                    return(true);
                }
            }
            for (int i = source.Length - (source.Length % count); i < source.Length; i++)
            {
                if (GenericOperators.Equals(source[i], value))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #4
0
        public static T2 SumF <T, T2>(this IReadOnlyList <T> source, Func <T, T2> selector)
        {
            switch (source)
            {
            case null:
                throw Error.ArgumentNull(nameof(source));

            case T[] sa:
                return(sa.SumF(selector));

            case List <T> sl:
                return(sl.SumF(selector));

            default:
            {
                if (selector == null)
                {
                    throw Error.ArgumentNull(nameof(selector));
                }

                int sourceCount = source.Count;
                T2  a           = default(T2);
                checked
                {
                    for (int index = 0; index < sourceCount; index++)
                    {
                        a = GenericOperators.Add(a, selector(source[index]));
                    }
                }

                return(a);
            }
            }
        }
예제 #5
0
        public static T2 SumF <T, T2>(this List <T> source, Func <T, T2> selector)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (selector == null)
            {
                throw Error.ArgumentNull(nameof(selector));
            }

            T2 a = default(T2);

            checked
            {
                for (int index = 0; index < source.Count; index++)
                {
                    a = GenericOperators.Add(a, selector(source[index]));
                }
            }

            return(a);
        }
예제 #6
0
 /// <summary>
 /// When the effector is InPlace and RelativeToStart, this
 /// combines the initial and combined values created in "GetNext".
 ///
 /// This exists for optimization purposes. InPlace RelativeToStart
 /// effectors are common, and GenericOperators.Add is really slow
 /// by comparison to simply adding the two values. Thus, for extra
 /// speed, child classes with explicit T2 values can reimplement a
 /// faster version of this method.
 /// </summary>
 /// <param name="init"></param>
 /// <param name="combined"></param>
 /// <returns></returns>
 protected virtual T2 Combine_InPlaceAndRelativeToStart(T2 init, T2 combined)
 {
     return(GenericOperators.Add <T2>(init, combined));
 }