Collection
Provides utility methods to work with collections.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Collections.Generic
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Collection
{
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})]
public static ReadOnlyCollectionView<TInput, TOutput> Convert<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IReadOnlyCollection<TInput> collection, Converter<TInput, TOutput> converter)
{
return new ReadOnlyCollectionView<TInput, TOutput>(collection, converter);
}
public static T[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] T>(ICollection<T> collection)
{
int count = collection.Count;
if (count == 0)
return Array.Empty<T>();
T[] array = GC.AllocateUninitializedArray<T>(count, false);
collection.CopyTo(array, 0);
return array;
}
public unsafe static T[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] T>(IReadOnlyCollection<T> collection)
{
int count = collection.Count;
if (count == 0)
return Array.Empty<T>();
T[] array = GC.AllocateUninitializedArray<T>(count, false);
UIntPtr uIntPtr = (UIntPtr)(void*)null;
foreach (T item in (IEnumerable<T>)collection) {
T[] array2 = array;
UIntPtr intPtr = uIntPtr;
uIntPtr = (UIntPtr)(void*)((long)(ulong)intPtr + 1);
array2[(ulong)intPtr] = item;
}
return array;
}
public static void AddAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this ICollection<T> collection, IEnumerable<T> items)
{
items.ForEach(collection.Add);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Optional<T> PeekRandom<[System.Runtime.CompilerServices.Nullable(2)] T>(this IReadOnlyCollection<T> collection, Random random)
{
switch (collection.Count) {
case 0:
return Optional<T>.None;
case 1:
return collection.FirstOrNone();
default: {
T[] array = collection as T[];
if (array == null) {
List<T> list = collection as List<T>;
if (list == null)
return <PeekRandom>g__PeekRandomSlow|4_0(collection, random);
return Span.PeekRandom<T>(CollectionsMarshal.AsSpan(list), random);
}
return Span.PeekRandom<T>(array, random);
}
}
}
}
}