src/share/vm/utilities/quickSort.hpp

Tue, 19 Aug 2014 08:34:25 -0400

author
zgu
date
Tue, 19 Aug 2014 08:34:25 -0400
changeset 7078
c6211b707068
parent 3335
3c648b9ad052
child 6876
710a3c8b516e
permissions
-rw-r--r--

8055007: NMT2: emptyStack missing in minimal build
Summary: Refactored emptyStack to a static member of NativeCallStack, which is accessible in minimal build.
Reviewed-by: coleenp, dholmes

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #ifndef SHARE_VM_UTILITIES_QUICKSORT_HPP
    26 #define SHARE_VM_UTILITIES_QUICKSORT_HPP
    28 #include "memory/allocation.hpp"
    29 #include "runtime/globals.hpp"
    30 #include "utilities/debug.hpp"
    32 class QuickSort : AllStatic {
    34  private:
    35   template<class T>
    36   static void swap(T* array, int x, int y) {
    37     T tmp = array[x];
    38     array[x] = array[y];
    39     array[y] = tmp;
    40   }
    42   // As pivot we use the median of the first, last and middle elements.
    43   // We swap in these three values at the right place in the array. This
    44   // means that this method not only returns the index of the pivot
    45   // element. It also alters the array so that:
    46   //     array[first] <= array[middle] <= array[last]
    47   // A side effect of this is that arrays of length <= 3 are sorted.
    48   template<class T, class C>
    49   static int find_pivot(T* array, int length, C comparator) {
    50     assert(length > 1, "length of array must be > 0");
    52     int middle_index = length / 2;
    53     int last_index = length - 1;
    55     if (comparator(array[0], array[middle_index]) == 1) {
    56       swap(array, 0, middle_index);
    57     }
    58     if (comparator(array[0], array[last_index]) == 1) {
    59       swap(array, 0, last_index);
    60     }
    61     if (comparator(array[middle_index], array[last_index]) == 1) {
    62       swap(array, middle_index, last_index);
    63     }
    64     // Now the value in the middle of the array is the median
    65     // of the fist, last and middle values. Use this as pivot.
    66     return middle_index;
    67   }
    69   template<class T, class C, bool idempotent>
    70   static int partition(T* array, int pivot, int length, C comparator) {
    71     int left_index = -1;
    72     int right_index = length;
    73     T pivot_val = array[pivot];
    75     while (true) {
    76       do {
    77         left_index++;
    78       } while (comparator(array[left_index], pivot_val) == -1);
    79       do {
    80         right_index--;
    81       } while (comparator(array[right_index], pivot_val) == 1);
    83       if (left_index < right_index) {
    84         if (!idempotent || comparator(array[left_index], array[right_index]) != 0) {
    85           swap(array, left_index, right_index);
    86         }
    87       } else {
    88         return right_index;
    89       }
    90     }
    92     ShouldNotReachHere();
    93     return 0;
    94   }
    96   template<class T, class C, bool idempotent>
    97   static void inner_sort(T* array, int length, C comparator) {
    98     if (length < 2) {
    99       return;
   100     }
   101     int pivot = find_pivot(array, length, comparator);
   102     if (length < 4) {
   103       // arrays up to length 3 will be sorted after finding the pivot
   104       return;
   105     }
   106     int split = partition<T, C, idempotent>(array, pivot, length, comparator);
   107     int first_part_length = split + 1;
   108     inner_sort<T, C, idempotent>(array, first_part_length, comparator);
   109     inner_sort<T, C, idempotent>(&array[first_part_length], length - first_part_length, comparator);
   110   }
   112  public:
   113   // The idempotent parameter prevents the sort from
   114   // reordering a previous valid sort by not swapping
   115   // fields that compare as equal. This requires extra
   116   // calls to the comparator, so the performance
   117   // impact depends on the comparator.
   118   template<class T, class C>
   119   static void sort(T* array, int length, C comparator, bool idempotent) {
   120     // Switch "idempotent" from function paramter to template parameter
   121     if (idempotent) {
   122       inner_sort<T, C, true>(array, length, comparator);
   123     } else {
   124       inner_sort<T, C, false>(array, length, comparator);
   125     }
   126   }
   128   // for unit testing
   129 #ifndef PRODUCT
   130   static void print_array(const char* prefix, int* array, int length);
   131   static bool compare_arrays(int* actual, int* expected, int length);
   132   template <class C> static bool sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent = false);
   133   static void test_quick_sort();
   134 #endif
   135 };
   138 #endif //SHARE_VM_UTILITIES_QUICKSORT_HPP

mercurial