giarray.c
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 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032 #include <string.h>
00033
00034 #include <cxmemory.h>
00035 #include <cxmessages.h>
00036
00037 #include "giarray.h"
00038
00039
00048 inline static void
00049 _giraffe_swap(cxdouble* a, cxdouble* b)
00050 {
00051 register cxdouble tmp = *a;
00052
00053 *a = *b;
00054 *b = tmp;
00055
00056 return;
00057
00058 }
00059
00060
00061 inline static cxint
00062 _giraffe_array_heap_sort(cxdouble* array, cxsize size)
00063 {
00064
00065 register cxsize l = size >> 1;
00066
00067 cxsize ir = size - 1;
00068
00069
00070 while (1) {
00071
00072 register cxsize i = 0;
00073 register cxsize j = 0;
00074
00075 register cxdouble rra = 0.;
00076
00077 if ( l > 0) {
00078 rra = array[--l];
00079 }
00080 else {
00081 rra = array[ir];
00082 array[ir--] = array[0];
00083 if (ir == 0) {
00084 array[0] = rra;
00085 return 0;
00086 }
00087 }
00088
00089 i = l;
00090 j = (l << 1) + 1;
00091 while (j <= ir) {
00092
00093
00094 if (j < ir && (array[j+1] - array[j]) > DBL_EPSILON) {
00095 ++j;
00096 }
00097
00098 if (array[j] - rra > DBL_EPSILON) {
00099 array[i] = array[j];
00100 j += (i = j) + 1;
00101 }
00102 else {
00103 j = ir + 1;
00104 }
00105 }
00106 array[i] = rra;
00107 }
00108
00109 }
00110
00111
00112 inline static cxdouble
00113 _giraffe_array_get_sorted(cxdouble* a, cxint n, cxint k)
00114 {
00115
00116 register cxint l = 0;
00117 register cxint m = n - 1;
00118
00119
00120 while (l < m) {
00121
00122 register cxint i = l;
00123 register cxint j = m;
00124
00125 register cxdouble x = a[k];
00126
00127
00128 do {
00129
00130 while (x - a[i] > DBL_EPSILON) {
00131 ++i;
00132 }
00133
00134 while (a [j] - x > DBL_EPSILON) {
00135 --j;
00136 }
00137
00138 if (i <= j) {
00139 _giraffe_swap(&a[i], &a[j]) ;
00140 ++i;
00141 --j;
00142 }
00143
00144 } while (i <= j);
00145
00146 if (j < k) {
00147 l = i;
00148 }
00149
00150 if (k < i) {
00151 m = j;
00152 }
00153
00154 }
00155
00156 return a[k];
00157
00158 }
00159
00160
00176 cxint
00177 giraffe_array_sort(cxdouble* array, cxsize size)
00178 {
00179
00180 return _giraffe_array_heap_sort(array, size);
00181
00182 }
00183
00184
00185 cxdouble
00186 giraffe_array_mean(const cxdouble* array, cxsize size)
00187 {
00188 register cxsize i = 0;
00189
00190 register cxdouble sum = 0.;
00191
00192
00193 for (i = 0; i < size; i++) {
00194 sum += array[i];
00195 }
00196
00197 return sum / size;
00198
00199 }
00200
00201
00202 cxdouble
00203 giraffe_array_median(const cxdouble* array, cxsize size)
00204 {
00205
00206 register cxsize position = (size & 1) == 1 ? size / 2 : size / 2 - 1;
00207
00208 cxdouble median = 0.;
00209 cxdouble* a = NULL;
00210
00211
00212 cx_assert(array != NULL);
00213
00214 a = cx_calloc(size, sizeof(cxdouble));
00215 memcpy(a, array, size * sizeof(cxdouble));
00216
00217
00218 median = _giraffe_array_get_sorted(a, size, position);
00219
00220 cx_free(a);
00221 a = NULL;
00222
00223 return median;
00224
00225 }
00226