List
Provides various extensions for IList<T> interface.
using DotNext.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.Reflection;
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 List
{
[System.Runtime.CompilerServices.Nullable(0)]
public static class Indexer<[System.Runtime.CompilerServices.Nullable(2)] T>
{
public static Func<IReadOnlyList<T>, int, T> ReadOnly { get; }
public static Func<IList<T>, int, T> Getter { get; }
public static Action<IList<T>, int, T> Setter { get; }
static Indexer()
{
RuntimeMethodHandle handle = (RuntimeMethodHandle);
RuntimeTypeHandle declaringType = typeof(IReadOnlyList<T>).TypeHandle;
ReadOnly = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Func<IReadOnlyList<T>, int, T>>();
handle = (RuntimeMethodHandle);
declaringType = typeof(IList<T>).TypeHandle;
Getter = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Func<IList<T>, int, T>>();
handle = (RuntimeMethodHandle);
Setter = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Action<IList<T>, int, T>>();
}
}
public static Func<int, T> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] T>(this IReadOnlyList<T> list)
{
return list.get_Item;
}
public static Func<int, T> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list)
{
return list.get_Item;
}
public static Action<int, T> IndexerSetter<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list)
{
return list.set_Item;
}
private static TOutput[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput, [System.Runtime.CompilerServices.Nullable(0)] TConverter>(this IList<TInput> input, [System.Runtime.CompilerServices.Nullable(0)] TConverter mapper) where TConverter : struct, ISupplier<TInput, TOutput>
{
int count = ((ICollection<TInput>)input).Count;
if (count == 0)
return Array.Empty<TOutput>();
TOutput[] array = GC.AllocateUninitializedArray<TOutput>(count, false);
for (int i = 0; i < count; i++) {
array[i] = ((ISupplier<TInput, TOutput>)mapper).Invoke(input[i]);
}
return array;
}
public static TOutput[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IList<TInput> input, Converter<TInput, TOutput> mapper)
{
return input.ToArray<TInput, TOutput, DelegatingConverter<TInput, TOutput>>(mapper);
}
[CLSCompliant(false)]
public static TOutput[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IList<TInput> input, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr mapper)
{
return input.ToArray<TInput, TOutput, Supplier<TInput, TOutput>>(mapper);
}
private static TOutput[] ToArrayWithIndex<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput, [System.Runtime.CompilerServices.Nullable(0)] TConverter>(this IList<TInput> input, [System.Runtime.CompilerServices.Nullable(0)] TConverter mapper) where TConverter : struct, ISupplier<int, TInput, TOutput>
{
int count = ((ICollection<TInput>)input).Count;
TOutput[] array = GC.AllocateUninitializedArray<TOutput>(count, false);
for (int i = 0; i < count; i++) {
array[i] = ((ISupplier<int, TInput, TOutput>)mapper).Invoke(i, input[i]);
}
return array;
}
public static TOutput[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IList<TInput> input, Func<int, TInput, TOutput> mapper)
{
return input.ToArrayWithIndex<TInput, TOutput, DelegatingSupplier<int, TInput, TOutput>>(mapper);
}
[CLSCompliant(false)]
public static TOutput[] ToArray<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IList<TInput> input, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr mapper)
{
return input.ToArrayWithIndex<TInput, TOutput, Supplier<int, TInput, TOutput>>(mapper);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})]
public static ReadOnlyListView<TInput, TOutput> Convert<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this IReadOnlyList<TInput> list, Converter<TInput, TOutput> converter)
{
return new ReadOnlyListView<TInput, TOutput>(list, converter);
}
public static IReadOnlyList<T> Singleton<[System.Runtime.CompilerServices.Nullable(2)] T>(T item)
{
return new SingletonList<T>(item);
}
public static int InsertOrdered<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TComparer>(this List<T> list, T item, TComparer comparer) where TComparer : IComparer<T>
{
Span<T> span = CollectionsMarshal.AsSpan(list);
int num = 0;
int num2 = span.Length;
while (num < num2) {
int num3 = (num + num2) / 2;
if (((IComparer<T>)comparer).Compare(Unsafe.Add(ref MemoryMarshal.GetReference(span), num3), item) > 0)
num2 = num3;
else
num = num3 + 1;
}
list.Insert(num, item);
return num;
}
public static int InsertOrdered<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TComparer>(this IList<T> list, T item, TComparer comparer) where TComparer : IComparer<T>
{
int num = 0;
int num2 = ((ICollection<T>)list).Count;
while (num < num2) {
int num3 = (num + num2) / 2;
if (((IComparer<T>)comparer).Compare(list[num3], item) > 0)
num2 = num3;
else
num = num3 + 1;
}
list.Insert(num, item);
return num;
}
public static int InsertOrdered<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, T item, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})] Comparison<T> comparer)
{
return list.InsertOrdered(item, (DelegatingComparer<T>)comparer);
}
[CLSCompliant(false)]
public static int InsertOrdered<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, T item, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
2,
2
})] IntPtr comparer)
{
return list.InsertOrdered(item, (ComparerWrapper<T>)(long)comparer);
}
public static void RemoveRange<[System.Runtime.CompilerServices.Nullable(2)] T>(this List<T> list, Range range)
{
(int Offset, int Length) offsetAndLength = range.GetOffsetAndLength(list.Count);
int item = offsetAndLength.Offset;
int item2 = offsetAndLength.Length;
list.RemoveRange(item, item2);
}
public static void Insert<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, Index index, T item)
{
list.Insert(index.GetOffset(((ICollection<T>)list).Count), item);
}
public static void RemoveAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, Index index)
{
list.RemoveAt(index.GetOffset(((ICollection<T>)list).Count));
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static ListSegment<T> Slice<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, Range range)
{
return new ListSegment<T>(list, range);
}
public static void Shuffle<[System.Runtime.CompilerServices.Nullable(2)] T>(this IList<T> list, Random random)
{
List<T> list2 = list as List<T>;
if (list2 == null) {
T[] array = list as T[];
if (array != null)
Span.Shuffle<T>(array, random);
else
<Shuffle>g__ShuffleSlow|20_0(list, random);
} else
CollectionsMarshal.AsSpan(list2).Shuffle(random);
}
}
}