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