예제 #1
0
 public void Sort(T[] keys, int index, int length, IComparer <T> comparer)
 {
     try
     {
         if (comparer == null || comparer == Comparer <T> .Default)
         {
             if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
             {
                 GenericArraySortHelper <T> .IntrospectiveSort(keys, index, length);
             }
             else
             {
                 GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, index, length + index - 1, 32);
             }
         }
         else if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
         {
             ArraySortHelper <T> .IntrospectiveSort(keys, index, length, comparer);
         }
         else
         {
             ArraySortHelper <T> .DepthLimitedQuickSort(keys, index, length + index - 1, comparer, 32);
         }
     }
     catch (IndexOutOfRangeException ex)
     {
         IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer((object)comparer);
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), ex);
     }
 }
 public void Sort(T[] keys, int index, int length, IComparer <T> comparer)
 {
     try
     {
         if ((comparer == null) || (comparer == Comparer <T> .Default))
         {
             GenericArraySortHelper <T> .QuickSort(keys, index, index + (length - 1));
         }
         else
         {
             ArraySortHelper <T> .QuickSort(keys, index, index + (length - 1), comparer);
         }
     }
     catch (IndexOutOfRangeException)
     {
         object[] values = new object[3];
         values[0] = default(T);
         values[1] = typeof(T).Name;
         throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", values));
     }
     catch (Exception exception)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
     }
 }
예제 #3
0
 internal static void IntrospectiveSort(T[] keys, int left, int length)
 {
     if (length < 2)
     {
         return;
     }
     GenericArraySortHelper <T> .IntroSort(keys, left, length + left - 1, 2 *IntrospectiveSortUtilities.FloorLog2(keys.Length));
 }
예제 #4
0
        private static int PickPivotAndPartition(T[] keys, int lo, int hi)
        {
            int index = lo + (hi - lo) / 2;

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, index);

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index, hi);

            T obj = keys[index];

            GenericArraySortHelper <T> .Swap(keys, index, hi - 1);

            int i = lo;
            int j = hi - 1;

            while (i < j)
            {
                if ((object)obj == null)
                {
                    do
                    {
                        ;
                    }while (i < hi - 1 && (object)keys[++i] == null);
                    while (j > lo && (object)keys[--j] != null)
                    {
                        ;
                    }
                }
                else
                {
                    do
                    {
                        ;
                    }while (obj.CompareTo(keys[++i]) > 0);
                    while (obj.CompareTo(keys[--j]) < 0)
                    {
                        ;
                    }
                }
                if (i < j)
                {
                    GenericArraySortHelper <T> .Swap(keys, i, j);
                }
                else
                {
                    break;
                }
            }
            GenericArraySortHelper <T> .Swap(keys, i, hi - 1);

            return(i);
        }
        // Token: 0x06003A30 RID: 14896 RVA: 0x000DCA48 File Offset: 0x000DAC48
        private static int PickPivotAndPartition(T[] keys, int lo, int hi)
        {
            int num = lo + (hi - lo) / 2;

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, num);

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

            GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num, hi);

            T t = keys[num];

            GenericArraySortHelper <T> .Swap(keys, num, hi - 1);

            int i = lo;
            int j = hi - 1;

            while (i < j)
            {
                if (t == null)
                {
                    while (i < hi - 1 && keys[++i] == null)
                    {
                    }
                    while (j > lo)
                    {
                        if (keys[--j] == null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    while (t.CompareTo(keys[++i]) > 0)
                    {
                    }
                    while (t.CompareTo(keys[--j]) < 0)
                    {
                    }
                }
                if (i >= j)
                {
                    break;
                }
                GenericArraySortHelper <T> .Swap(keys, i, j);
            }
            GenericArraySortHelper <T> .Swap(keys, i, hi - 1);

            return(i);
        }
예제 #6
0
 public int BinarySearch(T[] array, int index, int length, T value, IComparer <T> comparer)
 {
     try
     {
         if (comparer == null || comparer == Comparer <T> .Default)
         {
             return(GenericArraySortHelper <T> .BinarySearch(array, index, length, value));
         }
         return(ArraySortHelper <T> .InternalBinarySearch(array, index, length, value, comparer));
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), ex);
     }
 }
        // Token: 0x06003A31 RID: 14897 RVA: 0x000DCB18 File Offset: 0x000DAD18
        private static void Heapsort(T[] keys, int lo, int hi)
        {
            int num = hi - lo + 1;

            for (int i = num / 2; i >= 1; i--)
            {
                GenericArraySortHelper <T> .DownHeap(keys, i, num, lo);
            }
            for (int j = num; j > 1; j--)
            {
                GenericArraySortHelper <T> .Swap(keys, lo, lo + j - 1);

                GenericArraySortHelper <T> .DownHeap(keys, 1, j - 1, lo);
            }
        }
        // Token: 0x06003A2F RID: 14895 RVA: 0x000DC9C4 File Offset: 0x000DABC4
        private static void IntroSort(T[] keys, int lo, int hi, int depthLimit)
        {
            while (hi > lo)
            {
                int num = hi - lo + 1;
                if (num <= 16)
                {
                    if (num == 1)
                    {
                        return;
                    }
                    if (num == 2)
                    {
                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

                        return;
                    }
                    if (num == 3)
                    {
                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi - 1);

                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, hi - 1, hi);

                        return;
                    }
                    GenericArraySortHelper <T> .InsertionSort(keys, lo, hi);

                    return;
                }
                else
                {
                    if (depthLimit == 0)
                    {
                        GenericArraySortHelper <T> .Heapsort(keys, lo, hi);

                        return;
                    }
                    depthLimit--;
                    int num2 = GenericArraySortHelper <T> .PickPivotAndPartition(keys, lo, hi);

                    GenericArraySortHelper <T> .IntroSort(keys, num2 + 1, hi, depthLimit);

                    hi = num2 - 1;
                }
            }
        }
