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 |
5157
|
671 // Convert an array with a single dimension to be a row vector. |
|
672 // Octave should never write files like this, other software |
|
673 // might. |
|
674 |
|
675 if (mdims == 1) |
|
676 { |
|
677 mdims = 2; |
|
678 dv.resize (mdims); |
|
679 dv(1) = dv(0); |
|
680 dv(0) = 1; |
|
681 } |
|
682 |
4687
|
683 int nel = dv.numel (); |
|
684 Cell tmp(dv); |
|
685 |
|
686 for (int i = 0; i < nel; i++) |
|
687 { |
|
688 octave_value t2; |
|
689 bool dummy; |
|
690 std::string doc; |
|
691 |
|
692 // recurse to read cell elements |
|
693 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
|
694 dummy, t2, doc); |
|
695 |
|
696 if (nm == CELL_ELT_TAG) |
|
697 { |
|
698 if (is) |
|
699 tmp.elem (i) = t2; |
|
700 } |
|
701 else |
|
702 { |
|
703 error ("load: cell array element had unexpected name"); |
|
704 success = false; |
|
705 break; |
|
706 } |
|
707 } |
|
708 |
|
709 if (is) |
|
710 matrix = tmp; |
|
711 else |
|
712 { |
|
713 error ("load: failed to load matrix constant"); |
|
714 success = false; |
|
715 } |
|
716 |
|
717 return success; |
|
718 } |
|
719 |
|
720 #if defined (HAVE_HDF5) |
4815
|
721 |
4687
|
722 bool |
|
723 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) |
|
724 { |
4814
|
725 dim_vector dv = dims (); |
4837
|
726 int empty = save_hdf5_empty (loc_id, name, dv); |
|
727 if (empty) |
|
728 return (empty > 0); |
|
729 |
4815
|
730 hsize_t rank = dv.length (); |
4687
|
731 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
|
732 |
|
733 data_hid = H5Gcreate (loc_id, name, 0); |
4815
|
734 |
|
735 if (data_hid < 0) |
|
736 return false; |
4687
|
737 |
4814
|
738 // Have to save cell array shape, since can't have a |
|
739 // dataset of groups.... |
4815
|
740 |
|
741 space_hid = H5Screate_simple (1, &rank, 0); |
|
742 |
4687
|
743 if (space_hid < 0) |
|
744 { |
|
745 H5Gclose (data_hid); |
|
746 return false; |
|
747 } |
|
748 |
4814
|
749 OCTAVE_LOCAL_BUFFER (int, hdims, rank); |
|
750 |
|
751 // Octave uses column-major, while HDF5 uses row-major ordering |
4933
|
752 for (hsize_t i = 0; i < rank; i++) |
4815
|
753 hdims[i] = dv(rank-i-1); |
4814
|
754 |
|
755 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_INT, space_hid, |
4687
|
756 H5P_DEFAULT); |
|
757 if (size_hid < 0) |
|
758 { |
|
759 H5Sclose (space_hid); |
|
760 H5Gclose (data_hid); |
|
761 return false; |
|
762 } |
|
763 |
|
764 if (! H5Dwrite (size_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
4815
|
765 H5P_DEFAULT, hdims) < 0) |
4687
|
766 { |
|
767 H5Dclose (size_hid); |
|
768 H5Sclose (space_hid); |
|
769 H5Gclose (data_hid); |
|
770 return false; |
|
771 } |
4815
|
772 |
4687
|
773 H5Dclose (size_hid); |
|
774 H5Sclose (space_hid); |
|
775 |
4815
|
776 // Recursively add each element of the cell to this group. |
|
777 |
4687
|
778 Cell tmp = cell_value (); |
|
779 |
4814
|
780 for (int i = 0; i < dv.numel (); i++) |
4687
|
781 { |
4814
|
782 char s[20]; |
|
783 sprintf (s, "_%d", i); |
4687
|
784 |
4814
|
785 if (! add_hdf5_data(data_hid, tmp.elem (i), s, "", false, |
|
786 save_as_floats)) |
|
787 { |
|
788 H5Gclose (data_hid); |
|
789 return false; |
4687
|
790 } |
|
791 } |
|
792 |
|
793 H5Gclose (data_hid); |
4815
|
794 |
4687
|
795 return true; |
|
796 } |
|
797 |
|
798 bool |
|
799 octave_cell::load_hdf5 (hid_t loc_id, const char *name, |
|
800 bool have_h5giterate_bug) |
|
801 { |
|
802 bool retval = false; |
4837
|
803 |
|
804 dim_vector dv; |
|
805 int empty = load_hdf5_empty (loc_id, name, dv); |
|
806 if (empty > 0) |
|
807 matrix.resize(dv); |
|
808 if (empty) |
|
809 return (empty > 0); |
|
810 |
4687
|
811 hid_t group_id = H5Gopen (loc_id, name); |
|
812 |
|
813 if (group_id < 0) |
|
814 return false; |
|
815 |
4814
|
816 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687
|
817 hid_t space_hid = H5Dget_space (data_hid); |
|
818 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
4814
|
819 if (rank != 1) |
4687
|
820 { |
4837
|
821 H5Dclose (data_hid); |
|
822 H5Gclose (group_id); |
4687
|
823 return false; |
|
824 } |
|
825 |
4814
|
826 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
827 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
4815
|
828 |
4814
|
829 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687
|
830 |
4815
|
831 // Octave uses column-major, while HDF5 uses row-major ordering. |
|
832 |
4814
|
833 dv.resize (hdims[0]); |
4815
|
834 |
4814
|
835 OCTAVE_LOCAL_BUFFER (int, tmp, hdims[0]); |
|
836 |
4687
|
837 if (H5Dread (data_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
4815
|
838 H5P_DEFAULT, tmp) < 0) |
4687
|
839 { |
4837
|
840 H5Dclose (data_hid); |
|
841 H5Gclose (group_id); |
4687
|
842 return false; |
|
843 } |
4815
|
844 |
4687
|
845 H5Dclose (data_hid); |
|
846 H5Gclose (group_id); |
|
847 |
4815
|
848 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814
|
849 dv(j) = tmp[i]; |
|
850 |
4687
|
851 hdf5_callback_data dsub; |
|
852 |
|
853 herr_t retval2 = -1; |
4815
|
854 |
4814
|
855 Cell m (dv); |
4815
|
856 |
4687
|
857 int current_item = 0; |
4815
|
858 |
4687
|
859 if (have_h5giterate_bug) |
4815
|
860 current_item = 1; // Skip dims items in group. |
4687
|
861 |
4696
|
862 #ifdef HAVE_H5GGET_NUM_OBJS |
|
863 hsize_t num_obj = 0; |
5060
|
864 group_id = H5Gopen (loc_id, name); |
|
865 H5Gget_num_objs (group_id, &num_obj); |
|
866 H5Gclose (group_id); |
4696
|
867 #endif |
|
868 |
4814
|
869 for (int i = 0; i < dv.numel (); i++) |
4687
|
870 { |
4696
|
871 |
|
872 #ifdef HAVE_H5GGET_NUM_OBJS |
4814
|
873 if (current_item >= static_cast<int> (num_obj)) |
|
874 retval2 = -1; |
|
875 else |
4696
|
876 #endif |
4814
|
877 retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
878 hdf5_read_next_data, &dsub); |
|
879 |
4687
|
880 if (retval2 <= 0) |
|
881 break; |
4814
|
882 |
|
883 octave_value ov = dsub.tc; |
|
884 m.elem (i) = ov; |
|
885 |
|
886 if (have_h5giterate_bug) |
4815
|
887 current_item++; // H5Giterate returned the last index processed. |
4814
|
888 |
4687
|
889 } |
|
890 |
|
891 if (retval2 >= 0) |
|
892 { |
|
893 matrix = m; |
|
894 retval = true; |
|
895 } |
|
896 |
|
897 return retval; |
|
898 } |
4815
|
899 |
4687
|
900 #endif |
|
901 |
3354
|
902 DEFUN (iscell, args, , |
3448
|
903 "-*- texinfo -*-\n\ |
|
904 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
905 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
906 false.\n\ |
|
907 @end deftypefn") |
3354
|
908 { |
|
909 octave_value retval; |
|
910 |
|
911 if (args.length () == 1) |
|
912 retval = args(0).is_cell (); |
|
913 else |
|
914 print_usage ("iscell"); |
|
915 |
|
916 return retval; |
|
917 } |
|
918 |
|
919 DEFUN (cell, args, , |
3448
|
920 "-*- texinfo -*-\n\ |
|
921 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
922 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
923 Create a new cell array object. If invoked with a single scalar\n\ |
|
924 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
925 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
926 them to be the number of rows and columns. If given a vector with two\n\ |
|
927 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
928 rows and columns, respectively.\n\ |
|
929 @end deftypefn") |
3354
|
930 { |
|
931 octave_value retval; |
|
932 |
|
933 int nargin = args.length (); |
|
934 |
4563
|
935 dim_vector dims; |
|
936 |
3354
|
937 switch (nargin) |
|
938 { |
4563
|
939 case 0: |
|
940 dims = dim_vector (0, 0); |
3354
|
941 break; |
|
942 |
4563
|
943 case 1: |
|
944 get_dimensions (args(0), "cell", dims); |
3354
|
945 break; |
|
946 |
|
947 default: |
4563
|
948 { |
|
949 dims.resize (nargin); |
|
950 |
|
951 for (int i = 0; i < nargin; i++) |
|
952 { |
|
953 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); |
|
954 |
|
955 if (error_state) |
|
956 { |
|
957 error ("cell: expecting scalar arguments"); |
|
958 break; |
|
959 } |
|
960 } |
|
961 } |
3354
|
962 break; |
|
963 } |
|
964 |
4563
|
965 if (! error_state) |
|
966 { |
|
967 int ndim = dims.length (); |
|
968 |
|
969 check_dimensions (dims, "cell"); |
|
970 |
|
971 if (! error_state) |
|
972 { |
|
973 switch (ndim) |
|
974 { |
|
975 case 1: |
|
976 retval = Cell (dims(0), dims(0), Matrix ()); |
|
977 break; |
|
978 |
|
979 default: |
|
980 retval = Cell (dims, Matrix ()); |
|
981 break; |
|
982 } |
|
983 } |
|
984 } |
|
985 |
3354
|
986 return retval; |
|
987 } |
|
988 |
4610
|
989 DEFUN (iscellstr, args, , |
|
990 "-*- texinfo -*-\n\ |
|
991 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ |
|
992 Return true if every element of the cell array @var{cell} is a\n\ |
|
993 character string\n\ |
|
994 @end deftypefn") |
|
995 { |
4699
|
996 octave_value retval; |
4610
|
997 |
|
998 if (args.length () == 1) |
|
999 { |
4699
|
1000 retval = true; |
|
1001 |
4610
|
1002 octave_value arg = args (0); |
|
1003 |
|
1004 if (arg.is_cell ()) |
|
1005 { |
|
1006 Cell c = args(0).cell_value (); |
|
1007 |
|
1008 if (! error_state) |
|
1009 { |
|
1010 for (int i = 0; i < c.length (); i++) |
|
1011 { |
|
1012 if (! c(i).is_string ()) |
|
1013 { |
|
1014 retval = false; |
|
1015 break; |
|
1016 } |
|
1017 } |
|
1018 } |
|
1019 else |
4817
|
1020 retval = false; |
4610
|
1021 } |
|
1022 else |
|
1023 retval = false; |
|
1024 } |
|
1025 else |
|
1026 print_usage ("iscellstr"); |
|
1027 |
|
1028 return retval; |
|
1029 } |
|
1030 |
4817
|
1031 // Note that since Fcellstr calls Fiscellstr, we need to have |
|
1032 // Fiscellstr defined first (to provide a declaration) and also we |
|
1033 // should keep it in the same file (so we don't have to provide a |
|
1034 // declaration) and so we don't have to use feval to call it. |
|
1035 |
|
1036 DEFUN (cellstr, args, , |
|
1037 "-*- texinfo -*-\n\ |
|
1038 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ |
|
1039 Create a new cell array object from the elements of the string\n\ |
|
1040 array @var{string}.\n\ |
|
1041 @end deftypefn") |
|
1042 { |
|
1043 octave_value retval; |
|
1044 |
|
1045 if (args.length () == 1) |
|
1046 { |
|
1047 octave_value_list tmp = Fiscellstr (args, 1); |
|
1048 |
|
1049 if (tmp(0).is_true ()) |
|
1050 retval = args(0); |
|
1051 else |
|
1052 { |
|
1053 string_vector s = args(0).all_strings (); |
|
1054 |
|
1055 if (! error_state) |
|
1056 retval = Cell (s); |
|
1057 else |
|
1058 error ("cellstr: expecting argument to be a 2-d character array"); |
|
1059 } |
|
1060 } |
|
1061 else |
|
1062 print_usage ("cellstr"); |
|
1063 |
|
1064 return retval; |
|
1065 } |
|
1066 |
4762
|
1067 DEFUN (struct2cell, args, , |
|
1068 "-*- texinfo -*-\n\ |
|
1069 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ |
|
1070 Create a new cell array from the objects stored in the struct object.\n\ |
4764
|
1071 If @var{f} is the number of fields in the structure, the resulting\n\ |
|
1072 cell array will have a dimension vector corresponding to\n\ |
|
1073 @code{[@var{F} size(@var{S})]}.\n\ |
4762
|
1074 @end deftypefn\n\ |
|
1075 @seealso{cell2struct, fieldnames}") |
|
1076 { |
|
1077 octave_value retval; |
4764
|
1078 |
4762
|
1079 int nargin = args.length (); |
4764
|
1080 |
4762
|
1081 if (nargin == 1) |
|
1082 { |
4764
|
1083 Octave_map m = args(0).map_value (); |
|
1084 |
4762
|
1085 if (! error_state) |
|
1086 { |
|
1087 dim_vector m_dv = m.dims (); |
4764
|
1088 |
4762
|
1089 string_vector keys = m.keys (); |
4764
|
1090 |
4762
|
1091 int fields_numel = keys.length (); |
4764
|
1092 |
4762
|
1093 // The resulting dim_vector should have dimensions: |
|
1094 // [numel(fields) size(struct)] |
4764
|
1095 |
4762
|
1096 dim_vector result_dv; |
4764
|
1097 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
|
1098 |
|
1099 result_dv(0) = fields_numel; |
4762
|
1100 |
|
1101 for (int i = 1; i < result_dv.length (); i++) |
|
1102 result_dv(i) = m_dv(i-1); |
4764
|
1103 |
|
1104 // Squeeze to be sure that a (3,1) vector doesn't get turned |
|
1105 // into a (3,3,1) vector. |
|
1106 |
|
1107 result_dv = result_dv.squeeze (); |
|
1108 |
4762
|
1109 Cell c (result_dv); |
4764
|
1110 |
|
1111 // Use ra_idx both for counting and for assignments, so |
|
1112 // ra_idx(0) will both contain fields_numel for each call to |
|
1113 // increment_index and j for each assignment. |
|
1114 |
4762
|
1115 Array<int> ra_idx (result_dv.length (), 0); |
4764
|
1116 ra_idx(0) = fields_numel; |
4762
|
1117 |
|
1118 for (int i = 0; i < m_dv.numel (); i++) |
|
1119 { |
|
1120 for (int j = 0; j < fields_numel; j++) |
|
1121 { |
|
1122 ra_idx(0) = j; |
4764
|
1123 |
4762
|
1124 Cell c_tmp = m.contents (keys(j)); |
4764
|
1125 |
|
1126 if (c_tmp.length () > 1) // Is a cell. |
4762
|
1127 c(ra_idx) = c_tmp; |
4764
|
1128 else if (c_tmp.length () == 1) // Get octave_value. |
4762
|
1129 c(ra_idx) = c_tmp(0); |
4764
|
1130 else |
|
1131 c(ra_idx) = Cell (); |
|
1132 |
4762
|
1133 ra_idx(0) = fields_numel; |
|
1134 } |
|
1135 |
4764
|
1136 increment_index (ra_idx, result_dv); |
4762
|
1137 } |
|
1138 |
|
1139 retval = c; |
|
1140 } |
|
1141 else |
|
1142 error ("struct2cell: expecting argument to be a cell array"); |
|
1143 } |
|
1144 else |
|
1145 print_usage ("struct2cell"); |
4764
|
1146 |
4762
|
1147 return retval; |
|
1148 } |
|
1149 |
3353
|
1150 /* |
|
1151 ;;; Local Variables: *** |
|
1152 ;;; mode: C++ *** |
|
1153 ;;; End: *** |
|
1154 */ |