ImmutableArrayExtensions
LINQ extension method overrides that offer greater efficiency for ImmutableArray<T> than the standard LINQ methods
NuGet package: System.Collections.Immutable (about immutable collections and how to install)
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
namespace System.Linq
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class ImmutableArrayExtensions
{
public static IEnumerable<TResult> Select<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, TResult> selector)
{
immutableArray.ThrowNullRefIfNotInitialized();
return immutableArray.array.Select(selector);
}
public static IEnumerable<TResult> SelectMany<[System.Runtime.CompilerServices.Nullable(2)] TSource, [System.Runtime.CompilerServices.Nullable(2)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<TSource> immutableArray, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
immutableArray.ThrowNullRefIfNotInitialized();
if (collectionSelector == null || resultSelector == null)
return Enumerable.SelectMany(immutableArray, collectionSelector, resultSelector);
if (immutableArray.Length != 0)
return immutableArray.SelectManyIterator(collectionSelector, resultSelector);
return Enumerable.Empty<TResult>();
}
public static IEnumerable<T> Where<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
immutableArray.ThrowNullRefIfNotInitialized();
return immutableArray.array.Where(predicate);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static bool Any<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
return immutableArray.Length > 0;
}
public static bool Any<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
immutableArray.ThrowNullRefIfNotInitialized();
Requires.NotNull(predicate, "predicate");
T[] array = immutableArray.array;
foreach (T arg in array) {
if (predicate(arg))
return true;
}
return false;
}
public static bool All<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
immutableArray.ThrowNullRefIfNotInitialized();
Requires.NotNull(predicate, "predicate");
T[] array = immutableArray.array;
foreach (T arg in array) {
if (!predicate(arg))
return false;
}
return true;
}
[System.Runtime.CompilerServices.NullableContext(0)]
public static bool SequenceEqual<TDerived, [System.Runtime.CompilerServices.Nullable(2)] TBase>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<TBase> immutableArray, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ImmutableArray<TDerived> items, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] IEqualityComparer<TBase> comparer = null) where TDerived : TBase
{
immutableArray.ThrowNullRefIfNotInitialized();
items.ThrowNullRefIfNotInitialized();
if (immutableArray.array == items.array)
return true;
if (immutableArray.Length != items.Length)
return false;
if (comparer == null)
comparer = EqualityComparer<TBase>.Default;
for (int i = 0; i < immutableArray.Length; i++) {
if (!comparer.Equals(immutableArray.array[i], (TBase)(object)items.array[i]))
return false;
}
return true;
}
public static bool SequenceEqual<[System.Runtime.CompilerServices.Nullable(0)] TDerived, [System.Runtime.CompilerServices.Nullable(2)] TBase>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<TBase> immutableArray, IEnumerable<TDerived> items, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] IEqualityComparer<TBase> comparer = null) where TDerived : TBase
{
Requires.NotNull(items, "items");
if (comparer == null)
comparer = EqualityComparer<TBase>.Default;
int num = 0;
int length = immutableArray.Length;
foreach (TDerived item in items) {
if (num == length)
return false;
if (!comparer.Equals(immutableArray[num], (TBase)(object)item))
return false;
num++;
}
return num == length;
}
public static bool SequenceEqual<[System.Runtime.CompilerServices.Nullable(0)] TDerived, [System.Runtime.CompilerServices.Nullable(2)] TBase>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<TBase> immutableArray, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ImmutableArray<TDerived> items, Func<TBase, TBase, bool> predicate) where TDerived : TBase
{
Requires.NotNull(predicate, "predicate");
immutableArray.ThrowNullRefIfNotInitialized();
items.ThrowNullRefIfNotInitialized();
if (immutableArray.array == items.array)
return true;
if (immutableArray.Length != items.Length)
return false;
int i = 0;
for (int length = immutableArray.Length; i < length; i++) {
if (!predicate(immutableArray[i], (TBase)(object)items[i]))
return false;
}
return true;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T Aggregate<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, [System.Runtime.CompilerServices.Nullable(1)] Func<T, T, T> func)
{
Requires.NotNull(func, "func");
if (immutableArray.Length == 0)
return default(T);
T val = immutableArray[0];
int i = 1;
for (int length = immutableArray.Length; i < length; i++) {
val = func(val, immutableArray[i]);
}
return val;
}
public static TAccumulate Aggregate<[System.Runtime.CompilerServices.Nullable(2)] TAccumulate, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, TAccumulate seed, Func<TAccumulate, T, TAccumulate> func)
{
Requires.NotNull(func, "func");
TAccumulate val = seed;
T[] array = immutableArray.array;
foreach (T arg in array) {
val = func(val, arg);
}
return val;
}
public static TResult Aggregate<[System.Runtime.CompilerServices.Nullable(2)] TAccumulate, [System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, TAccumulate seed, Func<TAccumulate, T, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
{
Requires.NotNull(resultSelector, "resultSelector");
return resultSelector(immutableArray.Aggregate(seed, func));
}
public static T ElementAt<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, int index)
{
return immutableArray[index];
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T ElementAtOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, int index)
{
if (index < 0 || index >= immutableArray.Length)
return default(T);
return immutableArray[index];
}
public static T First<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
T[] array = immutableArray.array;
foreach (T val in array) {
if (predicate(val))
return val;
}
return Enumerable.Empty<T>().First();
}
public static T First<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
if (immutableArray.Length <= 0)
return immutableArray.array.First();
return immutableArray[0];
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T FirstOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
if (immutableArray.array.Length == 0)
return default(T);
return immutableArray.array[0];
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T FirstOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, [System.Runtime.CompilerServices.Nullable(1)] Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
T[] array = immutableArray.array;
foreach (T val in array) {
if (predicate(val))
return val;
}
return default(T);
}
public static T Last<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
if (immutableArray.Length <= 0)
return immutableArray.array.Last();
return immutableArray[immutableArray.Length - 1];
}
public static T Last<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
for (int num = immutableArray.Length - 1; num >= 0; num--) {
if (predicate(immutableArray[num]))
return immutableArray[num];
}
return Enumerable.Empty<T>().Last();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T LastOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
immutableArray.ThrowNullRefIfNotInitialized();
return immutableArray.array.LastOrDefault();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T LastOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, [System.Runtime.CompilerServices.Nullable(1)] Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
for (int num = immutableArray.Length - 1; num >= 0; num--) {
if (predicate(immutableArray[num]))
return immutableArray[num];
}
return default(T);
}
public static T Single<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
immutableArray.ThrowNullRefIfNotInitialized();
return immutableArray.array.Single();
}
public static T Single<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
bool flag = true;
T result = default(T);
T[] array = immutableArray.array;
foreach (T val in array) {
if (predicate(val)) {
if (!flag)
ImmutableArray.TwoElementArray.Single();
flag = false;
result = val;
}
}
if (flag)
Enumerable.Empty<T>().Single();
return result;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T SingleOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
immutableArray.ThrowNullRefIfNotInitialized();
return immutableArray.array.SingleOrDefault();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T SingleOrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, [System.Runtime.CompilerServices.Nullable(1)] Func<T, bool> predicate)
{
Requires.NotNull(predicate, "predicate");
bool flag = true;
T result = default(T);
T[] array = immutableArray.array;
foreach (T val in array) {
if (predicate(val)) {
if (!flag)
ImmutableArray.TwoElementArray.Single();
flag = false;
result = val;
}
}
return result;
}
public static Dictionary<TKey, T> ToDictionary<TKey, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, TKey> keySelector)
{
return immutableArray.ToDictionary(keySelector, EqualityComparer<TKey>.Default);
}
public static Dictionary<TKey, TElement> ToDictionary<TKey, [System.Runtime.CompilerServices.Nullable(2)] TElement, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, TKey> keySelector, Func<T, TElement> elementSelector)
{
return immutableArray.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default);
}
public static Dictionary<TKey, T> ToDictionary<TKey, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, TKey> keySelector, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] IEqualityComparer<TKey> comparer)
{
Requires.NotNull(keySelector, "keySelector");
Dictionary<TKey, T> dictionary = new Dictionary<TKey, T>(immutableArray.Length, comparer);
ImmutableArray<T>.Enumerator enumerator = immutableArray.GetEnumerator();
while (enumerator.MoveNext()) {
T current = enumerator.Current;
dictionary.Add(keySelector(current), current);
}
return dictionary;
}
public static Dictionary<TKey, TElement> ToDictionary<TKey, [System.Runtime.CompilerServices.Nullable(2)] TElement, [System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray, Func<T, TKey> keySelector, Func<T, TElement> elementSelector, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] IEqualityComparer<TKey> comparer)
{
Requires.NotNull(keySelector, "keySelector");
Requires.NotNull(elementSelector, "elementSelector");
Dictionary<TKey, TElement> dictionary = new Dictionary<TKey, TElement>(immutableArray.Length, comparer);
T[] array = immutableArray.array;
foreach (T arg in array) {
dictionary.Add(keySelector(arg), elementSelector(arg));
}
return dictionary;
}
public static T[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] this ImmutableArray<T> immutableArray)
{
immutableArray.ThrowNullRefIfNotInitialized();
if (immutableArray.array.Length == 0)
return ImmutableArray<T>.Empty.array;
return (T[])immutableArray.array.Clone();
}
public static T First<[System.Runtime.CompilerServices.Nullable(2)] T>(this ImmutableArray<T>.Builder builder)
{
Requires.NotNull(builder, "builder");
if (!builder.Any())
throw new InvalidOperationException();
return builder[0];
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T FirstOrDefault<T>([System.Runtime.CompilerServices.Nullable(1)] this ImmutableArray<T>.Builder builder)
{
Requires.NotNull(builder, "builder");
if (!builder.Any())
return default(T);
return builder[0];
}
public static T Last<[System.Runtime.CompilerServices.Nullable(2)] T>(this ImmutableArray<T>.Builder builder)
{
Requires.NotNull(builder, "builder");
if (!builder.Any())
throw new InvalidOperationException();
return builder[builder.Count - 1];
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T LastOrDefault<T>([System.Runtime.CompilerServices.Nullable(1)] this ImmutableArray<T>.Builder builder)
{
Requires.NotNull(builder, "builder");
if (!builder.Any())
return default(T);
return builder[builder.Count - 1];
}
public static bool Any<[System.Runtime.CompilerServices.Nullable(2)] T>(this ImmutableArray<T>.Builder builder)
{
Requires.NotNull(builder, "builder");
return builder.Count > 0;
}
private static IEnumerable<TResult> SelectManyIterator<TSource, TCollection, TResult>(this ImmutableArray<TSource> immutableArray, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
TSource[] array = immutableArray.array;
foreach (TSource item in array) {
foreach (TCollection item2 in collectionSelector(item)) {
yield return resultSelector(item, item2);
}
}
}
}
}