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