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