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