DotNext by .NET Foundation and Contributors

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

.NET API 437,208 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.Runtime.InteropServices; 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 ((ulong)startIndex > (ulong)left.LongLength) 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([NotNullWhen(false)] this Array array) { if (array != null) return Intrinsics.GetLength(array) == (IntPtr)0; return true; } public unsafe static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [System.Runtime.CompilerServices.NativeInteger] RefAction<T, IntPtr> action) { for (IntPtr intPtr = (IntPtr)0; (long)intPtr < (long)Intrinsics.GetLength(array); intPtr = (IntPtr)(void*)((long)intPtr + 1)) { action(ref array[(long)intPtr], intPtr); } } [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) { long num = array.LongLength; if ((ulong)index > (ulong)num) throw new ArgumentOutOfRangeException("index"); T[] array2; if (Intrinsics.GetLength(array) == (IntPtr)0) array2 = new T[1] { element }; else { array2 = new T[num + 1]; Array.Copy(array, 0, array2, 0, Math.Min(index + 1, num)); Array.Copy(array, index, array2, index + 1, num - index); array2[index] = element; } return array2; } public static T[] Insert<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, T element, Index index) { return array.Insert(element, (long)index.GetOffset(array.Length)); } public unsafe static T[] RemoveAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, long index) { IntPtr length = Intrinsics.GetLength(array); if ((ulong)index >= (ulong)(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; } public static T[] RemoveAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, Index index) { return array.RemoveAt((long)index.GetOffset(array.Length)); } [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); } 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 MemoryMarshal.GetArrayDataReference(first)), ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(second)), (IntPtr)(void*)checked(unchecked((long)Intrinsics.GetLength(first)) * unchecked((long)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 { IntPtr length = Intrinsics.GetLength(array); if ((long)length <= 0) return 0; return Intrinsics.GetHashCode32(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), (IntPtr)(void*)checked(unchecked((long)length) * unchecked((long)sizeof(T))), salted); } [System.Runtime.CompilerServices.NullableContext(0)] private unsafe static void BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] T[] array, ref THashFunction hashFunction, bool salted) where T : struct where THashFunction : struct, IConsumer<int> { IntPtr length = Intrinsics.GetLength(array); if ((long)length > 0) Intrinsics.GetHashCode32(ref hashFunction, ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), (IntPtr)(void*)checked(unchecked((long)length) * unchecked((long)sizeof(T)))); if (salted) ((IConsumer<int>)hashFunction).Invoke(RandomExtensions.BitwiseHashSalt); } [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 { Accumulator<int, int> hashFunction2 = new Accumulator<int, int>(hashFunction, hash); BitwiseHashCode(array, ref hashFunction2, salted); return hashFunction2.Invoke(); } [System.Runtime.CompilerServices.NullableContext(0)] [CLSCompliant(false)] public static int BitwiseHashCode<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, bool salted = true) where T : struct where THashFunction : struct, IConsumer<int>, ISupplier<int> { THashFunction hashFunction = new THashFunction(); BitwiseHashCode(array, ref hashFunction, salted); return ((ISupplier<int>)hashFunction).Invoke(); } [System.Runtime.CompilerServices.NullableContext(0)] private unsafe static void BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] T[] array, ref THashFunction hashFunction, bool salted) where T : struct where THashFunction : struct, IConsumer<long> { IntPtr length = Intrinsics.GetLength(array); if ((long)length > 0) Intrinsics.GetHashCode64(ref hashFunction, ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), (IntPtr)(void*)checked(unchecked((long)length) * unchecked((long)sizeof(T)))); if (salted) ((IConsumer<long>)hashFunction).Invoke((long)RandomExtensions.BitwiseHashSalt); } [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 { Accumulator<long, long> hashFunction2 = new Accumulator<long, long>(hashFunction, hash); BitwiseHashCode64(array, ref hashFunction2, salted); return hashFunction2.Invoke(); } [System.Runtime.CompilerServices.NullableContext(0)] [CLSCompliant(false)] public static long BitwiseHashCode64<[System.Runtime.CompilerServices.IsUnmanaged] T, THashFunction>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this T[] array, bool salted = true) where T : struct where THashFunction : struct, IConsumer<long>, ISupplier<long> { THashFunction hashFunction = new THashFunction(); BitwiseHashCode64(array, ref hashFunction, salted); return ((ISupplier<long>)hashFunction).Invoke(); } [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 { IntPtr length = Intrinsics.GetLength(array); if ((long)length <= 0) return 0; return Intrinsics.GetHashCode64(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), (IntPtr)(void*)checked(unchecked((long)length) * unchecked((long)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|29_0(first, second); return <SequenceEqual>g__EqualsParallel|29_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; IntPtr length = Intrinsics.GetLength(first); int num = length.CompareTo(Intrinsics.GetLength(second)); if (num == 0) num = Intrinsics.Compare(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(first)), ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(second)), (IntPtr)(void*)checked(unchecked((long)length) * unchecked((long)sizeof(T)))); return num; } } }