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