Mercurial > hg > octave-lyh
changeset 7458:1032e24f199f
make sort work for ranges
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 07 Feb 2008 22:36:11 -0500 |
parents | aab45fbcd71f |
children | d3fe4d466bc2 |
files | liboctave/ChangeLog liboctave/Range.cc liboctave/Range.h src/ChangeLog src/ov-range.h |
diffstat | 5 files changed, 132 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,5 +1,13 @@ 2008-02-07 John W. Eaton <jwe@octave.org> + * Range.cc (Range::sort_internal): Rename from sort. New arg, + ASCENDING, with default value of true. + (Range::sort_internal (Array<octave_idx_type>&, bool)): New function. + (Range::sort (octave_idx_type, sortmode) const): New function. + (Range::sort (Array<octave_idx_type>&, octave_idx_type, sortmode) + const): New function. + * Range.h: Fix/provide decls. + * intNDArray.cc (intNDArray<T>::any (int)): Use != for comparison. 2008-02-06 John W. Eaton <jwe@octave.org>
--- a/liboctave/Range.cc +++ b/liboctave/Range.cc @@ -31,6 +31,7 @@ #include <limits> #include "Range.h" +#include "lo-error.h" #include "lo-mappers.h" #include "lo-math.h" #include "lo-utils.h" @@ -119,9 +120,9 @@ } void -Range::sort (void) +Range::sort_internal (bool ascending) { - if (rng_base > rng_limit && rng_inc < 0.0) + if (ascending && rng_base > rng_limit && rng_inc < 0.0) { double tmp = rng_base; rng_base = min (); @@ -129,6 +130,107 @@ rng_inc = -rng_inc; clear_cache (); } + else if (rng_base < rng_limit && rng_inc > 0.0) + { + double tmp = rng_limit; + rng_limit = min (); + rng_base = tmp; + rng_inc = -rng_inc; + clear_cache (); + } +} + +void +Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) +{ + octave_idx_type nel = nelem (); + + sidx.resize (dim_vector (1, nel)); + + octave_idx_type *psidx = sidx.fortran_vec (); + + bool reverse = false; + + if (ascending && rng_base > rng_limit && rng_inc < 0.0) + { + double tmp = rng_base; + rng_base = min (); + rng_limit = tmp; + rng_inc = -rng_inc; + clear_cache (); + reverse = true; + } + else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) + { + double tmp = rng_limit; + rng_limit = min (); + rng_base = tmp; + rng_inc = -rng_inc; + clear_cache (); + reverse = true; + } + + octave_idx_type tmp = reverse ? nel - 1 : 0; + octave_idx_type inc = reverse ? -1 : 1; + + for (octave_idx_type i = 0; i < nel; i++, tmp += inc) + psidx[i] = tmp; + +} + +Range +Range::sort (octave_idx_type dim, sortmode mode) const +{ + Range retval = *this; + + if (dim == 1) + { + switch (mode) + { + case ASCENDING: + retval.sort_internal (true); + break; + + case DESCENDING: + retval.sort_internal (false); + break; + + default: + (*current_liboctave_error_handler) ("Range::sort: invalid sort mode"); + } + } + else if (dim != 0) + (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); + + return retval; +} + +Range +Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, + sortmode mode) const +{ + Range retval = *this; + + if (dim == 1) + { + switch (mode) + { + case ASCENDING: + retval.sort_internal (sidx, true); + break; + + case DESCENDING: + retval.sort_internal (sidx, false); + break; + + default: + (*current_liboctave_error_handler) ("Range::sort: invalid sort mode"); + } + } + else if (dim != 0) + (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); + + return retval; } std::ostream&
--- a/liboctave/Range.h +++ b/liboctave/Range.h @@ -25,7 +25,9 @@ #define octave_Range_h 1 #include <iostream> + #include "dMatrix.h" +#include "oct-sort.h" class OCTAVE_API @@ -60,7 +62,13 @@ double min (void) const; double max (void) const; - void sort (void); + void sort_internal (bool ascending = true); + void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true); + + Range sort (octave_idx_type dim = 0, sortmode mode = UNDEFINED) const; + + Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0, + sortmode mode = UNDEFINED) const; void set_base (double b) {
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2008-02-07 John W. Eaton <jwe@octave.org> + + * ov-range.h (octave_range::sort): New functions. + 2008-02-07 David Bateman <dbateman@free.fr> * Makefile.in (DLD_XSRC): Delete spkron.cc.
--- a/src/ov-range.h +++ b/src/ov-range.h @@ -132,6 +132,13 @@ octave_value any (int dim = 0) const; + octave_value sort (octave_idx_type dim = 0, sortmode mode = UNDEFINED) const + { return range.sort (dim, mode); } + + octave_value sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0, + sortmode mode = UNDEFINED) const + { return range.sort (sidx, dim, mode); } + bool is_real_type (void) const { return true; } bool is_double_type (void) const { return true; }