Mercurial > octave-nkf
comparison libinterp/corefcn/Cell.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 |
---|---|
160 { | 160 { |
161 Cell retval; | 161 Cell retval; |
162 | 162 |
163 octave_idx_type n = idx_arg.length (); | 163 octave_idx_type n = idx_arg.length (); |
164 | 164 |
165 switch (n) | 165 // If we catch an indexing error in index_vector, we flag an error |
166 { | 166 // in index k. Ensure it is the right value befor each idx_vector |
167 case 0: | 167 // call. Same variable as used in for loop in default case. |
168 retval = *this; | 168 |
169 break; | 169 octave_idx_type k = 0; |
170 | 170 |
171 case 1: | 171 try |
172 { | 172 { |
173 idx_vector i = idx_arg(0).index_vector (); | 173 switch (n) |
174 | 174 { |
175 if (! error_state) | 175 case 0: |
176 retval = Array<octave_value>::index (i, resize_ok, Matrix ()); | 176 retval = *this; |
177 } | 177 break; |
178 break; | 178 |
179 | 179 case 1: |
180 case 2: | |
181 { | |
182 idx_vector i = idx_arg(0).index_vector (); | |
183 | |
184 if (! error_state) | |
185 { | 180 { |
186 idx_vector j = idx_arg(1).index_vector (); | 181 idx_vector i = idx_arg(0).index_vector (); |
187 | 182 |
188 if (! error_state) | 183 if (! error_state) |
189 retval = Array<octave_value>::index (i, j, resize_ok, Matrix ()); | 184 retval = Array<octave_value>::index (i, resize_ok, Matrix ()); |
190 } | 185 } |
191 } | 186 break; |
192 break; | 187 |
193 | 188 case 2: |
194 default: | |
195 { | |
196 Array<idx_vector> iv (dim_vector (n, 1)); | |
197 | |
198 for (octave_idx_type i = 0; i < n; i++) | |
199 { | 189 { |
200 iv(i) = idx_arg(i).index_vector (); | 190 idx_vector i = idx_arg(0).index_vector (); |
201 | 191 |
202 if (error_state) | 192 if (! error_state) |
203 break; | 193 { |
194 k = 1; | |
195 idx_vector j = idx_arg(1).index_vector (); | |
196 | |
197 if (! error_state) | |
198 retval = Array<octave_value>::index (i, j, resize_ok, Matrix ()); | |
199 } | |
204 } | 200 } |
205 | 201 break; |
206 if (!error_state) | 202 |
207 retval = Array<octave_value>::index (iv, resize_ok, Matrix ()); | 203 default: |
208 } | 204 { |
209 break; | 205 Array<idx_vector> iv (dim_vector (n, 1)); |
206 | |
207 for (k = 0; k < n; k++) | |
208 { | |
209 iv(k) = idx_arg(k).index_vector (); | |
210 | |
211 if (error_state) | |
212 break; | |
213 } | |
214 if (!error_state) | |
215 retval = Array<octave_value>::index (iv, resize_ok, Matrix ()); | |
216 } | |
217 break; | |
218 } | |
219 } | |
220 catch (index_exception& e) | |
221 { | |
222 // Rethrow to allow more info to be reported later. | |
223 e.set_pos_if_unset (n, k+1); | |
224 throw; | |
210 } | 225 } |
211 | 226 |
212 return retval; | 227 return retval; |
213 } | 228 } |
214 | 229 |
226 octave_idx_type len = idx_arg.length (); | 241 octave_idx_type len = idx_arg.length (); |
227 | 242 |
228 Array<idx_vector> ra_idx (dim_vector (len, 1)); | 243 Array<idx_vector> ra_idx (dim_vector (len, 1)); |
229 | 244 |
230 for (octave_idx_type i = 0; i < len; i++) | 245 for (octave_idx_type i = 0; i < len; i++) |
231 ra_idx(i) = idx_arg(i).index_vector (); | 246 { |
247 try | |
248 { | |
249 ra_idx(i) = idx_arg(i).index_vector (); | |
250 } | |
251 catch (index_exception& e) | |
252 { | |
253 // Rethrow to allow more info to be reported later. | |
254 e.set_pos (len, i+1); | |
255 throw; | |
256 } | |
257 } | |
232 | 258 |
233 Array<octave_value>::assign (ra_idx, rhs, fill_val); | 259 Array<octave_value>::assign (ra_idx, rhs, fill_val); |
234 } | 260 } |
235 | 261 |
236 void | 262 void |
240 octave_idx_type len = idx_arg.length (); | 266 octave_idx_type len = idx_arg.length (); |
241 | 267 |
242 Array<idx_vector> ra_idx (dim_vector (len, 1)); | 268 Array<idx_vector> ra_idx (dim_vector (len, 1)); |
243 | 269 |
244 for (octave_idx_type i = 0; i < len; i++) | 270 for (octave_idx_type i = 0; i < len; i++) |
245 ra_idx.xelem (i) = idx_arg(i).index_vector (); | 271 try |
272 { | |
273 ra_idx.xelem (i) = idx_arg(i).index_vector (); | |
274 } | |
275 catch (index_exception& e) | |
276 { | |
277 // Rethrow to allow more info to be reported later. | |
278 e.set_pos (len, i+1); | |
279 throw; | |
280 } | |
246 | 281 |
247 Array<octave_value>::delete_elements (ra_idx); | 282 Array<octave_value>::delete_elements (ra_idx); |
248 } | 283 } |
249 | 284 |
250 octave_idx_type | 285 octave_idx_type |