Exemple #1
0
        /// <summary>
        /// Insertion sort
        /// </summary>
        /// <param name="arr">Array to sort.</param>
        /// <param name="sortSize">Size of the array to sort. If greater than array capacity, it will get clamped.</param>
        public static unsafe void InsertionSort(NativeArray <uint> arr, int sortSize)
        {
            sortSize = Math.Min(arr.Length, sortSize);
            if (!arr.IsCreated || sortSize == 0)
            {
                return;
            }

            CoreUnsafeUtils.InsertionSort((uint *)arr.GetUnsafePtr(), sortSize);
        }
Exemple #2
0
        /// <summary>
        /// Insertion sort
        /// </summary>
        /// <param name="arr">Array to sort.</param>
        /// <param name="sortSize">Size of the array to sort. If greater than array capacity, it will get clamped.</param>
        public static unsafe void InsertionSort(uint[] arr, int sortSize)
        {
            sortSize = Math.Min(arr.Length, sortSize);
            if (arr == null || sortSize == 0)
            {
                return;

                fixed(uint *ptr = arr)
                CoreUnsafeUtils.InsertionSort(ptr, sortSize);
        }
Exemple #3
0
        /// <summary>
        /// Merge sort - non recursive
        /// </summary>
        /// <param name="arr">Array to sort.</param>
        /// <param name="sortSize">Size of the array to sort. If greater than array capacity, it will get clamped.</param>
        /// <param name="supportArray">Secondary array reference, used to store intermediate merge results.</param>
        public static unsafe void MergeSort(NativeArray <uint> arr, int sortSize, ref NativeArray <uint> supportArray)
        {
            sortSize = Math.Min(sortSize, arr.Length);
            if (!arr.IsCreated || sortSize == 0)
            {
                return;
            }

            if (!supportArray.IsCreated || supportArray.Length < sortSize)
            {
                supportArray.ResizeArray(arr.Length);
            }

            CoreUnsafeUtils.MergeSort((uint *)arr.GetUnsafePtr(), (uint *)supportArray.GetUnsafePtr(), sortSize);
        }
Exemple #4
0
        /// <summary>
        /// Merge sort - non recursive
        /// </summary>
        /// <param name="arr">Array to sort.</param>
        /// <param name="sortSize">Size of the array to sort. If greater than array capacity, it will get clamped.</param>
        /// <param name="supportArray">Secondary array reference, used to store intermediate merge results.</param>
        public static unsafe void MergeSort(uint[] arr, int sortSize, ref uint[] supportArray)
        {
            sortSize = Math.Min(sortSize, arr.Length);
            if (arr == null || sortSize == 0)
            {
                return;
            }

            if (supportArray == null || supportArray.Length < sortSize)
            {
                supportArray = new uint[sortSize];

                fixed(uint *arrPtr = arr)
                fixed(uint *supportPtr = supportArray)
                CoreUnsafeUtils.MergeSort(arrPtr, supportPtr, sortSize);
        }
Exemple #5
0
        /// <summary>
        /// Radix sort or bucket sort, stable and non in place.
        /// </summary>
        /// <param name="arr">Array to sort.</param>
        /// <param name="sortSize">Size of the array to sort. If greater than array capacity, it will get clamped.</param>
        /// <param name="supportArray">Array of uints that is used for support data. The algorithm will automatically allocate it if necessary.</param>
        /// <param name="radixBits">Number of bits to use for each bucket. Can only be 8, 4 or 2.</param>
        public static unsafe void RadixSort(uint[] arr, int sortSize, ref uint[] supportArray, int radixBits = 8)
        {
            sortSize = Math.Min(sortSize, arr.Length);
            CalculateRadixParams(radixBits, out int bitStates);
            if (arr == null || sortSize == 0)
            {
                return;
            }

            int supportSize = CalculateRadixSupportSize(bitStates, sortSize);

            if (supportArray == null || supportArray.Length < supportSize)
                supportArray = new uint[supportSize];

            fixed(uint *ptr = arr)
            fixed(uint *supportArrayPtr = supportArray)
            CoreUnsafeUtils.RadixSort(ptr, supportArrayPtr, radixBits, bitStates, sortSize);
        }
 /// <summary>
 /// Quick Sort
 /// </summary>
 /// <param name="arr">uint array.</param>
 /// <param name="left">Left boundary.</param>
 /// <param name="right">Left boundary.</param>
 public static unsafe void QuickSort(uint[] arr, int left, int right)
 {
     fixed(uint *ptr = arr)
     CoreUnsafeUtils.QuickSort <uint, uint, UintKeyGetter>(ptr, left, right);
 }