Mercurial > hg > octave-lojdl
changeset 9310:256c0db275b6
fix behavior of find
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 07 Jun 2009 08:15:31 +0200 |
parents | fb8b8589dd46 |
children | 868fbeb2e365 |
files | liboctave/Array.cc liboctave/ChangeLog src/ChangeLog src/DLD-FUNCTIONS/find.cc |
diffstat | 4 files changed, 29 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -2476,7 +2476,7 @@ const T *src = data (); octave_idx_type nel = nelem (); const T zero = T (); - if (n < 0) + if (n < 0 || n >= nel) { // We want all elements, which means we'll almost surely need // to resize. So count first, then allocate array of exact size. @@ -2509,6 +2509,8 @@ } if (k < n) retval.resize (k); + octave_idx_type *rdata = retval.fortran_vec (); + std::reverse (rdata, rdata + k); } else {
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2009-06-07 Jaroslav Hajek <highegg@gmail.com> + + * Array.cc (Array<T>::find): Avoid allocating excessive memory. Fix + order for backward searches. + 2009-06-02 Jaroslav Hajek <highegg@gmail.com> * Array-C.cc (class DiagArray2<Complex>::Proxy): Only explicitly
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2009-06-07 Jaroslav Hajek <highegg@gmail.com> + + * DLD-FUNCTIONS/find.cc (Ffind): Fix docs. Improve second argument + handling. Add regression tests. + 2009-06-06 Rik <rdrider0-list@yahoo.com> * data.cc: Update documentation for 'complex' function
--- a/src/DLD-FUNCTIONS/find.cc +++ b/src/DLD-FUNCTIONS/find.cc @@ -399,12 +399,12 @@ @end group\n\ @end example\n\ \n\ -If two inputs are given, @var{n} indicates the number of elements to\n\ -find from the beginning of the matrix or vector.\n\ +If two inputs are given, @var{n} indicates the maximum number of\n\ +elements to find from the beginning of the matrix or vector.\n\ \n\ If three inputs are given, @var{direction} should be one of \"first\" or\n\ -\"last\" indicating that it should start counting found elements from the\n\ -first or last element.\n\ +\"last\", requesting only the first or last @var{n} indices, respectively.\n\ +However, the indices are always returned in ascending order.\n\ \n\ Note that this function is particularly useful for sparse matrices, as\n\ it extracts the non-zero elements as vectors, which can then be used to\n\ @@ -434,12 +434,15 @@ octave_idx_type n_to_find = -1; if (nargin > 1) { - n_to_find = args(1).int_value (); - if (error_state) + double val = args(1).scalar_value (); + + if (error_state || (! xisinf (val) && (val < 0 || val != xround (val)))) { - error ("find: expecting second argument to be an integer"); + error ("find: expecting second argument to be a nonnegative integer"); return retval; } + else + n_to_find = val; } // Direction to do the searching (1 == forward, -1 == reverse). @@ -636,6 +639,12 @@ %! assert (j, jfull); %! assert (all (v == 1)); +%!assert (find ([2 0 1 0 5 0], 1), 1) +%!assert (find ([2 0 1 0 5 0], 2, "last"), [3, 5]) + +%!assert (find ([2 0 1 0 5 0], Inf), [1, 3, 5]) +%!assert (find ([2 0 1 0 5 0], Inf, "last"), [1, 3, 5]) + %!error <Invalid call to find.*> find (); */