using System; using System.Collections.Generic; namespace PKHeX.Core; /// /// Array reusable logic /// public static class ArrayUtil { public static int Count(this Span data, T value) where T : IEquatable { return ((ReadOnlySpan)data).Count(value); } public static T Find(this Span data, Func value) where T : unmanaged { foreach (var x in data) { if (value(x)) return x; } return default; } public static int Count(this ReadOnlySpan data, T value) where T : IEquatable { int count = 0; for (int i = data.Length - 1; i >= 0; i--) { if (data[i].Equals(value)) count++; } return count; } public static byte[] Slice(this byte[] src, int offset, int length) => src.AsSpan(offset, length).ToArray(); public static T[] Slice(this T[] src, int offset, int length) => src.AsSpan(offset, length).ToArray(); /// /// Checks the range (exclusive max) if the is inside. /// public static bool WithinRange(int value, int min, int max) => min <= value && value < max; public static IEnumerable EnumerateSplit(T[] bin, int size, int start = 0) { for (int i = start; i < bin.Length; i += size) yield return bin.Slice(i, size); } /// /// Copies a list to the destination list, with an option to copy to a starting point. /// /// Source list to copy from /// Destination list/array /// Criteria for skipping a slot /// Starting point to copy to /// Count of copied. public static int CopyTo(this IEnumerable list, IList dest, Func skip, int start = 0) { int ctr = start; int skipped = 0; foreach (var z in list) { // seek forward to next open slot int next = FindNextValidIndex(dest, skip, ctr); if (next == -1) break; skipped += next - ctr; ctr = next; dest[ctr++] = z; } return ctr - start - skipped; } public static int FindNextValidIndex(IList dest, Func skip, int ctr) { while (true) { if ((uint)ctr >= dest.Count) return -1; var exist = dest[ctr]; if (exist == null || !skip(ctr)) return ctr; ctr++; } } /// /// Copies an list to the destination list, with an option to copy to a starting point. /// /// Typed object to copy /// Source list to copy from /// Destination list/array /// Starting point to copy to /// Count of copied. public static int CopyTo(this IEnumerable list, IList dest, int start = 0) { int ctr = start; foreach (var z in list) { if ((uint)ctr >= dest.Count) break; dest[ctr++] = z; } return ctr - start; } internal static T[] ConcatAll(params T[][] arr) { int len = 0; foreach (var a in arr) len += a.Length; var result = new T[len]; int ctr = 0; foreach (var a in arr) { a.CopyTo(result, ctr); ctr += a.Length; } return result; } internal static T[] ConcatAll(T[] arr1, T[] arr2) { int len = arr1.Length + arr2.Length; var result = new T[len]; arr1.CopyTo(result, 0); arr2.CopyTo(result, arr1.Length); return result; } internal static T[] ConcatAll(T[] arr1, T[] arr2, T[] arr3) { int len = arr1.Length + arr2.Length + arr3.Length; var result = new T[len]; arr1.CopyTo(result, 0); arr2.CopyTo(result, arr1.Length); arr3.CopyTo(result, arr1.Length + arr2.Length); return result; } internal static T[] ConcatAll(T[] arr1, ReadOnlySpan arr2) { int len = arr1.Length + arr2.Length; var result = new T[len]; arr1.CopyTo(result, 0); arr2.CopyTo(result.AsSpan(arr1.Length)); return result; } internal static T[] ConcatAll(T[] arr1, T[] arr2, ReadOnlySpan arr3) { int len = arr1.Length + arr2.Length + arr3.Length; var result = new T[len]; arr1.CopyTo(result, 0); arr2.CopyTo(result, arr1.Length); arr3.CopyTo(result.AsSpan(arr1.Length + arr2.Length)); return result; } }