348 lines
7.7 KiB
C#
348 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace AspClassic.Scripting.Utils;
|
|
|
|
internal static class ArrayUtils
|
|
{
|
|
internal sealed class FunctorComparer<T> : IComparer<T>
|
|
{
|
|
private readonly Comparison<T> _comparison;
|
|
|
|
public FunctorComparer(Comparison<T> comparison)
|
|
{
|
|
_comparison = comparison;
|
|
}
|
|
|
|
public int Compare(T x, T y)
|
|
{
|
|
return _comparison(x, y);
|
|
}
|
|
}
|
|
|
|
public static readonly string[] EmptyStrings = new string[0];
|
|
|
|
public static readonly object[] EmptyObjects = new object[0];
|
|
|
|
public static IComparer<T> ToComparer<T>(Comparison<T> comparison)
|
|
{
|
|
return new FunctorComparer<T>(comparison);
|
|
}
|
|
|
|
public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] input, Converter<TInput, TOutput> conv)
|
|
{
|
|
return Array.ConvertAll(input, conv);
|
|
}
|
|
|
|
public static T[] FindAll<T>(T[] array, Predicate<T> match)
|
|
{
|
|
return Array.FindAll(array, match);
|
|
}
|
|
|
|
public static void PrintTable(StringBuilder output, string[,] table)
|
|
{
|
|
ContractUtils.RequiresNotNull(output, "output");
|
|
ContractUtils.RequiresNotNull(table, "table");
|
|
int num = 0;
|
|
for (int i = 0; i < table.GetLength(0); i++)
|
|
{
|
|
if (table[i, 0].Length > num)
|
|
{
|
|
num = table[i, 0].Length;
|
|
}
|
|
}
|
|
for (int j = 0; j < table.GetLength(0); j++)
|
|
{
|
|
output.Append(" ");
|
|
output.Append(table[j, 0]);
|
|
for (int k = table[j, 0].Length; k < num + 1; k++)
|
|
{
|
|
output.Append(' ');
|
|
}
|
|
output.AppendLine(table[j, 1]);
|
|
}
|
|
}
|
|
|
|
public static T[] Copy<T>(T[] array)
|
|
{
|
|
if (array.Length <= 0)
|
|
{
|
|
return array;
|
|
}
|
|
return (T[])array.Clone();
|
|
}
|
|
|
|
public static T[] MakeArray<T>(ICollection<T> list)
|
|
{
|
|
if (list.Count == 0)
|
|
{
|
|
return new T[0];
|
|
}
|
|
T[] array = new T[list.Count];
|
|
list.CopyTo(array, 0);
|
|
return array;
|
|
}
|
|
|
|
public static T[] MakeArray<T>(ICollection<T> elements, int reservedSlotsBefore, int reservedSlotsAfter)
|
|
{
|
|
if (reservedSlotsAfter < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("reservedSlotsAfter");
|
|
}
|
|
if (reservedSlotsBefore < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("reservedSlotsBefore");
|
|
}
|
|
if (elements == null)
|
|
{
|
|
return new T[reservedSlotsBefore + reservedSlotsAfter];
|
|
}
|
|
T[] array = new T[reservedSlotsBefore + elements.Count + reservedSlotsAfter];
|
|
elements.CopyTo(array, reservedSlotsBefore);
|
|
return array;
|
|
}
|
|
|
|
public static T[] RotateRight<T>(T[] array, int count)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
if (count < 0 || count > array.Length)
|
|
{
|
|
throw new ArgumentOutOfRangeException("count");
|
|
}
|
|
T[] array2 = new T[array.Length];
|
|
int num = array.Length - count;
|
|
Array.Copy(array, 0, array2, count, num);
|
|
Array.Copy(array, num, array2, 0, count);
|
|
return array2;
|
|
}
|
|
|
|
public static T[] ShiftRight<T>(T[] array, int count)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
if (count < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("count");
|
|
}
|
|
T[] array2 = new T[array.Length + count];
|
|
Array.Copy(array, 0, array2, count, array.Length);
|
|
return array2;
|
|
}
|
|
|
|
public static T[] ShiftLeft<T>(T[] array, int count)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
if (count < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("count");
|
|
}
|
|
T[] array2 = new T[array.Length - count];
|
|
Array.Copy(array, count, array2, 0, array2.Length);
|
|
return array2;
|
|
}
|
|
|
|
public static T[] Insert<T>(T item, IList<T> list)
|
|
{
|
|
T[] array = new T[list.Count + 1];
|
|
array[0] = item;
|
|
list.CopyTo(array, 1);
|
|
return array;
|
|
}
|
|
|
|
public static T[] Insert<T>(T item1, T item2, IList<T> list)
|
|
{
|
|
T[] array = new T[list.Count + 2];
|
|
array[0] = item1;
|
|
array[1] = item2;
|
|
list.CopyTo(array, 2);
|
|
return array;
|
|
}
|
|
|
|
public static T[] Insert<T>(T item, T[] array)
|
|
{
|
|
T[] array2 = ShiftRight(array, 1);
|
|
array2[0] = item;
|
|
return array2;
|
|
}
|
|
|
|
public static T[] Insert<T>(T item1, T item2, T[] array)
|
|
{
|
|
T[] array2 = ShiftRight(array, 2);
|
|
array2[0] = item1;
|
|
array2[1] = item2;
|
|
return array2;
|
|
}
|
|
|
|
public static T[] Append<T>(T[] array, T item)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
Array.Resize(ref array, array.Length + 1);
|
|
array[array.Length - 1] = item;
|
|
return array;
|
|
}
|
|
|
|
public static T[] AppendRange<T>(T[] array, IList<T> items)
|
|
{
|
|
return AppendRange(array, items, 0);
|
|
}
|
|
|
|
public static T[] AppendRange<T>(T[] array, IList<T> items, int additionalItemCount)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
if (additionalItemCount < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("additionalItemCount");
|
|
}
|
|
int num = array.Length;
|
|
Array.Resize(ref array, array.Length + items.Count + additionalItemCount);
|
|
int num2 = 0;
|
|
while (num2 < items.Count)
|
|
{
|
|
array[num] = items[num2];
|
|
num2++;
|
|
num++;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static T[,] Concatenate<T>(T[,] array1, T[,] array2)
|
|
{
|
|
int length = array1.GetLength(1);
|
|
int length2 = array1.GetLength(0);
|
|
int length3 = array2.GetLength(0);
|
|
int num = length2 + length3;
|
|
T[,] array3 = new T[num, length];
|
|
for (int i = 0; i < length2; i++)
|
|
{
|
|
for (int j = 0; j < length; j++)
|
|
{
|
|
array3[i, j] = array1[i, j];
|
|
}
|
|
}
|
|
for (int k = 0; k < length3; k++)
|
|
{
|
|
for (int l = 0; l < length; l++)
|
|
{
|
|
array3[k + length2, l] = array2[k, l];
|
|
}
|
|
}
|
|
return array3;
|
|
}
|
|
|
|
public static void SwapLastTwo<T>(T[] array)
|
|
{
|
|
T val = array[array.Length - 1];
|
|
array[array.Length - 1] = array[array.Length - 2];
|
|
array[array.Length - 2] = val;
|
|
}
|
|
|
|
public static T[] RemoveFirst<T>(IList<T> list)
|
|
{
|
|
return ShiftLeft(MakeArray(list), 1);
|
|
}
|
|
|
|
public static T[] RemoveFirst<T>(T[] array)
|
|
{
|
|
return ShiftLeft(array, 1);
|
|
}
|
|
|
|
public static T[] RemoveLast<T>(T[] array)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
Array.Resize(ref array, array.Length - 1);
|
|
return array;
|
|
}
|
|
|
|
public static T[] RemoveAt<T>(IList<T> list, int indexToRemove)
|
|
{
|
|
return RemoveAt(MakeArray(list), indexToRemove);
|
|
}
|
|
|
|
public static T[] RemoveAt<T>(T[] array, int indexToRemove)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
ContractUtils.Requires(indexToRemove >= 0 && indexToRemove < array.Length, "index");
|
|
T[] array2 = new T[array.Length - 1];
|
|
if (indexToRemove > 0)
|
|
{
|
|
Array.Copy(array, 0, array2, 0, indexToRemove);
|
|
}
|
|
int num = array.Length - indexToRemove - 1;
|
|
if (num > 0)
|
|
{
|
|
Array.Copy(array, array.Length - num, array2, array2.Length - num, num);
|
|
}
|
|
return array2;
|
|
}
|
|
|
|
public static T[] InsertAt<T>(IList<T> list, int index, params T[] items)
|
|
{
|
|
return InsertAt(MakeArray(list), index, items);
|
|
}
|
|
|
|
public static T[] InsertAt<T>(T[] array, int index, params T[] items)
|
|
{
|
|
ContractUtils.RequiresNotNull(array, "array");
|
|
ContractUtils.RequiresNotNull(items, "items");
|
|
ContractUtils.Requires(index >= 0 && index <= array.Length, "index");
|
|
if (items.Length == 0)
|
|
{
|
|
return Copy(array);
|
|
}
|
|
T[] array2 = new T[array.Length + items.Length];
|
|
if (index > 0)
|
|
{
|
|
Array.Copy(array, 0, array2, 0, index);
|
|
}
|
|
Array.Copy(items, 0, array2, index, items.Length);
|
|
int num = array.Length - index;
|
|
if (num > 0)
|
|
{
|
|
Array.Copy(array, array.Length - num, array2, array2.Length - num, num);
|
|
}
|
|
return array2;
|
|
}
|
|
|
|
public static T[] ToArray<T>(ICollection<T> list)
|
|
{
|
|
if (!(list is T[] result))
|
|
{
|
|
T[] array = new T[list.Count];
|
|
int num = 0;
|
|
{
|
|
foreach (T item in list)
|
|
{
|
|
array[num++] = item;
|
|
}
|
|
return array;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static bool ValueEquals<T>(this T[] array, T[] other)
|
|
{
|
|
if (other.Length != array.Length)
|
|
{
|
|
return false;
|
|
}
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
if (!object.Equals(array[i], other[i]))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static T[] Reverse<T>(this T[] array)
|
|
{
|
|
T[] array2 = new T[array.Length];
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
array2[array.Length - i - 1] = array[i];
|
|
}
|
|
return array2;
|
|
}
|
|
}
|