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