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