# HG changeset patch # User jwe # Date 1095727457 0 # Node ID 000cfedea91ca37ab7b57498771d0f4af343d51e # Parent c2bb27ada496b190db379ffafdeab1dc46cded33 [project @ 2004-09-21 00:44:17 by jwe] diff -r c2bb27ada496 -r 000cfedea91c src/ChangeLog --- a/src/ChangeLog Fri Sep 17 14:45:39 2004 +0000 +++ b/src/ChangeLog Tue Sep 21 00:44:17 2004 +0000 @@ -1,3 +1,14 @@ +2004-09-17 David Bateman + + * DLD-FUNCTIONS/sort.cc (ascending_compare, descending_compare): + Now templates (avoids g++ 3.4.x compile problems). + Fix other uses of these functions to also treat them as templates. + (mx_sort): For unsigned EIGHT_BYTE_INT versions correct the test + for the position of NaN. Problems when mode was DESCENDING or + UNDEFINED. Use static_cast (dim) rather than + (unsigned int) dim. + (IFloatFlip): Now static. + 2004-09-17 David Bateman * DLD-FUNCTIONS/det.cc (Fdet): Only compute estimate of the diff -r c2bb27ada496 -r 000cfedea91c src/DLD-FUNCTIONS/sort.cc --- a/src/DLD-FUNCTIONS/sort.cc Fri Sep 17 14:45:39 2004 +0000 +++ b/src/DLD-FUNCTIONS/sort.cc Tue Sep 21 00:44:17 2004 +0000 @@ -50,6 +50,34 @@ }; template +bool +ascending_compare (T a, T b) +{ + return (a < b); +} + +template +bool +descending_compare (T a, T b) +{ + return (a > b); +} + +template +bool +ascending_compare (vec_index *a, vec_index *b) +{ + return (a->vec < b->vec); +} + +template +bool +descending_compare (vec_index *a, vec_index *b) +{ + return (a->vec > b->vec); +} + +template static octave_value mx_sort (ArrayN &m, int dim, sortmode mode = UNDEFINED) { @@ -214,7 +242,7 @@ return f ^ mask; } -inline unsigned EIGHT_BYTE_INT +static inline unsigned EIGHT_BYTE_INT IFloatFlip (unsigned EIGHT_BYTE_INT f) { unsigned EIGHT_BYTE_INT mask = ((f >> 63) - 1) | 0x8000000000000000ULL; @@ -222,6 +250,7 @@ return f ^ mask; } +template <> bool ascending_compare (unsigned EIGHT_BYTE_INT a, unsigned EIGHT_BYTE_INT b) @@ -229,6 +258,7 @@ return (a < b); } +template <> bool ascending_compare (vec_index *a, vec_index *b) @@ -236,6 +266,7 @@ return (a->vec < b->vec); } +template <> bool descending_compare (unsigned EIGHT_BYTE_INT a, unsigned EIGHT_BYTE_INT b) @@ -243,6 +274,7 @@ return (a > b); } +template <> bool descending_compare (vec_index *a, vec_index *b) @@ -267,7 +299,7 @@ unsigned int ns = dv(dim); unsigned int iter = dv.numel () / ns; unsigned int stride = 1; - for (unsigned int i = 0; i < (unsigned int)dim; i++) + for (unsigned int i = 0; i < static_cast (dim); i++) stride *= dv(i); double *v = m.fortran_vec (); @@ -301,20 +333,30 @@ // There are two representations of NaN. One will be // sorted to the beginning of the vector and the other - // to the end. If it will be sorted to the beginning, - // fix things up. + // to the end. If it will be sorted incorrectly, fix + // things up. - if ((lo_ieee_signbit (octave_NaN) && (mode == ASCENDING)) || - (! lo_ieee_signbit (octave_NaN) && (mode == DESCENDING))) - { - unsigned int i = 0; - double *vtmp = (double *)p; - while (xisnan (vtmp[i++]) && i < ns); - for (unsigned int l = 0; l < ns - i + 1; l++) - vtmp[l] = vtmp[l+i-1]; - for (unsigned int l = ns - i + 1; l < ns; l++) - vtmp[l] = octave_NaN; - } + if (lo_ieee_signbit (octave_NaN)) + if (mode == UNDEFINED || mode == ASCENDING) + { + unsigned int i = 0; + double *vtmp = (double *)p; + while (xisnan (vtmp[i++]) && i < ns); + for (unsigned int l = 0; l < ns - i + 1; l++) + vtmp[l] = vtmp[l+i-1]; + for (unsigned int l = ns - i + 1; l < ns; l++) + vtmp[l] = octave_NaN; + } + else + { + unsigned int i = ns; + double *vtmp = (double *)p; + while (xisnan (vtmp[--i]) && i > 0); + for (int l = i; l >= 0; l--) + vtmp[l-i+ns-1] = vtmp[l]; + for (unsigned int l = 0; l < ns - i - 1; l++) + vtmp[l] = octave_NaN; + } p += ns; } @@ -353,16 +395,25 @@ // to the end. If it will be sorted to the beginning, // fix things up. - if ((lo_ieee_signbit (octave_NaN) && (mode == ASCENDING)) || - (! lo_ieee_signbit (octave_NaN) && (mode == DESCENDING))) - { - unsigned int i = 0; - while (xisnan (v[i++*stride + offset]) && i < ns); - for (unsigned int l = 0; l < ns - i + 1; l++) - v[l*stride + offset] = v[(l+i-1)*stride + offset]; - for (unsigned int l = ns - i + 1; l < ns; l++) - v[l*stride + offset] = octave_NaN; - } + if (lo_ieee_signbit (octave_NaN)) + if (mode == UNDEFINED || mode == ASCENDING) + { + unsigned int i = 0; + while (xisnan (v[i++*stride + offset]) && i < ns); + for (unsigned int l = 0; l < ns - i + 1; l++) + v[l*stride + offset] = v[(l+i-1)*stride + offset]; + for (unsigned int l = ns - i + 1; l < ns; l++) + v[l*stride + offset] = octave_NaN; + } + else + { + unsigned int i = ns; + while (xisnan (v[--i*stride + offset]) && i > 0); + for (int l = i; l >= 0; l--) + v[(l-i+ns-1)*stride + offset] = v[l*stride + offset]; + for (unsigned int l = 0; l < ns - i - 1; l++) + v[l*stride + offset] = octave_NaN; + } } } @@ -441,25 +492,43 @@ // to the beginning of the vector and the other to the end. // If it will be sorted to the beginning, fix things up. - if ((lo_ieee_signbit (octave_NaN) && (mode == ASCENDING)) || - (! lo_ieee_signbit (octave_NaN) && (mode == DESCENDING))) - { - unsigned int i = 0; - while (xisnan (v[i++*stride+offset]) && i < ns); - OCTAVE_LOCAL_BUFFER (double, itmp, i - 1); - for (unsigned int l = 0; l < i -1; l++) - itmp[l] = idx(l*stride + offset); - for (unsigned int l = 0; l < ns - i + 1; l++) - { - v[l*stride + offset] = v[(l+i-1)*stride + offset]; - idx(l*stride + offset) = idx((l+i-1)*stride + offset); - } - for (unsigned int k = 0, l = ns - i + 1; l < ns; l++, k++) - { - v[l*stride + offset] = octave_NaN; - idx(l*stride + offset) = itmp[k]; - } - } + if (lo_ieee_signbit (octave_NaN)) + if (mode == UNDEFINED || mode == ASCENDING) + { + unsigned int i = 0; + while (xisnan (v[i++*stride+offset]) && i < ns); + OCTAVE_LOCAL_BUFFER (double, itmp, i - 1); + for (unsigned int l = 0; l < i -1; l++) + itmp[l] = idx(l*stride + offset); + for (unsigned int l = 0; l < ns - i + 1; l++) + { + v[l*stride + offset] = v[(l+i-1)*stride + offset]; + idx(l*stride + offset) = idx((l+i-1)*stride + offset); + } + for (unsigned int k = 0, l = ns - i + 1; l < ns; l++, k++) + { + v[l*stride + offset] = octave_NaN; + idx(l*stride + offset) = itmp[k]; + } + } + else + { + unsigned int i = ns; + while (xisnan (v[--i*stride+offset]) && i > 0); + OCTAVE_LOCAL_BUFFER (double, itmp, ns - i - 1); + for (unsigned int l = 0; l < ns - i -1; l++) + itmp[l] = idx((l+i+1)*stride + offset); + for (int l = i; l >= 0; l--) + { + v[(l-i+ns-1)*stride + offset] = v[l*stride + offset]; + idx((l-i+ns-1)*stride + offset) = idx(l*stride + offset); + } + for (unsigned int k = 0, l = 0; l < ns - i - 1; l++, k++) + { + v[l*stride + offset] = octave_NaN; + idx(l*stride + offset) = itmp[k]; + } + } } retval(1) = idx; @@ -470,24 +539,28 @@ #else +template <> bool ascending_compare (double a, double b) { return (xisnan (b) || (a < b)); } +template <> bool ascending_compare (vec_index *a, vec_index *b) { return (xisnan (b->vec) || (a->vec < b->vec)); } +template <> bool descending_compare (double a, double b) { return (xisnan (a) || (a > b)); } +template <> bool descending_compare (vec_index *a, vec_index *b) { @@ -514,6 +587,7 @@ return (xisinf (x.real ()) || xisinf (x.imag ())) ? octave_Inf : abs (x); } +template <> bool ascending_compare (vec_index *a, vec_index *b) { @@ -523,6 +597,7 @@ && (arg (a->vec) < arg (b->vec)))); } +template <> bool descending_compare (vec_index *a, vec_index *b) { @@ -540,35 +615,23 @@ mx_sort_indexed (ArrayN &m, int dim, sortmode mode); #endif -bool -ascending_compare (char a, char b) -{ - return (a < b); -} - -bool -ascending_compare (vec_index *a, vec_index *b) -{ - return (a->vec < b->vec); -} - -bool -descending_compare (char a, char b) -{ - return (a > b); -} - -bool -descending_compare (vec_index *a, vec_index *b) -{ - return (a->vec > b->vec); -} - template class octave_sort; template class vec_index; template class octave_sort *>; #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) +bool +ascending_compare (char a, char b); + +bool +ascending_compare (vec_index *a, vec_index *b); + +bool +descending_compare (char a, char b); + +bool +descending_compare (vec_index *a, vec_index *b); + static octave_value_list mx_sort (ArrayN &m, int dim, sortmode mode); @@ -576,12 +639,14 @@ mx_sort_indexed (ArrayN &m, int dim, sortmode mode); #endif +template <> bool ascending_compare (vec_index *a, vec_index *b) { return (a->vec.string_value () < b->vec.string_value ()); } +template <> bool descending_compare (vec_index *a, vec_index *b) {