예제 #1
0
 public static MemoryWhereAtEnumerable <TSource> Where <TSource>(this TSource[] source, PredicateAt <TSource> predicate)
 => Where(source.AsMemory(), predicate);
        public static MemoryWhereIndexEnumerable <TSource> Where <TSource>(this ReadOnlyMemory <TSource> source, PredicateAt <TSource> predicate)
        {
            if (predicate is null)
            {
                Throw.ArgumentNullException(nameof(predicate));
            }

            return(new MemoryWhereIndexEnumerable <TSource>(source, predicate));
        }
예제 #3
0
        public static bool Any <TEnumerable, TEnumerator, TSource>(this TEnumerable source, PredicateAt <TSource> predicate)
            where TEnumerable : notnull, IValueEnumerable <TSource, TEnumerator>
            where TEnumerator : struct, IEnumerator <TSource>
        {
            if (predicate is null)
            {
                Throw.ArgumentNullException(nameof(predicate));
            }

            using var enumerator = source.GetEnumerator();
            checked
            {
                for (var index = 0; enumerator.MoveNext(); index++)
                {
                    if (predicate(enumerator.Current, index))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #4
0
 public static bool Any <TSource>(this ImmutableStack <TSource> source, PredicateAt <TSource> predicate)
 => ValueEnumerable.Any <ValueWrapper <TSource>, ValueWrapper <TSource> .Enumerator, TSource>(new ValueWrapper <TSource>(source), predicate);
예제 #5
0
 static TSource[] ToArray <TSource>(this Span <TSource> source, PredicateAt <TSource> predicate)
 => ToArray((ReadOnlySpan <TSource>)source, predicate);
예제 #6
0
 public static bool Any <TKey, TValue>(this SortedDictionary <TKey, TValue> source, PredicateAt <KeyValuePair <TKey, TValue> > predicate)
 => ValueReadOnlyCollectionExtensions.Any <ValueWrapper <TKey, TValue>, SortedDictionary <TKey, TValue> .Enumerator, KeyValuePair <TKey, TValue> >(new ValueWrapper <TKey, TValue>(source), predicate);
예제 #7
0
        static Dictionary <TKey, TElement> ToDictionary <TEnumerable, TEnumerator, TSource, TKey, TElement>(this TEnumerable source, Selector <TSource, TKey> keySelector, NullableSelector <TSource, TElement> elementSelector, IEqualityComparer <TKey>?comparer, PredicateAt <TSource> predicate)
            where TEnumerable : notnull, IValueEnumerable <TSource, TEnumerator>
            where TEnumerator : struct, IEnumerator <TSource>
            where TKey : notnull
        {
            if (keySelector is null)
            {
                Throw.ArgumentNullException(nameof(keySelector));
            }
            if (elementSelector is null)
            {
                Throw.ArgumentNullException(nameof(elementSelector));
            }

            using var enumerator = source.GetEnumerator();
            checked
            {
                var dictionary = new Dictionary <TKey, TElement>(0, comparer);
                for (var index = 0; enumerator.MoveNext(); index++)
                {
                    var item = enumerator.Current;
                    if (predicate(item, index))
                    {
                        dictionary.Add(keySelector(item), elementSelector(item) !);
                    }
                }
                return(dictionary);
            }
        }
예제 #8
0
        public void ElementAt_Skip_Take_PredicateAt_With_ValidData_Must_Return_Some(int[] source, int skipCount, int takeCount, PredicateAt <int> predicate)
        {
            // Arrange
            var expected = Enumerable
                           .Skip(source, skipCount)
                           .Take(takeCount)
                           .Where(predicate.AsFunc())
                           .ToList();

            for (var index = 0; index < expected.Count; index++)
            {
                // Act
                var result = ArrayExtensions
                             .Skip <int>(source, skipCount)
                             .Take(takeCount)
                             .Where(predicate)
                             .ElementAt(index);

                // Assert
                _ = result.Match(
                    value => value.Must().BeEqualTo(expected[index]),
                    () => throw new Exception());
            }
        }
        public async ValueTask ForEachAsync_Action_PredicateAt_With_ValidData_Must_Succeed(int[] source, PredicateAt <int> predicate)
        {
            // Arrange
            var wrapped  = Wrap.AsAsyncValueEnumerable(source);
            var result   = new List <int>();
            var expected = new List <int>();

            System.Linq.EnumerableEx.ForEach(
                System.Linq.Enumerable.Where(source, predicate.AsFunc()), item => expected.Add(item));

            // Act
            await AsyncValueEnumerable
            .Where <Wrap.AsyncValueEnumerable <int>, Wrap.AsyncEnumerator <int>, int>(wrapped, predicate.AsAsync())
            .ForEachAsync((item, cancellationToken) => { result.Add(item); return(new ValueTask()); });

            // Assert
            _ = result.Must()
                .BeEnumerableOf <int>()
                .BeEqualTo(expected);
        }
        public void All_Skip_Take_PredicateAt_With_ValidData_Must_Succeed(int[] source, int skip, int take, PredicateAt <int> predicate)
        {
            // Arrange
            var(offset, count) = Utils.SkipTake(source.Length, skip, take);
            var wrapped  = new ArraySegment <int>(source, offset, count);
            var expected = Enumerable
                           .Where(wrapped, predicate.AsFunc())
                           .Count() == count;

            // Act
            var result = ArrayExtensions
                         .All(wrapped, predicate);

            // Assert
            _ = result.Must()
                .BeEqualTo(expected);
        }
예제 #11
0
        public void ElementAt_Skip_Take_PredicateAt_With_OutOfRange_Must_Return_None(int[] source, int skipCount, int takeCount, PredicateAt <int> predicate)
        {
            // Arrange

            // Act
            var optionTooSmall = ArrayExtensions
                                 .Skip <int>(source, skipCount)
                                 .Take(takeCount)
                                 .Where(predicate)
                                 .ElementAt(-1);
            var optionTooLarge = ArrayExtensions
                                 .Skip <int>(source, skipCount)
                                 .Take(takeCount)
                                 .Where(predicate)
                                 .ElementAt(takeCount);

            // Assert
            _ = optionTooSmall.Must()
                .BeOfType <Option <int> >()
                .EvaluateTrue(option => option.IsNone);
            _ = optionTooLarge.Must()
                .BeOfType <Option <int> >()
                .EvaluateTrue(option => option.IsNone);
        }
예제 #12
0
 public static bool All <TSource>(this Span <TSource> source, PredicateAt <TSource> predicate)
 => All((ReadOnlySpan <TSource>)source, predicate);
예제 #13
0
 static WhereAtEnumerable <TSource> Where <TSource>(this TSource[] source, PredicateAt <TSource> predicate, int skipCount, int takeCount)
 => new WhereAtEnumerable <TSource>(in source, predicate, skipCount, takeCount);
예제 #14
0
        public static WhereAtEnumerable <TSource> Where <TSource>(this TSource[] source, PredicateAt <TSource> predicate)
        {
            if (predicate is null)
            {
                Throw.ArgumentNullException(nameof(predicate));
            }

            return(new WhereAtEnumerable <TSource>(in source, predicate, 0, source.Length));
        }
 public static bool Any <TKey, TValue>(this Dictionary <TKey, TValue> .KeyCollection source, PredicateAt <TKey> predicate)
     where TKey : notnull
 => ValueReadOnlyCollectionExtensions.Any <ValueWrapper <TKey, TValue>, Dictionary <TKey, TValue> .KeyCollection.Enumerator, TKey>(new ValueWrapper <TKey, TValue>(source), predicate);
예제 #16
0
        public void Where_With_ValidData_Must_Succeed(int[] source, int skip, int take, PredicateAt <int> predicate)
        {
            // Arrange
            var(offset, count) = Utils.SkipTake(source.Length, skip, take);
            var wrapped  = new ArraySegment <int>(source, offset, count);
            var expected = Enumerable
                           .Where(wrapped, predicate.AsFunc());

            // Act
            var result = ArrayExtensions
                         .Where(wrapped, predicate);

            // Assert
            _ = result.Must()
                .BeEnumerableOf <int>()
                .BeEqualTo(expected, testRefStructs: false, testRefReturns: false);
            _ = result.SequenceEqual(expected).Must().BeTrue();
        }
 public static ValueEnumerableExtensions.WhereAtEnumerable <ValueWrapper <TKey, TValue>, Dictionary <TKey, TValue> .KeyCollection.Enumerator, TKey> Where <TKey, TValue>(
     this Dictionary <TKey, TValue> .KeyCollection source,
     PredicateAt <TKey> predicate)
     where TKey : notnull
 => ValueEnumerableExtensions.Where <ValueWrapper <TKey, TValue>, Dictionary <TKey, TValue> .KeyCollection.Enumerator, TKey>(new ValueWrapper <TKey, TValue>(source), predicate);
예제 #18
0
        public async void ElementAtAsync_PredicateAt_With_OutOfRange_Must_Return_None(int[] source, PredicateAt <int> predicate)
        {
            // Arrange
            var wrapped = Wrap.AsAsyncValueEnumerable(source);

            // Act
            var optionNegative = await AsyncValueEnumerable
                                 .Where <Wrap.AsyncValueEnumerable <int>, Wrap.AsyncEnumerator <int>, int>(wrapped, predicate.AsAsync())
                                 .ElementAtAsync(-1);

            var optionTooLarge = await AsyncValueEnumerable
                                 .Where <Wrap.AsyncValueEnumerable <int>, Wrap.AsyncEnumerator <int>, int>(wrapped, predicate.AsAsync())
                                 .ElementAtAsync(source.Length);

            // Assert
            _ = optionNegative.Must()
                .BeOfType <Option <int> >()
                .EvaluateTrue(option => option.IsNone);
            _ = optionTooLarge.Must()
                .BeOfType <Option <int> >()
                .EvaluateTrue(option => option.IsNone);
        }
예제 #19
0
 public static ValueEnumerableExtensions.WhereAtEnumerable <ValueWrapper <TKey, TValue>, SortedDictionary <TKey, TValue> .Enumerator, KeyValuePair <TKey, TValue> > Where <TKey, TValue>(
     this SortedDictionary <TKey, TValue> source,
     PredicateAt <KeyValuePair <TKey, TValue> > predicate)
 => ValueEnumerableExtensions.Where <ValueWrapper <TKey, TValue>, SortedDictionary <TKey, TValue> .Enumerator, KeyValuePair <TKey, TValue> >(new ValueWrapper <TKey, TValue>(source), predicate);
예제 #20
0
        public async ValueTask ElementAtAsync_PredicateAt_With_ValidData_Must_Return_Some(int[] source, PredicateAt <int> predicate)
        {
            // Arrange
            var wrapped  = Wrap.AsAsyncValueEnumerable(source);
            var expected =
                System.Linq.Enumerable.ToList(
                    System.Linq.Enumerable.Where(source, predicate.AsFunc()));

            for (var index = 0; index < expected.Count; index++)
            {
                // Act
                var result = await AsyncValueEnumerable
                             .Where <Wrap.AsyncValueEnumerable <int>, Wrap.AsyncEnumerator <int>, int>(wrapped, predicate.AsAsync())
                             .ElementAtAsync(index);

                // Assert
                _ = result.Match(
                    value => value.Must().BeEqualTo(expected[index]),
                    () => throw new Exception());
            }
        }
예제 #21
0
        static Dictionary <TKey, TSource> ToDictionary <TEnumerable, TEnumerator, TSource, TKey>(this TEnumerable source, NullableSelector <TSource, TKey> keySelector, IEqualityComparer <TKey>?comparer, PredicateAt <TSource> predicate)
            where TEnumerable : IValueReadOnlyCollection <TSource, TEnumerator>
            where TEnumerator : struct, IEnumerator <TSource>
        {
            if (keySelector is null)
            {
                Throw.ArgumentNullException(nameof(keySelector));
            }

            var dictionary = new Dictionary <TKey, TSource>(0, comparer);

            if (source.Count != 0)
            {
                using var enumerator = source.GetEnumerator();
                checked
                {
                    for (var index = 0; enumerator.MoveNext(); index++)
                    {
                        var item = enumerator.Current;
                        if (predicate(item, index))
                        {
                            dictionary.Add(keySelector(item), item);
                        }
                    }
                }
            }
            return(dictionary);
        }
예제 #22
0
 public static bool Any <TSource>(this TSource[] source, PredicateAt <TSource> predicate)
 => Any((ReadOnlySpan <TSource>)source.AsSpan(), predicate);
예제 #23
0
 public static ValueEnumerable.WhereIndexEnumerable <ValueWrapper <TSource>, ValueWrapper <TSource> .Enumerator, TSource> Where <TSource>(
     this ImmutableStack <TSource> source,
     PredicateAt <TSource> predicate)
 => ValueEnumerable.Where <ValueWrapper <TSource>, ValueWrapper <TSource> .Enumerator, TSource>(new ValueWrapper <TSource>(source), predicate);
        public void All_Skip_Take_PredicateAt_With_ValidData_Must_Succeed(int[] source, int skipCount, int takeCount, PredicateAt <int> predicate)
        {
            // Arrange
            var wrapped = Wrap.AsValueReadOnlyList(source);
            var count   =
                System.Linq.Enumerable.Count(
                    System.Linq.Enumerable.Take(
                        System.Linq.Enumerable.Skip(source, skipCount), takeCount));
            var expected =
                System.Linq.Enumerable.Count(
                    System.Linq.Enumerable.Where(
                        System.Linq.Enumerable.Take(
                            System.Linq.Enumerable.Skip(source, skipCount), takeCount), predicate.AsFunc())) == count;

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

            // Assert
            _ = result.Must()
                .BeEqualTo(expected);
        }
예제 #25
0
 static IMemoryOwner <TSource> ToArray <TSource>(this Span <TSource> source, PredicateAt <TSource> predicate, MemoryPool <TSource> pool)
 => ToArray((ReadOnlySpan <TSource>)source, predicate, pool);
 static List <TSource> ToList <TSource>(this ReadOnlySpan <TSource> source, PredicateAt <TSource> predicate)
 {
     using var arrayBuilder = ToArrayBuilder(source, predicate, ArrayPool <TSource> .Shared);
     return(new List <TSource>(arrayBuilder));
 }
        static Dictionary <TKey, TElement> ToDictionary <TSource, TKey, TElement>(this TSource[] source, NullableSelector <TSource, TKey> keySelector, NullableSelector <TSource, TElement> elementSelector, IEqualityComparer <TKey>?comparer, PredicateAt <TSource> predicate, int skipCount, int takeCount)
        {
            var dictionary = new Dictionary <TKey, TElement>(source.Length, comparer);
            var end        = skipCount + takeCount;

            if (skipCount == 0 && takeCount == source.Length)
            {
                for (var index = 0; index < source.Length; index++)
                {
                    if (predicate(source[index], index))
                    {
                        dictionary.Add(keySelector(source[index]), elementSelector(source[index]));
                    }
                }
            }
            else
            {
                for (var index = skipCount; index < end; index++)
                {
                    if (predicate(source[index], index))
                    {
                        dictionary.Add(keySelector(source[index]), elementSelector(source[index]));
                    }
                }
            }
            return(dictionary);
        }
        static Dictionary <TKey, TSource> ToDictionary <TList, TSource, TKey>(this TList source, Selector <TSource, TKey> keySelector, IEqualityComparer <TKey>?comparer, PredicateAt <TSource> predicate, int offset, int count)
            where TList : notnull, IReadOnlyList <TSource>
            where TKey : notnull
        {
            var dictionary = new Dictionary <TKey, TSource>(source.Count, comparer);
            var end        = offset + count - 1;

            for (var index = offset; index <= end; index++)
            {
                if (predicate(source[index], index))
                {
                    dictionary.Add(keySelector(source[index]), source[index]);
                }
            }
            return(dictionary);
        }
예제 #29
0
        public static SpanWhereRefAtEnumerable <TSource> WhereRef <TSource>(this Span <TSource> source, PredicateAt <TSource> predicate)
        {
            if (predicate is null)
            {
                Throw.ArgumentNullException(nameof(predicate));
            }

            return(new SpanWhereRefAtEnumerable <TSource>(source, predicate));
        }
예제 #30
0
 public static bool All <TSource>(this ReadOnlyMemory <TSource> source, PredicateAt <TSource> predicate)
 => All(source.Span, predicate);