using System; using System.Collections.Generic; using System.Linq; namespace pkNX.Structures; /// /// Array reusable logic /// public static class ArrayUtil { public static byte[] Slice(this byte[] src, int offset, int length) { byte[] data = new byte[length]; Buffer.BlockCopy(src, offset, data, 0, data.Length); return data; } public static byte[] SliceEnd(this byte[] src, int offset) { int length = src.Length - offset; byte[] data = new byte[length]; Buffer.BlockCopy(src, offset, data, 0, data.Length); return data; } public static T[] Slice(this T[] src, int offset, int length) { var data = new T[length]; Array.Copy(src, offset, data, 0, data.Length); return data; } public static T[] SliceEnd(this T[] src, int offset) { int length = src.Length - offset; var data = new T[length]; Array.Copy(src, offset, data, 0, data.Length); return data; } public static bool WithinRange(int value, int min, int max) => min <= value && value < max; public static T[][] Split(this T[] data, int size) { var result = new T[data.Length / size][]; for (int i = 0; i < data.Length; i += size) result[i / size] = data.Slice(i, size); return result; } 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); } public static IEnumerable EnumerateSplit(T[] bin, int size, int start, int end) { if (end < 0) end = bin.Length; for (int i = start; i < end; i += size) yield return bin.Slice(i, size); } public static bool[] GitBitFlagArray(byte[] data, int offset, int count) { bool[] result = new bool[count]; for (int i = 0; i < result.Length; i++) result[i] = (data[offset + (i >> 3)] >> (i & 7) & 0x1) == 1; return result; } public static void SetBitFlagArray(byte[] data, int offset, bool[] value) { for (int i = 0; i < value.Length; i++) { var ofs = offset + (i >> 3); var mask = (1 << (i & 7)); if (value[i]) data[ofs] |= (byte)mask; else data[ofs] &= (byte)~mask; } } public static byte[] SetBitFlagArray(bool[] value) { byte[] data = new byte[value.Length / 8]; SetBitFlagArray(data, 0, value); return data; } /// /// 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(exist)) 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, 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; } } public static class ArrayUtilsExt { public static TSource[] Append(this TSource[] first, TSource second, params TSource[] third) { return ArrayUtil.ConcatAll(first, new[] { second }, third); } public static TSource[] Remove(this TSource[] first, TSource toRemove) { var list = first.ToList(); list.Remove(toRemove); return list.ToArray(); } } public static class FlagUtil { public static bool GetFlag(ReadOnlySpan arr, int offset, int bitIndex) { bitIndex &= 7; // ensure bit access is 0-7 return (arr[offset] >> bitIndex & 1) != 0; } public static void SetFlag(Span arr, int offset, int bitIndex, bool value) { bitIndex &= 7; // ensure bit access is 0-7 arr[offset] &= (byte)~(1 << bitIndex); arr[offset] |= (byte)((value ? 1 : 0) << bitIndex); } public static void GetFlagArray(ReadOnlySpan data, Span result) { for (int i = 0; i < result.Length; i++) result[i] = GetFlag(data, i >> 3, i); } public static void SetFlagArray(Span data, ReadOnlySpan result) { for (int i = 0; i < result.Length; i++) SetFlag(data, i >> 3, i, result[i]); } }