public void AddRange <TEnumerable, TEnumerator, U>(TEnumerable items, SelectorAt <U, T> selector)
            where TEnumerable : notnull, IValueEnumerable <U, TEnumerator>
            where TEnumerator : struct, IEnumerator <U>
        {
            Debug.Assert(items is object);

            using var enumerator = items.GetEnumerator();
            var destination = _current;
            var index       = _index;

            // Continuously read in items from the enumerator, updating _count
            // and _index when we run out of space.

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;

                if ((uint)index >= (uint)destination.Length)
                {
                    AddWithBufferAllocation(selector(item, index), ref destination, ref index);
                }
                else
                {
                    destination[index] = selector(item, index);
                }

                index++;
            }

            // Final update to _count and _index.
            _count += index - _index;
            _index  = index;
        }
Esempio n. 2
0
        public static SelectIndexEnumerable<TEnumerable, TEnumerator, TSource, TResult> Select<TEnumerable, TEnumerator, TSource, TResult>(
            this TEnumerable source, 
            SelectorAt<TSource, TResult> selector)
            where TEnumerable : notnull, IValueReadOnlyCollection<TSource, TEnumerator>
            where TEnumerator : struct, IEnumerator<TSource>
        {
            if(selector is null) Throw.ArgumentNullException(nameof(selector));

            return new SelectIndexEnumerable<TEnumerable, TEnumerator, TSource, TResult>(in source, selector);
        }
        public static MemorySelectIndexEnumerable <TSource, TResult> Select <TSource, TResult>(
            this ReadOnlyMemory <TSource> source,
            SelectorAt <TSource, TResult> selector)
        {
            if (selector is null)
            {
                Throw.ArgumentNullException(nameof(selector));
            }

            return(new MemorySelectIndexEnumerable <TSource, TResult>(in source, selector));
        }
        public static SelectIndexEnumerable <TList, TSource, TResult> Select <TList, TSource, TResult>(
            this TList source,
            SelectorAt <TSource, TResult> selector)
            where TList : notnull, IReadOnlyList <TSource>
        {
            if (selector is null)
            {
                Throw.ArgumentNullException(nameof(selector));
            }

            return(new SelectIndexEnumerable <TList, TSource, TResult>(in source, selector, 0, source.Count));
        }
        public void Single_SelectorAt_With_Multiple_Must_Return_None(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange

            // Act
            var result = Array
                         .Select <int, string>(source.AsSpan(), selector)
                         .Single();

            // Assert
            _ = result.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
        }
Esempio n. 6
0
        public void First_SelectorAt_With_Empty_Must_Return_None(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlyMemory <int>)source.AsMemory(), selector)
                         .First();

            // Assert
            _ = result.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
        }
        public void First_SelectorAt_With_Empty_Must_Return_None(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped = Wrap
                          .AsValueEnumerable(source);

            // Act
            var result = ValueEnumerable
                         .Select <Wrap.ValueEnumerable <int>, Wrap.Enumerator <int>, int, string>(wrapped, selector)
                         .First();

            // Assert
            _ = result.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
        }
        public void Single_SelectorAt_With_Multiple_Must_Return_None(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped = Wrap
                          .AsValueReadOnlyCollection(source);

            // Act
            var result = ValueReadOnlyCollection
                         .Select <Wrap.ValueReadOnlyCollection <int>, Wrap.Enumerator <int>, int, string>(wrapped, selector)
                         .Single();

            // Assert
            _ = result.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
        }
        public void Single_SelectorAt_With_Single_Must_Return_Some(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var expected =
                System.Linq.Enumerable.Single(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = Array
                         .Select <int, string>(source.AsSpan(), selector)
                         .Single();

            // Assert
            _ = result.Match(
                value => value.Must().BeEqualTo(expected),
                () => throw new Exception());
        }
        public void ToArray_With_SelectorAt_Must_Succeed(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var expected =
                System.Linq.Enumerable.ToArray(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlyMemory <int>)source.AsMemory(), selector)
                         .ToArray();

            // Assert
            _ = result.Must()
                .BeArrayOf <string>()
                .BeEqualTo(expected);
        }
