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