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