Mercurial > hg > octave-lyh
comparison src/ov-cell.cc @ 10315:57a59eae83cc
untabify src C++ source files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 11 Feb 2010 12:41:46 -0500 |
parents | cd96d29c5efa |
children | 4d1fc073fbb7 |
comparison
equal
deleted
inserted
replaced
10314:07ebe522dac2 | 10315:57a59eae83cc |
---|---|
104 error ("assignment to cell array failed"); | 104 error ("assignment to cell array failed"); |
105 } | 105 } |
106 | 106 |
107 octave_value_list | 107 octave_value_list |
108 octave_cell::subsref (const std::string& type, | 108 octave_cell::subsref (const std::string& type, |
109 const std::list<octave_value_list>& idx, | 109 const std::list<octave_value_list>& idx, |
110 int nargout) | 110 int nargout) |
111 { | 111 { |
112 octave_value_list retval; | 112 octave_value_list retval; |
113 | 113 |
114 switch (type[0]) | 114 switch (type[0]) |
115 { | 115 { |
117 retval(0) = do_index_op (idx.front ()); | 117 retval(0) = do_index_op (idx.front ()); |
118 break; | 118 break; |
119 | 119 |
120 case '{': | 120 case '{': |
121 { | 121 { |
122 octave_value tmp = do_index_op (idx.front ()); | 122 octave_value tmp = do_index_op (idx.front ()); |
123 | 123 |
124 if (! error_state) | 124 if (! error_state) |
125 { | 125 { |
126 Cell tcell = tmp.cell_value (); | 126 Cell tcell = tmp.cell_value (); |
127 | 127 |
128 if (tcell.length () == 1) | 128 if (tcell.length () == 1) |
129 retval(0) = tcell(0,0); | 129 retval(0) = tcell(0,0); |
130 else | 130 else |
131 retval = octave_value (octave_value_list (tcell), true); | 131 retval = octave_value (octave_value_list (tcell), true); |
132 } | 132 } |
133 } | 133 } |
134 break; | 134 break; |
135 | 135 |
136 case '.': | 136 case '.': |
137 { | 137 { |
138 std::string nm = type_name (); | 138 std::string nm = type_name (); |
139 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | 139 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
140 } | 140 } |
141 break; | 141 break; |
142 | 142 |
143 default: | 143 default: |
144 panic_impossible (); | 144 panic_impossible (); |
154 return retval; | 154 return retval; |
155 } | 155 } |
156 | 156 |
157 octave_value | 157 octave_value |
158 octave_cell::subsref (const std::string& type, | 158 octave_cell::subsref (const std::string& type, |
159 const std::list<octave_value_list>& idx, | 159 const std::list<octave_value_list>& idx, |
160 bool auto_add) | 160 bool auto_add) |
161 { | 161 { |
162 octave_value retval; | 162 octave_value retval; |
163 | 163 |
164 switch (type[0]) | 164 switch (type[0]) |
165 { | 165 { |
167 retval = do_index_op (idx.front (), auto_add); | 167 retval = do_index_op (idx.front (), auto_add); |
168 break; | 168 break; |
169 | 169 |
170 case '{': | 170 case '{': |
171 { | 171 { |
172 octave_value tmp = do_index_op (idx.front (), auto_add); | 172 octave_value tmp = do_index_op (idx.front (), auto_add); |
173 | 173 |
174 if (! error_state) | 174 if (! error_state) |
175 { | 175 { |
176 const Cell tcell = tmp.cell_value (); | 176 const Cell tcell = tmp.cell_value (); |
177 | 177 |
178 if (tcell.length () == 1) | 178 if (tcell.length () == 1) |
179 retval = tcell(0,0); | 179 retval = tcell(0,0); |
180 else | 180 else |
181 retval = octave_value (octave_value_list (tcell), true); | 181 retval = octave_value (octave_value_list (tcell), true); |
182 } | 182 } |
183 } | 183 } |
184 break; | 184 break; |
185 | 185 |
186 case '.': | 186 case '.': |
187 { | 187 { |
188 std::string nm = type_name (); | 188 std::string nm = type_name (); |
189 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | 189 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
190 } | 190 } |
191 break; | 191 break; |
192 | 192 |
193 default: | 193 default: |
194 panic_impossible (); | 194 panic_impossible (); |
204 return retval; | 204 return retval; |
205 } | 205 } |
206 | 206 |
207 octave_value | 207 octave_value |
208 octave_cell::subsasgn (const std::string& type, | 208 octave_cell::subsasgn (const std::string& type, |
209 const std::list<octave_value_list>& idx, | 209 const std::list<octave_value_list>& idx, |
210 const octave_value& rhs) | 210 const octave_value& rhs) |
211 { | 211 { |
212 octave_value retval; | 212 octave_value retval; |
213 | 213 |
214 int n = type.length (); | 214 int n = type.length (); |
215 | 215 |
224 } | 224 } |
225 | 225 |
226 if (n > 1) | 226 if (n > 1) |
227 { | 227 { |
228 switch (type[0]) | 228 switch (type[0]) |
229 { | 229 { |
230 case '(': | 230 case '(': |
231 { | 231 { |
232 if (is_empty () && type[1] == '.') | 232 if (is_empty () && type[1] == '.') |
233 { | 233 { |
234 // Allow conversion of empty cell array to some other | 234 // Allow conversion of empty cell array to some other |
235 // type in cases like | 235 // type in cases like |
236 // | 236 // |
237 // x = []; x(i).f = rhs | 237 // x = []; x(i).f = rhs |
238 | 238 |
239 octave_value tmp = octave_value::empty_conv (type, rhs); | 239 octave_value tmp = octave_value::empty_conv (type, rhs); |
240 | 240 |
241 return tmp.subsasgn (type, idx, rhs); | 241 return tmp.subsasgn (type, idx, rhs); |
242 } | 242 } |
243 else | 243 else |
244 { | 244 { |
245 octave_value tmp = do_index_op (idx.front (), true); | 245 octave_value tmp = do_index_op (idx.front (), true); |
246 | 246 |
247 if (! tmp.is_defined ()) | 247 if (! tmp.is_defined ()) |
248 tmp = octave_value::empty_conv (type.substr (1), rhs); | 248 tmp = octave_value::empty_conv (type.substr (1), rhs); |
249 | 249 |
250 if (! error_state) | 250 if (! error_state) |
251 { | 251 { |
252 std::list<octave_value_list> next_idx (idx); | 252 std::list<octave_value_list> next_idx (idx); |
253 | 253 |
254 next_idx.erase (next_idx.begin ()); | 254 next_idx.erase (next_idx.begin ()); |
255 | 255 |
256 tmp.make_unique (); | 256 tmp.make_unique (); |
257 | 257 |
258 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | 258 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
259 } | 259 } |
260 } | 260 } |
261 } | 261 } |
262 break; | 262 break; |
263 | 263 |
264 case '{': | 264 case '{': |
265 { | 265 { |
266 matrix.make_unique (); | 266 matrix.make_unique (); |
267 Cell tmpc = matrix.index (idx.front (), true); | 267 Cell tmpc = matrix.index (idx.front (), true); |
268 | 268 |
269 if (! error_state) | 269 if (! error_state) |
270 { | 270 { |
271 std::list<octave_value_list> next_idx (idx); | 271 std::list<octave_value_list> next_idx (idx); |
272 | 272 |
273 next_idx.erase (next_idx.begin ()); | 273 next_idx.erase (next_idx.begin ()); |
274 | 274 |
275 std::string next_type = type.substr (1); | 275 std::string next_type = type.substr (1); |
276 | 276 |
277 if (tmpc.numel () == 1) | 277 if (tmpc.numel () == 1) |
278 { | 278 { |
279 octave_value tmp = tmpc(0); | 279 octave_value tmp = tmpc(0); |
280 tmpc = Cell (); | 280 tmpc = Cell (); |
281 | 281 |
282 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) | 282 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
283 { | 283 { |
284 tmp = octave_value::empty_conv (type.substr (1), rhs); | 284 tmp = octave_value::empty_conv (type.substr (1), rhs); |
285 tmp.make_unique (); // probably a no-op. | 285 tmp.make_unique (); // probably a no-op. |
286 } | 286 } |
287 else | 287 else |
288 // optimization: ignore the copy still stored inside our array. | 288 // optimization: ignore the copy still stored inside our array. |
289 tmp.make_unique (1); | 289 tmp.make_unique (1); |
290 | 290 |
291 if (! error_state) | 291 if (! error_state) |
292 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); | 292 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
293 } | 293 } |
294 else | 294 else |
295 gripe_indexed_cs_list (); | 295 gripe_indexed_cs_list (); |
296 } | 296 } |
297 } | 297 } |
298 break; | 298 break; |
299 | 299 |
300 case '.': | 300 case '.': |
301 { | 301 { |
302 std::string nm = type_name (); | 302 std::string nm = type_name (); |
303 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | 303 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
304 } | 304 } |
305 break; | 305 break; |
306 | 306 |
307 default: | 307 default: |
308 panic_impossible (); | 308 panic_impossible (); |
309 } | 309 } |
310 } | 310 } |
311 | 311 |
312 if (! error_state) | 312 if (! error_state) |
313 { | 313 { |
314 switch (type[0]) | 314 switch (type[0]) |
315 { | 315 { |
316 case '(': | 316 case '(': |
317 { | 317 { |
318 octave_value_list i = idx.front (); | 318 octave_value_list i = idx.front (); |
319 | 319 |
320 if (t_rhs.is_cell ()) | 320 if (t_rhs.is_cell ()) |
321 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | 321 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); |
322 else | 322 else |
323 if (t_rhs.is_null_value ()) | 323 if (t_rhs.is_null_value ()) |
324 octave_base_matrix<Cell>::delete_elements (i); | 324 octave_base_matrix<Cell>::delete_elements (i); |
325 else | 325 else |
326 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | 326 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
327 | 327 |
328 if (! error_state) | 328 if (! error_state) |
329 { | 329 { |
330 count++; | 330 count++; |
331 retval = octave_value (this); | 331 retval = octave_value (this); |
332 } | 332 } |
333 else | 333 else |
334 gripe_failed_assignment (); | 334 gripe_failed_assignment (); |
335 } | 335 } |
336 break; | 336 break; |
337 | 337 |
338 case '{': | 338 case '{': |
339 { | 339 { |
340 octave_value_list idxf = idx.front (); | 340 octave_value_list idxf = idx.front (); |
341 | 341 |
342 if (t_rhs.is_cs_list ()) | 342 if (t_rhs.is_cs_list ()) |
343 { | 343 { |
344 Cell tmp_cell = Cell (t_rhs.list_value ()); | 344 Cell tmp_cell = Cell (t_rhs.list_value ()); |
345 | 345 |
346 // Inquire the proper shape of the RHS. | 346 // Inquire the proper shape of the RHS. |
347 | 347 |
348 dim_vector didx = dims ().redim (idxf.length ()); | 348 dim_vector didx = dims ().redim (idxf.length ()); |
349 for (octave_idx_type k = 0; k < idxf.length (); k++) | 349 for (octave_idx_type k = 0; k < idxf.length (); k++) |
351 | 351 |
352 if (didx.numel () == tmp_cell.numel ()) | 352 if (didx.numel () == tmp_cell.numel ()) |
353 tmp_cell = tmp_cell.reshape (didx); | 353 tmp_cell = tmp_cell.reshape (didx); |
354 | 354 |
355 | 355 |
356 octave_base_matrix<Cell>::assign (idxf, tmp_cell); | 356 octave_base_matrix<Cell>::assign (idxf, tmp_cell); |
357 } | 357 } |
358 else if (idxf.all_scalars () || do_index_op (idxf, true).numel () == 1) | 358 else if (idxf.all_scalars () || do_index_op (idxf, true).numel () == 1) |
359 // Regularize a null matrix if stored into a cell. | 359 // Regularize a null matrix if stored into a cell. |
360 octave_base_matrix<Cell>::assign (idxf, Cell (t_rhs.storable_value ())); | 360 octave_base_matrix<Cell>::assign (idxf, Cell (t_rhs.storable_value ())); |
361 else if (! error_state) | 361 else if (! error_state) |
362 gripe_nonbraced_cs_list_assignment (); | 362 gripe_nonbraced_cs_list_assignment (); |
363 | 363 |
364 if (! error_state) | 364 if (! error_state) |
365 { | 365 { |
366 count++; | 366 count++; |
367 retval = octave_value (this); | 367 retval = octave_value (this); |
368 } | 368 } |
369 else | 369 else |
370 gripe_failed_assignment (); | 370 gripe_failed_assignment (); |
371 } | 371 } |
372 break; | 372 break; |
373 | 373 |
374 case '.': | 374 case '.': |
375 { | 375 { |
376 std::string nm = type_name (); | 376 std::string nm = type_name (); |
377 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | 377 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
378 } | 378 } |
379 break; | 379 break; |
380 | 380 |
381 default: | 381 default: |
382 panic_impossible (); | 382 panic_impossible (); |
383 } | 383 } |
384 } | 384 } |
385 | 385 |
386 return retval; | 386 return retval; |
387 } | 387 } |
388 | 388 |
456 return retval; | 456 return retval; |
457 } | 457 } |
458 | 458 |
459 octave_value | 459 octave_value |
460 octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, | 460 octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, |
461 sortmode mode) const | 461 sortmode mode) const |
462 { | 462 { |
463 octave_value retval; | 463 octave_value retval; |
464 | 464 |
465 if (is_cellstr ()) | 465 if (is_cellstr ()) |
466 { | 466 { |
558 for (octave_idx_type i = 0; i < nel; i++) | 558 for (octave_idx_type i = 0; i < nel; i++) |
559 { | 559 { |
560 string_vector s = matrix(i).all_strings (); | 560 string_vector s = matrix(i).all_strings (); |
561 | 561 |
562 if (error_state) | 562 if (error_state) |
563 return retval; | 563 return retval; |
564 | 564 |
565 octave_idx_type s_len = s.length (); | 565 octave_idx_type s_len = s.length (); |
566 | 566 |
567 n_elts += s_len ? s_len : 1; | 567 n_elts += s_len ? s_len : 1; |
568 | 568 |
569 octave_idx_type s_max_len = s.max_length (); | 569 octave_idx_type s_max_len = s.max_length (); |
570 | 570 |
571 if (s_max_len > max_len) | 571 if (s_max_len > max_len) |
572 max_len = s_max_len; | 572 max_len = s_max_len; |
573 | 573 |
574 strvec_queue.push (s); | 574 strvec_queue.push (s); |
575 } | 575 } |
576 | 576 |
577 retval = string_vector (n_elts); | 577 retval = string_vector (n_elts); |
584 strvec_queue.pop (); | 584 strvec_queue.pop (); |
585 | 585 |
586 octave_idx_type s_len = s.length (); | 586 octave_idx_type s_len = s.length (); |
587 | 587 |
588 if (s_len) | 588 if (s_len) |
589 { | 589 { |
590 for (octave_idx_type j = 0; j < s_len; j++) | 590 for (octave_idx_type j = 0; j < s_len; j++) |
591 { | 591 { |
592 std::string t = s[j]; | 592 std::string t = s[j]; |
593 int t_len = t.length (); | 593 int t_len = t.length (); |
594 | 594 |
595 if (pad && max_len > t_len) | 595 if (pad && max_len > t_len) |
596 t += std::string (max_len - t_len, ' '); | 596 t += std::string (max_len - t_len, ' '); |
597 | 597 |
598 retval[k++] = t; | 598 retval[k++] = t; |
599 } | 599 } |
600 } | 600 } |
601 else if (pad) | 601 else if (pad) |
602 retval[k++] = std::string (max_len, ' '); | 602 retval[k++] = std::string (max_len, ' '); |
603 else | 603 else |
604 retval[k++] = std::string (); | 604 retval[k++] = std::string (); |
605 } | 605 } |
606 | 606 |
607 return retval; | 607 return retval; |
608 } | 608 } |
609 | 609 |
646 { | 646 { |
647 octave_idx_type nr = rows (); | 647 octave_idx_type nr = rows (); |
648 octave_idx_type nc = columns (); | 648 octave_idx_type nc = columns (); |
649 | 649 |
650 if (nr > 0 && nc > 0) | 650 if (nr > 0 && nc > 0) |
651 { | 651 { |
652 indent (os); | 652 indent (os); |
653 os << "{"; | 653 os << "{"; |
654 newline (os); | 654 newline (os); |
655 | 655 |
656 increment_indent_level (); | 656 increment_indent_level (); |
657 | 657 |
658 for (octave_idx_type j = 0; j < nc; j++) | 658 for (octave_idx_type j = 0; j < nc; j++) |
659 { | 659 { |
660 for (octave_idx_type i = 0; i < nr; i++) | 660 for (octave_idx_type i = 0; i < nr; i++) |
661 { | 661 { |
662 octave_quit (); | 662 octave_quit (); |
663 | 663 |
664 std::ostringstream buf; | 664 std::ostringstream buf; |
665 buf << "[" << i+1 << "," << j+1 << "]"; | 665 buf << "[" << i+1 << "," << j+1 << "]"; |
666 | 666 |
667 octave_value val = matrix(i,j); | 667 octave_value val = matrix(i,j); |
668 | 668 |
669 val.print_with_name (os, buf.str ()); | 669 val.print_with_name (os, buf.str ()); |
670 } | 670 } |
671 } | 671 } |
672 | 672 |
673 decrement_indent_level (); | 673 decrement_indent_level (); |
674 | 674 |
675 indent (os); | 675 indent (os); |
676 os << "}"; | 676 os << "}"; |
677 newline (os); | 677 newline (os); |
678 } | 678 } |
679 else | 679 else |
680 { | 680 { |
681 indent (os); | 681 indent (os); |
682 os << "{}"; | 682 os << "{}"; |
683 if (Vprint_empty_dimensions) | 683 if (Vprint_empty_dimensions) |
684 os << "(" << nr << "x" << nc << ")"; | 684 os << "(" << nr << "x" << nc << ")"; |
685 newline (os); | 685 newline (os); |
686 } | 686 } |
687 } | 687 } |
688 else | 688 else |
689 { | 689 { |
690 indent (os); | 690 indent (os); |
691 dim_vector dv = matrix.dims (); | 691 dim_vector dv = matrix.dims (); |
703 if (d.length () > 2) | 703 if (d.length () > 2) |
704 { | 704 { |
705 os << "# ndims: " << d.length () << "\n"; | 705 os << "# ndims: " << d.length () << "\n"; |
706 | 706 |
707 for (int i = 0; i < d.length (); i++) | 707 for (int i = 0; i < d.length (); i++) |
708 os << " " << d (i); | 708 os << " " << d (i); |
709 os << "\n"; | 709 os << "\n"; |
710 | 710 |
711 Cell tmp = cell_value (); | 711 Cell tmp = cell_value (); |
712 | 712 |
713 for (octave_idx_type i = 0; i < d.numel (); i++) | 713 for (octave_idx_type i = 0; i < d.numel (); i++) |
714 { | 714 { |
715 octave_value o_val = tmp.elem (i); | 715 octave_value o_val = tmp.elem (i); |
716 | 716 |
717 // Recurse to print sub-value. | 717 // Recurse to print sub-value. |
718 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); | 718 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
719 | 719 |
720 if (! b) | 720 if (! b) |
721 return os; | 721 return os; |
722 } | 722 } |
723 } | 723 } |
724 else | 724 else |
725 { | 725 { |
726 // Keep this case, rather than use generic code above for backward | 726 // Keep this case, rather than use generic code above for backward |
727 // compatiability. Makes load_ascii much more complex!! | 727 // compatiability. Makes load_ascii much more complex!! |
728 os << "# rows: " << rows () << "\n" | 728 os << "# rows: " << rows () << "\n" |
729 << "# columns: " << columns () << "\n"; | 729 << "# columns: " << columns () << "\n"; |
730 | 730 |
731 Cell tmp = cell_value (); | 731 Cell tmp = cell_value (); |
732 | 732 |
733 for (octave_idx_type j = 0; j < tmp.cols (); j++) | 733 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
734 { | 734 { |
735 for (octave_idx_type i = 0; i < tmp.rows (); i++) | 735 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
736 { | 736 { |
737 octave_value o_val = tmp.elem (i, j); | 737 octave_value o_val = tmp.elem (i, j); |
738 | 738 |
739 // Recurse to print sub-value. | 739 // Recurse to print sub-value. |
740 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); | 740 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
741 | 741 |
742 if (! b) | 742 if (! b) |
743 return os; | 743 return os; |
744 } | 744 } |
745 | 745 |
746 os << "\n"; | 746 os << "\n"; |
747 } | 747 } |
748 } | 748 } |
749 | 749 |
750 return true; | 750 return true; |
751 } | 751 } |
752 | 752 |
766 octave_idx_type val = 0; | 766 octave_idx_type val = 0; |
767 | 767 |
768 if (extract_keyword (is, keywords, kw, val, true)) | 768 if (extract_keyword (is, keywords, kw, val, true)) |
769 { | 769 { |
770 if (kw == "ndims") | 770 if (kw == "ndims") |
771 { | 771 { |
772 int mdims = static_cast<int> (val); | 772 int mdims = static_cast<int> (val); |
773 | 773 |
774 if (mdims >= 0) | 774 if (mdims >= 0) |
775 { | 775 { |
776 dim_vector dv; | 776 dim_vector dv; |
777 dv.resize (mdims); | 777 dv.resize (mdims); |
778 | 778 |
779 for (int i = 0; i < mdims; i++) | 779 for (int i = 0; i < mdims; i++) |
780 is >> dv(i); | 780 is >> dv(i); |
781 | 781 |
782 Cell tmp(dv); | 782 Cell tmp(dv); |
783 | 783 |
784 for (octave_idx_type i = 0; i < dv.numel (); i++) | 784 for (octave_idx_type i = 0; i < dv.numel (); i++) |
785 { | 785 { |
786 octave_value t2; | 786 octave_value t2; |
787 bool dummy; | 787 bool dummy; |
788 | 788 |
789 // recurse to read cell elements | 789 // recurse to read cell elements |
790 std::string nm = read_ascii_data (is, std::string (), | 790 std::string nm = read_ascii_data (is, std::string (), |
791 dummy, t2, i); | 791 dummy, t2, i); |
792 | 792 |
793 if (nm == CELL_ELT_TAG) | 793 if (nm == CELL_ELT_TAG) |
794 { | 794 { |
795 if (is) | 795 if (is) |
796 tmp.elem (i) = t2; | 796 tmp.elem (i) = t2; |
797 } | 797 } |
798 else | 798 else |
799 { | 799 { |
800 error ("load: cell array element had unexpected name"); | 800 error ("load: cell array element had unexpected name"); |
801 success = false; | 801 success = false; |
802 break; | 802 break; |
803 } | 803 } |
804 } | 804 } |
805 | 805 |
806 if (is) | 806 if (is) |
807 matrix = tmp; | 807 matrix = tmp; |
808 else | 808 else |
809 { | 809 { |
810 error ("load: failed to load matrix constant"); | 810 error ("load: failed to load matrix constant"); |
811 success = false; | 811 success = false; |
812 } | 812 } |
813 } | 813 } |
814 else | 814 else |
815 { | 815 { |
816 error ("load: failed to extract number of rows and columns"); | 816 error ("load: failed to extract number of rows and columns"); |
817 success = false; | 817 success = false; |
818 } | 818 } |
819 } | 819 } |
820 else if (kw == "rows") | 820 else if (kw == "rows") |
821 { | 821 { |
822 octave_idx_type nr = val; | 822 octave_idx_type nr = val; |
823 octave_idx_type nc = 0; | 823 octave_idx_type nc = 0; |
824 | 824 |
825 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) | 825 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) |
826 { | 826 { |
827 if (nr > 0 && nc > 0) | 827 if (nr > 0 && nc > 0) |
828 { | 828 { |
829 Cell tmp (nr, nc); | 829 Cell tmp (nr, nc); |
830 | 830 |
831 for (octave_idx_type j = 0; j < nc; j++) | 831 for (octave_idx_type j = 0; j < nc; j++) |
832 { | 832 { |
833 for (octave_idx_type i = 0; i < nr; i++) | 833 for (octave_idx_type i = 0; i < nr; i++) |
834 { | 834 { |
835 octave_value t2; | 835 octave_value t2; |
836 bool dummy; | 836 bool dummy; |
837 | 837 |
838 // recurse to read cell elements | 838 // recurse to read cell elements |
839 std::string nm = read_ascii_data (is, std::string (), | 839 std::string nm = read_ascii_data (is, std::string (), |
840 dummy, t2, i); | 840 dummy, t2, i); |
841 | 841 |
842 if (nm == CELL_ELT_TAG) | 842 if (nm == CELL_ELT_TAG) |
843 { | 843 { |
844 if (is) | 844 if (is) |
845 tmp.elem (i, j) = t2; | 845 tmp.elem (i, j) = t2; |
846 } | 846 } |
847 else | 847 else |
848 { | 848 { |
849 error ("load: cell array element had unexpected name"); | 849 error ("load: cell array element had unexpected name"); |
850 success = false; | 850 success = false; |
851 goto cell_read_error; | 851 goto cell_read_error; |
852 } | 852 } |
853 } | 853 } |
854 } | 854 } |
855 | 855 |
856 cell_read_error: | 856 cell_read_error: |
857 | 857 |
858 if (is) | 858 if (is) |
859 matrix = tmp; | 859 matrix = tmp; |
860 else | 860 else |
861 { | 861 { |
862 error ("load: failed to load cell element"); | 862 error ("load: failed to load cell element"); |
863 success = false; | 863 success = false; |
864 } | 864 } |
865 } | 865 } |
866 else if (nr == 0 || nc == 0) | 866 else if (nr == 0 || nc == 0) |
867 matrix = Cell (nr, nc); | 867 matrix = Cell (nr, nc); |
868 else | 868 else |
869 panic_impossible (); | 869 panic_impossible (); |
870 } | 870 } |
871 else | 871 else |
872 { | 872 { |
873 error ("load: failed to extract number of rows and columns for cell array"); | 873 error ("load: failed to extract number of rows and columns for cell array"); |
874 success = false; | 874 success = false; |
875 } | 875 } |
876 } | 876 } |
877 else | 877 else |
878 panic_impossible (); | 878 panic_impossible (); |
879 } | 879 } |
880 else | 880 else |
881 { | 881 { |
882 error ("load: failed to extract number of rows and columns"); | 882 error ("load: failed to extract number of rows and columns"); |
883 success = false; | 883 success = false; |
908 { | 908 { |
909 octave_value o_val = tmp.elem (i); | 909 octave_value o_val = tmp.elem (i); |
910 | 910 |
911 // Recurse to print sub-value. | 911 // Recurse to print sub-value. |
912 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | 912 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, |
913 save_as_floats); | 913 save_as_floats); |
914 | 914 |
915 if (! b) | 915 if (! b) |
916 return false; | 916 return false; |
917 } | 917 } |
918 | 918 |
919 return true; | 919 return true; |
920 } | 920 } |
921 | 921 |
940 dv.resize (mdims); | 940 dv.resize (mdims); |
941 | 941 |
942 for (int i = 0; i < mdims; i++) | 942 for (int i = 0; i < mdims; i++) |
943 { | 943 { |
944 if (! is.read (reinterpret_cast<char *> (&di), 4)) | 944 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
945 return false; | 945 return false; |
946 if (swap) | 946 if (swap) |
947 swap_bytes<4> (&di); | 947 swap_bytes<4> (&di); |
948 dv(i) = di; | 948 dv(i) = di; |
949 } | 949 } |
950 | 950 |
951 // Convert an array with a single dimension to be a row vector. | 951 // Convert an array with a single dimension to be a row vector. |
952 // Octave should never write files like this, other software | 952 // Octave should never write files like this, other software |
969 bool dummy; | 969 bool dummy; |
970 std::string doc; | 970 std::string doc; |
971 | 971 |
972 // recurse to read cell elements | 972 // recurse to read cell elements |
973 std::string nm = read_binary_data (is, swap, fmt, std::string (), | 973 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
974 dummy, t2, doc); | 974 dummy, t2, doc); |
975 | 975 |
976 if (nm == CELL_ELT_TAG) | 976 if (nm == CELL_ELT_TAG) |
977 { | 977 { |
978 if (is) | 978 if (is) |
979 tmp.elem (i) = t2; | 979 tmp.elem (i) = t2; |
980 } | 980 } |
981 else | 981 else |
982 { | 982 { |
983 error ("load: cell array element had unexpected name"); | 983 error ("load: cell array element had unexpected name"); |
984 success = false; | 984 success = false; |
985 break; | 985 break; |
986 } | 986 } |
987 } | 987 } |
988 | 988 |
989 if (is) | 989 if (is) |
990 matrix = tmp; | 990 matrix = tmp; |
991 else | 991 else |
1043 for (hsize_t i = 0; i < rank; i++) | 1043 for (hsize_t i = 0; i < rank; i++) |
1044 hdims[i] = dv(rank-i-1); | 1044 hdims[i] = dv(rank-i-1); |
1045 | 1045 |
1046 #if HAVE_HDF5_18 | 1046 #if HAVE_HDF5_18 |
1047 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, | 1047 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
1048 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | 1048 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
1049 #else | 1049 #else |
1050 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, | 1050 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
1051 H5P_DEFAULT); | 1051 H5P_DEFAULT); |
1052 #endif | 1052 #endif |
1053 if (size_hid < 0) | 1053 if (size_hid < 0) |
1054 { | 1054 { |
1055 H5Sclose (space_hid); | 1055 H5Sclose (space_hid); |
1056 H5Gclose (data_hid); | 1056 H5Gclose (data_hid); |
1057 return false; | 1057 return false; |
1058 } | 1058 } |
1059 | 1059 |
1060 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | 1060 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
1061 H5P_DEFAULT, hdims) < 0) | 1061 H5P_DEFAULT, hdims) < 0) |
1062 { | 1062 { |
1063 H5Dclose (size_hid); | 1063 H5Dclose (size_hid); |
1064 H5Sclose (space_hid); | 1064 H5Sclose (space_hid); |
1065 H5Gclose (data_hid); | 1065 H5Gclose (data_hid); |
1066 return false; | 1066 return false; |
1081 int digits = static_cast<int> (::floor (::log10 (static_cast<double> (nel)) + 1.0)); | 1081 int digits = static_cast<int> (::floor (::log10 (static_cast<double> (nel)) + 1.0)); |
1082 buf << "_" << std::setw (digits) << std::setfill ('0') << i; | 1082 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
1083 std::string s = buf.str (); | 1083 std::string s = buf.str (); |
1084 | 1084 |
1085 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, | 1085 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
1086 save_as_floats)) | 1086 save_as_floats)) |
1087 { | 1087 { |
1088 H5Gclose (data_hid); | 1088 H5Gclose (data_hid); |
1089 return false; | 1089 return false; |
1090 } | 1090 } |
1091 } | 1091 } |
1092 | 1092 |
1093 H5Gclose (data_hid); | 1093 H5Gclose (data_hid); |
1094 | 1094 |
1095 return true; | 1095 return true; |
1142 dv.resize (hdims[0]); | 1142 dv.resize (hdims[0]); |
1143 | 1143 |
1144 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); | 1144 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
1145 | 1145 |
1146 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | 1146 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
1147 H5P_DEFAULT, tmp) < 0) | 1147 H5P_DEFAULT, tmp) < 0) |
1148 { | 1148 { |
1149 H5Dclose (data_hid); | 1149 H5Dclose (data_hid); |
1150 H5Gclose (group_id); | 1150 H5Gclose (group_id); |
1151 return false; | 1151 return false; |
1152 } | 1152 } |
1176 | 1176 |
1177 for (octave_idx_type i = 0; i < dv.numel (); i++) | 1177 for (octave_idx_type i = 0; i < dv.numel (); i++) |
1178 { | 1178 { |
1179 | 1179 |
1180 if (current_item >= static_cast<int> (num_obj)) | 1180 if (current_item >= static_cast<int> (num_obj)) |
1181 retval2 = -1; | 1181 retval2 = -1; |
1182 else | 1182 else |
1183 retval2 = H5Giterate (loc_id, name, ¤t_item, | 1183 retval2 = H5Giterate (loc_id, name, ¤t_item, |
1184 hdf5_read_next_data, &dsub); | 1184 hdf5_read_next_data, &dsub); |
1185 | 1185 |
1186 if (retval2 <= 0) | 1186 if (retval2 <= 0) |
1187 break; | 1187 break; |
1188 | 1188 |
1189 octave_value ov = dsub.tc; | 1189 octave_value ov = dsub.tc; |
1190 m.elem (i) = ov; | 1190 m.elem (i) = ov; |
1191 | 1191 |
1192 } | 1192 } |
1247 get_dimensions (args(0), "cell", dims); | 1247 get_dimensions (args(0), "cell", dims); |
1248 break; | 1248 break; |
1249 | 1249 |
1250 default: | 1250 default: |
1251 { | 1251 { |
1252 dims.resize (nargin); | 1252 dims.resize (nargin); |
1253 | 1253 |
1254 for (int i = 0; i < nargin; i++) | 1254 for (int i = 0; i < nargin; i++) |
1255 { | 1255 { |
1256 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); | 1256 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); |
1257 | 1257 |
1258 if (error_state) | 1258 if (error_state) |
1259 { | 1259 { |
1260 error ("cell: expecting scalar arguments"); | 1260 error ("cell: expecting scalar arguments"); |
1261 break; | 1261 break; |
1262 } | 1262 } |
1263 } | 1263 } |
1264 } | 1264 } |
1265 break; | 1265 break; |
1266 } | 1266 } |
1267 | 1267 |
1268 if (! error_state) | 1268 if (! error_state) |
1312 if (args.length () == 1) | 1312 if (args.length () == 1) |
1313 { | 1313 { |
1314 octave_value_list tmp = Fiscellstr (args, 1); | 1314 octave_value_list tmp = Fiscellstr (args, 1); |
1315 | 1315 |
1316 if (tmp(0).is_true ()) | 1316 if (tmp(0).is_true ()) |
1317 retval = args(0); | 1317 retval = args(0); |
1318 else | 1318 else |
1319 { | 1319 { |
1320 string_vector s = args(0).all_strings (); | 1320 string_vector s = args(0).all_strings (); |
1321 | 1321 |
1322 if (! error_state) | 1322 if (! error_state) |
1323 retval = (s.is_empty () | 1323 retval = (s.is_empty () |
1324 ? Cell (octave_value (std::string ())) | 1324 ? Cell (octave_value (std::string ())) |
1325 : Cell (s, true)); | 1325 : Cell (s, true)); |
1326 else | 1326 else |
1327 error ("cellstr: expecting argument to be a 2-d character array"); | 1327 error ("cellstr: expecting argument to be a 2-d character array"); |
1328 } | 1328 } |
1329 } | 1329 } |
1330 else | 1330 else |
1331 print_usage (); | 1331 print_usage (); |
1332 | 1332 |
1333 return retval; | 1333 return retval; |
1373 if (nargin == 1) | 1373 if (nargin == 1) |
1374 { | 1374 { |
1375 Octave_map m = args(0).map_value (); | 1375 Octave_map m = args(0).map_value (); |
1376 | 1376 |
1377 if (! error_state) | 1377 if (! error_state) |
1378 { | 1378 { |
1379 dim_vector m_dv = m.dims (); | 1379 dim_vector m_dv = m.dims (); |
1380 | 1380 |
1381 string_vector keys = m.keys (); | 1381 string_vector keys = m.keys (); |
1382 | 1382 |
1383 octave_idx_type num_fields = keys.length (); | 1383 octave_idx_type num_fields = keys.length (); |
1384 | 1384 |
1385 // The resulting dim_vector should have dimensions: | 1385 // The resulting dim_vector should have dimensions: |
1386 // [numel(fields) size(struct)] | 1386 // [numel(fields) size(struct)] |
1387 // except if the struct is a column vector. | 1387 // except if the struct is a column vector. |
1388 | 1388 |
1389 dim_vector result_dv; | 1389 dim_vector result_dv; |
1390 if (m_dv (m_dv.length () - 1) == 1) | 1390 if (m_dv (m_dv.length () - 1) == 1) |
1391 result_dv.resize (m_dv.length ()); | 1391 result_dv.resize (m_dv.length ()); |
1392 else | 1392 else |
1393 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. | 1393 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
1394 | 1394 |
1395 result_dv(0) = num_fields; | 1395 result_dv(0) = num_fields; |
1396 | 1396 |
1397 for (int i = 1; i < result_dv.length (); i++) | 1397 for (int i = 1; i < result_dv.length (); i++) |
1398 result_dv(i) = m_dv(i-1); | 1398 result_dv(i) = m_dv(i-1); |
1399 | 1399 |
1400 Cell c (result_dv); | 1400 Cell c (result_dv); |
1401 | 1401 |
1402 octave_idx_type n_elts = m.numel (); | 1402 octave_idx_type n_elts = m.numel (); |
1403 | 1403 |
1404 for (octave_idx_type j = 0; j < num_fields; j++) | 1404 for (octave_idx_type j = 0; j < num_fields; j++) |
1405 { | 1405 { |
1406 octave_idx_type k = j; | 1406 octave_idx_type k = j; |
1407 | 1407 |
1408 const Cell vals = m.contents (keys(j)); | 1408 const Cell vals = m.contents (keys(j)); |
1409 | 1409 |
1410 for (octave_idx_type i = 0; i < n_elts; i++) | 1410 for (octave_idx_type i = 0; i < n_elts; i++) |
1411 { | 1411 { |
1412 c(k) = vals(i); | 1412 c(k) = vals(i); |
1413 k += num_fields; | 1413 k += num_fields; |
1414 } | 1414 } |
1415 } | 1415 } |
1416 | 1416 |
1417 retval = c; | 1417 retval = c; |
1418 } | 1418 } |
1419 else | 1419 else |
1420 error ("struct2cell: expecting argument to be a cell array"); | 1420 error ("struct2cell: expecting argument to be a cell array"); |
1421 } | 1421 } |
1422 else | 1422 else |
1423 print_usage (); | 1423 print_usage (); |
1424 | 1424 |
1425 return retval; | 1425 return retval; |