DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="3.2.1" />

.NET API 366,080 bytes

 OneDimensionalArray

public static class OneDimensionalArray
Provides specialized methods to work with one-dimensional array.
using DotNext.Runtime; using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class OneDimensionalArray { [System.Runtime.CompilerServices.NullableContext(0)] private struct RemovalCounter<[System.Runtime.CompilerServices.Nullable(2)] T> : IConsumer<T> { internal long Count; [System.Runtime.CompilerServices.NullableContext(1)] void IConsumer<T>.Invoke(T item) { Count++; } } [System.Runtime.CompilerServices.NullableContext(0)] private sealed class ArrayEqualityComparer { [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] private readonly object[] first; [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] private readonly object[] second; internal ArrayEqualityComparer([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] object[] first, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] object[] second) { this.first = first; this.second = second; } [System.Runtime.CompilerServices.NullableContext(1)] internal void Iteration(long index, ParallelLoopState state) { if (!state.ShouldExitCurrentIteration && !object.Equals(first[index], second[index])) state.Break(); } } public static T[] Concat<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] left, T[] right, long startIndex) { if (startIndex < 0 || startIndex > (long)Intrinsics.GetLength(left)) throw new ArgumentOutOfRangeException("startIndex"); T[] array = new T[startIndex + right.LongLength]; Array.Copy(left, array, startIndex); Array.Copy(right, 0, array, startIndex, right.Length); return array; } [System.Runtime.CompilerServices.NullableContext(2)] public static bool IsNullOrEmpty<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] [NotNullWhen(false)] this T[] array) { if (array != null) return Intrinsics.GetLength(array) == (IntPtr)0; return true; } public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, RefAction<T, long> action) { for (long num = 0; num < array.LongLength; num++) { action(ref array[num], num); } } [CLSCompliant(false)] public unsafe static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TArg>(this T[] array, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 1 })] IntPtr action, TArg arg) { if (action == (IntPtr)(void*)null) throw new ArgumentNullException("action"); for (IntPtr intPtr = (IntPtr)0; (long)intPtr < (long)Intrinsics.GetLength(array); intPtr = (IntPtr)(void*)((long)intPtr + 1)) { ; } } public static T[] Insert<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, T element, long index) { if (index < 0 || index > array.LongLength) throw new ArgumentOutOfRangeException("index"); T[] array2; if (Intrinsics.GetLength(array) == (IntPtr)0) array2 = new T[1] { element }; else { array2 = new T[array.LongLength + 1]; Array.Copy(array, 0, array2, 0, Math.Min(index + 1, array.LongLength)); Array.Copy(array, index, array2, index + 1, array.LongLength - index); array2[index] = element; } return array2; } public unsafe static T[] RemoveAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, long index) { IntPtr length = Intrinsics.GetLength(array); if (index < 0 || index >= (long)length) throw new ArgumentOutOfRangeException("index"); if (length == (IntPtr)1) return Array.Empty<T>(); T[] array2 = new T[(long)(IntPtr)(void*)((long)length - 1)]; Array.Copy(array, 0, array2, 0, index); Array.Copy(array, index + 1, array2, index, (long)length - index - 1); return array2; } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(1)] private unsafe static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T, TPredicate, TConsumer>([System.Runtime.CompilerServices.Nullable(1)] T[] array, TPredicate condition, ref TConsumer callback) where TPredicate : struct, ISupplier<T, bool> where TConsumer : struct, IConsumer<T> { IntPtr length = Intrinsics.GetLength(array); if (length == (IntPtr)0) return array; IntPtr intPtr = (IntPtr)0; T[] array2 = new T[(long)length]; T[] array3 = array; foreach (T val in array3) { if (((ISupplier<T, bool>)condition).Invoke(val)) ((IConsumer<T>)callback).Invoke(val); else { T[] array4 = array2; IntPtr intPtr2 = intPtr; intPtr = (IntPtr)(void*)((long)intPtr2 + 1); array4[(long)intPtr2] = val; } } if ((IntPtr)(void*)((long)length - (long)intPtr) == (IntPtr)0) return array; if (intPtr == (IntPtr)0) return Array.Empty<T>(); array = new T[(long)intPtr]; Array.Copy(array2, 0, array, 0, (long)intPtr); return array; } private static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TPredicate>(T[] array, [System.Runtime.CompilerServices.Nullable(0)] TPredicate condition, out long count) where TPredicate : struct, ISupplier<T, bool> { RemovalCounter<T> callback = default(RemovalCounter<T>); T[] result = RemoveAll(array, condition, ref callback); count = callback.Count; return result; } public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, Predicate<T> match, out long count) { return RemoveAll(array, (DelegatingPredicate<T>)match, out count); } [CLSCompliant(false)] public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] IntPtr match, out long count) { return RemoveAll(array, (Supplier<T, bool>)(long)match, out count); } public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, Predicate<T> match, Action<T> callback) { DelegatingConsumer<T> callback2 = new DelegatingConsumer<T>(callback); return RemoveAll(array, new DelegatingPredicate<T>(match), ref callback2); } [CLSCompliant(false)] public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] IntPtr match, Action<T> callback) { DelegatingConsumer<T> callback2 = new DelegatingConsumer<T>(callback); return RemoveAll(array, new Supplier<T, bool>(match), ref callback2); } internal static T[] New<[System.Runtime.CompilerServices.Nullable(2)] T>(long length) { if (length != 0) return new T[length]; return Array.Empty<T>(); } public static T[] RemoveFirst<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] input, long count) { if (count == 0) return input; IntPtr length = Intrinsics.GetLength(input); if (count >= (long)length) return Array.Empty<T>(); T[] array = new T[(long)length - count]; Array.Copy(input, count, array, 0, array.LongLength); return array; } public static T[] Slice<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] input, long startIndex, long length) { if (startIndex >= (long)Intrinsics.GetLength(input) || length == 0) return Array.Empty<T>(); if (startIndex == 0 && length == input.Length) return input; length = Math.Min(input.LongLength - startIndex, length); T[] array = new T[length]; Array.Copy(input, startIndex, array, 0, length); return array; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static ArraySegment<T> Slice<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] input, Range range) { (int Offset, int Length) offsetAndLength = range.GetOffsetAndLength(input.Length); int item = offsetAndLength.Offset; int item2 = offsetAndLength.Length; return new ArraySegment<T>(input, item, item2); } public static T[] RemoveLast<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] input, long count) { if (count == 0) return input; IntPtr length = Intrinsics.GetLength(input); if (count >= (long)length) return Array.Empty<T>(); T[] array = new T[(long)length - count]; Array.Copy(input, array, array.LongLength); return array; } [System.Runtime.CompilerServices.NullableContext(0)] public unsafe static bool BitwiseEquals<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] this T[] first, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] T[] second) where T : struct { if (first == null || second == null) return first == second; if (Intrinsics.GetLength(first) != Intrinsics.GetLength(second)) return false; if (Intrinsics.GetLength(first) == (IntPtr)0) return true; return Intrinsics.EqualsAligned(ref Unsafe.As<T, byte>(ref first[0]), ref Unsafe.As<T, byte>(ref second[0]), first.LongLength * sizeof(T)); } [System.Runtime.CompilerServices.NullableContext(0)] public unsafe static int BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, bool salted = true) where T : struct { if ((long)Intrinsics.GetLength(array) <= 0) return 0; return Intrinsics.GetHashCode32(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), salted); } [System.Runtime.CompilerServices.NullableContext(0)] private unsafe static int BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] T[] array, int hash, THashFunction hashFunction, bool salted) where T : struct where THashFunction : struct, ISupplier<int, int, int> { if ((long)Intrinsics.GetLength(array) <= 0) return hash; return Intrinsics.GetHashCode32(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), hash, hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] public static int BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, int hash, [System.Runtime.CompilerServices.Nullable(1)] Func<int, int, int> hashFunction, bool salted = true) where T : struct { return BitwiseHashCode(array, hash, (DelegatingSupplier<int, int, int>)hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] [CLSCompliant(false)] public static int BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, int hash, IntPtr hashFunction, bool salted = true) where T : struct { return BitwiseHashCode(array, hash, (Supplier<int, int, int>)(long)hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] private unsafe static long BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] T[] array, long hash, THashFunction hashFunction, bool salted) where T : struct where THashFunction : struct, ISupplier<long, long, long> { if ((long)Intrinsics.GetLength(array) <= 0) return hash; return Intrinsics.GetHashCode64(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), hash, hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] public static long BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, long hash, [System.Runtime.CompilerServices.Nullable(1)] Func<long, long, long> hashFunction, bool salted = true) where T : struct { return BitwiseHashCode64(array, hash, (DelegatingSupplier<long, long, long>)hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] [CLSCompliant(false)] public static long BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, long hash, IntPtr hashFunction, bool salted = true) where T : struct { return BitwiseHashCode64(array, hash, (Supplier<long, long, long>)(long)hashFunction, salted); } [System.Runtime.CompilerServices.NullableContext(0)] public unsafe static long BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, bool salted = true) where T : struct { if ((long)Intrinsics.GetLength(array) <= 0) return 0; return Intrinsics.GetHashCode64(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), salted); } [System.Runtime.CompilerServices.NullableContext(2)] public static bool SequenceEqual(this object[] first, object[] second, bool parallel = false) { if (first == second) return true; if (first == null) return second == null; if (second == null || Intrinsics.GetLength(first) != Intrinsics.GetLength(second)) return false; if (!parallel) return <SequenceEqual>g__EqualsSequential|28_0(first, second); return <SequenceEqual>g__EqualsParallel|28_1(first, second); } [System.Runtime.CompilerServices.NullableContext(0)] public unsafe static int BitwiseCompare<[System.Runtime.CompilerServices.IsUnmanaged] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] this T[] first, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] T[] second) where T : struct { if (first.IsNullOrEmpty()) { if (!second.IsNullOrEmpty()) return -1; return 0; } if (second.IsNullOrEmpty()) return 1; int num = first.LongLength.CompareTo(second.LongLength); if (num == 0) num = Intrinsics.Compare(ref Unsafe.As<T, byte>(ref first[0]), ref Unsafe.As<T, byte>(ref second[0]), first.LongLength * sizeof(T)); return num; } } }