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