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 (startIndex < 0 || startIndex > left.Length)
throw new ArgumentOutOfRangeException("startIndex");
T[] array = new T[startIndex + right.Length];
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 array.LongLength == 0;
return true;
}
public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueRefAction<T, long> action)
{
for (long num = 0; num < array.LongLength; num++) {
action.Invoke(ref array[num], num);
}
}
public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, RefAction<T, long> action)
{
ValueRefAction<T, long> action2 = new ValueRefAction<T, long>(action, true);
array.ForEach(ref action2);
}
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");
if (array.LongLength == 0)
return new T[1] {
element
};
T[] 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 static T[] RemoveAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, long index)
{
if (index < 0 || index >= array.LongLength)
throw new ArgumentOutOfRangeException("index");
if (array.LongLength == 1)
return Array.Empty<T>();
T[] array2 = new T[array.LongLength - 1];
Array.Copy(array, 0, array2, 0, index);
Array.Copy(array, index + 1, array2, index, array.LongLength - index - 1);
return array2;
}
private static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] C>(T[] array, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueFunc<T, bool> match, [System.Runtime.CompilerServices.Nullable(0)] ref C callback) where C : struct, IConsumer<T>
{
if (array.LongLength == 0)
return array;
long num = 0;
T[] array2 = new T[array.LongLength];
T[] array3 = array;
foreach (T val in array3) {
if (match.Invoke(val))
((IConsumer<T>)callback).Invoke(val);
else {
T[] array4 = array2;
long num2 = num;
num = num2 + 1;
array4[num2] = val;
}
}
if (array.LongLength - num == 0)
return array;
if (num == 0)
return Array.Empty<T>();
array = new T[num];
Array.Copy(array2, 0, array, 0, num);
return array;
}
public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueFunc<T, bool> match, out long count)
{
RemovalCounter<T> callback = default(RemovalCounter<T>);
T[] result = RemoveAll(array, ref match, 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)
{
ValueFunc<T, bool> match2 = match.AsValueFunc(true);
return array.RemoveAll(ref match2, out count);
}
public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueFunc<T, bool> match, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueAction<T> callback)
{
return OneDimensionalArray.RemoveAll<T, ValueAction<T>>(array, ref match, ref Unsafe.AsRef(ref callback));
}
public static T[] RemoveAll<[System.Runtime.CompilerServices.Nullable(2)] T>(this T[] array, Predicate<T> match, Action<T> callback)
{
ValueFunc<T, bool> match2 = match.AsValueFunc(true);
ValueAction<T> callback2 = new ValueAction<T>(callback, true);
return array.RemoveAll(ref match2, 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;
if (count >= input.LongLength)
return Array.Empty<T>();
T[] array = new T[input.LongLength - 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 >= input.LongLength || 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, [In] [IsReadOnly] ref 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;
if (count >= input.LongLength)
return Array.Empty<T>();
T[] array = new T[input.LongLength - 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 (first.LongLength != second.LongLength)
return false;
if (first.LongLength == 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 (array.LongLength <= 0)
return 0;
return Intrinsics.GetHashCode32(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), salted);
}
[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, int hash, [In] [IsReadOnly] ref ValueFunc<int, int, int> hashFunction, bool salted = true) where T : struct
{
if (array.LongLength <= 0)
return hash;
return Intrinsics.GetHashCode32(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), hash, ref 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
{
ValueFunc<int, int, int> hashFunction2 = new ValueFunc<int, int, int>(hashFunction, true);
return array.BitwiseHashCode(hash, ref hashFunction2, 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, long hash, [In] [IsReadOnly] ref ValueFunc<long, long, long> hashFunction, bool salted = true) where T : struct
{
if (array.LongLength <= 0)
return hash;
return Intrinsics.GetHashCode64(ref Unsafe.As<T, byte>(ref array[0]), array.LongLength * sizeof(T), hash, ref 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
{
ValueFunc<long, long, long> hashFunction2 = new ValueFunc<long, long, long>(hashFunction, true);
return array.BitwiseHashCode64(hash, ref hashFunction2, 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 (array.LongLength <= 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 || first.LongLength != second.LongLength)
return false;
if (!parallel)
return <SequenceEqual>g__EqualsSequential|25_0(first, second);
return <SequenceEqual>g__EqualsParallel|25_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 == null) {
if (second != null)
return -1;
return 0;
}
if (second == null)
return 1;
if (first.LongLength != second.LongLength)
return first.LongLength.CompareTo(second.LongLength);
return Intrinsics.Compare(ref Unsafe.As<T, byte>(ref first[0]), ref Unsafe.As<T, byte>(ref second[0]), first.LongLength * sizeof(T));
}
}
}