using System; using System.Collections.Generic; using Unity.Collections.LowLevel.Unsafe; namespace UnityEngine.Experimental.Rendering { public static unsafe class CoreUnsafeUtils { public static void CopyTo(this List list, void* dest, int count) where T : struct { var c = Mathf.Min(count, list.Count); for (int i = 0; i < c; ++i) UnsafeUtility.WriteArrayElement(dest, i, list[i]); } public static void CopyTo(this T[] list, void* dest, int count) where T : struct { var c = Mathf.Min(count, list.Length); for (int i = 0; i < c; ++i) UnsafeUtility.WriteArrayElement(dest, i, list[i]); } public static void QuickSort(int count, void* data) where T : struct, IComparable { QuickSort(data, 0, count - 1); } public static void QuickSort(void* data, int left, int right) where T : struct, IComparable { // For Recursion if (left < right) { int pivot = Partition(data, left, right); if (pivot > 1) QuickSort(data, left, pivot); if (pivot + 1 < right) QuickSort(data, pivot + 1, right); } } // Just a sort function that doesn't allocate memory // Note: Shoud be repalc by a radix sort for positive integer static int Partition(void* data, int left, int right) where T : struct, IComparable { var pivot = UnsafeUtility.ReadArrayElement(data, left); --left; ++right; while (true) { var lvalue = default(T); do { ++left; } while ((lvalue = UnsafeUtility.ReadArrayElement(data, left)).CompareTo(pivot) < 0); var rvalue = default(T); do { --right; } while ((rvalue = UnsafeUtility.ReadArrayElement(data, right)).CompareTo(pivot) > 0); if (left < right) { UnsafeUtility.WriteArrayElement(data, right, lvalue); UnsafeUtility.WriteArrayElement(data, left, rvalue); } else { return right; } } } } }