# HG changeset patch # User Jaroslav Hajek # Date 1244355331 -7200 # Node ID 3daadc82aee909ee792effcc3f1ee64d36cd84d9 # Parent 087e5d3a82a68444a9ba3097000b64f476f92c54 fix behavior of find diff --git a/liboctave/Array.cc b/liboctave/Array.cc --- 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 { diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2009-06-07 Jaroslav Hajek + + * Array.cc (Array::find): Avoid allocating excessive memory. Fix + order for backward searches. + 2009-05-25 Jaroslav Hajek Version 3.2.0 released. diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2009-06-07 Jaroslav Hajek + + * DLD-FUNCTIONS/find.cc (Ffind): Fix docs. Improve second argument + handling. Add regression tests. + 2009-06-06 Rik * data.cc: Update documentation for 'complex' function 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 @@ -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 find (); */