comparison libinterp/octave-value/ov-struct.cc @ 20593:c41595061186

eliminate more simple uses of error_state * betainc.cc, file-io.cc, ov-class.cc, ov-struct.cc: Eliminate simple uses of error_state.
author John W. Eaton <jwe@octave.org>
date Mon, 05 Oct 2015 23:09:54 -0400
parents c6224b4e7774
children eef93a493ce3
comparison
equal deleted inserted replaced
20592:56fee8f84fe7 20593:c41595061186
151 std::list<octave_value_list>::const_iterator p = idx.begin (); 151 std::list<octave_value_list>::const_iterator p = idx.begin ();
152 octave_value_list key_idx = *++p; 152 octave_value_list key_idx = *++p;
153 153
154 const Cell tmp = dotref (key_idx); 154 const Cell tmp = dotref (key_idx);
155 155
156 if (! error_state) 156 const Cell t = tmp.index (idx.front ());
157 { 157
158 const Cell t = tmp.index (idx.front ()); 158 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true);
159 159
160 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true); 160 // We handled two index elements, so tell
161 161 // next_subsref to skip both of them.
162 // We handled two index elements, so tell 162
163 // next_subsref to skip both of them. 163 skip++;
164
165 skip++;
166 }
167 } 164 }
168 else 165 else
169 retval(0) = do_index_op (idx.front ()); 166 retval(0) = do_index_op (idx.front ());
170 } 167 }
171 break; 168 break;
217 std::list<octave_value_list>::const_iterator p = idx.begin (); 214 std::list<octave_value_list>::const_iterator p = idx.begin ();
218 octave_value_list key_idx = *++p; 215 octave_value_list key_idx = *++p;
219 216
220 const Cell tmp = dotref (key_idx, auto_add); 217 const Cell tmp = dotref (key_idx, auto_add);
221 218
222 if (! error_state) 219 const Cell t = tmp.index (idx.front (), auto_add);
223 { 220
224 const Cell t = tmp.index (idx.front (), auto_add); 221 retval = (t.numel () == 1) ? t(0) : octave_value (t, true);
225 222
226 retval = (t.numel () == 1) ? t(0) : octave_value (t, true); 223 // We handled two index elements, so tell
227 224 // next_subsref to skip both of them.
228 // We handled two index elements, so tell 225
229 // next_subsref to skip both of them. 226 skip++;
230
231 skip++;
232 }
233 } 227 }
234 else 228 else
235 retval = do_index_op (idx.front (), auto_add); 229 retval = do_index_op (idx.front (), auto_add);
236 } 230 }
237 break; 231 break;
322 316
323 std::string key = key_idx(0).string_value (); 317 std::string key = key_idx(0).string_value ();
324 318
325 maybe_warn_invalid_field_name (key, "subsasgn"); 319 maybe_warn_invalid_field_name (key, "subsasgn");
326 320
327 if (error_state)
328 return retval;
329
330 std::list<octave_value_list> next_idx (idx); 321 std::list<octave_value_list> next_idx (idx);
331 322
332 // We handled two index elements, so subsasgn to 323 // We handled two index elements, so subsasgn to
333 // needs to skip both of them. 324 // needs to skip both of them.
334 325
345 tmpc = map.contents (pkey).index (idx.front (), true); 336 tmpc = map.contents (pkey).index (idx.front (), true);
346 } 337 }
347 338
348 // FIXME: better code reuse? 339 // FIXME: better code reuse?
349 // cf. octave_cell::subsasgn and the case below. 340 // cf. octave_cell::subsasgn and the case below.
350 if (! error_state)
351 {
352 if (tmpc.numel () == 1)
353 {
354 octave_value& tmp = tmpc(0);
355
356 bool orig_undefined = tmp.is_undefined ();
357
358 if (orig_undefined || tmp.is_zero_by_zero ())
359 {
360 tmp = octave_value::empty_conv (next_type, rhs);
361 tmp.make_unique (); // probably a no-op.
362 }
363 else
364 // optimization: ignore the copy
365 // still stored inside our map.
366 tmp.make_unique (1);
367
368 if (! error_state)
369 t_rhs =
370 (orig_undefined
371 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
372 : tmp.subsasgn (next_type, next_idx, rhs));
373 }
374 else
375 gripe_indexed_cs_list ();
376 }
377 }
378 else
379 gripe_invalid_index_for_assignment ();
380 }
381 break;
382
383 case '.':
384 {
385 octave_value_list key_idx = idx.front ();
386
387 assert (key_idx.length () == 1);
388
389 std::string key = key_idx(0).string_value ();
390
391 maybe_warn_invalid_field_name (key, "subsasgn");
392
393 if (error_state)
394 return retval;
395
396 std::list<octave_value_list> next_idx (idx);
397
398 next_idx.erase (next_idx.begin ());
399
400 std::string next_type = type.substr (1);
401
402 Cell tmpc (1, 1);
403 octave_map::iterator pkey = map.seek (key);
404 if (pkey != map.end ())
405 {
406 map.contents (pkey).make_unique ();
407 tmpc = map.contents (pkey);
408 }
409
410 // FIXME: better code reuse?
411 if (! error_state)
412 {
413 if (tmpc.numel () == 1) 341 if (tmpc.numel () == 1)
414 { 342 {
415 octave_value& tmp = tmpc(0); 343 octave_value& tmp = tmpc(0);
416 344
417 bool orig_undefined = tmp.is_undefined (); 345 bool orig_undefined = tmp.is_undefined ();
424 else 352 else
425 // optimization: ignore the copy 353 // optimization: ignore the copy
426 // still stored inside our map. 354 // still stored inside our map.
427 tmp.make_unique (1); 355 tmp.make_unique (1);
428 356
429 if (! error_state) 357 t_rhs =(orig_undefined
430 t_rhs = (orig_undefined 358 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
431 ? tmp.undef_subsasgn (next_type, next_idx, rhs) 359 : tmp.subsasgn (next_type, next_idx, rhs));
432 : tmp.subsasgn (next_type, next_idx, rhs));
433 } 360 }
434 else 361 else
435 gripe_indexed_cs_list (); 362 gripe_indexed_cs_list ();
436 } 363 }
364 else
365 gripe_invalid_index_for_assignment ();
366 }
367 break;
368
369 case '.':
370 {
371 octave_value_list key_idx = idx.front ();
372
373 assert (key_idx.length () == 1);
374
375 std::string key = key_idx(0).string_value ();
376
377 maybe_warn_invalid_field_name (key, "subsasgn");
378
379 std::list<octave_value_list> next_idx (idx);
380
381 next_idx.erase (next_idx.begin ());
382
383 std::string next_type = type.substr (1);
384
385 Cell tmpc (1, 1);
386 octave_map::iterator pkey = map.seek (key);
387 if (pkey != map.end ())
388 {
389 map.contents (pkey).make_unique ();
390 tmpc = map.contents (pkey);
391 }
392
393 // FIXME: better code reuse?
394
395 if (tmpc.numel () == 1)
396 {
397 octave_value& tmp = tmpc(0);
398
399 bool orig_undefined = tmp.is_undefined ();
400
401 if (orig_undefined || tmp.is_zero_by_zero ())
402 {
403 tmp = octave_value::empty_conv (next_type, rhs);
404 tmp.make_unique (); // probably a no-op.
405 }
406 else
407 // optimization: ignore the copy
408 // still stored inside our map.
409 tmp.make_unique (1);
410
411 t_rhs = (orig_undefined
412 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
413 : tmp.subsasgn (next_type, next_idx, rhs));
414 }
415 else
416 gripe_indexed_cs_list ();
437 } 417 }
438 break; 418 break;
439 419
440 case '{': 420 case '{':
441 gripe_invalid_index_type (type_name (), type[0]); 421 gripe_invalid_index_type (type_name (), type[0]);
461 assert (key_idx.length () == 1); 441 assert (key_idx.length () == 1);
462 442
463 std::string key = key_idx(0).string_value (); 443 std::string key = key_idx(0).string_value ();
464 444
465 maybe_warn_invalid_field_name (key, "subsasgn"); 445 maybe_warn_invalid_field_name (key, "subsasgn");
466
467 if (error_state)
468 return retval;
469 446
470 if (! error_state) 447 if (! error_state)
471 { 448 {
472 if (t_rhs.is_cs_list ()) 449 if (t_rhs.is_cs_list ())
473 { 450 {
570 547
571 std::string key = key_idx(0).string_value (); 548 std::string key = key_idx(0).string_value ();
572 549
573 maybe_warn_invalid_field_name (key, "subsasgn"); 550 maybe_warn_invalid_field_name (key, "subsasgn");
574 551
575 if (error_state)
576 return retval;
577
578 if (t_rhs.is_cs_list ()) 552 if (t_rhs.is_cs_list ())
579 { 553 {
580 Cell tmp_cell = Cell (t_rhs.list_value ()); 554 Cell tmp_cell = Cell (t_rhs.list_value ());
581 555
582 // The shape of the RHS is irrelevant, we just want 556 // The shape of the RHS is irrelevant, we just want
1154 1128
1155 std::string nm = idx(0).string_value (); 1129 std::string nm = idx(0).string_value ();
1156 1130
1157 maybe_warn_invalid_field_name (nm, "subsref"); 1131 maybe_warn_invalid_field_name (nm, "subsref");
1158 1132
1159 if (error_state)
1160 return retval;
1161
1162 retval = map.getfield (nm); 1133 retval = map.getfield (nm);
1163 1134
1164 if (! auto_add && retval.is_undefined ()) 1135 if (! auto_add && retval.is_undefined ())
1165 error_with_id ("Octave:invalid-indexing", 1136 error_with_id ("Octave:invalid-indexing",
1166 "structure has no member '%s'", nm.c_str ()); 1137 "structure has no member '%s'", nm.c_str ());
1281 1252
1282 std::string key = key_idx(0).string_value (); 1253 std::string key = key_idx(0).string_value ();
1283 1254
1284 maybe_warn_invalid_field_name (key, "subsasgn"); 1255 maybe_warn_invalid_field_name (key, "subsasgn");
1285 1256
1286 if (error_state)
1287 return retval;
1288
1289 if (n > 1) 1257 if (n > 1)
1290 { 1258 {
1291 std::list<octave_value_list> next_idx (idx); 1259 std::list<octave_value_list> next_idx (idx);
1292 1260
1293 next_idx.erase (next_idx.begin ()); 1261 next_idx.erase (next_idx.begin ());
1300 { 1268 {
1301 map.contents (pkey).make_unique (); 1269 map.contents (pkey).make_unique ();
1302 tmp = map.contents (pkey); 1270 tmp = map.contents (pkey);
1303 } 1271 }
1304 1272
1305 if (! error_state) 1273 bool orig_undefined = tmp.is_undefined ();
1274
1275 if (orig_undefined || tmp.is_zero_by_zero ())
1306 { 1276 {
1307 bool orig_undefined = tmp.is_undefined (); 1277 tmp = octave_value::empty_conv (next_type, rhs);
1308 1278 tmp.make_unique (); // probably a no-op.
1309 if (orig_undefined || tmp.is_zero_by_zero ())
1310 {
1311 tmp = octave_value::empty_conv (next_type, rhs);
1312 tmp.make_unique (); // probably a no-op.
1313 }
1314 else
1315 // optimization: ignore the copy still stored inside our map.
1316 tmp.make_unique (1);
1317
1318 if (! error_state)
1319 t_rhs = (orig_undefined
1320 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
1321 : tmp.subsasgn (next_type, next_idx, rhs));
1322 } 1279 }
1280 else
1281 // optimization: ignore the copy still stored inside our map.
1282 tmp.make_unique (1);
1283
1284 t_rhs = (orig_undefined
1285 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
1286 : tmp.subsasgn (next_type, next_idx, rhs));
1323 } 1287 }
1324 1288
1325 if (! error_state) 1289 if (! error_state)
1326 map.setfield (key, t_rhs.storable_value ()); 1290 map.setfield (key, t_rhs.storable_value ());
1327 else 1291 else
1923 { 1887 {
1924 // Get key. 1888 // Get key.
1925 1889
1926 std::string key (args(i).string_value ()); 1890 std::string key (args(i).string_value ());
1927 1891
1928 if (error_state)
1929 return retval;
1930
1931 maybe_warn_invalid_field_name (key, "struct"); 1892 maybe_warn_invalid_field_name (key, "struct");
1932
1933 if (error_state)
1934 return retval;
1935 1893
1936 // Value may be v, { v }, or { v1, v2, ... } 1894 // Value may be v, { v }, or { v1, v2, ... }
1937 // In the first two cases, we need to create a cell array of 1895 // In the first two cases, we need to create a cell array of
1938 // the appropriate dimensions filled with v. In the last case, 1896 // the appropriate dimensions filled with v. In the last case,
1939 // the cell array has already been determined to be of the 1897 // the cell array has already been determined to be of the
1941 1899
1942 if (args(i+1).is_cell ()) 1900 if (args(i+1).is_cell ())
1943 { 1901 {
1944 const Cell c (args(i+1).cell_value ()); 1902 const Cell c (args(i+1).cell_value ());
1945 1903
1946 if (error_state)
1947 return retval;
1948
1949 if (scalar (c.dims ())) 1904 if (scalar (c.dims ()))
1950 map.setfield (key, Cell (dims, c(0))); 1905 map.setfield (key, Cell (dims, c(0)));
1951 else 1906 else
1952 map.setfield (key, c); 1907 map.setfield (key, c);
1953 } 1908 }
1954 else 1909 else
1955 map.setfield (key, Cell (dims, args(i+1))); 1910 map.setfield (key, Cell (dims, args(i+1)));
1956
1957 if (error_state)
1958 return retval;
1959 } 1911 }
1960 1912
1961 return octave_value (map); 1913 return octave_value (map);
1962 } 1914 }
1963 1915
2173 int dim = 0; 2125 int dim = 0;
2174 2126
2175 if (nargin == 3) 2127 if (nargin == 3)
2176 { 2128 {
2177 if (args(2).is_real_scalar ()) 2129 if (args(2).is_real_scalar ())
2178 { 2130 dim = nargin == 2 ? 0 : args(2).int_value () - 1;
2179 dim = nargin == 2 ? 0 : args(2).int_value () - 1;
2180
2181 if (error_state)
2182 return retval;
2183 }
2184 else 2131 else
2185 { 2132 {
2186 error ("cell2struct: DIM must be a real scalar"); 2133 error ("cell2struct: DIM must be a real scalar");
2187 return retval; 2134 return retval;
2188 } 2135 }
2277 { 2224 {
2278 octave_map m = args(0).map_value (); 2225 octave_map m = args(0).map_value ();
2279 2226
2280 octave_value_list fval = Fcellstr (args(1), 1); 2227 octave_value_list fval = Fcellstr (args(1), 1);
2281 2228
2282 if (! error_state) 2229 Cell fcell = fval(0).cell_value ();
2283 { 2230
2284 Cell fcell = fval(0).cell_value (); 2231 for (int i = 0; i < fcell.numel (); i++)
2285 2232 {
2286 for (int i = 0; i < fcell.numel (); i++) 2233 std::string key = fcell(i).string_value ();
2234
2235 if (m.isfield (key))
2236 m.rmfield (key);
2237 else
2287 { 2238 {
2288 std::string key = fcell(i).string_value (); 2239 error ("rmfield: structure does not contain field %s",
2289 2240 key.c_str ());
2290 if (m.isfield (key)) 2241
2291 m.rmfield (key); 2242 break;
2292 else
2293 {
2294 error ("rmfield: structure does not contain field %s",
2295 key.c_str ());
2296
2297 break;
2298 }
2299 } 2243 }
2300 2244 }
2301 if (! error_state) 2245
2302 retval = m; 2246 retval = m;
2303 }
2304 } 2247 }
2305 else 2248 else
2306 print_usage (); 2249 print_usage ();
2307 2250
2308 return retval; 2251 return retval;