예제 #9
0
        private static void IntroSort(T[] keys, int lo, int hi, int depthLimit)
        {
            int num1;

            for (; hi > lo; hi = num1 - 1)
            {
                int num2 = hi - lo + 1;
                if (num2 <= 16)
                {
                    if (num2 == 1)
                    {
                        break;
                    }
                    if (num2 == 2)
                    {
                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

                        break;
                    }
                    if (num2 == 3)
                    {
                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi - 1);

                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, lo, hi);

                        GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, hi - 1, hi);

                        break;
                    }
                    GenericArraySortHelper <T> .InsertionSort(keys, lo, hi);

                    break;
                }
                if (depthLimit == 0)
                {
                    GenericArraySortHelper <T> .Heapsort(keys, lo, hi);

                    break;
                }
                --depthLimit;
                num1 = GenericArraySortHelper <T> .PickPivotAndPartition(keys, lo, hi);

                GenericArraySortHelper <T> .IntroSort(keys, num1 + 1, hi, depthLimit);
            }
        }
예제 #10
0
        private static void Heapsort(T[] keys, int lo, int hi)
        {
            int n = hi - lo + 1;

            for (int i = n / 2; i >= 1; --i)
            {
                GenericArraySortHelper <T> .DownHeap(keys, i, n, lo);
            }
            for (int index = n; index > 1; --index)
            {
                T[] a   = keys;
                int i   = lo;
                int num = index;
                int j   = i + num - 1;
                GenericArraySortHelper <T> .Swap(a, i, j);

                GenericArraySortHelper <T> .DownHeap(keys, 1, index - 1, lo);
            }
        }
예제 #11
0
        private static void Heapsort(TKey[] keys, TValue[] values, int lo, int hi)
        {
            int n = hi - lo + 1;

            for (int i = n / 2; i >= 1; --i)
            {
                GenericArraySortHelper <TKey, TValue> .DownHeap(keys, values, i, n, lo);
            }
            for (int index = n; index > 1; --index)
            {
                TKey[]   keys1   = keys;
                TValue[] values1 = values;
                int      i       = lo;
                int      num     = index;
                int      j       = i + num - 1;
                GenericArraySortHelper <TKey, TValue> .Swap(keys1, values1, i, j);

                GenericArraySortHelper <TKey, TValue> .DownHeap(keys, values, 1, index - 1, lo);
            }
        }
예제 #12
0
 public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer <TKey> comparer)
 {
     try
     {
         if ((comparer == null) || (comparer == Comparer <TKey> .Default))
         {
             GenericArraySortHelper <TKey, TValue> .QuickSort(keys, values, index, (index + length) - 1);
         }
         else
         {
             ArraySortHelper <TKey, TValue> .QuickSort(keys, values, index, (index + length) - 1, comparer);
         }
     }
     catch (IndexOutOfRangeException)
     {
         object[] objArray = new object[3];
         objArray[1] = typeof(TKey).Name;
         throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", objArray));
     }
     catch (Exception exception)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
     }
 }
