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 { |
4705
|
84 int n = tcell.numel (); |
|
85 |
|
86 octave_value_list lst (n, octave_value ()); |
|
87 |
|
88 for (int i = 0; i < n; i++) |
|
89 { |
|
90 OCTAVE_QUIT; |
|
91 lst(i) = tcell(i); |
|
92 } |
|
93 |
4582
|
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 |
4722
|
283 for (int j = 0; j < nc; j++) |
4358
|
284 { |
4722
|
285 for (int i = 0; i < nr; i++) |
4358
|
286 { |
4722
|
287 string_vector s = matrix(i,j).all_strings (); |
4358
|
288 |
4722
|
289 if (error_state) |
|
290 return retval; |
4358
|
291 |
4722
|
292 n_elts += s.length (); |
4358
|
293 |
4722
|
294 int s_max_len = s.max_length (); |
4358
|
295 |
4722
|
296 if (s_max_len > max_len) |
|
297 max_len = s_max_len; |
4358
|
298 } |
|
299 } |
|
300 |
|
301 retval.resize (n_elts); |
4243
|
302 |
|
303 int k = 0; |
|
304 |
|
305 for (int j = 0; j < nc; j++) |
|
306 { |
|
307 for (int i = 0; i < nr; i++) |
|
308 { |
4358
|
309 string_vector s = matrix(i,j).all_strings (); |
|
310 |
|
311 int n = s.length (); |
4243
|
312 |
4358
|
313 for (int ii = 0; ii < n; ii++) |
|
314 { |
|
315 std::string t = s[ii]; |
|
316 int t_len = t.length (); |
|
317 |
|
318 if (pad && max_len > t_len) |
|
319 t += std::string (max_len - t_len, ' '); |
|
320 |
|
321 retval[k++] = t; |
|
322 } |
4243
|
323 } |
|
324 } |
|
325 |
|
326 return retval; |
|
327 } |
|
328 |
4604
|
329 bool |
|
330 octave_cell::print_as_scalar (void) const |
|
331 { |
|
332 return (ndims () > 2 || numel () == 0); |
|
333 } |
|
334 |
3933
|
335 void |
|
336 octave_cell::print (std::ostream& os, bool) const |
|
337 { |
|
338 print_raw (os); |
|
339 } |
|
340 |
|
341 void |
|
342 octave_cell::print_raw (std::ostream& os, bool) const |
|
343 { |
4587
|
344 int nd = matrix.ndims (); |
4513
|
345 |
4587
|
346 if (nd == 2) |
4513
|
347 { |
|
348 int nr = rows (); |
|
349 int nc = columns (); |
|
350 |
|
351 if (nr > 0 && nc > 0) |
|
352 { |
|
353 indent (os); |
|
354 os << "{"; |
|
355 newline (os); |
|
356 |
|
357 increment_indent_level (); |
|
358 |
|
359 for (int j = 0; j < nc; j++) |
|
360 { |
|
361 for (int i = 0; i < nr; i++) |
|
362 { |
|
363 OCTAVE_QUIT; |
|
364 |
|
365 OSSTREAM buf; |
|
366 buf << "[" << i+1 << "," << j+1 << "]" << OSSTREAM_ENDS; |
3933
|
367 |
4513
|
368 octave_value val = matrix(i,j); |
|
369 |
|
370 val.print_with_name (os, OSSTREAM_STR (buf)); |
|
371 |
|
372 OSSTREAM_FREEZE (buf); |
|
373 } |
|
374 } |
|
375 |
|
376 decrement_indent_level (); |
|
377 |
|
378 indent (os); |
|
379 os << "}"; |
|
380 newline (os); |
|
381 } |
|
382 else |
|
383 { |
|
384 os << "{}"; |
|
385 if (nr > 0 || nc > 0) |
|
386 os << "(" << nr << "x" << nc << ")"; |
|
387 os << "\n"; |
|
388 } |
|
389 } |
|
390 else |
3933
|
391 { |
|
392 indent (os); |
4513
|
393 dim_vector dv = matrix.dims (); |
4587
|
394 os << "{" << dv.str () << " Cell Array}"; |
3933
|
395 newline (os); |
|
396 } |
|
397 } |
|
398 |
4687
|
399 #define CELL_ELT_TAG "<cell-element>" |
|
400 |
|
401 bool |
|
402 octave_cell::save_ascii (std::ostream& os, bool& infnan_warned, |
|
403 bool strip_nan_and_inf) |
|
404 { |
|
405 dim_vector d = dims (); |
|
406 if (d.length () > 2) |
|
407 { |
|
408 os << "# ndims: " << d.length () << "\n"; |
|
409 |
|
410 for (int i=0; i < d.length (); i++) |
|
411 os << " " << d (i); |
|
412 os << "\n"; |
|
413 |
|
414 Cell tmp = cell_value (); |
|
415 |
|
416 for (int i = 0; i < d.numel (); i++) |
|
417 { |
|
418 octave_value o_val = tmp.elem (i); |
|
419 |
|
420 // Recurse to print sub-value. |
|
421 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, infnan_warned, |
|
422 strip_nan_and_inf, 0, 0); |
|
423 |
|
424 if (! b) |
|
425 return os; |
|
426 } |
|
427 } |
|
428 else |
|
429 { |
|
430 // Keep this case, rather than use generic code above for backward |
|
431 // compatiability. Makes load_ascii much more complex!! |
|
432 os << "# rows: " << rows () << "\n" |
|
433 << "# columns: " << columns () << "\n"; |
|
434 |
|
435 Cell tmp = cell_value (); |
|
436 |
|
437 for (int j = 0; j < tmp.cols (); j++) |
|
438 { |
|
439 for (int i = 0; i < tmp.rows (); i++) |
|
440 { |
|
441 octave_value o_val = tmp.elem (i, j); |
|
442 |
|
443 // Recurse to print sub-value. |
|
444 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, |
|
445 infnan_warned, |
|
446 strip_nan_and_inf, 0, 0); |
|
447 |
|
448 if (! b) |
|
449 return os; |
|
450 } |
|
451 |
|
452 os << "\n"; |
|
453 } |
|
454 } |
|
455 |
|
456 return true; |
|
457 } |
|
458 |
|
459 bool |
|
460 octave_cell::load_ascii (std::istream& is) |
|
461 { |
|
462 int mdims = 0; |
|
463 bool success = true; |
|
464 std::streampos pos = is.tellg (); |
|
465 |
|
466 if (extract_keyword (is, "ndims", mdims, true)) |
|
467 { |
|
468 if (mdims >= 0) |
|
469 { |
|
470 dim_vector dv; |
|
471 dv.resize (mdims); |
|
472 |
|
473 for (int i = 0; i < mdims; i++) |
|
474 is >> dv(i); |
|
475 |
|
476 Cell tmp(dv); |
|
477 |
|
478 for (int i = 0; i < dv.numel (); i++) |
|
479 { |
|
480 octave_value t2; |
|
481 bool dummy; |
|
482 |
|
483 // recurse to read cell elements |
|
484 std::string nm = read_ascii_data (is, std::string (), |
|
485 dummy, t2, count); |
|
486 |
|
487 if (nm == CELL_ELT_TAG) |
|
488 { |
|
489 if (is) |
|
490 tmp.elem (i) = t2; |
|
491 } |
|
492 else |
|
493 { |
|
494 error ("load: cell array element had unexpected name"); |
|
495 success = false; |
|
496 break; |
|
497 } |
|
498 } |
|
499 |
|
500 if (is) |
|
501 matrix = tmp; |
|
502 else |
|
503 { |
|
504 error ("load: failed to load matrix constant"); |
|
505 success = false; |
|
506 } |
|
507 } |
|
508 else |
|
509 { |
|
510 error ("load: failed to extract number of rows and columns"); |
|
511 success = false; |
|
512 } |
|
513 |
|
514 } |
|
515 else |
|
516 { |
|
517 int nr = 0; |
|
518 int nc = 0; |
|
519 |
|
520 // re-read the same line again |
|
521 is.clear (); |
|
522 is.seekg (pos); |
|
523 |
|
524 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
525 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
526 { |
|
527 if (nr > 0 && nc > 0) |
|
528 { |
|
529 Cell tmp (nr, nc); |
|
530 |
|
531 for (int j = 0; j < nc; j++) |
|
532 { |
|
533 for (int i = 0; i < nr; i++) |
|
534 { |
|
535 octave_value t2; |
|
536 bool dummy; |
|
537 |
|
538 // recurse to read cell elements |
|
539 std::string nm = read_ascii_data (is, std::string (), |
|
540 dummy, t2, count); |
|
541 |
|
542 if (nm == CELL_ELT_TAG) |
|
543 { |
|
544 if (is) |
|
545 tmp.elem (i, j) = t2; |
|
546 } |
|
547 else |
|
548 { |
|
549 error ("load: cell array element had unexpected name"); |
|
550 success = false; |
|
551 goto cell_read_error; |
|
552 } |
|
553 } |
|
554 } |
|
555 |
|
556 cell_read_error: |
|
557 |
|
558 if (is) |
|
559 matrix = tmp; |
|
560 else |
|
561 { |
|
562 error ("load: failed to load cell element"); |
|
563 success = false; |
|
564 } |
|
565 } |
|
566 else if (nr == 0 || nc == 0) |
|
567 matrix = Cell (nr, nc); |
|
568 else |
|
569 panic_impossible (); |
|
570 } |
|
571 else { |
|
572 error ("load: failed to extract number of rows and columns for cell array"); |
|
573 success = false; |
|
574 } |
|
575 } |
|
576 |
|
577 return success; |
|
578 } |
|
579 |
|
580 bool |
|
581 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) |
|
582 { |
|
583 dim_vector d = dims (); |
|
584 if (d.length () < 1) |
|
585 return false; |
|
586 |
|
587 // Use negative value for ndims |
|
588 FOUR_BYTE_INT di = - d.length(); |
|
589 os.write (X_CAST (char *, &di), 4); |
|
590 for (int i=0; i < d.length (); i++) |
|
591 { |
|
592 di = d(i); |
|
593 os.write (X_CAST (char *, &di), 4); |
|
594 } |
|
595 |
|
596 Cell tmp = cell_value (); |
|
597 |
|
598 for (int i = 0; i < d.numel (); i++) |
|
599 { |
|
600 octave_value o_val = tmp.elem (i); |
|
601 |
|
602 // Recurse to print sub-value. |
|
603 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, |
|
604 save_as_floats); |
|
605 |
|
606 if (! b) |
|
607 return false; |
|
608 } |
|
609 |
|
610 return true; |
|
611 } |
|
612 |
|
613 bool |
|
614 octave_cell::load_binary (std::istream& is, bool swap, |
|
615 oct_mach_info::float_format fmt) |
|
616 { |
|
617 bool success = true; |
|
618 FOUR_BYTE_INT mdims; |
|
619 if (! is.read (X_CAST (char *, &mdims), 4)) |
|
620 return false; |
|
621 if (swap) |
|
622 swap_4_bytes (X_CAST (char *, &mdims)); |
|
623 if (mdims >= 0) |
|
624 return false; |
|
625 |
|
626 mdims = -mdims; |
|
627 FOUR_BYTE_INT di; |
|
628 dim_vector dv; |
|
629 dv.resize (mdims); |
|
630 |
|
631 for (int i = 0; i < mdims; i++) |
|
632 { |
|
633 if (! is.read (X_CAST (char *, &di), 4)) |
|
634 return false; |
|
635 if (swap) |
|
636 swap_4_bytes (X_CAST (char *, &di)); |
|
637 dv(i) = di; |
|
638 } |
|
639 |
|
640 int nel = dv.numel (); |
|
641 Cell tmp(dv); |
|
642 |
|
643 for (int i = 0; i < nel; i++) |
|
644 { |
|
645 octave_value t2; |
|
646 bool dummy; |
|
647 std::string doc; |
|
648 |
|
649 // recurse to read cell elements |
|
650 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
|
651 dummy, t2, doc); |
|
652 |
|
653 if (nm == CELL_ELT_TAG) |
|
654 { |
|
655 if (is) |
|
656 tmp.elem (i) = t2; |
|
657 } |
|
658 else |
|
659 { |
|
660 error ("load: cell array element had unexpected name"); |
|
661 success = false; |
|
662 break; |
|
663 } |
|
664 } |
|
665 |
|
666 if (is) |
|
667 matrix = tmp; |
|
668 else |
|
669 { |
|
670 error ("load: failed to load matrix constant"); |
|
671 success = false; |
|
672 } |
|
673 |
|
674 return success; |
|
675 } |
|
676 |
|
677 #if defined (HAVE_HDF5) |
|
678 bool |
|
679 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) |
|
680 { |
|
681 hsize_t dimens[3]; |
|
682 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
|
683 |
|
684 data_hid = H5Gcreate (loc_id, name, 0); |
|
685 if (data_hid < 0) return false; |
|
686 |
|
687 // Have to save rows/columns since can't have a dataset of groups.... |
|
688 space_hid = H5Screate_simple (0, dimens, (hsize_t*) 0); |
|
689 if (space_hid < 0) |
|
690 { |
|
691 H5Gclose (data_hid); |
|
692 return false; |
|
693 } |
|
694 |
|
695 size_hid = H5Dcreate (data_hid, "rows", H5T_NATIVE_INT, space_hid, |
|
696 H5P_DEFAULT); |
|
697 if (size_hid < 0) |
|
698 { |
|
699 H5Sclose (space_hid); |
|
700 H5Gclose (data_hid); |
|
701 return false; |
|
702 } |
|
703 |
|
704 int rc = rows(); |
|
705 if (! H5Dwrite (size_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
706 H5P_DEFAULT, (void*) &rc) < 0) |
|
707 { |
|
708 H5Dclose (size_hid); |
|
709 H5Sclose (space_hid); |
|
710 H5Gclose (data_hid); |
|
711 return false; |
|
712 } |
|
713 H5Dclose (size_hid); |
|
714 |
|
715 size_hid = H5Dcreate (data_hid, "columns", H5T_NATIVE_INT, space_hid, |
|
716 H5P_DEFAULT); |
|
717 if (size_hid < 0) |
|
718 { |
|
719 H5Sclose (space_hid); |
|
720 H5Gclose (data_hid); |
|
721 return false; |
|
722 } |
|
723 |
|
724 rc = columns(); |
|
725 if (! H5Dwrite (size_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
726 H5P_DEFAULT, (void*) &rc) < 0) |
|
727 { |
|
728 H5Dclose (size_hid); |
|
729 H5Sclose (space_hid); |
|
730 H5Gclose (data_hid); |
|
731 return false; |
|
732 } |
|
733 H5Dclose (size_hid); |
|
734 H5Sclose (space_hid); |
|
735 |
|
736 // recursively add each element of the cell to this group |
|
737 Cell tmp = cell_value (); |
|
738 |
|
739 for (int j = 0; j < tmp.cols (); j++) |
|
740 { |
|
741 for (int i = 0; i < tmp.rows (); i++) |
|
742 { |
|
743 char s[20]; |
|
744 sprintf (s, "_%d", (i + j * tmp.rows ())); |
|
745 |
|
746 if (! add_hdf5_data(data_hid, tmp.elem (i, j), s, "", false, |
|
747 save_as_floats)) |
|
748 { |
|
749 H5Gclose (data_hid); |
|
750 return false; |
|
751 } |
|
752 } |
|
753 } |
|
754 |
|
755 H5Gclose (data_hid); |
|
756 return true; |
|
757 } |
|
758 |
|
759 bool |
|
760 octave_cell::load_hdf5 (hid_t loc_id, const char *name, |
|
761 bool have_h5giterate_bug) |
|
762 { |
|
763 bool retval = false; |
|
764 hid_t group_id = H5Gopen (loc_id, name); |
|
765 |
|
766 if (group_id < 0) |
|
767 return false; |
|
768 |
|
769 hid_t data_hid = H5Dopen (group_id, "rows"); |
|
770 hid_t space_hid = H5Dget_space (data_hid); |
|
771 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
772 if (rank != 0) |
|
773 { |
|
774 H5Dclose(data_hid); |
|
775 H5Gclose(group_id); |
|
776 return false; |
|
777 } |
|
778 |
|
779 int nr; |
|
780 if (H5Dread (data_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
781 H5P_DEFAULT, (void *) &nr) < 0) |
|
782 { |
|
783 H5Dclose(data_hid); |
|
784 H5Gclose(group_id); |
|
785 return false; |
|
786 } |
|
787 H5Dclose (data_hid); |
|
788 |
|
789 data_hid = H5Dopen (group_id, "columns"); |
|
790 space_hid = H5Dget_space (data_hid); |
|
791 rank = H5Sget_simple_extent_ndims (space_hid); |
|
792 if (rank != 0) |
|
793 { |
|
794 H5Dclose(data_hid); |
|
795 H5Gclose(group_id); |
|
796 return false; |
|
797 } |
|
798 |
|
799 int nc; |
|
800 if (H5Dread (data_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
801 H5P_DEFAULT, (void *) &nc) < 0) |
|
802 { |
|
803 H5Dclose(data_hid); |
|
804 H5Gclose(group_id); |
|
805 return false; |
|
806 } |
|
807 H5Dclose (data_hid); |
|
808 H5Gclose (group_id); |
|
809 |
|
810 hdf5_callback_data dsub; |
|
811 |
|
812 herr_t retval2 = -1; |
|
813 Cell m (nr, nc); |
|
814 int current_item = 0; |
|
815 if (have_h5giterate_bug) |
|
816 current_item = 2; // Skip row/columns items in group |
|
817 |
4696
|
818 #ifdef HAVE_H5GGET_NUM_OBJS |
|
819 hsize_t num_obj = 0; |
|
820 H5Gget_num_objs (loc_id, &num_obj); |
|
821 #endif |
|
822 |
4687
|
823 for (int j = 0; j < nc; j++) |
|
824 { |
|
825 for (int i = 0; i < nr; i++) |
|
826 { |
4696
|
827 |
|
828 #ifdef HAVE_H5GGET_NUM_OBJS |
|
829 if (current_item >= static_cast<int> (num_obj)) |
|
830 retval2 = -1; |
|
831 else |
|
832 #endif |
|
833 retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
834 hdf5_read_next_data, &dsub); |
|
835 |
4687
|
836 if (retval2 <= 0) |
|
837 break; |
|
838 |
|
839 octave_value ov = dsub.tc; |
|
840 m.elem (i, j) = ov; |
|
841 |
|
842 if (have_h5giterate_bug) |
|
843 current_item++; // H5Giterate returned the last index processed |
|
844 |
|
845 } |
|
846 if (retval2 <= 0) |
|
847 break; |
|
848 } |
|
849 |
|
850 if (retval2 >= 0) |
|
851 { |
|
852 matrix = m; |
|
853 retval = true; |
|
854 } |
|
855 |
|
856 return retval; |
|
857 } |
|
858 #endif |
|
859 |
3354
|
860 DEFUN (iscell, args, , |
3448
|
861 "-*- texinfo -*-\n\ |
|
862 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
863 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
864 false.\n\ |
|
865 @end deftypefn") |
3354
|
866 { |
|
867 octave_value retval; |
|
868 |
|
869 if (args.length () == 1) |
|
870 retval = args(0).is_cell (); |
|
871 else |
|
872 print_usage ("iscell"); |
|
873 |
|
874 return retval; |
|
875 } |
|
876 |
|
877 DEFUN (cell, args, , |
3448
|
878 "-*- texinfo -*-\n\ |
|
879 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
880 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
881 Create a new cell array object. If invoked with a single scalar\n\ |
|
882 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
883 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
884 them to be the number of rows and columns. If given a vector with two\n\ |
|
885 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
886 rows and columns, respectively.\n\ |
|
887 @end deftypefn") |
3354
|
888 { |
|
889 octave_value retval; |
|
890 |
|
891 int nargin = args.length (); |
|
892 |
4563
|
893 dim_vector dims; |
|
894 |
3354
|
895 switch (nargin) |
|
896 { |
4563
|
897 case 0: |
|
898 dims = dim_vector (0, 0); |
3354
|
899 break; |
|
900 |
4563
|
901 case 1: |
|
902 get_dimensions (args(0), "cell", dims); |
3354
|
903 break; |
|
904 |
|
905 default: |
4563
|
906 { |
|
907 dims.resize (nargin); |
|
908 |
|
909 for (int i = 0; i < nargin; i++) |
|
910 { |
|
911 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); |
|
912 |
|
913 if (error_state) |
|
914 { |
|
915 error ("cell: expecting scalar arguments"); |
|
916 break; |
|
917 } |
|
918 } |
|
919 } |
3354
|
920 break; |
|
921 } |
|
922 |
4563
|
923 if (! error_state) |
|
924 { |
|
925 int ndim = dims.length (); |
|
926 |
|
927 check_dimensions (dims, "cell"); |
|
928 |
|
929 if (! error_state) |
|
930 { |
|
931 switch (ndim) |
|
932 { |
|
933 case 1: |
|
934 retval = Cell (dims(0), dims(0), Matrix ()); |
|
935 break; |
|
936 |
|
937 default: |
|
938 retval = Cell (dims, Matrix ()); |
|
939 break; |
|
940 } |
|
941 } |
|
942 } |
|
943 |
3354
|
944 return retval; |
|
945 } |
|
946 |
4358
|
947 DEFUN (cellstr, args, , |
|
948 "-*- texinfo -*-\n\ |
|
949 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ |
|
950 Create a new cell array object from the elements of the string\n\ |
|
951 array @var{string}.\n\ |
|
952 @end deftypefn") |
|
953 { |
|
954 octave_value retval; |
|
955 |
|
956 if (args.length () == 1) |
|
957 { |
|
958 string_vector s = args(0).all_strings (); |
|
959 |
|
960 if (! error_state) |
|
961 retval = Cell (s); |
|
962 else |
|
963 error ("cellstr: expecting argument to be a string"); |
|
964 } |
|
965 else |
|
966 print_usage ("cellstr"); |
|
967 |
|
968 return retval; |
|
969 } |
|
970 |
4610
|
971 DEFUN (iscellstr, args, , |
|
972 "-*- texinfo -*-\n\ |
|
973 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ |
|
974 Return true if every element of the cell array @var{cell} is a\n\ |
|
975 character string\n\ |
|
976 @end deftypefn") |
|
977 { |
4699
|
978 octave_value retval; |
4610
|
979 |
|
980 if (args.length () == 1) |
|
981 { |
4699
|
982 retval = true; |
|
983 |
4610
|
984 octave_value arg = args (0); |
|
985 |
|
986 if (arg.is_cell ()) |
|
987 { |
|
988 Cell c = args(0).cell_value (); |
|
989 |
|
990 if (! error_state) |
|
991 { |
|
992 for (int i = 0; i < c.length (); i++) |
|
993 { |
|
994 if (! c(i).is_string ()) |
|
995 { |
|
996 retval = false; |
|
997 break; |
|
998 } |
|
999 } |
|
1000 } |
|
1001 else |
|
1002 error ("iscellstr: expecting argument to be a cell"); |
|
1003 } |
|
1004 else |
|
1005 retval = false; |
|
1006 } |
|
1007 else |
|
1008 print_usage ("iscellstr"); |
|
1009 |
|
1010 return retval; |
|
1011 } |
|
1012 |
3353
|
1013 /* |
|
1014 ;;; Local Variables: *** |
|
1015 ;;; mode: C++ *** |
|
1016 ;;; End: *** |
|
1017 */ |