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