コード例 #1
0
 public static IEnumerable <TSource> ToEnumerable <TSource>(this System.Memory <TSource> source)
 {
     for (int i = 0; i <= source.Length; i++)
     {
         yield return(source.Span[i]);
     }
 }
コード例 #2
0
        public static IEnumerable <IGrouping <TKey, TElement> > GroupBy <TSource, TKey, TElement>
        (
            this System.Memory <TSource> source,
            Func <TSource, TKey> keySelector,
            Func <TSource, TElement> elementSelector
        )
        {
            // Need to preserve output ordering!
            List <TKey> keys = new List <TKey>();

            Lookup <TKey, TElement> ret = new Lookup <TKey, TElement>();

            foreach (TSource item in source.ToEnumerable())
            {
                TKey key = keySelector(item);
                if (ret.Add(key, elementSelector(item)))
                {
                    keys.Add(key);
                }
            }
            foreach (TKey key in keys)
            {
                yield return(new Grouping <TKey, TElement>(key, ret[key]));
            }
        }
コード例 #3
0
        public static IEnumerable <TResult> GroupJoin <TOuter, TInner, TKey, TResult>
        (
            this System.Memory <TOuter> outer,
            Memory <TInner> inner,
            Func <TOuter, TKey> outerKeySelector,
            Func <TInner, TKey> innerKeySelector,
            Func <TOuter, Memory <TInner>, TResult> resultSelector
        )
        {
            ILookup <TKey, TInner> innerLookup = inner.ToLookup(innerKeySelector);

            foreach (TOuter outerElement in outer.ToEnumerable <TOuter>())
            {
                TKey            key = outerKeySelector(outerElement);
                Memory <TInner> innerMatches;
                if (innerLookup.Contains(key))
                {
                    innerMatches = innerLookup[key];
                }
                else
                {
                    innerMatches = Memory <TInner> .Empty;
                }
                yield return(resultSelector(outerElement, innerMatches));
            }
        }
コード例 #4
0
        public static IEnumerable <TResult> Join <TOuter, TInner, TKey, TResult>
        (
            this System.Memory <TOuter> outer,
            Memory <TInner> inner,
            Func <TOuter, TKey> outerKeySelector,
            Func <TInner, TKey> innerKeySelector,
            Func <TOuter, TInner, TResult> resultSelector
        )
        {
            IEnumerable <Grouping <TOuter, TInner> > wah = outer.GroupJoin
                                                           (
                inner,
                outerKeySelector,
                innerKeySelector,
                (outerItem, sequenceOfInners)
                =>
                new Grouping <TOuter, TInner>(outerItem, sequenceOfInners)
                                                           );

            foreach (Grouping <TOuter, TInner> grouping in wah)
            {
                foreach (var innerMatch in grouping)
                {
                    yield return(resultSelector(grouping.Key, innerMatch));
                }
            }
        }
コード例 #5
0
        public static bool SequenceEqual <TSource>
        (
            this System.Memory <TSource> first,
            Memory <TSource> second
        )
        {
            IEqualityComparer <TSource> comparer = EqualityComparer <TSource> .Default;

            using (IEnumerator <TSource> firstIterator = first.ToEnumerable().GetEnumerator())
            {
                foreach (TSource secondElement in second.ToEnumerable())
                {
                    if (!firstIterator.MoveNext())
                    {
                        // First sequence is shorter than second
                        return(false);
                    }
                    if (!comparer.Equals(firstIterator.Current, secondElement))
                    {
                        return(false);
                    }
                }
                if (firstIterator.MoveNext())
                {
                    // First sequence is longer than first
                    return(false);
                }
            }

            return(true);
        }
コード例 #6
0
 public static ILookup <TKey, TSource> ToLookup <TSource, TKey>
 (
     this System.Memory <TSource> source,
     Func <TSource, TKey> keySelector
 )
 {
     return(ToLookup(source, keySelector, element => element));
 }
コード例 #7
0
 public static TSource First <TSource>(this System.Memory <TSource> source)
 {
     foreach (TSource element in source.ToEnumerable())
     {
         return(element);
     }
     throw new System.InvalidOperationException("Empty sequence");
 }
コード例 #8
0
 public static TSource FirstOrDefault <TSource>(this System.Memory <TSource> source)
 {
     foreach (TSource element in source.ToEnumerable())
     {
         return(element);
     }
     return(default(TSource));
 }
コード例 #9
0
 public static IEnumerable <TResult> Select <TSource, TResult>
 (
     this System.Memory <TSource> source,
     Func <TSource, TResult> projection
 )
 {
     return(SelectImpl(source, projection));
 }
コード例 #10
0
 public static IEnumerable <IGrouping <TKey, TSource> > GroupBy <TSource, TKey>
 (
     this System.Memory <TSource> source,
     Func <TSource, TKey> keySelector
 )
 {
     return(source.GroupBy(keySelector, element => element));
 }
