# HG changeset patch # User Jaroslav Hajek # Date 1265716607 -3600 # Node ID 22a7913bbeb5a24ea2d3e4d2e5ff8f466bed7633 # Parent c3df189b1b154ced10c22a7e343e4a5c76528da1 optimize return values of find and sort diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,11 @@ +2010-02-09 Jaroslav Hajek + + * idx-vector.cc (idx_vector::idx_vector_rep::idx_vector_rep (const + Array&, octave_idx_type, direct)): New constructor. + * idx-vector.h: Declare it. + (idx_vector::idx_vector (const Array&, + octave_idx_type)): New constructor. + 2010-02-08 John W. Eaton * oct-time.cc: Include "strftime.h", not . diff --git a/liboctave/idx-vector.cc b/liboctave/idx-vector.cc --- a/liboctave/idx-vector.cc +++ b/liboctave/idx-vector.cc @@ -330,6 +330,23 @@ } } +idx_vector::idx_vector_rep::idx_vector_rep (const Array& inda, + octave_idx_type _ext, direct) + : data (inda.data ()), len (inda.numel ()), ext (_ext), + aowner (new Array (inda)), orig_dims (inda.dims ()) +{ + // No checking. + if (ext < 0) + { + octave_idx_type max = -1; + for (octave_idx_type i = 0; i < len; i++) + if (data[i] > max) + max = data[i]; + + ext = max + 1; + } +} + idx_vector::idx_vector_rep::idx_vector_rep (bool b) : data (0), len (b ? 1 : 0), ext (0), aowner (0), orig_dims (len, len) { diff --git a/liboctave/idx-vector.h b/liboctave/idx-vector.h --- a/liboctave/idx-vector.h +++ b/liboctave/idx-vector.h @@ -287,6 +287,9 @@ // Zero-based constructor. idx_vector_rep (const Array& inda); + idx_vector_rep (const Array& inda, + octave_idx_type _ext, direct); + template idx_vector_rep (const Array&); @@ -473,6 +476,11 @@ : rep (new idx_vector_rep (inda)) { chkerr (); } + // Directly pass extent, no checking. + idx_vector (const Array& inda, octave_idx_type ext) + : rep (new idx_vector_rep (inda, ext, DIRECT)) + { } + // Colon is best constructed by simply copying (or referencing) this member. static const idx_vector colon; diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2010-02-09 Jaroslav Hajek + + * DLD-FUNCTIONS/find.cc (find_nonzero_elem_idx (const Array&, ...)): + Optimize creation of result indices. + * data.cc (Fsort): Ditto. + 2010-02-09 Jaroslav Hajek * DLD-FUNCTIONS/colamd.cc (Fcolamd, Fsymamd, Fetree): Fix improper arg diff --git a/src/DLD-FUNCTIONS/find.cc b/src/DLD-FUNCTIONS/find.cc --- a/src/DLD-FUNCTIONS/find.cc +++ b/src/DLD-FUNCTIONS/find.cc @@ -49,6 +49,9 @@ else idx = nda.find (); + // The maximum element is always at the end. + octave_idx_type iext = idx.is_empty () ? 0 : idx.xelem (idx.numel () - 1) + 1; + switch (nargout) { default: @@ -65,13 +68,14 @@ jdx.xelem (i) = idx.xelem (i) / nr; idx.xelem (i) %= nr; } - retval(1) = octave_value (jdx, true, true); + iext = -1; + retval(1) = idx_vector (jdx, -1); } // Fall through! case 1: case 0: - retval(0) = octave_value (idx, true, true); + retval(0) = idx_vector (idx, iext); break; } diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -5810,21 +5810,11 @@ } } - dim_vector dv = arg.dims (); - if (error_state) - { - gripe_wrong_type_arg ("sort", arg); - return retval; - } + const dim_vector dv = arg.dims (); if (nargin == 1 || args(1).is_string ()) { // Find first non singleton dimension - for (int i = 0; i < dv.length (); i++) - if (dv(i) > 1) - { - dim = i; - break; - } + dim = dv.first_non_singleton (); } else { @@ -5840,7 +5830,7 @@ Array sidx; retval (0) = arg.sort (sidx, dim, smode); - retval (1) = octave_value (sidx, true, true); + retval (1) = idx_vector (sidx, dv(dim)); // No checking, the extent is known. } else retval(0) = arg.sort (dim, smode);