using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DCFApixels.DragonECS.Utils { internal static class ArrayUtility { public static void Fill(T[] array, T value, int startIndex = 0, int length = -1) { if (length < 0) length = array.Length; else length = startIndex + length; for (int i = startIndex; i < length; i++) array[i] = value; } } internal static unsafe class UnmanagedArrayUtility { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T* New(int capacity) where T : unmanaged { return (T*)Marshal.AllocHGlobal(Marshal.SizeOf() * capacity).ToPointer(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T* NewAndInit(int capacity) where T : unmanaged { int newSize = Marshal.SizeOf(typeof(T)) * capacity; byte* newPointer = (byte*)Marshal.AllocHGlobal(newSize).ToPointer(); for (int i = 0; i < newSize; i++) *(newPointer + i) = 0; return (T*)newPointer; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Free(void* pointer) { Marshal.FreeHGlobal(new IntPtr(pointer)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T* Resize(void* oldPointer, int newCount) where T : unmanaged { return (T*)(Marshal.ReAllocHGlobal( new IntPtr(oldPointer), new IntPtr(Marshal.SizeOf(typeof(T)) * newCount))).ToPointer(); } } public static class EntitiesCollectionUtility { public static string AutoToString(IEnumerable range, string name) { return $"{name}({range.Count()}) {{{string.Join(", ", range.OrderBy(o => o))}}})"; } } }