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
00028
00052
00053 cpl_error_code
00054 CONCAT2X(xxx_tools_sort_stable_pattern, CPL_TYPE_NAME)(
00055 CPL_TYPE const *pix_arr,
00056 int n,
00057 int reverse,
00058 int stable,
00059 int *sort_pattern)
00060 {
00061 int i;
00062
00063
00064 cpl_ensure(pix_arr, CPL_ERROR_NULL_INPUT,
00065 CPL_ERROR_NULL_INPUT) ;
00066
00067 cpl_ensure(sort_pattern, CPL_ERROR_NULL_INPUT,
00068 CPL_ERROR_NULL_INPUT) ;
00069
00070 if (n == 0) return CPL_ERROR_NONE;
00071
00072 for (i = 0; i < n; i++) {
00073 sort_pattern[i] = i;
00074 }
00075
00076
00077
00078
00079 for (i = n / 2 - 1; i >= 0; i--) {
00080 int done = 0;
00081 int root = i;
00082 int bottom = n - 1;
00083
00084 while ((root*2 + 1 <= bottom) && (!done)) {
00085 int child = root*2 + 1;
00086
00087 if (child+1 <= bottom) {
00088 if ((!reverse && CPL_TOOLS_SORT_LT(
00089 pix_arr[sort_pattern[child]],
00090 pix_arr[sort_pattern[child + 1]]))
00091 ||
00092 (reverse && CPL_TOOLS_SORT_LT(
00093 pix_arr[sort_pattern[child + 1]],
00094 pix_arr[sort_pattern[child]]))
00095 ) {
00096 child += 1;
00097 }
00098 }
00099
00100 if ((!reverse && CPL_TOOLS_SORT_LT(
00101 pix_arr[sort_pattern[root]],
00102 pix_arr[sort_pattern[child]]))
00103 ||
00104 (reverse && CPL_TOOLS_SORT_LT(
00105 pix_arr[sort_pattern[child]],
00106 pix_arr[sort_pattern[root]]))
00107 ) {
00108 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00109 root = child;
00110 }
00111 else {
00112 done = 1;
00113 }
00114 }
00115 }
00116
00117 for (i = n - 1; i >= 1; i--) {
00118 int done = 0;
00119 int root = 0;
00120 int bottom = i - 1;
00121 CPL_INT_SWAP(sort_pattern[0], sort_pattern[i])
00122
00123 while ((root*2 + 1 <= bottom) && (!done)) {
00124 int child = root*2 + 1;
00125
00126 if (child+1 <= bottom) {
00127 if ((!reverse && CPL_TOOLS_SORT_LT(
00128 pix_arr[sort_pattern[child]],
00129 pix_arr[sort_pattern[child + 1]]))
00130 ||
00131 (reverse && CPL_TOOLS_SORT_LT(
00132 pix_arr[sort_pattern[child + 1]],
00133 pix_arr[sort_pattern[child]]))
00134 ) {
00135 child += 1;
00136 }
00137 }
00138 if ((!reverse && CPL_TOOLS_SORT_LT(
00139 pix_arr[sort_pattern[root]],
00140 pix_arr[sort_pattern[child]]))
00141 ||
00142 (reverse && CPL_TOOLS_SORT_LT(
00143 pix_arr[sort_pattern[child]],
00144 pix_arr[sort_pattern[root]]))
00145 ) {
00146 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00147 root = child;
00148 }
00149 else {
00150 done = 1;
00151 }
00152 }
00153 }
00154
00155
00156
00157
00158 if (stable) {
00159 for (i = 0; i < n; i++) {
00160 int j;
00161 j = i + 1;
00162 while(j < n &&
00163 !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[i]],
00164 pix_arr[sort_pattern[j]]) &&
00165 !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[j]],
00166 pix_arr[sort_pattern[i]])) {
00167 j++;
00168 }
00169 if (j - i > 1) {
00170 xxx_tools_sort_int(sort_pattern + i, j - i);
00171 }
00172 i = j - 1;
00173 }
00174 }
00175
00176 return CPL_ERROR_NONE ;
00177 }