# HG changeset patch # User John W. Eaton # Date 1234424954 18000 # Node ID d5af326a3ede175a37518ba14a8e8687bdcd3a5b # Parent a50228129dba4abe6e594a9831aebc542ffcb9ef [mq]: sort-traits diff -r a50228129dba -r d5af326a3ede liboctave/Array-C.cc --- a/liboctave/Array-C.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array-C.cc Thu Feb 12 02:49:14 2009 -0500 @@ -35,14 +35,15 @@ #include "oct-sort.cc" template <> -inline bool _sort_isnan (Complex x) +inline bool +sort_isnan (const Complex& x) { return xisnan (x); } template <> bool -octave_sort::ascending_compare (Complex a, Complex b) +octave_sort::ascending_compare (const Complex& a, const Complex& b) { return ((std::abs (a) < std::abs (b)) || ((std::abs (a) == std::abs (b)) && (arg (a) < arg (b)))); @@ -50,29 +51,34 @@ template <> bool -octave_sort::descending_compare (Complex a, Complex b) +octave_sort::descending_compare (const Complex& a, const Complex& b) { return ((std::abs (a) > std::abs (b)) || ((std::abs (a) == std::abs (b)) && (arg (a) > arg (b)))); } -static bool nan_ascending_compare (Complex x, Complex y) +static bool +nan_ascending_compare (const Complex& x, const Complex& y) { - return xisnan (y) ? ! xisnan (x) : ((std::abs (x) < std::abs (x)) - || ((std::abs (x) == std::abs (x)) && (arg (x) < arg (x)))); + return (xisnan (y) + ? ! xisnan (x) + : ((std::abs (x) < std::abs (x)) + || ((std::abs (x) == std::abs (x)) && (arg (x) < arg (x))))); } -static bool nan_descending_compare (Complex x, Complex y) +static bool +nan_descending_compare (const Complex& x, const Complex& y) { - return xisnan (x) ? ! xisnan (y) : ((std::abs (x) > std::abs (x)) - || ((std::abs (x) == std::abs (x)) && (arg (x) > arg (x)))); + return (xisnan (x) + ? ! xisnan (y) + : ((std::abs (x) > std::abs (x)) + || ((std::abs (x) == std::abs (x)) && (arg (x) > arg (x))))); } -bool (*_sortrows_comparator (sortmode mode, - const Array& a , bool allow_chk)) -(Complex, Complex) +Array::compare_fcn_type +sortrows_comparator (sortmode mode, const Array& a , bool allow_chk) { - bool (*result) (Complex, Complex) = 0; + Array::compare_fcn_type result = 0; if (allow_chk) { diff -r a50228129dba -r d5af326a3ede liboctave/Array-d.cc --- a/liboctave/Array-d.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array-d.cc Thu Feb 12 02:49:14 2009 -0500 @@ -37,26 +37,28 @@ #include "oct-sort.cc" template <> -inline bool _sort_isnan (double x) +inline bool +sort_isnan (double x) { return lo_ieee_isnan (x); } -static bool nan_ascending_compare (double x, double y) +static bool +nan_ascending_compare (double x, double y) { return lo_ieee_isnan (y) ? ! lo_ieee_isnan (x) : x < y; } -static bool nan_descending_compare (double x, double y) +static bool +nan_descending_compare (double x, double y) { return lo_ieee_isnan (x) ? ! lo_ieee_isnan (y) : x > y; } -bool (*_sortrows_comparator (sortmode mode, - const Array& a , bool allow_chk)) -(double, double) +Array::compare_fcn_type +sortrows_comparator (sortmode mode, const Array& a , bool allow_chk) { - bool (*result) (double, double) = 0; + Array::compare_fcn_type result = 0; if (allow_chk) { diff -r a50228129dba -r d5af326a3ede liboctave/Array-f.cc --- a/liboctave/Array-f.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array-f.cc Thu Feb 12 02:49:14 2009 -0500 @@ -37,26 +37,28 @@ #include "oct-sort.cc" template <> -inline bool _sort_isnan (float x) +inline bool +sort_isnan (float x) { return lo_ieee_isnan (x); } -static bool nan_ascending_compare (float x, float y) +static bool +nan_ascending_compare (float x, float y) { return lo_ieee_isnan (y) ? ! lo_ieee_isnan (x) : x < y; } -static bool nan_descending_compare (float x, float y) +static bool +nan_descending_compare (float x, float y) { return lo_ieee_isnan (x) ? ! lo_ieee_isnan (y) : x > y; } -bool (*_sortrows_comparator (sortmode mode, - const Array& a , bool allow_chk)) -(float, float) +Array::compare_fcn_type +sortrows_comparator (sortmode mode, const Array& a , bool allow_chk) { - bool (*result) (float, float) = 0; + Array::compare_fcn_type result = 0; if (allow_chk) { diff -r a50228129dba -r d5af326a3ede liboctave/Array-fC.cc --- a/liboctave/Array-fC.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array-fC.cc Thu Feb 12 02:49:14 2009 -0500 @@ -35,14 +35,16 @@ #include "oct-sort.cc" template <> -inline bool _sort_isnan (FloatComplex x) +inline bool +sort_isnan (const FloatComplex& x) { return xisnan (x); } template <> bool -octave_sort::ascending_compare (FloatComplex a, FloatComplex b) +octave_sort::ascending_compare (const FloatComplex& a, + const FloatComplex& b) { return ((std::abs (a) < std::abs (b)) || ((std::abs (a) == std::abs (b)) && (arg (a) < arg (b)))); @@ -50,29 +52,36 @@ template <> bool -octave_sort::descending_compare (FloatComplex a, FloatComplex b) +octave_sort::descending_compare (const FloatComplex& a, + const FloatComplex& b) { return ((std::abs (a) > std::abs (b)) || ((std::abs (a) == std::abs (b)) && (arg (a) > arg (b)))); } -static bool nan_ascending_compare (FloatComplex x, FloatComplex y) +static bool +nan_ascending_compare (const FloatComplex& x, const FloatComplex& y) { - return xisnan (y) ? ! xisnan (x) : ((std::abs (x) < std::abs (x)) - || ((std::abs (x) == std::abs (x)) && (arg (x) < arg (x)))); + return (xisnan (y) + ? ! xisnan (x) + : ((std::abs (x) < std::abs (x)) + || ((std::abs (x) == std::abs (x)) && (arg (x) < arg (x))))); } -static bool nan_descending_compare (FloatComplex x, FloatComplex y) +static bool +nan_descending_compare (const FloatComplex& x, const FloatComplex& y) { - return xisnan (x) ? ! xisnan (y) : ((std::abs (x) > std::abs (x)) - || ((std::abs (x) == std::abs (x)) && (arg (x) > arg (x)))); + return (xisnan (x) + ? ! xisnan (y) + : ((std::abs (x) > std::abs (x)) + || ((std::abs (x) == std::abs (x)) && (arg (x) > arg (x))))); } -bool (*_sortrows_comparator (sortmode mode, - const Array& a , bool allow_chk)) -(FloatComplex, FloatComplex) +Array::compare_fcn_type +sortrows_comparator (sortmode mode, const Array& a, + bool allow_chk) { - bool (*result) (FloatComplex, FloatComplex) = 0; + Array::compare_fcn_type result = 0; if (allow_chk) { diff -r a50228129dba -r d5af326a3ede liboctave/Array.cc --- a/liboctave/Array.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array.cc Thu Feb 12 02:49:14 2009 -0500 @@ -1977,8 +1977,8 @@ // Non-real types don't have NaNs. template -inline -bool _sort_isnan (T) +inline bool +sort_isnan (typename ref_param::type) { return false; } @@ -2028,7 +2028,7 @@ for (octave_idx_type i = 0; i < ns; i++) { T tmp = ov[i]; - if (_sort_isnan (tmp)) + if (sort_isnan (tmp)) v[--ku] = tmp; else v[kl++] = tmp; @@ -2072,7 +2072,7 @@ for (octave_idx_type i = 0; i < ns; i++) { T tmp = ov[i*stride + offset]; - if (_sort_isnan (tmp)) + if (sort_isnan (tmp)) buf[--ku] = tmp; else buf[kl++] = tmp; @@ -2150,7 +2150,7 @@ for (octave_idx_type i = 0; i < ns; i++) { T tmp = ov[i]; - if (_sort_isnan (tmp)) + if (sort_isnan (tmp)) { --ku; v[ku] = tmp; @@ -2208,7 +2208,7 @@ for (octave_idx_type i = 0; i < ns; i++) { T tmp = ov[i*stride + offset]; - if (_sort_isnan (tmp)) + if (sort_isnan (tmp)) { --ku; buf[ku] = tmp; @@ -2258,19 +2258,19 @@ const T *lo = data (), *hi = data () + nelem () - 1; // Check for NaNs at the beginning and end. - if (mode != ASCENDING && _sort_isnan (*lo)) + if (mode != ASCENDING && sort_isnan (*lo)) { mode = DESCENDING; do ++lo; - while (lo < hi && _sort_isnan (*lo)); + while (lo < hi && sort_isnan (*lo)); } - else if (mode != DESCENDING && _sort_isnan (*hi)) + else if (mode != DESCENDING && sort_isnan (*hi)) { mode = ASCENDING; do --hi; - while (lo < hi && _sort_isnan (*hi)); + while (lo < hi && sort_isnan (*hi)); } octave_sort lsort; @@ -2295,8 +2295,9 @@ } template -bool (*_sortrows_comparator (sortmode mode, - const Array& /* a */, bool /* allow_chk */)) (T, T) +typename Array::compare_fcn_type +sortrows_comparator (sortmode mode, const Array& /* a */, + bool /* allow_chk */) { if (mode == ASCENDING) return octave_sort::ascending_compare; @@ -2314,7 +2315,7 @@ octave_sort lsort; - lsort.set_compare (_sortrows_comparator (mode, *this, true)); + lsort.set_compare (sortrows_comparator (mode, *this, true)); octave_idx_type r = rows (), c = cols (); @@ -2340,7 +2341,8 @@ if (! mode) { // Auto-detect mode. - bool (*compare) (T, T) = _sortrows_comparator (ASCENDING, *this, false); + compare_fcn_type compare + = sortrows_comparator (ASCENDING, *this, false); octave_idx_type i; for (i = 0; i < cols (); i++) @@ -2373,7 +2375,7 @@ if (mode) { - lsort.set_compare (_sortrows_comparator (mode, *this, false)); + lsort.set_compare (sortrows_comparator (mode, *this, false)); if (! lsort.is_sorted_rows (data (), r, c)) mode = UNSORTED; @@ -2398,9 +2400,8 @@ Array::is_sorted (sortmode) const \ { return UNSORTED; } \ \ -template <> \ -bool (*_sortrows_comparator (sortmode, \ - const Array&, bool)) (T, T) \ +Array::compare_fcn_type \ +sortrows_comparator (sortmode, const Array&, bool) \ { return 0; } \ \ template <> Array \ diff -r a50228129dba -r d5af326a3ede liboctave/Array.h --- a/liboctave/Array.h Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Array.h Thu Feb 12 02:49:14 2009 -0500 @@ -34,6 +34,7 @@ #include "dim-vector.h" #include "idx-vector.h" +#include "lo-traits.h" #include "lo-utils.h" #include "oct-sort.h" #include "quit.h" @@ -101,6 +102,9 @@ typedef T element_type; + typedef bool (*compare_fcn_type) (typename ref_param::type, + typename ref_param::type); + protected: typename Array::ArrayRep *rep; diff -r a50228129dba -r d5af326a3ede liboctave/ChangeLog --- a/liboctave/ChangeLog Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/ChangeLog Thu Feb 12 02:49:14 2009 -0500 @@ -1,3 +1,32 @@ +2009-02-12 John W. Eaton + + * lo-traits.h: New file. + * Makefile.in (INCLUDES): Add it to the list. + + * Array.h (compare_fcn_type): New typedef. + * oct-sort.h (compare_fcn_type): Ditto. + + * oct-sort.h, oct-sort.cc (octave_sort::octave_sort, + octave_sort::set_compare, octave_sort::compare): + Use typedef to simplify decl. + (octave_sort::ascending_compare, + octave_sort::descending_compare): + Use ref_param::type for parameter decl. + + * Array.cc (sort_isnan): Use ref_param::type for parameter decl. + (Array::sort): Use explicit template parameter for sort_isnan calls. + + * Array.cc, Array-C.cc, Array-fC.cc, Array-d.cc, Array-f.cc + (sortrows_comparator): Rename from _sortrows_comparator. Change + all uses. Use typedef for return value to simplify decl. + (sort_isnan): Rename from _sort_isnan. Change all uses. + (NO_INSTANTIATE_ARRAY_SORT): Use typedef to simplify instantiation + of sortrows_comparator. + + * Array-C.cc, Array-fC.cc (sort_isnan, ascending_compare, + descending_compare, nan_ascending_compare, + nan_descending_compare): + 2009-02-11 Jaroslav Hajek * oct-sort.cc (octave_sort::is_sorted, octave_sort::sort_rows, diff -r a50228129dba -r d5af326a3ede liboctave/Makefile.in --- a/liboctave/Makefile.in Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/Makefile.in Thu Feb 12 02:49:14 2009 -0500 @@ -85,8 +85,8 @@ base-de.h base-min.h byte-swap.h cmd-edit.h cmd-hist.h \ data-conv.h dir-ops.h file-ops.h file-stat.h functor.h getopt.h \ glob-match.h idx-vector.h kpse-xfns.h \ - lo-ieee.h lo-mappers.h lo-math.h lo-specfun.h \ - lo-sysdep.h lo-utils.h mach-info.h md5.h oct-alloc.h oct-cmplx.h \ + lo-ieee.h lo-mappers.h lo-math.h lo-specfun.h lo-sysdep.h \ + lo-traits.h lo-utils.h mach-info.h md5.h oct-alloc.h oct-cmplx.h \ oct-env.h oct-fftw.h oct-getopt.h oct-group.h oct-inttypes.h \ oct-locbuf.h oct-lookup.h oct-md5.h oct-mutex.h oct-norm.h \ oct-passwd.h oct-rand.h oct-rl-edit.h oct-rl-hist.h oct-shlib.h \ diff -r a50228129dba -r d5af326a3ede liboctave/lo-traits.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/lo-traits.h Thu Feb 12 02:49:14 2009 -0500 @@ -0,0 +1,80 @@ +/* + +Copyright (C) 2009 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#if !defined (octave_liboctave_traits_h) +#define octave_liboctave_traits_h 1 + +template +class if_then_else; + +template +class if_then_else +{ +public: + + typedef T1 result; +}; + +template +class if_then_else +{ +public: + + typedef T2 result; +}; + +template +class is_class_type +{ +private: + + typedef char one; + typedef struct { char c[2]; } two; + + // Classes can have pointers to members. + template static one is_class_type_test (int T2::*); + + // Catch everything else. + template static two is_class_type_test (...); + +public: + + enum { yes = sizeof (is_class_type_test (0)) == 1 }; + enum { no = ! yes }; +}; + +template +class ref_param +{ +public: + + typedef typename if_then_else::no, T, T const&>::result type; +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C *** +;;; page-delimiter: "^/\\*" *** +;;; End: *** +*/ diff -r a50228129dba -r d5af326a3ede liboctave/oct-sort.cc --- a/liboctave/oct-sort.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/oct-sort.cc Thu Feb 12 02:49:14 2009 -0500 @@ -111,7 +111,7 @@ } template -octave_sort::octave_sort (bool (*comp) (T, T)) : compare (comp) +octave_sort::octave_sort (compare_fcn_type comp) : compare (comp) { merge_init (); } @@ -1763,14 +1763,16 @@ template bool -octave_sort::ascending_compare (T x, T y) +octave_sort::ascending_compare (typename ref_param::type x, + typename ref_param::type y) { return x < y; } template bool -octave_sort::descending_compare (T x, T y) +octave_sort::descending_compare (typename ref_param::type x, + typename ref_param::type y) { return x > y; } diff -r a50228129dba -r d5af326a3ede liboctave/oct-sort.h --- a/liboctave/oct-sort.h Wed Feb 11 19:46:23 2009 -0500 +++ b/liboctave/oct-sort.h Thu Feb 12 02:49:14 2009 -0500 @@ -82,6 +82,8 @@ #if !defined (octave_sort_h) #define octave_sort_h 1 +#include "lo-traits.h" + // The maximum number of entries in a MergeState's pending-runs stack. // This is enough to sort arrays of size up to about // 32 * phi ** MAX_MERGE_PENDING @@ -105,13 +107,16 @@ { public: + typedef bool (*compare_fcn_type) (typename ref_param::type, + typename ref_param::type); + octave_sort (void); - octave_sort (bool (*comp) (T, T)); + octave_sort (compare_fcn_type); ~octave_sort (void) { merge_freemem (); } - void set_compare (bool (*comp) (T, T)) { compare = comp; } + void set_compare (compare_fcn_type comp) { compare = comp; } void set_compare (sortmode mode); @@ -133,8 +138,11 @@ bool is_sorted_rows (const T *data, octave_idx_type rows, octave_idx_type cols); - static bool ascending_compare (T, T); - static bool descending_compare (T, T); + static bool ascending_compare (typename ref_param::type, + typename ref_param::type); + + static bool descending_compare (typename ref_param::type, + typename ref_param::type); private: @@ -177,7 +185,7 @@ struct s_slice pending[MAX_MERGE_PENDING]; }; - bool (*compare) (T, T); + compare_fcn_type compare; MergeState ms; diff -r a50228129dba -r d5af326a3ede src/ChangeLog --- a/src/ChangeLog Wed Feb 11 19:46:23 2009 -0500 +++ b/src/ChangeLog Thu Feb 12 02:49:14 2009 -0500 @@ -1,3 +1,12 @@ +2009-02-12 John W. Eaton + + * TEMPLATE-INST/Array-tc.cc + (octave_sort::ascending_compare, + octave_sort::descending_compare): + Delete unused template specializations. + Use NO_INSTANTIATE_ARRAY_SORT instead of INSTANTIATE_ARRAY_SORT + for octave_values. + 2009-02-11 Thomas Treichl * gl-render.h: Use HAVE_FRAMEWORK_OPENGL. diff -r a50228129dba -r d5af326a3ede src/TEMPLATE-INST/Array-tc.cc --- a/src/TEMPLATE-INST/Array-tc.cc Wed Feb 11 19:46:23 2009 -0500 +++ b/src/TEMPLATE-INST/Array-tc.cc Thu Feb 12 02:49:14 2009 -0500 @@ -42,21 +42,7 @@ #include "oct-sort.cc" -template <> -bool -octave_sort::ascending_compare (octave_value a, octave_value b) -{ - return (a.string_value () < b.string_value ()); -} - -template <> -bool -octave_sort::descending_compare (octave_value a, octave_value b) -{ - return (a.string_value () > b.string_value ()); -} - -INSTANTIATE_ARRAY_SORT (octave_value); +NO_INSTANTIATE_ARRAY_SORT (octave_value); INSTANTIATE_ARRAY (octave_value, OCTINTERP_API);