Esempio n. 11
0
        public void First_SelectorAt_With_ValidData_Must_Return_Some(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var expected =
                System.Linq.Enumerable.First(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlyMemory <int>)source.AsMemory(), selector)
                         .First();

            // Assert
            _ = result.Match(
                value => value.Must().BeEqualTo(expected),
                () => throw new Exception());
        }
        public void ToArray_With_SelectorAt_Must_Succeed(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped  = Wrap.AsValueReadOnlyList(source);
            var expected =
                System.Linq.Enumerable.ToArray(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = ReadOnlyList
                         .Select <Wrap.ValueReadOnlyList <int>, int, string>(wrapped, selector)
                         .ToArray();

            // Assert
            _ = result.Must()
                .BeArrayOf <string>()
                .BeEqualTo(expected);
        }
Esempio n. 13
0
        public void ToList_With_SelectorAt_Must_Succeed(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var expected =
                System.Linq.Enumerable.ToList(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlySpan <int>)source.AsSpan(), selector)
                         .ToList();

            // Assert
            _ = result.Must()
                .BeOfType <List <string> >()
                .BeEnumerableOf <string>()
                .BeEqualTo(expected);
        }
        public void First_SelectorAt_With_ValidData_Must_Return_Some(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped = Wrap
                          .AsValueEnumerable(source);
            var expected =
                System.Linq.Enumerable.First(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = ValueEnumerable
                         .Select <Wrap.ValueEnumerable <int>, Wrap.Enumerator <int>, int, string>(wrapped, selector)
                         .First();

            // Assert
            _ = result.Match(
                value => value.Must().BeEqualTo(expected),
                () => throw new Exception());
        }
Esempio n. 15
0
        public void ToList_SelectorAt_With_ValidData_Must_Succeed(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped = Wrap
                          .AsValueEnumerable(source);
            var expected =
                System.Linq.Enumerable.ToList(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = ValueEnumerable
                         .Select <Wrap.ValueEnumerable <int>, Wrap.Enumerator <int>, int, string>(wrapped, selector)
                         .ToList();

            // Assert
            _ = result.Must()
                .BeOfType <List <string> >()
                .BeEnumerableOf <string>()
                .BeEqualTo(expected);
        }
Esempio n. 16
0
        static TResult[] ToArray <TList, TSource, TResult>(this TList source, SelectorAt <TSource, TResult> selector, int skipCount, int takeCount)
            where TList : notnull, IReadOnlyList <TSource>
        {
            var array = new TResult[takeCount];

            if (skipCount == 0)
            {
                for (var index = 0; index < takeCount; index++)
                {
                    array[index] = selector(source[index], index);
                }
            }
            else
            {
                for (var index = 0; index < takeCount; index++)
                {
                    array[index] = selector(source[index + skipCount], index);
                }
            }
            return(array);
        }
 public static Func <TSource, int, TResult> AsFunc <TSource, TResult>(this SelectorAt <TSource, TResult> selector)
 => new Func <TSource, int, TResult>(selector);
Esempio n. 18
0
 public static ValueEnumerable.SelectIndexEnumerable <ValueWrapper <TSource>, ValueWrapper <TSource> .Enumerator, TSource, TResult> Select <TSource, TResult>(
     this ImmutableQueue <TSource> source,
     SelectorAt <TSource, TResult> selector)
 => ValueEnumerable.Select <ValueWrapper <TSource>, ValueWrapper <TSource> .Enumerator, TSource, TResult>(new ValueWrapper <TSource>(source), selector);
Esempio n. 19
0
 public static ValueReadOnlyCollection.SelectIndexEnumerable <ValueWrapper <TSource>, SortedSet <TSource> .Enumerator, TSource, TResult> Select <TSource, TResult>(
     this SortedSet <TSource> source,
     SelectorAt <TSource, TResult> selector)
 => ValueReadOnlyCollection.Select <ValueWrapper <TSource>, SortedSet <TSource> .Enumerator, TSource, TResult>(new ValueWrapper <TSource>(source), selector);
Esempio n. 20
0
        public void ElementAt_Skip_Take_SelectorAt_With_ValidData_Must_Return_Some(int[] source, int skipCount, int takeCount, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped  = Wrap.AsValueReadOnlyList(source);
            var expected =
                System.Linq.Enumerable.ToList(
                    System.Linq.Enumerable.Select(
                        System.Linq.Enumerable.Take(
                            System.Linq.Enumerable.Skip(source, skipCount), takeCount), selector.AsFunc()));

            for (var index = 0; index < expected.Count; index++)
            {
                // Act
                var result = ReadOnlyList
                             .Skip <Wrap.ValueReadOnlyList <int>, int>(wrapped, skipCount)
                             .Take(takeCount)
                             .Select(selector)
                             .ElementAt(index);

                // Assert
                _ = result.Match(
                    value => value.Must().BeEqualTo(expected[index]),
                    () => throw new Exception());
            }
        }
Esempio n. 21
0
        public void ElementAt_Skip_Take_SelectorAt_With_OutOfRange_Must_Return_None(int[] source, int skipCount, int takeCount, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped = Wrap.AsValueReadOnlyList(source);

            // Act
            var optionNegative = ReadOnlyList
                                 .Skip <Wrap.ValueReadOnlyList <int>, int>(wrapped, skipCount)
                                 .Take(takeCount)
                                 .Select(selector)
                                 .ElementAt(-1);
            var optionTooLarge = ReadOnlyList
                                 .Skip <Wrap.ValueReadOnlyList <int>, int>(wrapped, skipCount)
                                 .Take(takeCount)
                                 .Select(selector)
                                 .ElementAt(wrapped.Count);

            // Assert
            _ = optionNegative.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
            _ = optionTooLarge.Must()
                .BeOfType <Option <string> >()
                .EvaluateTrue(option => option.IsNone);
        }
Esempio n. 22
0
        public void Contains_SelectorAt_With_Comparer_And_Contains_Must_ReturnTrue(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var value =
                System.Linq.Enumerable.Last(
                    System.Linq.Enumerable.Select(source, selector.AsFunc()));

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlySpan <int>)source.AsSpan(), selector)
                         .Contains(value, EqualityComparer <string> .Default);

            // Assert
            _ = result.Must()
                .BeTrue();
        }