예제 #13
0
        private static void QuickSort(TKey[] keys, TValue[] values, int left, int right)
        {
            int num;

Label_0000:
            num = left;
            int b    = right;
            int num3 = num + ((b - num) >> 1);

            GenericArraySortHelper <TKey, TValue> .SwapIfGreaterWithItems(keys, values, num, num3);

            GenericArraySortHelper <TKey, TValue> .SwapIfGreaterWithItems(keys, values, num, b);

            GenericArraySortHelper <TKey, TValue> .SwapIfGreaterWithItems(keys, values, num3, b);

            TKey local = keys[num3];

Label_002F:
            if (local != null)
            {
                while (local.CompareTo(keys[num]) > 0)
                {
                    num++;
                }
                while (local.CompareTo(keys[b]) < 0)
                {
                    b--;
                }
            }
            else
            {
                while (keys[b] != null)
                {
                    b--;
                }
            }
            if (num <= b)
            {
                if (num < b)
                {
                    TKey local2 = keys[num];
                    keys[num] = keys[b];
                    keys[b]   = local2;
                    if (values != null)
                    {
                        TValue local3 = values[num];
                        values[num] = values[b];
                        values[b]   = local3;
                    }
                }
                num++;
                b--;
                if (num <= b)
                {
                    goto Label_002F;
                }
            }
            if ((b - left) <= (right - num))
            {
                if (left < b)
                {
                    GenericArraySortHelper <TKey, TValue> .QuickSort(keys, values, left, b);
                }
                left = num;
            }
            else
            {
                if (num < right)
                {
                    GenericArraySortHelper <TKey, TValue> .QuickSort(keys, values, num, right);
                }
                right = b;
            }
            if (left >= right)
            {
                return;
            }
            goto Label_0000;
        }
예제 #14
0
        private static void DepthLimitedQuickSort(T[] keys, int left, int right, int depthLimit)
        {
            while (depthLimit != 0)
            {
                int index1 = left;
                int index2 = right;
                int index3 = index1 + (index2 - index1 >> 1);
                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index1, index3);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index1, index2);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index3, index2);

                T obj1 = keys[index3];
                do
                {
                    if ((object)obj1 == null)
                    {
                        while ((object)keys[index2] != null)
                        {
                            --index2;
                        }
                    }
                    else
                    {
                        while (obj1.CompareTo(keys[index1]) > 0)
                        {
                            ++index1;
                        }
                        while (obj1.CompareTo(keys[index2]) < 0)
                        {
                            --index2;
                        }
                    }
                    if (index1 <= index2)
                    {
                        if (index1 < index2)
                        {
                            T obj2 = keys[index1];
                            keys[index1] = keys[index2];
                            keys[index2] = obj2;
                        }
                        ++index1;
                        --index2;
                    }
                    else
                    {
                        break;
                    }
                }while (index1 <= index2);
                --depthLimit;
                if (index2 - left <= right - index1)
                {
                    if (left < index2)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, left, index2, depthLimit);
                    }
                    left = index1;
                }
                else
                {
                    if (index1 < right)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, index1, right, depthLimit);
                    }
                    right = index2;
                }
                if (left >= right)
                {
                    return;
                }
            }
            GenericArraySortHelper <T> .Heapsort(keys, left, right);
        }
        // Token: 0x06003A2D RID: 14893 RVA: 0x000DC89C File Offset: 0x000DAA9C
        private static void DepthLimitedQuickSort(T[] keys, int left, int right, int depthLimit)
        {
            while (depthLimit != 0)
            {
                int num  = left;
                int num2 = right;
                int num3 = num + (num2 - num >> 1);
                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num, num3);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num, num2);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num3, num2);

                T t = keys[num3];
                do
                {
                    if (t == null)
                    {
                        while (keys[num2] != null)
                        {
                            num2--;
                        }
                    }
                    else
                    {
                        while (t.CompareTo(keys[num]) > 0)
                        {
                            num++;
                        }
                        while (t.CompareTo(keys[num2]) < 0)
                        {
                            num2--;
                        }
                    }
                    if (num > num2)
                    {
                        break;
                    }
                    if (num < num2)
                    {
                        T t2 = keys[num];
                        keys[num]  = keys[num2];
                        keys[num2] = t2;
                    }
                    num++;
                    num2--;
                }while (num <= num2);
                depthLimit--;
                if (num2 - left <= right - num)
                {
                    if (left < num2)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, left, num2, depthLimit);
                    }
                    left = num;
                }
                else
                {
                    if (num < right)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, num, right, depthLimit);
                    }
                    right = num2;
                }
                if (left >= right)
                {
                    return;
                }
            }
            GenericArraySortHelper <T> .Heapsort(keys, left, right);
        }