3353
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3353
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
5850
|
28 #include <iomanip> |
3503
|
29 #include <iostream> |
5765
|
30 #include <sstream> |
5164
|
31 #include <vector> |
3353
|
32 |
5360
|
33 #include "Array-util.h" |
|
34 #include "byte-swap.h" |
3353
|
35 #include "lo-utils.h" |
4153
|
36 #include "quit.h" |
3353
|
37 |
|
38 #include "defun.h" |
|
39 #include "error.h" |
|
40 #include "ov-cell.h" |
3354
|
41 #include "oct-obj.h" |
3353
|
42 #include "unwind-prot.h" |
3354
|
43 #include "utils.h" |
3928
|
44 #include "ov-base-mat.h" |
|
45 #include "ov-base-mat.cc" |
|
46 #include "ov-re-mat.h" |
|
47 #include "ov-scalar.h" |
5360
|
48 #include "pr-output.h" |
|
49 #include "ov-scalar.h" |
3928
|
50 |
4687
|
51 #include "ls-oct-ascii.h" |
|
52 #include "ls-oct-binary.h" |
|
53 #include "ls-hdf5.h" |
|
54 #include "ls-utils.h" |
|
55 |
3928
|
56 template class octave_base_matrix<Cell>; |
3353
|
57 |
|
58 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
|
59 |
4612
|
60 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); |
3353
|
61 |
4994
|
62 octave_value_list |
4247
|
63 octave_cell::subsref (const std::string& type, |
4994
|
64 const std::list<octave_value_list>& idx, int nargout) |
3933
|
65 { |
4994
|
66 octave_value_list retval; |
3933
|
67 |
|
68 switch (type[0]) |
|
69 { |
|
70 case '(': |
4994
|
71 retval(0) = do_index_op (idx.front ()); |
3933
|
72 break; |
|
73 |
|
74 case '{': |
|
75 { |
|
76 octave_value tmp = do_index_op (idx.front ()); |
|
77 |
4582
|
78 if (! error_state) |
3933
|
79 { |
4582
|
80 Cell tcell = tmp.cell_value (); |
|
81 |
|
82 if (tcell.length () == 1) |
4994
|
83 retval(0) = tcell(0,0); |
4582
|
84 else |
|
85 { |
5275
|
86 octave_idx_type n = tcell.numel (); |
4705
|
87 |
|
88 octave_value_list lst (n, octave_value ()); |
|
89 |
5275
|
90 for (octave_idx_type i = 0; i < n; i++) |
4705
|
91 { |
|
92 OCTAVE_QUIT; |
|
93 lst(i) = tcell(i); |
|
94 } |
|
95 |
4994
|
96 retval(0) = octave_value (lst, true); |
4582
|
97 } |
3933
|
98 } |
|
99 } |
|
100 break; |
|
101 |
|
102 case '.': |
|
103 { |
|
104 std::string nm = type_name (); |
|
105 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
106 } |
|
107 break; |
|
108 |
|
109 default: |
|
110 panic_impossible (); |
|
111 } |
|
112 |
5775
|
113 // FIXME -- perhaps there should be an |
4994
|
114 // octave_value_list::next_subsref member function? See also |
|
115 // octave_user_function::subsref. |
|
116 |
|
117 if (idx.size () > 1) |
|
118 retval = retval(0).next_subsref (nargout, type, idx); |
|
119 |
|
120 return retval; |
3933
|
121 } |
|
122 |
|
123 octave_value |
4247
|
124 octave_cell::subsasgn (const std::string& type, |
4219
|
125 const std::list<octave_value_list>& idx, |
3933
|
126 const octave_value& rhs) |
|
127 { |
|
128 octave_value retval; |
|
129 |
|
130 int n = type.length (); |
|
131 |
|
132 octave_value t_rhs = rhs; |
|
133 |
|
134 if (n > 1) |
|
135 { |
|
136 switch (type[0]) |
|
137 { |
|
138 case '(': |
|
139 { |
|
140 octave_value tmp = do_index_op (idx.front (), true); |
|
141 |
|
142 if (! tmp.is_defined ()) |
|
143 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
144 |
|
145 if (! error_state) |
|
146 { |
4219
|
147 std::list<octave_value_list> next_idx (idx); |
3933
|
148 |
4219
|
149 next_idx.erase (next_idx.begin ()); |
3933
|
150 |
4362
|
151 tmp.make_unique (); |
|
152 |
3933
|
153 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
154 } |
|
155 } |
|
156 break; |
|
157 |
|
158 case '{': |
|
159 { |
|
160 octave_value tmp = do_index_op (idx.front (), true); |
|
161 |
|
162 if (! tmp.is_defined ()) |
|
163 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
164 |
4362
|
165 if (! error_state) |
|
166 { |
|
167 Cell tcell = tmp.cell_value (); |
3933
|
168 |
4362
|
169 if (! error_state && tcell.length () == 1) |
|
170 { |
|
171 tmp = tcell(0,0); |
3933
|
172 |
4362
|
173 std::list<octave_value_list> next_idx (idx); |
|
174 |
|
175 next_idx.erase (next_idx.begin ()); |
3933
|
176 |
4362
|
177 tmp.make_unique (); |
3933
|
178 |
5927
|
179 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
4519
|
180 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
181 |
|
182 if (! error_state) |
|
183 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
4362
|
184 } |
3933
|
185 } |
|
186 } |
|
187 break; |
|
188 |
|
189 case '.': |
|
190 { |
|
191 std::string nm = type_name (); |
|
192 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
193 } |
|
194 break; |
|
195 |
|
196 default: |
|
197 panic_impossible (); |
|
198 } |
|
199 } |
|
200 |
3940
|
201 if (! error_state) |
3933
|
202 { |
3940
|
203 switch (type[0]) |
|
204 { |
|
205 case '(': |
|
206 { |
|
207 octave_value_list i = idx.front (); |
3933
|
208 |
3940
|
209 if (t_rhs.is_cell ()) |
|
210 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); |
|
211 else |
4941
|
212 if (t_rhs.is_empty ()) |
|
213 octave_base_matrix<Cell>::assign (i, Cell()); |
|
214 else |
|
215 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
216 |
5759
|
217 count++; |
|
218 retval = octave_value (this); |
3940
|
219 } |
|
220 break; |
3933
|
221 |
3940
|
222 case '{': |
|
223 { |
|
224 octave_value_list i = idx.front (); |
3933
|
225 |
5846
|
226 if (t_rhs.is_cs_list ()) |
|
227 { |
|
228 Cell tmp_cell = Cell (t_rhs.list_value ()); |
|
229 |
|
230 // FIXME -- shouldn't care if the dimensions of the |
|
231 // RHS don't match the dimensions of the subscripted |
|
232 // LHS. |
|
233 |
|
234 octave_base_matrix<Cell>::assign (i, tmp_cell); |
|
235 } |
|
236 else |
|
237 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
238 |
5759
|
239 count++; |
|
240 retval = octave_value (this); |
3940
|
241 } |
|
242 break; |
3933
|
243 |
3940
|
244 case '.': |
|
245 { |
|
246 std::string nm = type_name (); |
|
247 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
248 } |
|
249 break; |
3933
|
250 |
3940
|
251 default: |
|
252 panic_impossible (); |
|
253 } |
3933
|
254 } |
|
255 |
|
256 return retval; |
|
257 } |
|
258 |
3353
|
259 void |
|
260 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
|
261 { |
3928
|
262 if (rhs.is_cell ()) |
|
263 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); |
3353
|
264 else |
3928
|
265 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353
|
266 } |
|
267 |
4791
|
268 size_t |
|
269 octave_cell::byte_size (void) const |
|
270 { |
|
271 size_t retval = 0; |
|
272 |
5275
|
273 for (octave_idx_type i = 0; i < numel (); i++) |
4791
|
274 retval += matrix(i).byte_size (); |
|
275 |
|
276 return retval; |
|
277 } |
|
278 |
3933
|
279 octave_value_list |
|
280 octave_cell::list_value (void) const |
|
281 { |
|
282 octave_value_list retval; |
|
283 |
5275
|
284 octave_idx_type nr = rows (); |
|
285 octave_idx_type nc = columns (); |
3933
|
286 |
|
287 if (nr == 1 && nc > 0) |
|
288 { |
|
289 retval.resize (nc); |
|
290 |
5275
|
291 for (octave_idx_type i = 0; i < nc; i++) |
3933
|
292 retval(i) = matrix(0,i); |
|
293 } |
|
294 else if (nc == 1 && nr > 0) |
|
295 { |
|
296 retval.resize (nr); |
|
297 |
5275
|
298 for (octave_idx_type i = 0; i < nr; i++) |
3933
|
299 retval(i) = matrix(i,0); |
|
300 } |
|
301 else |
|
302 error ("invalid conversion from cell array to list"); |
|
303 |
|
304 return retval; |
|
305 } |
|
306 |
4243
|
307 string_vector |
5715
|
308 octave_cell::all_strings (bool pad) const |
4243
|
309 { |
4358
|
310 string_vector retval; |
|
311 |
5275
|
312 octave_idx_type nr = rows (); |
|
313 octave_idx_type nc = columns (); |
4243
|
314 |
4358
|
315 int n_elts = 0; |
|
316 |
5275
|
317 octave_idx_type max_len = 0; |
4358
|
318 |
5275
|
319 for (octave_idx_type j = 0; j < nc; j++) |
4358
|
320 { |
5275
|
321 for (octave_idx_type i = 0; i < nr; i++) |
4358
|
322 { |
4722
|
323 string_vector s = matrix(i,j).all_strings (); |
4358
|
324 |
4722
|
325 if (error_state) |
|
326 return retval; |
4358
|
327 |
4722
|
328 n_elts += s.length (); |
4358
|
329 |
5275
|
330 octave_idx_type s_max_len = s.max_length (); |
4358
|
331 |
4722
|
332 if (s_max_len > max_len) |
|
333 max_len = s_max_len; |
4358
|
334 } |
|
335 } |
|
336 |
|
337 retval.resize (n_elts); |
4243
|
338 |
5275
|
339 octave_idx_type k = 0; |
4243
|
340 |
5275
|
341 for (octave_idx_type j = 0; j < nc; j++) |
4243
|
342 { |
5275
|
343 for (octave_idx_type i = 0; i < nr; i++) |
4243
|
344 { |
4358
|
345 string_vector s = matrix(i,j).all_strings (); |
|
346 |
|
347 int n = s.length (); |
4243
|
348 |
5275
|
349 for (octave_idx_type ii = 0; ii < n; ii++) |
5715
|
350 { |
|
351 std::string t = s[ii]; |
|
352 int t_len = t.length (); |
|
353 |
|
354 if (pad && max_len > t_len) |
|
355 t += std::string (max_len - t_len, ' '); |
|
356 |
|
357 retval[k++] = t; |
|
358 } |
4243
|
359 } |
|
360 } |
|
361 |
|
362 return retval; |
|
363 } |
|
364 |
4604
|
365 bool |
|
366 octave_cell::print_as_scalar (void) const |
|
367 { |
|
368 return (ndims () > 2 || numel () == 0); |
|
369 } |
|
370 |
3933
|
371 void |
|
372 octave_cell::print (std::ostream& os, bool) const |
|
373 { |
|
374 print_raw (os); |
|
375 } |
|
376 |
|
377 void |
|
378 octave_cell::print_raw (std::ostream& os, bool) const |
|
379 { |
4587
|
380 int nd = matrix.ndims (); |
4513
|
381 |
4587
|
382 if (nd == 2) |
4513
|
383 { |
5275
|
384 octave_idx_type nr = rows (); |
|
385 octave_idx_type nc = columns (); |
4513
|
386 |
|
387 if (nr > 0 && nc > 0) |
|
388 { |
|
389 indent (os); |
|
390 os << "{"; |
|
391 newline (os); |
|
392 |
|
393 increment_indent_level (); |
|
394 |
5275
|
395 for (octave_idx_type j = 0; j < nc; j++) |
4513
|
396 { |
5275
|
397 for (octave_idx_type i = 0; i < nr; i++) |
4513
|
398 { |
|
399 OCTAVE_QUIT; |
|
400 |
5765
|
401 std::ostringstream buf; |
|
402 buf << "[" << i+1 << "," << j+1 << "]"; |
3933
|
403 |
4513
|
404 octave_value val = matrix(i,j); |
|
405 |
5765
|
406 val.print_with_name (os, buf.str ()); |
4513
|
407 } |
|
408 } |
|
409 |
|
410 decrement_indent_level (); |
|
411 |
|
412 indent (os); |
|
413 os << "}"; |
|
414 newline (os); |
|
415 } |
|
416 else |
|
417 { |
|
418 os << "{}"; |
5360
|
419 if (Vprint_empty_dimensions) |
4513
|
420 os << "(" << nr << "x" << nc << ")"; |
|
421 os << "\n"; |
|
422 } |
|
423 } |
|
424 else |
3933
|
425 { |
|
426 indent (os); |
4513
|
427 dim_vector dv = matrix.dims (); |
4587
|
428 os << "{" << dv.str () << " Cell Array}"; |
3933
|
429 newline (os); |
|
430 } |
|
431 } |
|
432 |
4687
|
433 #define CELL_ELT_TAG "<cell-element>" |
|
434 |
|
435 bool |
|
436 octave_cell::save_ascii (std::ostream& os, bool& infnan_warned, |
|
437 bool strip_nan_and_inf) |
|
438 { |
|
439 dim_vector d = dims (); |
|
440 if (d.length () > 2) |
|
441 { |
|
442 os << "# ndims: " << d.length () << "\n"; |
|
443 |
5275
|
444 for (int i = 0; i < d.length (); i++) |
4687
|
445 os << " " << d (i); |
|
446 os << "\n"; |
|
447 |
|
448 Cell tmp = cell_value (); |
|
449 |
5275
|
450 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687
|
451 { |
|
452 octave_value o_val = tmp.elem (i); |
|
453 |
|
454 // Recurse to print sub-value. |
|
455 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, infnan_warned, |
|
456 strip_nan_and_inf, 0, 0); |
|
457 |
|
458 if (! b) |
|
459 return os; |
|
460 } |
|
461 } |
|
462 else |
|
463 { |
|
464 // Keep this case, rather than use generic code above for backward |
|
465 // compatiability. Makes load_ascii much more complex!! |
|
466 os << "# rows: " << rows () << "\n" |
|
467 << "# columns: " << columns () << "\n"; |
|
468 |
|
469 Cell tmp = cell_value (); |
|
470 |
5275
|
471 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
4687
|
472 { |
5275
|
473 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
4687
|
474 { |
|
475 octave_value o_val = tmp.elem (i, j); |
|
476 |
|
477 // Recurse to print sub-value. |
|
478 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, |
|
479 infnan_warned, |
|
480 strip_nan_and_inf, 0, 0); |
|
481 |
|
482 if (! b) |
|
483 return os; |
|
484 } |
|
485 |
|
486 os << "\n"; |
|
487 } |
|
488 } |
|
489 |
|
490 return true; |
|
491 } |
|
492 |
|
493 bool |
|
494 octave_cell::load_ascii (std::istream& is) |
|
495 { |
|
496 bool success = true; |
5099
|
497 |
|
498 string_vector keywords(2); |
4687
|
499 |
5099
|
500 keywords[0] = "ndims"; |
|
501 keywords[1] = "rows"; |
|
502 |
|
503 std::string kw; |
5275
|
504 octave_idx_type val = 0; |
5099
|
505 |
|
506 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
507 { |
5099
|
508 if (kw == "ndims") |
4687
|
509 { |
5275
|
510 int mdims = static_cast<int> (val); |
4687
|
511 |
5099
|
512 if (mdims >= 0) |
|
513 { |
|
514 dim_vector dv; |
|
515 dv.resize (mdims); |
|
516 |
|
517 for (int i = 0; i < mdims; i++) |
|
518 is >> dv(i); |
4687
|
519 |
5099
|
520 Cell tmp(dv); |
4687
|
521 |
5275
|
522 for (octave_idx_type i = 0; i < dv.numel (); i++) |
5099
|
523 { |
|
524 octave_value t2; |
|
525 bool dummy; |
|
526 |
|
527 // recurse to read cell elements |
|
528 std::string nm = read_ascii_data (is, std::string (), |
5759
|
529 dummy, t2, i); |
4687
|
530 |
5099
|
531 if (nm == CELL_ELT_TAG) |
|
532 { |
|
533 if (is) |
|
534 tmp.elem (i) = t2; |
|
535 } |
|
536 else |
|
537 { |
|
538 error ("load: cell array element had unexpected name"); |
|
539 success = false; |
|
540 break; |
|
541 } |
|
542 } |
4687
|
543 |
5099
|
544 if (is) |
|
545 matrix = tmp; |
4687
|
546 else |
|
547 { |
5099
|
548 error ("load: failed to load matrix constant"); |
4687
|
549 success = false; |
|
550 } |
|
551 } |
|
552 else |
|
553 { |
5099
|
554 error ("load: failed to extract number of rows and columns"); |
|
555 success = false; |
|
556 } |
|
557 } |
|
558 else if (kw == "rows") |
|
559 { |
5275
|
560 octave_idx_type nr = val; |
|
561 octave_idx_type nc = 0; |
5099
|
562 |
|
563 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
564 { |
|
565 if (nr > 0 && nc > 0) |
|
566 { |
|
567 Cell tmp (nr, nc); |
|
568 |
5275
|
569 for (octave_idx_type j = 0; j < nc; j++) |
5099
|
570 { |
5275
|
571 for (octave_idx_type i = 0; i < nr; i++) |
5099
|
572 { |
|
573 octave_value t2; |
|
574 bool dummy; |
|
575 |
|
576 // recurse to read cell elements |
|
577 std::string nm = read_ascii_data (is, std::string (), |
5759
|
578 dummy, t2, i); |
5099
|
579 |
|
580 if (nm == CELL_ELT_TAG) |
|
581 { |
|
582 if (is) |
|
583 tmp.elem (i, j) = t2; |
|
584 } |
|
585 else |
|
586 { |
|
587 error ("load: cell array element had unexpected name"); |
|
588 success = false; |
|
589 goto cell_read_error; |
|
590 } |
|
591 } |
|
592 } |
|
593 |
|
594 cell_read_error: |
|
595 |
|
596 if (is) |
|
597 matrix = tmp; |
|
598 else |
|
599 { |
|
600 error ("load: failed to load cell element"); |
|
601 success = false; |
|
602 } |
|
603 } |
|
604 else if (nr == 0 || nc == 0) |
|
605 matrix = Cell (nr, nc); |
|
606 else |
|
607 panic_impossible (); |
|
608 } |
|
609 else |
|
610 { |
|
611 error ("load: failed to extract number of rows and columns for cell array"); |
4687
|
612 success = false; |
|
613 } |
|
614 } |
|
615 else |
5099
|
616 panic_impossible (); |
4687
|
617 } |
|
618 else |
|
619 { |
5099
|
620 error ("load: failed to extract number of rows and columns"); |
|
621 success = false; |
4687
|
622 } |
|
623 |
|
624 return success; |
|
625 } |
|
626 |
|
627 bool |
|
628 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) |
|
629 { |
|
630 dim_vector d = dims (); |
|
631 if (d.length () < 1) |
|
632 return false; |
|
633 |
|
634 // Use negative value for ndims |
5828
|
635 int32_t di = - d.length(); |
5760
|
636 os.write (reinterpret_cast<char *> (&di), 4); |
5275
|
637 for (int i = 0; i < d.length (); i++) |
4687
|
638 { |
|
639 di = d(i); |
5760
|
640 os.write (reinterpret_cast<char *> (&di), 4); |
4687
|
641 } |
|
642 |
|
643 Cell tmp = cell_value (); |
|
644 |
5275
|
645 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687
|
646 { |
|
647 octave_value o_val = tmp.elem (i); |
|
648 |
|
649 // Recurse to print sub-value. |
|
650 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, |
|
651 save_as_floats); |
|
652 |
|
653 if (! b) |
|
654 return false; |
|
655 } |
|
656 |
|
657 return true; |
|
658 } |
|
659 |
|
660 bool |
|
661 octave_cell::load_binary (std::istream& is, bool swap, |
|
662 oct_mach_info::float_format fmt) |
|
663 { |
|
664 bool success = true; |
5828
|
665 int32_t mdims; |
5760
|
666 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687
|
667 return false; |
|
668 if (swap) |
4944
|
669 swap_bytes<4> (&mdims); |
4687
|
670 if (mdims >= 0) |
|
671 return false; |
|
672 |
|
673 mdims = -mdims; |
5828
|
674 int32_t di; |
4687
|
675 dim_vector dv; |
|
676 dv.resize (mdims); |
|
677 |
|
678 for (int i = 0; i < mdims; i++) |
|
679 { |
5760
|
680 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687
|
681 return false; |
|
682 if (swap) |
4944
|
683 swap_bytes<4> (&di); |
4687
|
684 dv(i) = di; |
|
685 } |
|
686 |
5157
|
687 // Convert an array with a single dimension to be a row vector. |
|
688 // Octave should never write files like this, other software |
|
689 // might. |
|
690 |
|
691 if (mdims == 1) |
|
692 { |
|
693 mdims = 2; |
|
694 dv.resize (mdims); |
|
695 dv(1) = dv(0); |
|
696 dv(0) = 1; |
|
697 } |
|
698 |
5275
|
699 octave_idx_type nel = dv.numel (); |
4687
|
700 Cell tmp(dv); |
|
701 |
5275
|
702 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
703 { |
|
704 octave_value t2; |
|
705 bool dummy; |
|
706 std::string doc; |
|
707 |
|
708 // recurse to read cell elements |
|
709 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
|
710 dummy, t2, doc); |
|
711 |
|
712 if (nm == CELL_ELT_TAG) |
|
713 { |
|
714 if (is) |
|
715 tmp.elem (i) = t2; |
|
716 } |
|
717 else |
|
718 { |
|
719 error ("load: cell array element had unexpected name"); |
|
720 success = false; |
|
721 break; |
|
722 } |
|
723 } |
|
724 |
|
725 if (is) |
|
726 matrix = tmp; |
|
727 else |
|
728 { |
|
729 error ("load: failed to load matrix constant"); |
|
730 success = false; |
|
731 } |
|
732 |
|
733 return success; |
|
734 } |
|
735 |
|
736 #if defined (HAVE_HDF5) |
4815
|
737 |
4687
|
738 bool |
|
739 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) |
|
740 { |
4814
|
741 dim_vector dv = dims (); |
4837
|
742 int empty = save_hdf5_empty (loc_id, name, dv); |
|
743 if (empty) |
|
744 return (empty > 0); |
|
745 |
4815
|
746 hsize_t rank = dv.length (); |
4687
|
747 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
|
748 |
|
749 data_hid = H5Gcreate (loc_id, name, 0); |
4815
|
750 |
|
751 if (data_hid < 0) |
|
752 return false; |
4687
|
753 |
4814
|
754 // Have to save cell array shape, since can't have a |
|
755 // dataset of groups.... |
4815
|
756 |
|
757 space_hid = H5Screate_simple (1, &rank, 0); |
|
758 |
4687
|
759 if (space_hid < 0) |
|
760 { |
|
761 H5Gclose (data_hid); |
|
762 return false; |
|
763 } |
|
764 |
5351
|
765 OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); |
4814
|
766 |
|
767 // Octave uses column-major, while HDF5 uses row-major ordering |
4933
|
768 for (hsize_t i = 0; i < rank; i++) |
4815
|
769 hdims[i] = dv(rank-i-1); |
4814
|
770 |
5351
|
771 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
4687
|
772 H5P_DEFAULT); |
|
773 if (size_hid < 0) |
|
774 { |
|
775 H5Sclose (space_hid); |
|
776 H5Gclose (data_hid); |
|
777 return false; |
|
778 } |
|
779 |
5351
|
780 if (! H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815
|
781 H5P_DEFAULT, hdims) < 0) |
4687
|
782 { |
|
783 H5Dclose (size_hid); |
|
784 H5Sclose (space_hid); |
|
785 H5Gclose (data_hid); |
|
786 return false; |
|
787 } |
4815
|
788 |
4687
|
789 H5Dclose (size_hid); |
|
790 H5Sclose (space_hid); |
|
791 |
4815
|
792 // Recursively add each element of the cell to this group. |
|
793 |
4687
|
794 Cell tmp = cell_value (); |
5850
|
795 |
|
796 octave_idx_type nel = dv.numel (); |
|
797 |
|
798 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
799 { |
5765
|
800 std::ostringstream buf; |
5850
|
801 int digits = static_cast<int> (floor (log10 (nel) + 1.0)); |
|
802 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
5765
|
803 std::string s = buf.str (); |
4687
|
804 |
5850
|
805 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
|
806 save_as_floats)) |
4814
|
807 { |
|
808 H5Gclose (data_hid); |
|
809 return false; |
4687
|
810 } |
|
811 } |
|
812 |
|
813 H5Gclose (data_hid); |
4815
|
814 |
4687
|
815 return true; |
|
816 } |
|
817 |
|
818 bool |
|
819 octave_cell::load_hdf5 (hid_t loc_id, const char *name, |
|
820 bool have_h5giterate_bug) |
|
821 { |
|
822 bool retval = false; |
4837
|
823 |
|
824 dim_vector dv; |
|
825 int empty = load_hdf5_empty (loc_id, name, dv); |
|
826 if (empty > 0) |
|
827 matrix.resize(dv); |
|
828 if (empty) |
|
829 return (empty > 0); |
|
830 |
4687
|
831 hid_t group_id = H5Gopen (loc_id, name); |
|
832 |
|
833 if (group_id < 0) |
|
834 return false; |
|
835 |
4814
|
836 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687
|
837 hid_t space_hid = H5Dget_space (data_hid); |
|
838 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
4814
|
839 if (rank != 1) |
4687
|
840 { |
4837
|
841 H5Dclose (data_hid); |
|
842 H5Gclose (group_id); |
4687
|
843 return false; |
|
844 } |
|
845 |
4814
|
846 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
847 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
4815
|
848 |
4814
|
849 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687
|
850 |
4815
|
851 // Octave uses column-major, while HDF5 uses row-major ordering. |
|
852 |
4814
|
853 dv.resize (hdims[0]); |
4815
|
854 |
5351
|
855 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
4814
|
856 |
5351
|
857 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815
|
858 H5P_DEFAULT, tmp) < 0) |
4687
|
859 { |
4837
|
860 H5Dclose (data_hid); |
|
861 H5Gclose (group_id); |
4687
|
862 return false; |
|
863 } |
4815
|
864 |
4687
|
865 H5Dclose (data_hid); |
|
866 H5Gclose (group_id); |
|
867 |
4815
|
868 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814
|
869 dv(j) = tmp[i]; |
|
870 |
4687
|
871 hdf5_callback_data dsub; |
|
872 |
|
873 herr_t retval2 = -1; |
4815
|
874 |
4814
|
875 Cell m (dv); |
4815
|
876 |
4687
|
877 int current_item = 0; |
4815
|
878 |
4687
|
879 if (have_h5giterate_bug) |
4815
|
880 current_item = 1; // Skip dims items in group. |
4687
|
881 |
4696
|
882 #ifdef HAVE_H5GGET_NUM_OBJS |
|
883 hsize_t num_obj = 0; |
5060
|
884 group_id = H5Gopen (loc_id, name); |
|
885 H5Gget_num_objs (group_id, &num_obj); |
|
886 H5Gclose (group_id); |
4696
|
887 #endif |
|
888 |
5275
|
889 for (octave_idx_type i = 0; i < dv.numel (); i++) |
4687
|
890 { |
4696
|
891 |
|
892 #ifdef HAVE_H5GGET_NUM_OBJS |
4814
|
893 if (current_item >= static_cast<int> (num_obj)) |
|
894 retval2 = -1; |
|
895 else |
4696
|
896 #endif |
4814
|
897 retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
898 hdf5_read_next_data, &dsub); |
|
899 |
4687
|
900 if (retval2 <= 0) |
|
901 break; |
4814
|
902 |
|
903 octave_value ov = dsub.tc; |
|
904 m.elem (i) = ov; |
|
905 |
|
906 if (have_h5giterate_bug) |
4815
|
907 current_item++; // H5Giterate returned the last index processed. |
4814
|
908 |
4687
|
909 } |
|
910 |
|
911 if (retval2 >= 0) |
|
912 { |
|
913 matrix = m; |
|
914 retval = true; |
|
915 } |
|
916 |
|
917 return retval; |
|
918 } |
4815
|
919 |
4687
|
920 #endif |
|
921 |
3354
|
922 DEFUN (iscell, args, , |
3448
|
923 "-*- texinfo -*-\n\ |
|
924 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
925 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
926 false.\n\ |
|
927 @end deftypefn") |
3354
|
928 { |
|
929 octave_value retval; |
|
930 |
|
931 if (args.length () == 1) |
|
932 retval = args(0).is_cell (); |
|
933 else |
5823
|
934 print_usage (); |
3354
|
935 |
|
936 return retval; |
|
937 } |
|
938 |
|
939 DEFUN (cell, args, , |
3448
|
940 "-*- texinfo -*-\n\ |
|
941 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
942 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
943 Create a new cell array object. If invoked with a single scalar\n\ |
|
944 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
945 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
946 them to be the number of rows and columns. If given a vector with two\n\ |
|
947 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
948 rows and columns, respectively.\n\ |
|
949 @end deftypefn") |
3354
|
950 { |
|
951 octave_value retval; |
|
952 |
|
953 int nargin = args.length (); |
|
954 |
4563
|
955 dim_vector dims; |
|
956 |
3354
|
957 switch (nargin) |
|
958 { |
4563
|
959 case 0: |
|
960 dims = dim_vector (0, 0); |
3354
|
961 break; |
|
962 |
4563
|
963 case 1: |
|
964 get_dimensions (args(0), "cell", dims); |
3354
|
965 break; |
|
966 |
|
967 default: |
4563
|
968 { |
|
969 dims.resize (nargin); |
|
970 |
|
971 for (int i = 0; i < nargin; i++) |
|
972 { |
|
973 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); |
|
974 |
|
975 if (error_state) |
|
976 { |
|
977 error ("cell: expecting scalar arguments"); |
|
978 break; |
|
979 } |
|
980 } |
|
981 } |
3354
|
982 break; |
|
983 } |
|
984 |
4563
|
985 if (! error_state) |
|
986 { |
|
987 int ndim = dims.length (); |
|
988 |
|
989 check_dimensions (dims, "cell"); |
|
990 |
|
991 if (! error_state) |
|
992 { |
|
993 switch (ndim) |
|
994 { |
|
995 case 1: |
|
996 retval = Cell (dims(0), dims(0), Matrix ()); |
|
997 break; |
|
998 |
|
999 default: |
|
1000 retval = Cell (dims, Matrix ()); |
|
1001 break; |
|
1002 } |
|
1003 } |
|
1004 } |
|
1005 |
3354
|
1006 return retval; |
|
1007 } |
|
1008 |
4610
|
1009 DEFUN (iscellstr, args, , |
|
1010 "-*- texinfo -*-\n\ |
|
1011 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ |
|
1012 Return true if every element of the cell array @var{cell} is a\n\ |
|
1013 character string\n\ |
|
1014 @end deftypefn") |
|
1015 { |
4699
|
1016 octave_value retval; |
4610
|
1017 |
|
1018 if (args.length () == 1) |
|
1019 { |
4699
|
1020 retval = true; |
|
1021 |
4610
|
1022 octave_value arg = args (0); |
|
1023 |
|
1024 if (arg.is_cell ()) |
|
1025 { |
|
1026 Cell c = args(0).cell_value (); |
|
1027 |
|
1028 if (! error_state) |
|
1029 { |
|
1030 for (int i = 0; i < c.length (); i++) |
|
1031 { |
|
1032 if (! c(i).is_string ()) |
|
1033 { |
|
1034 retval = false; |
|
1035 break; |
|
1036 } |
|
1037 } |
|
1038 } |
|
1039 else |
4817
|
1040 retval = false; |
4610
|
1041 } |
|
1042 else |
|
1043 retval = false; |
|
1044 } |
|
1045 else |
5823
|
1046 print_usage (); |
4610
|
1047 |
|
1048 return retval; |
|
1049 } |
|
1050 |
4817
|
1051 // Note that since Fcellstr calls Fiscellstr, we need to have |
|
1052 // Fiscellstr defined first (to provide a declaration) and also we |
|
1053 // should keep it in the same file (so we don't have to provide a |
|
1054 // declaration) and so we don't have to use feval to call it. |
|
1055 |
|
1056 DEFUN (cellstr, args, , |
|
1057 "-*- texinfo -*-\n\ |
|
1058 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ |
|
1059 Create a new cell array object from the elements of the string\n\ |
|
1060 array @var{string}.\n\ |
|
1061 @end deftypefn") |
|
1062 { |
|
1063 octave_value retval; |
|
1064 |
|
1065 if (args.length () == 1) |
|
1066 { |
|
1067 octave_value_list tmp = Fiscellstr (args, 1); |
|
1068 |
|
1069 if (tmp(0).is_true ()) |
|
1070 retval = args(0); |
|
1071 else |
|
1072 { |
|
1073 string_vector s = args(0).all_strings (); |
|
1074 |
|
1075 if (! error_state) |
5805
|
1076 retval = Cell (s, true); |
4817
|
1077 else |
|
1078 error ("cellstr: expecting argument to be a 2-d character array"); |
|
1079 } |
|
1080 } |
|
1081 else |
5823
|
1082 print_usage (); |
4817
|
1083 |
|
1084 return retval; |
|
1085 } |
|
1086 |
4762
|
1087 DEFUN (struct2cell, args, , |
|
1088 "-*- texinfo -*-\n\ |
|
1089 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ |
|
1090 Create a new cell array from the objects stored in the struct object.\n\ |
4764
|
1091 If @var{f} is the number of fields in the structure, the resulting\n\ |
|
1092 cell array will have a dimension vector corresponding to\n\ |
|
1093 @code{[@var{F} size(@var{S})]}.\n\ |
5642
|
1094 @seealso{cell2struct, fieldnames}\n\ |
|
1095 @end deftypefn") |
4762
|
1096 { |
|
1097 octave_value retval; |
4764
|
1098 |
4762
|
1099 int nargin = args.length (); |
4764
|
1100 |
4762
|
1101 if (nargin == 1) |
|
1102 { |
4764
|
1103 Octave_map m = args(0).map_value (); |
|
1104 |
4762
|
1105 if (! error_state) |
|
1106 { |
|
1107 dim_vector m_dv = m.dims (); |
4764
|
1108 |
4762
|
1109 string_vector keys = m.keys (); |
4764
|
1110 |
5275
|
1111 octave_idx_type fields_numel = keys.length (); |
4764
|
1112 |
4762
|
1113 // The resulting dim_vector should have dimensions: |
|
1114 // [numel(fields) size(struct)] |
4764
|
1115 |
4762
|
1116 dim_vector result_dv; |
4764
|
1117 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
|
1118 |
|
1119 result_dv(0) = fields_numel; |
4762
|
1120 |
|
1121 for (int i = 1; i < result_dv.length (); i++) |
|
1122 result_dv(i) = m_dv(i-1); |
4764
|
1123 |
|
1124 // Squeeze to be sure that a (3,1) vector doesn't get turned |
|
1125 // into a (3,3,1) vector. |
|
1126 |
|
1127 result_dv = result_dv.squeeze (); |
|
1128 |
4762
|
1129 Cell c (result_dv); |
4764
|
1130 |
|
1131 // Use ra_idx both for counting and for assignments, so |
|
1132 // ra_idx(0) will both contain fields_numel for each call to |
|
1133 // increment_index and j for each assignment. |
|
1134 |
5275
|
1135 Array<octave_idx_type> ra_idx (result_dv.length (), 0); |
4764
|
1136 ra_idx(0) = fields_numel; |
4762
|
1137 |
5275
|
1138 for (octave_idx_type i = 0; i < m_dv.numel (); i++) |
4762
|
1139 { |
5275
|
1140 for (octave_idx_type j = 0; j < fields_numel; j++) |
4762
|
1141 { |
|
1142 ra_idx(0) = j; |
4764
|
1143 |
4762
|
1144 Cell c_tmp = m.contents (keys(j)); |
4764
|
1145 |
|
1146 if (c_tmp.length () > 1) // Is a cell. |
4762
|
1147 c(ra_idx) = c_tmp; |
4764
|
1148 else if (c_tmp.length () == 1) // Get octave_value. |
4762
|
1149 c(ra_idx) = c_tmp(0); |
4764
|
1150 else |
|
1151 c(ra_idx) = Cell (); |
|
1152 |
4762
|
1153 ra_idx(0) = fields_numel; |
|
1154 } |
|
1155 |
4764
|
1156 increment_index (ra_idx, result_dv); |
4762
|
1157 } |
|
1158 |
|
1159 retval = c; |
|
1160 } |
|
1161 else |
|
1162 error ("struct2cell: expecting argument to be a cell array"); |
|
1163 } |
|
1164 else |
5823
|
1165 print_usage (); |
4764
|
1166 |
4762
|
1167 return retval; |
|
1168 } |
|
1169 |
5900
|
1170 mxArray * |
|
1171 octave_cell::as_mxArray (void) const |
|
1172 { |
|
1173 mxArray *retval = new mxArray (dims ()); |
|
1174 |
|
1175 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); |
|
1176 |
|
1177 int nel = numel (); |
|
1178 |
|
1179 const octave_value *p = matrix.data (); |
|
1180 |
|
1181 for (int i = 0; i < nel; i++) |
|
1182 elts[i] = new mxArray (p[i]); |
|
1183 |
|
1184 return retval; |
|
1185 } |
|
1186 |
3353
|
1187 /* |
|
1188 ;;; Local Variables: *** |
|
1189 ;;; mode: C++ *** |
|
1190 ;;; End: *** |
|
1191 */ |