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