Skip to content

Commit

Permalink
Extend the operations on ValueReadOnlyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Feb 24, 2021
1 parent c0b6dd4 commit 37e2a3f
Show file tree
Hide file tree
Showing 19 changed files with 1,301 additions and 78 deletions.
2 changes: 2 additions & 0 deletions NetFabric.Hyperlinq.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Hyperlinq/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static int Count<TEnumerable, TEnumerator, TSource>(this TEnumerable sour
return counter;
}

static int Count<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
internal static int Count<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate: struct, IFunction<TSource, bool>
Expand All @@ -34,7 +34,7 @@ static int Count<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable
return counter;
}

static int CountAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
internal static int CountAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate: struct, IFunction<TSource, int, bool>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ public static int Count<TEnumerable, TEnumerator, TSource>(this TEnumerable sour
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
=> source.Count;

static int Count<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate: struct, IFunction<TSource, bool>
=> source switch
{
{Count: 0} => 0,
_ => ValueEnumerableExtensions.Count<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate)
};

static int CountAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate: struct, IFunction<TSource, int, bool>
=> source switch
{
{Count: 0} => 0,
_ => ValueEnumerableExtensions.CountAt<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate)
};

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static IMemoryOwner<TSource> ToArray<TEnumerable, TEnumerator, TSource>(t


[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TSource[] ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
internal static TSource[] ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
Expand All @@ -56,7 +56,7 @@ static TSource[] ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEn
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TSource> ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
internal static IMemoryOwner<TSource> ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
Expand All @@ -69,7 +69,7 @@ static IMemoryOwner<TSource> ToArray<TEnumerable, TEnumerator, TSource, TPredica


[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TSource[] ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
internal static TSource[] ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, int, bool>
Expand All @@ -79,7 +79,7 @@ static TSource[] ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this T
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TSource> ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
internal static IMemoryOwner<TSource> ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, int, bool>
Expand Down Expand Up @@ -137,7 +137,7 @@ static IMemoryOwner<TResult> ToArrayAt<TEnumerable, TEnumerator, TSource, TResul


[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TResult[] ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector)
internal static TResult[] ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
Expand All @@ -148,7 +148,7 @@ static TResult[] ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate,
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TResult> ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector, MemoryPool<TResult> pool)
internal static IMemoryOwner<TResult> ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector, MemoryPool<TResult> pool)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ static IMemoryOwner<TSource> BuildArray(TEnumerable source, MemoryPool<TSource>

//////////////////////////////////////////////////////////////////////////////////////////////////

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TSource[] ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
=> source switch
{
{ Count: 0 } => Array.Empty<TSource>(),
_ => ValueEnumerableExtensions.ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate)
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TSource> ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
=> source switch
{
{ Count: 0 } => pool.Rent(0),
_ => ValueEnumerableExtensions.ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate, pool)
};

//////////////////////////////////////////////////////////////////////////////////////////////////

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TSource[] ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, int, bool>
=> source switch
{
{ Count: 0 } => Array.Empty<TSource>(),
_ => ValueEnumerableExtensions.ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate)
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TSource> ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate, MemoryPool<TSource> pool)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, int, bool>
=> source switch
{
{ Count: 0 } => pool.Rent(0),
_ => ValueEnumerableExtensions.ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate, pool)
};

