00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 # include <config.h>
00029 #endif
00030
00031
00032
00056
00057 cpl_error_code
00058 CONCAT2X(xxx_tools_sort_stable_pattern, CPL_TYPE_NAME)(
00059 CPL_TYPE const *pix_arr,
00060 int n,
00061 int reverse,
00062 int stable,
00063 int *sort_pattern)
00064 {
00065 int i;
00066
00067
00068 cpl_ensure(pix_arr, CPL_ERROR_NULL_INPUT,
00069 CPL_ERROR_NULL_INPUT) ;
00070
00071 cpl_ensure(sort_pattern, CPL_ERROR_NULL_INPUT,
00072 CPL_ERROR_NULL_INPUT) ;
00073
00074 if (n == 0) return CPL_ERROR_NONE;
00075
00076 for (i = 0; i < n; i++) {
00077 sort_pattern[i] = i;
00078 }
00079
00080
00081
00082
00083 for (i = n / 2 - 1; i >= 0; i--) {
00084 int done = 0;
00085 int root = i;
00086 int bottom = n - 1;
00087
00088 while ((root*2 + 1 <= bottom) && (!done)) {
00089 int child = root*2 + 1;
00090
00091 if (child+1 <= bottom) {
00092 if ((!reverse && CPL_TOOLS_SORT_LT(
00093 pix_arr[sort_pattern[child]],
00094 pix_arr[sort_pattern[child + 1]]))
00095 ||
00096 (reverse && CPL_TOOLS_SORT_LT(
00097 pix_arr[sort_pattern[child + 1]],
00098 pix_arr[sort_pattern[child]]))
00099 ) {
00100 child += 1;
00101 }
00102 }
00103
00104 if ((!reverse && CPL_TOOLS_SORT_LT(
00105 pix_arr[sort_pattern[root]],
00106 pix_arr[sort_pattern[child]]))
00107 ||
00108 (reverse && CPL_TOOLS_SORT_LT(
00109 pix_arr[sort_pattern[child]],
00110 pix_arr[sort_pattern[root]]))
00111 ) {
00112 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00113 root = child;
00114 }
00115 else {
00116 done = 1;
00117 }
00118 }
00119 }
00120
00121 for (i = n - 1; i >= 1; i--) {
00122 int done = 0;
00123 int root = 0;
00124 int bottom = i - 1;
00125 CPL_INT_SWAP(sort_pattern[0], sort_pattern[i])
00126
00127 while ((root*2 + 1 <= bottom) && (!done)) {
00128 int child = root*2 + 1;
00129
00130 if (child+1 <= bottom) {
00131 if ((!reverse && CPL_TOOLS_SORT_LT(
00132 pix_arr[sort_pattern[child]],
00133 pix_arr[sort_pattern[child + 1]]))
00134 ||
00135 (reverse && CPL_TOOLS_SORT_LT(
00136 pix_arr[sort_pattern[child + 1]],
00137 pix_arr[sort_pattern[child]]))
00138 ) {
00139 child += 1;
00140 }
00141 }
00142 if ((!reverse && CPL_TOOLS_SORT_LT(
00143 pix_arr[sort_pattern[root]],
00144 pix_arr[sort_pattern[child]]))
00145 ||
00146 (reverse && CPL_TOOLS_SORT_LT(
00147 pix_arr[sort_pattern[child]],
00148 pix_arr[sort_pattern[root]]))
00149 ) {
00150 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00151 root = child;
00152 }
00153 else {
00154 done = 1;
00155 }
00156 }
00157 }
00158
00159
00160
00161
00162 if (stable) {
00163 for (i = 0; i < n; i++) {
00164 int j;
00165 j = i + 1;
00166 while(j < n &&
00167 !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[i]],
00168 pix_arr[sort_pattern[j]]) &&
00169 !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[j]],
00170 pix_arr[sort_pattern[i]])) {
00171 j++;
00172 }
00173 if (j - i > 1) {
00174 xxx_tools_sort_int(sort_pattern + i, j - i);
00175 }
00176 i = j - 1;
00177 }
00178 }
00179
00180 return CPL_ERROR_NONE ;
00181 }