コード例 #11
0
        /// <summary>
        /// Writes body/content bytes to the replay.
        /// </summary>
        /// <param name="bytes">
        /// The number of bytes to write to the FIFO queue.
        /// </param>
        /// <returns>
        /// True if the write was a success, false otherwise.
        /// </returns>
        /// <remarks>
        /// If a return value of false is given, that means that the write that failed would have
        /// exceeded the upper boundary of reserved memory of buffering the replay event. In this
        /// case, the engine will internally abandon the replay because the user is considered to
        /// have failed to act on the replay, and resources must be reclaimed.
        /// </remarks>
        internal bool WriteBodyBytes(System.Memory <byte> bytes)
        {
            if (BodySize > _maxBufferSize)
            {
                return(false);
            }

            _pendingBody.Enqueue(bytes.ToArray());
            return(true);
        }
コード例 #12
0
        public static double Average(this System.Memory <ulong> memory)
        {
            double sum = 0;

            for (int i = 0; i < memory.Length; i++)
            {
                sum += memory.Span[i];
            }

            return(sum / memory.Length);
        }
コード例 #13
0
        public static decimal Average(this System.Memory <decimal> memory)
        {
            decimal sum = 0M;

            for (int i = 0; i < memory.Length; i++)
            {
                sum += memory.Span[i];
            }

            return(sum / memory.Length);
        }
コード例 #14
0
 private static IEnumerable <TResult> SelectImpl <TSource, TResult>
 (
     this System.Memory <TSource> source,
     Func <TSource, TResult> projection
 )
 {
     for (int i = 0; i <= source.Length; i++)
     {
         yield return(projection(source.Span[i]));
     }
 }
コード例 #15
0
        public static decimal Sum(this System.Memory <decimal> m)
        {
            Span <decimal> s   = m.Span;
            decimal        sum = 0;

            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i];
            }

            return(sum);
        }
コード例 #16
0
        public static sbyte Sum(this System.Memory <sbyte> m)
        {
            Span <sbyte> s   = m.Span;
            sbyte        sum = 0;

            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i];
            }

            return(sum);
        }
コード例 #17
0
        public static ulong Sum(this System.Memory <ulong> m)
        {
            Span <ulong> s   = m.Span;
            ulong        sum = 0;

            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i];
            }

            return(sum);
        }
コード例 #18
0
        public static int Sum(this System.Memory <int> m)
        {
            Span <int> s   = m.Span;
            int        sum = 0;

            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i];
            }

            return(sum);
        }
コード例 #19
0
        public static double Sum(this System.Memory <double> m)
        {
            Span <double> s   = m.Span;
            double        sum = 0;

            for (int i = 0; i < s.Length; i++)
            {
                sum += s[i];
            }

            return(sum);
        }
 public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>
 (
     this System.Memory <TSource> source,
     Func <TSource, TKey> keySelector
 )
 {
     return(new OrderedEnumerable <TSource>
            (
                source.ToEnumerable(),
                ProjectionComparer.Create(keySelector)
            ));
 }
コード例 #21
0
        public static sbyte Max(this System.Memory <sbyte> memory)
        {
            sbyte max = sbyte.MinValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (max < memory.Span[i])
                {
                    max = memory.Span[i];
                }
            }

            return(max);
        }
コード例 #22
0
        public static sbyte Min(this System.Memory <sbyte> memory)
        {
            sbyte min = sbyte.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #23
0
        public static long Min(this System.Memory <long> memory)
        {
            long min = long.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #24
0
        public static decimal Min(this System.Memory <decimal> memory)
        {
            decimal min = decimal.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #25
0
        public static uint Min(this System.Memory <uint> memory)
        {
            uint min = uint.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #26
0
        public static double Min(this System.Memory <double> memory)
        {
            double min = double.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #27
0
        public static float Min(this System.Memory <float> memory)
        {
            float min = float.MaxValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (min > memory.Span[i])
                {
                    min = memory.Span[i];
                }
            }

            return(min);
        }
コード例 #28
0
        public static bool Any(this System.Memory <long> x, Func <long, bool> func)
        {
            bool any = false;

            for (int i = 0; i < x.Length; i++)
            {
                if (func(x.Span[i]))
                {
                    return(true);
                }
            }

            return(any);
        }
コード例 #29
0
 public static IEnumerable <TSource> Where <TSource>
 (
     this System.Memory <TSource> source,
     Func <TSource, bool> predicate
 )
 {
     foreach (TSource item in source.ToEnumerable())
     {
         if (predicate(item))
         {
             yield return(item);
         }
     }
 }
コード例 #30
0
        public static long Max(this System.Memory <long> memory)
        {
            long max = long.MinValue;

            for (int i = 0; i < memory.Length; i++)
            {
                if (max < memory.Span[i])
                {
                    max = memory.Span[i];
                }
            }

            return(max);
        }