comparison libinterp/corefcn/sub2ind.cc @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents a9574e3c6e9e
children f90c8372b7ba
comparison
equal deleted inserted replaced
20573:e3c0fee87493 20574:dd6345fd8a97
97 dv = dv.redim (nargin - 1); 97 dv = dv.redim (nargin - 1);
98 for (int j = 0; j < nargin - 1; j++) 98 for (int j = 0; j < nargin - 1; j++)
99 { 99 {
100 if (args(j+1).is_numeric_type ()) 100 if (args(j+1).is_numeric_type ())
101 { 101 {
102 idxa(j) = args(j+1).index_vector (); 102 try
103 if (error_state) 103 {
104 break; 104 idxa(j) = args(j+1).index_vector ();
105 else if (j > 0 && args(j+1).dims () != args(1).dims ()) 105 if (error_state)
106 error ("sub2ind: all subscripts must be of the same size"); 106 break;
107 else if (j > 0 && args(j+1).dims () != args(1).dims ())
108 error ("sub2ind: all subscripts must be of the same size");
109 }
110 catch (index_exception& e)
111 {
112 e.set_pos_if_unset (nargin-1, j+1);
113 e.set_var (""); // no particular variable
114 (*current_liboctave_error_with_id_handler)
115 (e.id(), e.err());
116 }
107 } 117 }
108 else 118 else
109 error ("sub2ind: subscripts must be numeric"); 119 error ("sub2ind: subscripts must be numeric");
110 120
111 if (error_state) 121 if (error_state)
132 %! in = [ 1 101 11 111 ; 2 102 12 112 ]; 142 %! in = [ 1 101 11 111 ; 2 102 12 112 ];
133 %! assert (sub2ind ([10 10 10], s1, s2, s3), in); 143 %! assert (sub2ind ([10 10 10], s1, s2, s3), in);
134 144
135 # Test low index 145 # Test low index
136 %!assert (sub2ind ([10 10 10], 1, 1, 1), 1) 146 %!assert (sub2ind ([10 10 10], 1, 1, 1), 1)
137 %!error <subscript indices> sub2ind ([10 10 10], 0, 1, 1) 147 %!error <index \(0,_,_\)> sub2ind ([10 10 10], 0, 1, 1)
138 %!error <subscript indices> sub2ind ([10 10 10], 1, 0, 1) 148 %!error <index \(_,0,_\)> sub2ind ([10 10 10], 1, 0, 1)
139 %!error <subscript indices> sub2ind ([10 10 10], 1, 1, 0) 149 %!error <index \(_,_,0\)> sub2ind ([10 10 10], 1, 1, 0)
140 150
141 # Test high index 151 # Test high index
142 %!assert (sub2ind ([10 10 10], 10, 10, 10), 1000) 152 %!assert (sub2ind ([10 10 10], 10, 10, 10), 1000)
143 %!error <index out of range> sub2ind ([10 10 10], 11, 10, 10) 153 %!error <index \(11,_,_\); out of bound 10> sub2ind ([10 10 10], 11, 10, 10)
144 %!error <index out of range> sub2ind ([10 10 10], 10, 11, 10) 154 %!error <index \(_,11,_\); out of bound 10> sub2ind ([10 10 10], 10, 11, 10)
145 %!error <index out of range> sub2ind ([10 10 10], 10, 10, 11) 155 %!error <index \(_,_,11\); out of bound 10> sub2ind ([10 10 10], 10, 10, 11)
146 156
147 # Test high index in the trailing dimensions 157 # Test high index in the trailing dimensions
148 %!assert (sub2ind ([10, 1], 2, 1, 1), 2) 158 %!assert (sub2ind ([10, 1], 2, 1, 1), 2)
149 %!error <index out of range> sub2ind ([10, 1], 1, 2, 1) 159 %!error <index \(_,2,_\); out of bound 1> sub2ind ([10, 1], 1, 2, 1)
150 %!error <index out of range> sub2ind ([10, 1], 1, 1, 2) 160 %!error <index \(_,_,2\); out of bound 1> sub2ind ([10, 1], 1, 1, 2)
151 %!assert (sub2ind ([10 10], 2, 2, 1), 12) 161 %!assert (sub2ind ([10 10], 2, 2, 1), 12)
152 %!error <index out of range> sub2ind ([10 10], 2, 1, 2) 162 %!error <index \(_,_,2\); out of bound 1> sub2ind ([10 10], 2, 1, 2)
153 %!error <index out of range> sub2ind ([10 10], 1, 2, 2) 163 %!error <index \(_,_,2\); out of bound 1> sub2ind ([10 10], 1, 2, 2)
154 164
155 # Test handling of empty arguments 165 # Test handling of empty arguments
156 %!assert (sub2ind ([10 10], zeros (0,0), zeros (0,0)), zeros (0,0)) 166 %!assert (sub2ind ([10 10], zeros (0,0), zeros (0,0)), zeros (0,0))
157 %!assert (sub2ind ([10 10], zeros (2,0), zeros (2,0)), zeros (2,0)) 167 %!assert (sub2ind ([10 10], zeros (2,0), zeros (2,0)), zeros (2,0))
158 %!assert (sub2ind ([10 10], zeros (0,2), zeros (0,2)), zeros (0,2)) 168 %!assert (sub2ind ([10 10], zeros (0,2), zeros (0,2)), zeros (0,2))
162 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (1,3)) 172 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (1,3))
163 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (2,1)) 173 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (2,1))
164 174
165 ## Test input validation 175 ## Test input validation
166 %!error <dimension vector> sub2ind ([10 10.5], 1, 1) 176 %!error <dimension vector> sub2ind ([10 10.5], 1, 1)
167 %!error <subscript indices> sub2ind ([10 10], 1.5, 1) 177 %!error <index \(1.5,_\)> sub2ind ([10 10], 1.5, 1)
168 %!error <subscript indices> sub2ind ([10 10], 1, 1.5) 178 %!error <index \(_,1.5\)> sub2ind ([10 10], 1, 1.5)
169 */ 179 */
170 180
171 DEFUN (ind2sub, args, nargout, 181 DEFUN (ind2sub, args, nargout,
172 "-*- texinfo -*-\n\ 182 "-*- texinfo -*-\n\
173 @deftypefn {Function File} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} ind2sub (@var{dims}, @var{ind})\n\ 183 @deftypefn {Function File} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} ind2sub (@var{dims}, @var{ind})\n\
193 if (nargin != 2) 203 if (nargin != 2)
194 print_usage (); 204 print_usage ();
195 else 205 else
196 { 206 {
197 dim_vector dv = get_dim_vector (args(0), "ind2sub"); 207 dim_vector dv = get_dim_vector (args(0), "ind2sub");
198 idx_vector idx = args(1).index_vector (); 208 try
199 if (! error_state) 209 {
200 { 210 idx_vector idx = args(1).index_vector ();
201 if (nargout > dv.length ()) 211 if (! error_state)
202 dv = dv.redim (nargout); 212 {
203 213 if (nargout > dv.length ())
204 Array<idx_vector> idxa = ind2sub (dv, idx); 214 dv = dv.redim (nargout);
205 retval = Array<octave_value> (idxa); 215
216 Array<idx_vector> idxa = ind2sub (dv, idx);
217 retval = Array<octave_value> (idxa);
218 }
219 }
220 catch (index_exception& e)
221 {
222 error ("ind2sub: Invalid index %s. %s", e.idx (), e.explain ());
206 } 223 }
207 } 224 }
208 225
209 return retval; 226 return retval;
210 } 227 }