Esempio n. 1
0
            void sort(int min, int max, int digitIndex)
            {
                if (max - min <= 1)
                    return;

                int exp = (int)System.Math.Pow(multi, digitIndex);
                int tailsStart = multi * digitIndex;
                int tailsEnd = multi * (digitIndex + 1);

                for (int i = 0; i < multi; i++)
                    UnsafeUtility.WriteArrayElement(histogram, i, 0);

                for (int i = min; i < max; i++)
                    RadixUtil.IncrementArrayElement(histogram, RadixUtil.GetDigit(array, i, exp, multi));

                UnsafeUtility.WriteArrayElement(heads, 0, min);
                UnsafeUtility.WriteArrayElement(tails, tailsStart, min + UnsafeUtility.ReadArrayElement<int>(histogram, 0));

                for (int i = 1; i < multi; i++)
                {
                    UnsafeUtility.WriteArrayElement(heads, i,
                        UnsafeUtility.ReadArrayElement<int>(heads, i - 1) +
                        UnsafeUtility.ReadArrayElement<int>(histogram, i - 1));
                    UnsafeUtility.WriteArrayElement(tails, tailsStart + i,
                        UnsafeUtility.ReadArrayElement<int>(tails, tailsStart + i - 1) +
                        UnsafeUtility.ReadArrayElement<int>(histogram, i));
                }

                uint temp = 0;
                for (int i = 0; i < multi; i++)
                {
                    while (UnsafeUtility.ReadArrayElement<int>(heads, i) < UnsafeUtility.ReadArrayElement<int>(tails, tailsStart + i))
                    {
                        uint element = UnsafeUtility.ReadArrayElement<uint>(array, UnsafeUtility.ReadArrayElement<int>(heads, i));
                        int elementIndex = (int)((element / exp) % multi);
                        while (elementIndex != i)
                        {
                            temp = element;
                            element = UnsafeUtility.ReadArrayElement<uint>(array, UnsafeUtility.ReadArrayElement<int>(heads, elementIndex));
                            UnsafeUtility.WriteArrayElement(array, UnsafeUtility.ReadArrayElement<int>(heads, elementIndex), temp);
                            RadixUtil.IncrementArrayElement(heads, elementIndex);
                            elementIndex = (int)((element / exp) % multi);
                        }
                        UnsafeUtility.WriteArrayElement(array, UnsafeUtility.ReadArrayElement<int>(heads, i), (int)element);
                        RadixUtil.IncrementArrayElement(heads, i);
                    }
                }

                digitIndex--;
                if (digitIndex >= 0)
                {
                    sort(min, UnsafeUtility.ReadArrayElement<int>(tails, tailsStart), digitIndex);
                    for (int i = tailsStart + 1; i < tailsEnd; i++)
                    {
                        sort(UnsafeUtility.ReadArrayElement<int>(tails, i - 1), UnsafeUtility.ReadArrayElement<int>(tails, i), digitIndex);
                    }
                }
            }