comparison extra/NaN/src/kth_element.cpp @ 12692:13815b367946 octave-forge

use size_t instead of mwSize/mwIndex whenever possible
author schloegl
date Sat, 12 Sep 2015 14:59:20 +0000
parents 7c1bc8d8c406
children
comparison
equal deleted inserted replaced
12691:6d6285a2a633 12692:13815b367946
57 typedef int mwSize; 57 typedef int mwSize;
58 typedef int mwIndex; 58 typedef int mwIndex;
59 #endif 59 #endif
60 #endif 60 #endif
61 61
62 /*
63 math.h has isnan() defined for all sizes of floating point numbers,
64 but c++ assumes isnan(double), causing possible conversions for float and long double
65 */
66 #define ISNAN(a) (a!=a)
67
62 68
63 #define SWAP(a,b) {temp = a; a=b; b=temp;} 69 #define SWAP(a,b) {temp = a; a=b; b=temp;}
64 70
65 static void findFirstK(double *array, size_t left, size_t right, size_t k) 71 static void findFirstK(double *array, size_t left, size_t right, size_t k)
66 { 72 {
67 while (right > left) { 73 while (right > left) {
68 mwIndex pivotIndex = (left + right) / 2; 74 size_t pivotIndex = (left + right) / 2;
69 75
70 /* partition */ 76 /* partition */
71 double temp; 77 double temp;
72 double pivotValue = array[pivotIndex]; 78 double pivotValue = array[pivotIndex];
73 SWAP(array[pivotIndex], array[right]); 79 SWAP(array[pivotIndex], array[right]);
74 pivotIndex = left; 80 pivotIndex = left;
75 for (mwIndex i = left; i <= right - 1; ++i ) { 81 for (size_t i = left; i <= right - 1; ++i ) {
76 // if (array[i] <= pivotValue || isnan(pivotValue)) // needed if data contains NaN's 82 // if (array[i] <= pivotValue || isnan(pivotValue)) // needed if data contains NaN's
77 if (array[i] <= pivotValue) 83 if (array[i] <= pivotValue)
78 { 84 {
79 SWAP(array[i], array[pivotIndex]); 85 SWAP(array[i], array[pivotIndex]);
80 ++pivotIndex; 86 ++pivotIndex;
91 } 97 }
92 98
93 99
94 void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) 100 void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[])
95 { 101 {
96 mwIndex k, n; // running indices 102 size_t k, n; // running indices
97 mwSize szK, szX; 103 size_t szK, szX;
98 double *T,*X,*Y,*K; 104 double *T,*X,*Y,*K;
99 char flag = 0; // default value 105 char flag = 0; // default value
100 106
101 // check for proper number of input and output arguments 107 // check for proper number of input and output arguments
102 if ( PInputCount < 2 || PInputCount > 3 ) { 108 if ( PInputCount < 2 || PInputCount > 3 ) {
110 mexPrintf("\nsee also: median, quantile\n\n"); 116 mexPrintf("\nsee also: median, quantile\n\n");
111 mexErrMsgTxt("KTH_ELEMENT requires two or three input arguments\n"); 117 mexErrMsgTxt("KTH_ELEMENT requires two or three input arguments\n");
112 } 118 }
113 else if (PInputCount == 3) { 119 else if (PInputCount == 3) {
114 // check value of flag 120 // check value of flag
115 mwSize N = mxGetNumberOfElements(PInputs[2]); 121 size_t N = mxGetNumberOfElements(PInputs[2]);
116 if (N>1) 122 if (N>1)
117 mexErrMsgTxt("KTH_ELEMENT: flag argument must be scalar\n"); 123 mexErrMsgTxt("KTH_ELEMENT: flag argument must be scalar\n");
118 else if (N==1) { 124 else if (N==1) {
119 switch (mxGetClassID(PInputs[2])) { 125 switch (mxGetClassID(PInputs[2])) {
120 case mxLOGICAL_CLASS: 126 case mxLOGICAL_CLASS:
178 if (flag==1) 184 if (flag==1)
179 memcpy(T,X,szX*sizeof(double)); 185 memcpy(T,X,szX*sizeof(double));
180 else { 186 else {
181 /* do not copy NaN's */ 187 /* do not copy NaN's */
182 for (k=0,n=0; k < szX; k++) { 188 for (k=0,n=0; k < szX; k++) {
183 if (!isnan(X[k])) T[n++]=X[k]; 189 if (!ISNAN(X[k])) T[n++]=X[k];
184 } 190 }
185 szX = n; 191 szX = n;
186 } 192 }
187 } 193 }
188 194