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