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