//////////////////////////////////////////////////////////////////////////////////////////////////

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TResult[] ToArray<TEnumerable, TEnumerator, TSource, TResult, TSelector>(this TEnumerable source, TSelector selector)
Expand Down Expand Up @@ -142,5 +189,31 @@ static IMemoryOwner<TResult> BuildArray(TEnumerable source, TSelector selector,
return result;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static TResult[] ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
where TSelector : struct, IFunction<TSource, TResult>
=> source switch
{
{ Count: 0 } => Array.Empty<TResult>(),
_ => ValueEnumerableExtensions.ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(source, predicate, selector)
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IMemoryOwner<TResult> ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector, MemoryPool<TResult> pool)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
where TSelector : struct, IFunction<TSource, TResult>
=> source switch
{
{ Count: 0 } => pool.Rent(0),
_ => ValueEnumerableExtensions.ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(source, predicate, selector, pool)
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ static Dictionary<TKey, TSource> ToDictionaryAt<TEnumerable, TEnumerator, TSourc
// ReSharper disable once HeapView.ObjectAllocation.Evident
? new Dictionary<TKey, TSource>()
: ValueEnumerableExtensions.ToDictionaryAt<TEnumerable, TEnumerator, TSource, TKey, TKeySelector, TPredicate>(source, keySelector, comparer, predicate);

static Dictionary<TKey, TResult> ToDictionary<TEnumerable, TEnumerator, TSource, TKey, TKeySelector, TResult, TPredicate, TSelector>(this TEnumerable source, TKeySelector keySelector, IEqualityComparer<TKey>? comparer, TPredicate predicate, TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TKey : notnull
where TKeySelector : struct, IFunction<TResult, TKey>
where TPredicate : struct, IFunction<TSource, bool>
where TSelector : struct, IFunction<TSource, TResult>
=> source.Count is 0
// ReSharper disable once HeapView.ObjectAllocation.Evident
? new Dictionary<TKey, TResult>()
: ValueEnumerableExtensions.ToDictionary<TEnumerable, TEnumerator, TSource, TKey, TKeySelector, TResult, TPredicate, TSelector>(source, keySelector, comparer, predicate, selector);


//////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -92,6 +105,19 @@ static Dictionary<TKey, TElement> ToDictionaryAt<TEnumerable, TEnumerator, TSour
// ReSharper disable once HeapView.ObjectAllocation.Evident
? new Dictionary<TKey, TElement>()
: ValueEnumerableExtensions.ToDictionaryAt<TEnumerable, TEnumerator, TSource, TKey, TElement, TKeySelector, TElementSelector, TPredicate>(source, keySelector, elementSelector, comparer, predicate);

static Dictionary<TKey, TElement> ToDictionary<TEnumerable, TEnumerator, TSource, TKey, TElement, TKeySelector, TElementSelector, TResult, TPredicate, TSelector>(this TEnumerable source, TKeySelector keySelector, TElementSelector elementSelector, IEqualityComparer<TKey>? comparer, TPredicate predicate, TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TKey : notnull
where TKeySelector : struct, IFunction<TResult, TKey>
where TElementSelector : struct, IFunction<TResult, TElement>
where TPredicate : struct, IFunction<TSource, bool>
where TSelector : struct, IFunction<TSource, TResult>
=> source.Count is 0
// ReSharper disable once HeapView.ObjectAllocation.Evident
? new Dictionary<TKey, TElement>()
: ValueEnumerableExtensions.ToDictionary<TEnumerable, TEnumerator, TSource, TKey, TElement, TKeySelector, TElementSelector, TResult, TPredicate, TSelector>(source, keySelector, elementSelector, comparer, predicate, selector);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
// ReSharper disable HeapView.ObjectAllocation.Evident

Expand All @@ -22,16 +23,61 @@ public static List<TSource> ToList<TEnumerable, TEnumerator, TSource>(this TEnum
}
};

static List<TResult> ToList<TEnumerable, TEnumerator, TSource, TResult, TSelector>(this TEnumerable source, TSelector selector)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static List<TSource> ToList<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
=> source switch
{
{Count: 0} => new List<TSource>(),
_ => ToArray<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate).AsList()
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static List<TSource> ToListAt<TEnumerable, TEnumerator, TSource, TPredicate>(this TEnumerable source, TPredicate predicate)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, int, bool>
=> source switch
{
{Count: 0} => new List<TSource>(),
_ => ToArrayAt<TEnumerable, TEnumerator, TSource, TPredicate>(source, predicate).AsList()
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static List<TResult> ToList<TEnumerable, TEnumerator, TSource, TResult, TSelector>(this TEnumerable source,
TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TSelector : struct, IFunction<TSource, TResult>
=> ToArray<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, selector).AsList();
=> source switch
{
{Count: 0} => new List<TResult>(),
_ => ToArray<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, selector).AsList()
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static List<TResult> ToListAt<TEnumerable, TEnumerator, TSource, TResult, TSelector>(this TEnumerable source, TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TSelector : struct, IFunction<TSource, int, TResult>
=> ToArrayAt<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, selector).AsList();
=> source switch
{
{Count: 0} => new List<TResult>(),
_ => ToArrayAt<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, selector).AsList()
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static List<TResult> ToList<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(this TEnumerable source, TPredicate predicate, TSelector selector)
where TEnumerable : IValueReadOnlyCollection<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
where TPredicate : struct, IFunction<TSource, bool>
where TSelector : struct, IFunction<TSource, TResult>
=> source switch
{
{Count: 0} => new List<TResult>(),
_ => ToArray<TEnumerable, TEnumerator, TSource, TResult, TPredicate, TSelector>(source, predicate, selector).AsList()
};
}
}
Loading

0 comments on commit 37e2a3f

Please sign in to comment.