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