# HG changeset patch # User jwe # Date 1071621396 0 # Node ID e1c2d8ca8bc00af1c6217f4299aa36458c3fd66a # Parent e650670557f7dc18d44dc2aa079b5d212b9ab795 [project @ 2003-12-17 00:36:36 by jwe] diff -r e650670557f7 -r e1c2d8ca8bc0 src/ChangeLog --- a/src/ChangeLog Wed Dec 17 00:15:30 2003 +0000 +++ b/src/ChangeLog Wed Dec 17 00:36:36 2003 +0000 @@ -1,5 +1,16 @@ 2003-12-16 John W. Eaton + * lex.l (is_keyword): Also allow varargout_kw if + lexer_flags.looking_at_return_list is false provided that we are + defining a function and we haven't seen the function name yet. + * parse.y (return_list): Don't require [] around varargout. + + * DLD-FUNCTIONS/find.cc (DO_FIND_OP): Delete macro. + (find_to_fortran_idx): Delete. Move guts of function to + find_nonzero_elem_idx. + (find_nonzero_elem_idx): Now a template function. Handle overall + array index here too. Make it N-d aware. + * pt-pr-code.cc (tree_print_code::visit_complex_for_command, tree_print_code::visit_octave_user_function_header, tree_print_code::visit_matrix, tree_print_code::visit_cell, diff -r e650670557f7 -r e1c2d8ca8bc0 src/DLD-FUNCTIONS/find.cc --- a/src/DLD-FUNCTIONS/find.cc Wed Dec 17 00:15:30 2003 +0000 +++ b/src/DLD-FUNCTIONS/find.cc Wed Dec 17 00:36:36 2003 +0000 @@ -31,39 +31,81 @@ #include "gripes.h" #include "oct-obj.h" -#define DO_FIND_OP(T) \ - do \ - { \ - T tmp (count); \ - \ - for (int i = 0; i < count; i++) \ - tmp (i) = nr * (j_idx(i) - 1.0) + i_idx(i); \ - \ - retval(0) = tmp; \ - } \ - while (0) +inline double real (double x) { return x; } + +template +octave_value_list +find_nonzero_elem_idx (const T& nda, int nargout) +{ + octave_value_list retval (((nargout == 0) ? 1 : nargout), Matrix ()); + + int count = 0; + + int nel = nda.nelem (); + + for (int i = 0; i < nel; i++) + { + OCTAVE_QUIT; + + if (nda(i) != 0.0) + count++; + } + + if (count == 0) + return retval; + + ColumnVector idx (count); + + ColumnVector i_idx (count); + ColumnVector j_idx (count); + + T val (dim_vector (count, 1)); + count = 0; -static octave_value_list -find_to_fortran_idx (const ColumnVector i_idx, const ColumnVector j_idx, - const octave_value& val, int nr, int nargout) -{ - octave_value_list retval; + int nr = nda.rows (); + + int i = 0; + int j = 0; + + for (int k = 0; k < nel; k++) + { + OCTAVE_QUIT; + + if (nda(k) != 0.0) + { + idx(count) = k + 1; + + i_idx(count) = i + 1; + j_idx(count) = j + 1; + + val(count) = nda(k); + + count++; + } + + i++; + + if (i == nr) + { + i = 0; + + j++; + } + } switch (nargout) { case 0: case 1: { - // If the original argument was a row vector, force a row - // vector of indices to be returned. - - int count = i_idx.length (); + // If the original argument was a row vector, force a row vector of + // the overall indices to be returned. - if (nr == 1) - DO_FIND_OP (RowVector); + if (nda.ndims () == 2 && nda.rows () == 1) + retval(0) = idx.transpose (); else - DO_FIND_OP (ColumnVector); + retval(0) = idx; } break; @@ -84,95 +126,9 @@ return retval; } -static octave_value_list -find_nonzero_elem_idx (const Matrix& m, int nargout) -{ - int count = 0; - int m_nr = m.rows (); - int m_nc = m.columns (); - - int i, j; - for (j = 0; j < m_nc; j++) - for (i = 0; i < m_nr; i++) - { - OCTAVE_QUIT; - - if (m (i, j) != 0.0) - count++; - } - - octave_value_list retval (((nargout == 0) ? 1 : nargout), Matrix ()); - - if (count == 0) - return retval; - - ColumnVector i_idx (count); - ColumnVector j_idx (count); - ColumnVector v (count); - - count = 0; - for (j = 0; j < m_nc; j++) - for (i = 0; i < m_nr; i++) - { - OCTAVE_QUIT; - - double d = m (i, j); - if (d != 0.0) - { - i_idx (count) = i + 1; - j_idx (count) = j + 1; - v (count) = d; - count++; - } - } - - return find_to_fortran_idx (i_idx, j_idx, octave_value (v), m_nr, nargout); -} +template octave_value_list find_nonzero_elem_idx (const NDArray&, int); -static octave_value_list -find_nonzero_elem_idx (const ComplexMatrix& m, int nargout) -{ - int count = 0; - int m_nr = m.rows (); - int m_nc = m.columns (); - - int i, j; - for (j = 0; j < m_nc; j++) - for (i = 0; i < m_nr; i++) - { - OCTAVE_QUIT; - - if (m (i, j) != 0.0) - count++; - } - - octave_value_list retval (((nargout == 0) ? 1 : nargout), Matrix ()); - - if (count == 0) - return retval; - - ColumnVector i_idx (count); - ColumnVector j_idx (count); - ComplexColumnVector v (count); - - count = 0; - for (j = 0; j < m_nc; j++) - for (i = 0; i < m_nr; i++) - { - OCTAVE_QUIT; - - Complex c = m (i, j); - if (c != 0.0) - { - i_idx (count) = i + 1; - j_idx (count) = j + 1; - v (count) = c; - count++; - } - } - - return find_to_fortran_idx (i_idx, j_idx, octave_value (v), m_nr, nargout); -} +template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int); DEFUN_DLD (find, args, nargout, "-*- texinfo -*-\n\ @@ -227,17 +183,17 @@ if (arg.is_real_type ()) { - Matrix m = arg.matrix_value (); + NDArray nda = arg.array_value (); if (! error_state) - retval = find_nonzero_elem_idx (m, nargout); + retval = find_nonzero_elem_idx (nda, nargout); } else if (arg.is_complex_type ()) { - ComplexMatrix m = arg.complex_matrix_value (); + ComplexNDArray cnda = arg.complex_array_value (); if (! error_state) - retval = find_nonzero_elem_idx (m, nargout); + retval = find_nonzero_elem_idx (cnda, nargout); } else { diff -r e650670557f7 -r e1c2d8ca8bc0 src/lex.l --- a/src/lex.l Wed Dec 17 00:15:30 2003 +0000 +++ b/src/lex.l Wed Dec 17 00:36:36 2003 +0000 @@ -1302,7 +1302,9 @@ break; case varargout_kw: - if (! lexer_flags.looking_at_return_list) + if (! (lexer_flags.looking_at_return_list + || (lexer_flags.defining_func + && ! lexer_flags.parsed_function_name))) return 0; break; diff -r e650670557f7 -r e1c2d8ca8bc0 src/parse.y --- a/src/parse.y Wed Dec 17 00:15:30 2003 +0000 +++ b/src/parse.y Wed Dec 17 00:36:36 2003 +0000 @@ -1222,6 +1222,13 @@ tmp->mark_varargs_only (); $$ = tmp; } + | VARARGOUT + { + lexer_flags.looking_at_return_list = false; + tree_parameter_list *tmp = new tree_parameter_list (); + tmp->mark_varargs_only (); + $$ = tmp; + } | return_list_beg return_list1 return_list_end { lexer_flags.looking_at_return_list = false;