Esempio n. 23
0
 public static ReadOnlyList.SelectIndexEnumerable <ImmutableList <TSource>, TSource, TResult> Select <TSource, TResult>(
     this ImmutableList <TSource> source,
     SelectorAt <TSource, TResult> selector)
 => ReadOnlyList.Select <ImmutableList <TSource>, TSource, TResult>(source, selector);
 public static ValueReadOnlyCollection.SelectIndexEnumerable <ValueWrapper <TKey, TValue>, SortedDictionary <TKey, TValue> .Enumerator, KeyValuePair <TKey, TValue>, TResult> Select <TKey, TValue, TResult>(
     this SortedDictionary <TKey, TValue> source,
     SelectorAt <KeyValuePair <TKey, TValue>, TResult> selector)
 => ValueReadOnlyCollection.Select <ValueWrapper <TKey, TValue>, SortedDictionary <TKey, TValue> .Enumerator, KeyValuePair <TKey, TValue>, TResult>(new ValueWrapper <TKey, TValue>(source), selector);
Esempio n. 25
0
        public void Contains_SelectorAt_With_Comparer_And_NotContains_Must_ReturnFalse(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var value = "!";

            // Act
            var result = Array
                         .Select <int, string>((ReadOnlySpan <int>)source.AsSpan(), selector)
                         .Contains(value, EqualityComparer <string> .Default);

            // Assert
            _ = result.Must()
                .BeFalse();
        }
        public void ToArray_Skip_Take_SelectorAt_With_ValidData_Must_Succeed(int[] source, int skipCount, int takeCount, SelectorAt <int, string> selector)
        {
            // Arrange
            var wrapped  = Wrap.AsValueReadOnlyList(source);
            var expected =
                System.Linq.Enumerable.ToArray(
                    System.Linq.Enumerable.Select(
                        System.Linq.Enumerable.Take(
                            System.Linq.Enumerable.Skip(source, skipCount), takeCount), selector.AsFunc()));

            // Act
            var result = ReadOnlyList
                         .Skip <Wrap.ValueReadOnlyList <int>, int>(wrapped, skipCount)
                         .Take(takeCount)
                         .Select(selector)
                         .ToArray();

            // Assert
            _ = result.Must()
                .BeArrayOf <string>()
                .BeEqualTo(expected);
        }
 static void ForEach <TList, TSource, TResult>(this TList source, Action <TResult> action, SelectorAt <TSource, TResult> selector, int skipCount, int takeCount)
     where TList : notnull, IReadOnlyList <TSource>
 {
     if (skipCount == 0)
     {
         for (var index = 0; index < takeCount; index++)
         {
             action(selector(source[index], index));
         }
     }
     else
     {
         for (var index = 0; index < takeCount; index++)
         {
             action(selector(source[index + skipCount], index));
         }
     }
 }
 public static AsyncSelectorAt <TSource, TResult> AsAsync <TSource, TResult>(this SelectorAt <TSource, TResult> selector)
 => (item, position, _) => new ValueTask <TResult>(selector(item, position));
Esempio n. 29
0
        public void ForEach_ActionAt_SelectorAt_With_ValidData_Must_Succeed(int[] source, SelectorAt <int, string> selector)
        {
            // Arrange
            var result   = new List <(string, int)>();
            var expected = new List <(string, int)>();

            System.Linq.EnumerableEx.ForEach(
                System.Linq.Enumerable.Select(source, selector.AsFunc()), (item, index) => expected.Add((item, index)));

            // Act
            Array
            .Select <int, string>(source.AsMemory(), selector)
            .ForEach((item, index) => result.Add((item, index)));

            // Assert
            _ = result.Must()
                .BeEnumerableOf <(string, int)>()
                .BeEqualTo(expected);
        }
 static SelectIndexEnumerable <TList, TSource, TResult> Select <TList, TSource, TResult>(
     this TList source,
     SelectorAt <TSource, TResult> selector,
     int skipCount, int takeCount)
     where TList : notnull, IReadOnlyList <TSource>
 => new SelectIndexEnumerable <TList, TSource, TResult>(source, selector, skipCount, takeCount);