Mercurial > hg > octave-avbm
changeset 8752:06b9903a029b
fix & clean up complex & sparse sorting
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 16 Feb 2009 10:15:43 +0100 |
parents | 9f7ce4bf7650 |
children | c141078e083a |
files | liboctave/Array-C.cc liboctave/Array-fC.cc liboctave/ChangeLog liboctave/Sparse-C.cc liboctave/Sparse-b.cc liboctave/Sparse-d.cc liboctave/Sparse.cc liboctave/sparse-sort.cc |
diffstat | 8 files changed, 36 insertions(+), 134 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array-C.cc +++ b/liboctave/Array-C.cc @@ -41,22 +41,6 @@ return xisnan (x); } -template <> -bool -octave_sort<Complex>::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)))); -} - -template <> -bool -octave_sort<Complex>::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 (const Complex& x, const Complex& y) {
--- a/liboctave/Array-fC.cc +++ b/liboctave/Array-fC.cc @@ -41,24 +41,6 @@ return xisnan (x); } -template <> -bool -octave_sort<FloatComplex>::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)))); -} - -template <> -bool -octave_sort<FloatComplex>::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 (const FloatComplex& x, const FloatComplex& y) {
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,12 @@ +2009-02-16 Jaroslav Hajek <highegg@gmail.com> + + * Array-C.cc, Array-fC.cc: Don't redefine complex comparison. + * Sparse.cc (Sparse::sort): Don't use vec_index. + * Sparse-C.cc, Sparse-d.cc, Sparse-b.cc: Don't reinstantiate + octave_sort, reflect changes. + * sparse-sort.cc: Explicitly instantiate octave_sort for requested + pointer types. + 2009-02-16 Jaroslav Hajek <highegg@gmail.com> * oct-cmplx.h (operator <, operator >): New operators.
--- a/liboctave/Sparse-C.cc +++ b/liboctave/Sparse-C.cc @@ -40,27 +40,10 @@ return (xisinf (x.real ()) || xisinf (x.imag ())) ? octave_Inf : abs (x); } -static bool -operator < (const Complex& a, const Complex& b) -{ - return (xisnan (b) || (xabs (a) < xabs (b)) - || ((xabs (a) == xabs (b)) && (arg (a) < arg (b)))); -} - -static bool -operator > (const Complex& a, const Complex& b) -{ - return (xisnan (a) || (xabs (a) > xabs (b)) - || ((xabs (a) == xabs (b)) && (arg (a) > arg (b)))); -} - -// This file must be included after the < and > operators are -// defined to avoid errors with the Intel C++ compiler. -#include "oct-sort.cc" template <> bool -sparse_ascending_compare (Complex a, Complex b) +sparse_ascending_compare<Complex> (const Complex& a, const Complex& b) { return (xisnan (b) || (xabs (a) < xabs (b)) || ((xabs (a) == xabs (b)) && (arg (a) < arg (b)))); @@ -68,32 +51,12 @@ template <> bool -sparse_ascending_compare (vec_index<Complex> *a, vec_index<Complex> *b) -{ - return (xisnan (b->vec) - || (xabs (a->vec) < xabs (b->vec)) - || ((xabs (a->vec) == xabs (b->vec)) - && (arg (a->vec) < arg (b->vec)))); -} - -template <> -bool -sparse_descending_compare (Complex a, Complex b) +sparse_descending_compare<Complex> (const Complex& a, const Complex& b) { return (xisnan (a) || (xabs (a) > xabs (b)) || ((xabs (a) == xabs (b)) && (arg (a) > arg (b)))); } -template <> -bool -sparse_descending_compare (vec_index<Complex> *a, vec_index<Complex> *b) -{ - return (xisnan (a->vec) - || (xabs (a->vec) > xabs (b->vec)) - || ((xabs (a->vec) == xabs (b->vec)) - && (arg (a->vec) > arg (b->vec)))); -} - INSTANTIATE_SPARSE_AND_ASSIGN (Complex, OCTAVE_API); INSTANTIATE_SPARSE_ASSIGN (Complex, double, OCTAVE_API);
--- a/liboctave/Sparse-b.cc +++ b/liboctave/Sparse-b.cc @@ -29,7 +29,6 @@ #include "Sparse.h" #include "Sparse.cc" -#include "oct-sort.cc" INSTANTIATE_SPARSE_AND_ASSIGN (bool, OCTAVE_API);
--- a/liboctave/Sparse-d.cc +++ b/liboctave/Sparse-d.cc @@ -30,36 +30,21 @@ #include "lo-mappers.h" #include "Sparse.h" #include "Sparse.cc" -#include "oct-sort.cc" template <> bool -sparse_ascending_compare (double a, double b) +sparse_ascending_compare<double> (double a, double b) { return (xisnan (b) || (a < b)); } template <> bool -sparse_ascending_compare (vec_index<double> *a, vec_index<double> *b) -{ - return (xisnan (b->vec) || (a->vec < b->vec)); -} - -template <> -bool -sparse_descending_compare (double a, double b) +sparse_descending_compare<double> (double a, double b) { return (xisnan (a) || (a > b)); } -template <> -bool -sparse_descending_compare (vec_index<double> *a, vec_index<double> *b) -{ - return (xisnan (b->vec) || (a->vec > b->vec)); -} - INSTANTIATE_SPARSE_AND_ASSIGN (double, OCTAVE_API); #if 0
--- a/liboctave/Sparse.cc +++ b/liboctave/Sparse.cc @@ -2074,33 +2074,21 @@ // instantiations for Array<double and Sparse<double>, etc template <class T> bool -sparse_ascending_compare (T a, T b) +sparse_ascending_compare (typename ref_param<T>::type a, + typename ref_param<T>::type b) { return (a < b); } template <class T> bool -sparse_descending_compare (T a, T b) +sparse_descending_compare (typename ref_param<T>::type a, + typename ref_param<T>::type b) { return (a > b); } template <class T> -bool -sparse_ascending_compare (vec_index<T> *a, vec_index<T> *b) -{ - return (a->vec < b->vec); -} - -template <class T> -bool -sparse_descending_compare (vec_index<T> *a, vec_index<T> *b) -{ - return (a->vec > b->vec); -} - -template <class T> Sparse<T> Sparse<T>::sort (octave_idx_type dim, sortmode mode) const { @@ -2122,9 +2110,9 @@ octave_sort<T> lsort; if (mode == ASCENDING) - lsort.set_compare (sparse_ascending_compare); + lsort.set_compare (sparse_ascending_compare<T>); else if (mode == DESCENDING) - lsort.set_compare (sparse_descending_compare); + lsort.set_compare (sparse_descending_compare<T>); else abort (); @@ -2141,13 +2129,13 @@ if (mode == ASCENDING) { for (i = 0; i < ns; i++) - if (sparse_ascending_compare (static_cast<T> (0), v [i])) + if (sparse_ascending_compare<T> (static_cast<T> (0), v [i])) break; } else { for (i = 0; i < ns; i++) - if (sparse_descending_compare (static_cast<T> (0), v [i])) + if (sparse_descending_compare<T> (static_cast<T> (0), v [i])) break; } for (octave_idx_type k = 0; k < i; k++) @@ -2188,12 +2176,12 @@ nc = m.columns (); } - octave_sort<vec_index<T> *> indexed_sort; + octave_sort<T> indexed_sort; if (mode == ASCENDING) - indexed_sort.set_compare (sparse_ascending_compare); + indexed_sort.set_compare (sparse_ascending_compare<T>); else if (mode == DESCENDING) - indexed_sort.set_compare (sparse_descending_compare); + indexed_sort.set_compare (sparse_descending_compare<T>); else abort (); @@ -2201,13 +2189,8 @@ octave_idx_type *mcidx = m.cidx (); octave_idx_type *mridx = m.ridx (); - OCTAVE_LOCAL_BUFFER (vec_index<T> *, vi, nr); - OCTAVE_LOCAL_BUFFER (vec_index<T>, vix, nr); - - for (octave_idx_type i = 0; i < nr; i++) - vi[i] = &vix[i]; - sidx = Array<octave_idx_type> (dim_vector (nr, nc)); + OCTAVE_LOCAL_BUFFER (octave_idx_type, vi, nr); for (octave_idx_type j = 0; j < nc; j++) { @@ -2222,26 +2205,21 @@ else { for (octave_idx_type i = 0; i < ns; i++) - { - vi[i]->vec = v[i]; - vi[i]->indx = mridx[i]; - } - - indexed_sort.sort (vi, ns); + vi[i] = mridx[i]; + + indexed_sort.sort (v, vi, ns); octave_idx_type i; if (mode == ASCENDING) { for (i = 0; i < ns; i++) - if (sparse_ascending_compare (static_cast<T> (0), - vi [i] -> vec)) + if (sparse_ascending_compare<T> (static_cast<T> (0), v[i])) break; } else { for (i = 0; i < ns; i++) - if (sparse_descending_compare (static_cast<T> (0), - vi [i] -> vec)) + if (sparse_descending_compare<T> (static_cast<T> (0), v[i])) break; } @@ -2257,15 +2235,13 @@ for (octave_idx_type k = 0; k < i; k++) { - v [k] = vi [k] -> vec; - sidx (k + offset) = vi [k] -> indx; + sidx (k + offset) = vi [k]; mridx [k] = k; } for (octave_idx_type k = i; k < ns; k++) { - v [k] = vi [k] -> vec; - sidx (k - ns + nr + offset) = vi [k] -> indx; + sidx (k - ns + nr + offset) = vi [k]; mridx [k] = k - ns + nr; }
--- a/liboctave/sparse-sort.cc +++ b/liboctave/sparse-sort.cc @@ -49,6 +49,8 @@ return (i->r < j->r); } +template class octave_sort<octave_sparse_sort_idxl *>; + // Need to know the original order of the sorted indexes in // sparse assignments, and this class does that bool @@ -58,6 +60,8 @@ return (i->i < j->i); } +template class octave_sort<octave_idx_vector_sort *>; + /* ;;; Local Variables: *** ;;; mode: C++ ***