Пример #1
0
        /// <summary> The number of true elements in a boolean sequence. </summary>
        public static int CountTrue <TIn, TProducer>(
            this SpanEnumerable <TIn, bool, TProducer> spanEnum)
            where TProducer : struct, IProducer <TIn, bool>
        {
            var count = 0;

            long        longOnStack = default;
            Span <bool> onStack     =
                MemoryMarshal.Cast <long, bool>(
                    MemoryMarshal.CreateSpan(ref longOnStack, 1));

            while (true)
            {
                onStack = spanEnum.ConsumeInto(onStack);
                if (onStack.Length == 0)
                {
                    break;
                }

                count += onStack.CountTrue();
            }

            return(count);
        }
Пример #2
0
 /// <summary>
 ///     Takes enough values from the sequence to fill the provided span.
 /// </summary>
 /// <remarks>
 ///     A copy of the enumerable is taken, which means that (beyond any
 ///     side-effects of consuming values from it) the original
 ///     enumerable is unchanged.
 /// </remarks>
 /// <returns>
 ///     Returns the span, maybe resized if there were not enough values
 ///     in the enumerable.
 /// </returns>
 public static Span <TOut> TakeInto <TOut>(
     this SpanEnumerable <TOut> self,
     Span <TOut> output)
 =>
 self.ConsumeInto(output);