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