Mercurial > hg > octave-lyh
annotate src/ov-struct.cc @ 8150:283989f2da9b
make null assignment matlab compatible
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 26 Sep 2008 11:52:01 -0400 |
parents | d9987dbdf91b |
children | 25bc2d31e1bf |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
4 2007 John W. Eaton | |
2376 | 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. | |
2376 | 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/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
2376 | 29 |
3933 | 30 #include "Cell.h" |
4358 | 31 #include "defun.h" |
2376 | 32 #include "error.h" |
4358 | 33 #include "gripes.h" |
2979 | 34 #include "oct-lvalue.h" |
3932 | 35 #include "ov-list.h" |
2376 | 36 #include "ov-struct.h" |
37 #include "unwind-prot.h" | |
6811 | 38 #include "utils.h" |
2948 | 39 #include "variables.h" |
2376 | 40 |
4750 | 41 #include "Array-util.h" |
42 | |
4687 | 43 #include "byte-swap.h" |
44 #include "ls-oct-ascii.h" | |
45 #include "ls-oct-binary.h" | |
46 #include "ls-hdf5.h" | |
47 #include "ls-utils.h" | |
5759 | 48 #include "pr-output.h" |
4687 | 49 |
3219 | 50 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376 | 51 |
4612 | 52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376 | 53 |
4513 | 54 Cell |
3933 | 55 octave_struct::dotref (const octave_value_list& idx) |
2962 | 56 { |
4513 | 57 Cell retval; |
3933 | 58 |
59 assert (idx.length () == 1); | |
2962 | 60 |
3933 | 61 std::string nm = idx(0).string_value (); |
62 | |
4219 | 63 Octave_map::const_iterator p = map.seek (nm); |
2376 | 64 |
4219 | 65 if (p != map.end ()) |
3933 | 66 retval = map.contents (p); |
67 else | |
2376 | 68 error ("structure has no member `%s'", nm.c_str ()); |
69 | |
70 return retval; | |
71 } | |
72 | |
4513 | 73 #if 0 |
3933 | 74 static void |
75 gripe_invalid_index (void) | |
76 { | |
77 error ("invalid index for structure array"); | |
78 } | |
4513 | 79 #endif |
3933 | 80 |
81 static void | |
82 gripe_invalid_index_for_assignment (void) | |
83 { | |
84 error ("invalid index for structure array assignment"); | |
85 } | |
86 | |
87 static void | |
88 gripe_invalid_index_type (const std::string& nm, char t) | |
89 { | |
90 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
91 } | |
92 | |
93 static void | |
94 gripe_failed_assignment (void) | |
95 { | |
96 error ("assignment to structure element failed"); | |
97 } | |
98 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
99 octave_value_list |
4247 | 100 octave_struct::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
|
101 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
|
102 int nargout) |
3933 | 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 octave_value_list retval; |
3933 | 105 |
106 int skip = 1; | |
107 | |
108 switch (type[0]) | |
109 { | |
110 case '(': | |
111 { | |
112 if (type.length () > 1 && type[1] == '.') | |
113 { | |
4219 | 114 std::list<octave_value_list>::const_iterator p = idx.begin (); |
115 octave_value_list key_idx = *++p; | |
3933 | 116 |
4513 | 117 Cell tmp = dotref (key_idx); |
3933 | 118 |
119 if (! error_state) | |
120 { | |
7460 | 121 Cell t = tmp.index (idx.front (), true); |
3933 | 122 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
123 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933 | 124 |
4513 | 125 // We handled two index elements, so tell |
126 // next_subsref to skip both of them. | |
3933 | 127 |
4513 | 128 skip++; |
3933 | 129 } |
130 } | |
131 else | |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
132 retval(0) = map.index (idx.front (), false); |
3933 | 133 } |
134 break; | |
135 | |
136 case '.': | |
137 { | |
5592 | 138 if (map.numel() > 0) |
139 { | |
140 Cell t = dotref (idx.front ()); | |
3933 | 141 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
142 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
5592 | 143 } |
3933 | 144 } |
145 break; | |
146 | |
147 case '{': | |
148 gripe_invalid_index_type (type_name (), type[0]); | |
149 break; | |
150 | |
151 default: | |
152 panic_impossible (); | |
153 } | |
154 | |
5775 | 155 // FIXME -- perhaps there should be an |
4994 | 156 // octave_value_list::next_subsref member function? See also |
157 // octave_user_function::subsref. | |
158 | |
159 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
|
160 retval = retval(0).next_subsref (nargout, type, idx, skip); |
3933 | 161 |
162 return retval; | |
163 } | |
164 | |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
165 /* |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
166 %!test |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
167 %! x(1).a.a = 1; x(2).a.a = 2; |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
168 %! assert (size (x), [1, 2]); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
169 %! assert (x(1).a.a, 1); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
170 %! assert (x(2).a.a, 2); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
171 */ |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
172 |
3933 | 173 octave_value |
4513 | 174 octave_struct::numeric_conv (const Cell& val, |
3933 | 175 const std::string& type) |
176 { | |
177 octave_value retval; | |
178 | |
179 if (val.length () == 1) | |
180 { | |
181 retval = val(0); | |
182 | |
183 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) | |
184 retval = Octave_map (); | |
185 } | |
186 else | |
187 gripe_invalid_index_for_assignment (); | |
188 | |
189 return retval; | |
190 } | |
191 | |
192 octave_value | |
4247 | 193 octave_struct::subsasgn (const std::string& type, |
4219 | 194 const std::list<octave_value_list>& idx, |
3933 | 195 const octave_value& rhs) |
2376 | 196 { |
3933 | 197 octave_value retval; |
198 | |
199 int n = type.length (); | |
200 | |
201 octave_value t_rhs = rhs; | |
202 | |
203 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) | |
204 { | |
205 switch (type[0]) | |
206 { | |
207 case '(': | |
208 { | |
209 if (type.length () > 1 && type[1] == '.') | |
210 { | |
4219 | 211 std::list<octave_value_list>::const_iterator p = idx.begin (); |
212 octave_value_list t_idx = *p; | |
3933 | 213 |
4513 | 214 octave_value_list key_idx = *++p; |
215 | |
216 assert (key_idx.length () == 1); | |
3933 | 217 |
4513 | 218 std::string key = key_idx(0).string_value (); |
3933 | 219 |
4513 | 220 octave_value u; |
3933 | 221 |
4513 | 222 if (! map.contains (key)) |
223 u = octave_value::empty_conv (type.substr (2), rhs); | |
224 else | |
225 { | |
4675 | 226 Cell map_val = map.contents (key); |
3933 | 227 |
4513 | 228 Cell map_elt = map_val.index (idx.front (), true); |
3933 | 229 |
4513 | 230 u = numeric_conv (map_elt, type.substr (2)); |
231 } | |
3933 | 232 |
4513 | 233 if (! error_state) |
234 { | |
235 std::list<octave_value_list> next_idx (idx); | |
3933 | 236 |
4513 | 237 // We handled two index elements, so subsasgn to |
238 // needs to skip both of them. | |
3933 | 239 |
4513 | 240 next_idx.erase (next_idx.begin ()); |
241 next_idx.erase (next_idx.begin ()); | |
4059 | 242 |
4513 | 243 u.make_unique (); |
244 | |
245 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); | |
3933 | 246 } |
247 } | |
248 else | |
249 gripe_invalid_index_for_assignment (); | |
250 } | |
251 break; | |
252 | |
253 case '.': | |
254 { | |
255 octave_value_list key_idx = idx.front (); | |
256 | |
257 assert (key_idx.length () == 1); | |
258 | |
259 std::string key = key_idx(0).string_value (); | |
260 | |
261 octave_value u; | |
262 | |
263 if (! map.contains (key)) | |
264 u = octave_value::empty_conv (type.substr (1), rhs); | |
265 else | |
266 { | |
4675 | 267 Cell map_val = map.contents (key); |
3933 | 268 |
269 u = numeric_conv (map_val, type.substr (1)); | |
270 } | |
271 | |
272 if (! error_state) | |
273 { | |
4219 | 274 std::list<octave_value_list> next_idx (idx); |
3933 | 275 |
4219 | 276 next_idx.erase (next_idx.begin ()); |
3933 | 277 |
4059 | 278 u.make_unique (); |
279 | |
3933 | 280 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
281 } | |
282 } | |
283 break; | |
284 | |
285 case '{': | |
286 gripe_invalid_index_type (type_name (), type[0]); | |
287 break; | |
288 | |
289 default: | |
290 panic_impossible (); | |
291 } | |
292 } | |
293 | |
294 if (! error_state) | |
295 { | |
296 switch (type[0]) | |
297 { | |
298 case '(': | |
299 { | |
300 if (n > 1 && type[1] == '.') | |
301 { | |
4219 | 302 std::list<octave_value_list>::const_iterator p = idx.begin (); |
303 octave_value_list key_idx = *++p; | |
3933 | 304 |
305 assert (key_idx.length () == 1); | |
306 | |
307 std::string key = key_idx(0).string_value (); | |
308 | |
309 if (! error_state) | |
310 { | |
4513 | 311 map.assign (idx.front (), key, t_rhs); |
3933 | 312 |
4513 | 313 if (! error_state) |
5759 | 314 { |
315 count++; | |
316 retval = octave_value (this); | |
317 } | |
3933 | 318 else |
4513 | 319 gripe_failed_assignment (); |
3933 | 320 } |
321 else | |
322 gripe_failed_assignment (); | |
323 } | |
324 else | |
4197 | 325 { |
5592 | 326 if (t_rhs.is_map()) |
4197 | 327 { |
5592 | 328 Octave_map rhs_map = t_rhs.map_value (); |
4197 | 329 |
330 if (! error_state) | |
5592 | 331 { |
332 map.assign (idx.front (), rhs_map); | |
333 | |
334 if (! error_state) | |
5759 | 335 { |
336 count++; | |
337 retval = octave_value (this); | |
338 } | |
5592 | 339 else |
340 gripe_failed_assignment (); | |
341 } | |
4197 | 342 else |
5592 | 343 error ("invalid structure assignment"); |
4197 | 344 } |
4513 | 345 else |
5592 | 346 { |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
347 if (t_rhs.is_null_value()) |
5592 | 348 { |
349 map.maybe_delete_elements (idx.front()); | |
350 | |
351 if (! error_state) | |
5759 | 352 { |
353 count++; | |
354 retval = octave_value (this); | |
355 } | |
5592 | 356 else |
357 gripe_failed_assignment (); | |
358 } | |
359 else | |
360 error ("invalid structure assignment"); | |
361 } | |
4197 | 362 } |
3933 | 363 } |
364 break; | |
365 | |
366 case '.': | |
367 { | |
368 octave_value_list key_idx = idx.front (); | |
369 | |
370 assert (key_idx.length () == 1); | |
371 | |
372 std::string key = key_idx(0).string_value (); | |
373 | |
6833 | 374 if (t_rhs.is_cs_list ()) |
375 { | |
376 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
377 | |
7040 | 378 // The shape of the RHS is irrelevant, we just want |
379 // the number of elements to agree and to preserve the | |
380 // shape of the left hand side of the assignment. | |
381 | |
382 if (numel () == tmp_cell.numel ()) | |
383 tmp_cell = tmp_cell.reshape (dims ()); | |
6833 | 384 |
385 map.assign (key, tmp_cell); | |
386 } | |
387 else | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
388 // Regularize a null matrix if stored into a struct component. |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
389 map.assign (key, t_rhs.non_null_value ()); |
3933 | 390 |
391 if (! error_state) | |
5759 | 392 { |
393 count++; | |
394 retval = octave_value (this); | |
395 } | |
3933 | 396 else |
397 gripe_failed_assignment (); | |
398 } | |
399 break; | |
400 | |
401 case '{': | |
402 gripe_invalid_index_type (type_name (), type[0]); | |
403 break; | |
404 | |
405 default: | |
406 panic_impossible (); | |
407 } | |
408 } | |
409 else | |
410 gripe_failed_assignment (); | |
411 | |
412 return retval; | |
2376 | 413 } |
414 | |
7046 | 415 octave_value |
416 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
417 { | |
418 octave_value retval; | |
419 | |
420 octave_idx_type n_idx = idx.length (); | |
421 | |
422 int nd = map.ndims (); | |
423 | |
424 switch (n_idx) | |
425 { | |
426 case 0: | |
427 retval = map; | |
428 break; | |
429 | |
430 case 1: | |
431 { | |
432 idx_vector i = idx (0).index_vector (); | |
433 | |
434 if (! error_state) | |
435 retval = map.index (i, resize_ok, Cell::resize_fill_value ()); | |
436 } | |
437 break; | |
438 | |
439 default: | |
440 { | |
441 if (n_idx == 2 && nd == 2) | |
442 { | |
443 idx_vector i = idx (0).index_vector (); | |
444 | |
445 if (! error_state) | |
446 { | |
447 idx_vector j = idx (1).index_vector (); | |
448 | |
449 if (! error_state) | |
450 retval = map.index (i, j, resize_ok, | |
451 Cell::resize_fill_value ()); | |
452 } | |
453 } | |
454 else | |
455 { | |
456 Array<idx_vector> idx_vec (n_idx); | |
457 | |
458 for (octave_idx_type i = 0; i < n_idx; i++) | |
459 { | |
460 idx_vec(i) = idx(i).index_vector (); | |
461 | |
462 if (error_state) | |
463 break; | |
464 } | |
465 | |
466 if (! error_state) | |
467 retval = map.index (idx_vec, resize_ok, | |
468 Cell::resize_fill_value ()); | |
469 } | |
470 } | |
471 break; | |
472 } | |
473 | |
474 return retval; | |
475 } | |
476 | |
4791 | 477 size_t |
478 octave_struct::byte_size (void) const | |
479 { | |
480 // Neglect the size of the fieldnames. | |
481 | |
482 size_t retval = 0; | |
483 | |
484 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
485 { | |
486 std::string key = map.key (p); | |
487 | |
488 octave_value val = octave_value (map.contents (p)); | |
489 | |
490 retval += val.byte_size (); | |
491 } | |
492 | |
493 return retval; | |
494 } | |
495 | |
2376 | 496 void |
3523 | 497 octave_struct::print (std::ostream& os, bool) const |
2901 | 498 { |
499 print_raw (os); | |
500 } | |
501 | |
502 void | |
3523 | 503 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 504 { |
2985 | 505 unwind_protect::begin_frame ("octave_struct_print"); |
2376 | 506 |
507 unwind_protect_int (Vstruct_levels_to_print); | |
508 | |
3961 | 509 if (Vstruct_levels_to_print >= 0) |
2376 | 510 { |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
511 bool print_keys_only = Vstruct_levels_to_print-- == 0; |
3961 | 512 |
2901 | 513 indent (os); |
514 os << "{"; | |
515 newline (os); | |
2376 | 516 |
2901 | 517 increment_indent_level (); |
2376 | 518 |
5598 | 519 octave_idx_type n = map.numel (); |
3932 | 520 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
521 if (n != 1 || print_keys_only) |
4604 | 522 { |
523 indent (os); | |
524 dim_vector dv = dims (); | |
525 os << dv.str () << " struct array containing the fields:"; | |
526 newline (os); | |
527 newline (os); | |
528 | |
529 increment_indent_level (); | |
530 } | |
531 | |
5880 | 532 string_vector key_list = map.keys (); |
533 | |
534 for (octave_idx_type i = 0; i < key_list.length (); i++) | |
2376 | 535 { |
5880 | 536 std::string key = key_list[i]; |
537 | |
538 Cell val = map.contents (key); | |
2376 | 539 |
4499 | 540 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961 | 541 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
542 if (n != 1 || print_keys_only) |
3932 | 543 { |
3961 | 544 indent (os); |
4604 | 545 os << key; |
546 if (n == 1) | |
547 { | |
548 dim_vector dv = tmp.dims (); | |
549 os << ": " << dv.str () << " " << tmp.type_name (); | |
550 } | |
3961 | 551 newline (os); |
3932 | 552 } |
3961 | 553 else |
4121 | 554 tmp.print_with_name (os, key); |
2376 | 555 } |
556 | |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
557 if (n != 1 || print_keys_only) |
4604 | 558 decrement_indent_level (); |
559 | |
2901 | 560 decrement_indent_level (); |
2376 | 561 |
2901 | 562 indent (os); |
563 os << "}"; | |
564 newline (os); | |
2376 | 565 } |
566 else | |
2901 | 567 { |
3961 | 568 indent (os); |
569 os << "<structure>"; | |
2901 | 570 newline (os); |
571 } | |
2376 | 572 |
2985 | 573 unwind_protect::run_frame ("octave_struct_print"); |
2376 | 574 } |
575 | |
2901 | 576 bool |
3523 | 577 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 578 { |
3961 | 579 bool retval = false; |
580 | |
2901 | 581 indent (os); |
3961 | 582 |
583 if (Vstruct_levels_to_print < 0) | |
584 os << name << " = "; | |
585 else | |
586 { | |
587 os << name << " ="; | |
588 newline (os); | |
589 retval = true; | |
590 } | |
591 | |
592 return retval; | |
2901 | 593 } |
594 | |
4744 | 595 static bool |
596 scalar (const dim_vector& dims) | |
597 { | |
598 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; | |
599 } | |
600 | |
601 /* | |
602 %!shared x | |
603 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; | |
604 %!assert(struct('a',1,'b',3),x(1)) | |
5592 | 605 %!assert(isempty(x([]))) |
606 %!assert(isempty(struct('a',{},'b',{}))) | |
4744 | 607 %!assert(struct('a',{1,2},'b',{3,3}),x) |
608 %!assert(struct('a',{1,2},'b',3),x) | |
609 %!assert(struct('a',{1,2},'b',{3}),x) | |
610 %!assert(struct('b',3,'a',{1,2}),x) | |
611 %!assert(struct('b',{3},'a',{1,2}),x) | |
612 %!test x=struct([]); | |
613 %!assert(size(x),[0,0]); | |
614 %!assert(isstruct(x)); | |
615 %!assert(isempty(fieldnames(x))); | |
616 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") | |
5582 | 617 %!fail("struct(1,2,3,4)","struct expects alternating \"field\", VALUE pairs"); |
618 %!fail("struct('1',2,'3')","struct expects alternating \"field\", VALUE pairs"); | |
4744 | 619 */ |
620 | |
621 DEFUN (struct, args, , | |
622 "-*- texinfo -*-\n\ | |
623 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ | |
624 \n\ | |
625 Create a structure and initialize its value.\n\ | |
626 \n\ | |
627 If the values are cell arrays, create a structure array and initialize\n\ | |
628 its values. The dimensions of each cell array of values must match.\n\ | |
629 Singleton cells and non-cell values are repeated so that they fill\n\ | |
630 the entire array. If the cells are empty, create an empty structure\n\ | |
4911 | 631 array with the specified field names.\n\ |
632 @end deftypefn") | |
4744 | 633 { |
6946 | 634 octave_value retval; |
4744 | 635 |
636 int nargin = args.length (); | |
637 | |
5444 | 638 // struct ([]) returns an empty struct. |
639 | |
640 // struct (empty_matrix) returns an empty struct with the same | |
641 // dimensions as the empty matrix. | |
642 | |
643 // Note that struct () creates a 1x1 struct with no fields for | |
644 // compatibility with Matlab. | |
4744 | 645 |
6946 | 646 if ((nargin == 1 || nargin == 2) |
647 && args(0).is_empty () && args(0).is_real_matrix ()) | |
648 { | |
649 Cell fields; | |
650 | |
651 if (nargin == 2) | |
652 { | |
653 if (args(1).is_cellstr ()) | |
654 retval = Octave_map (args(0).dims (), args(1).cell_value ()); | |
655 else | |
656 error ("struct: expecting cell array of field names as second argument"); | |
657 } | |
658 else | |
659 retval = Octave_map (args(0).dims ()); | |
660 | |
661 return retval; | |
662 } | |
4744 | 663 |
664 // Check for "field", VALUE pairs. | |
665 | |
666 for (int i = 0; i < nargin; i += 2) | |
667 { | |
668 if (! args(i).is_string () || i + 1 >= nargin) | |
669 { | |
670 error ("struct expects alternating \"field\", VALUE pairs"); | |
671 return retval; | |
672 } | |
673 } | |
674 | |
675 // Check that the dimensions of the values correspond. | |
676 | |
677 dim_vector dims (1, 1); | |
678 | |
679 int first_dimensioned_value = 0; | |
680 | |
681 for (int i = 1; i < nargin; i += 2) | |
682 { | |
683 if (args(i).is_cell ()) | |
684 { | |
685 dim_vector argdims (args(i).dims ()); | |
686 | |
687 if (! scalar (argdims)) | |
688 { | |
689 if (! first_dimensioned_value) | |
690 { | |
691 dims = argdims; | |
692 first_dimensioned_value = i + 1; | |
693 } | |
694 else if (dims != argdims) | |
695 { | |
696 error ("struct: dimensions of parameter %d do not match those of parameter %d", | |
697 first_dimensioned_value, i+1); | |
698 return retval; | |
699 } | |
700 } | |
701 } | |
702 } | |
703 | |
704 // Create the return value. | |
705 | |
706 Octave_map map (dims); | |
707 | |
708 for (int i = 0; i < nargin; i+= 2) | |
709 { | |
710 // Get key. | |
711 | |
712 std::string key (args(i).string_value ()); | |
713 | |
714 if (error_state) | |
715 return retval; | |
716 | |
6811 | 717 if (! valid_identifier (key)) |
718 { | |
719 error ("struct: invalid structure field name `%s'", key.c_str ()); | |
720 return retval; | |
721 } | |
722 | |
4744 | 723 // Value may be v, { v }, or { v1, v2, ... } |
724 // In the first two cases, we need to create a cell array of | |
725 // the appropriate dimensions filled with v. In the last case, | |
726 // the cell array has already been determined to be of the | |
727 // correct dimensions. | |
728 | |
729 if (args(i+1).is_cell ()) | |
730 { | |
731 const Cell c (args(i+1).cell_value ()); | |
732 | |
733 if (error_state) | |
734 return retval; | |
735 | |
736 if (scalar (c.dims ())) | |
737 map.assign (key, Cell (dims, c(0))); | |
738 else | |
739 map.assign (key, c); | |
740 } | |
741 else | |
742 map.assign (key, Cell (dims, args(i+1))); | |
743 | |
744 if (error_state) | |
745 return retval; | |
746 } | |
6946 | 747 |
4744 | 748 return octave_value (map); |
749 } | |
750 | |
4358 | 751 DEFUN (isstruct, args, , |
752 "-*- texinfo -*-\n\ | |
753 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ | |
754 Return 1 if the value of the expression @var{expr} is a structure.\n\ | |
755 @end deftypefn") | |
756 { | |
757 octave_value retval; | |
758 | |
759 if (args.length () == 1) | |
760 retval = args(0).is_map (); | |
761 else | |
5823 | 762 print_usage (); |
4358 | 763 |
764 return retval; | |
765 } | |
766 | |
767 DEFUN (fieldnames, args, , | |
768 "-*- texinfo -*-\n\ | |
769 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ | |
770 Return a cell array of strings naming the elements of the structure\n\ | |
771 @var{struct}. It is an error to call @code{fieldnames} with an\n\ | |
772 argument that is not a structure.\n\ | |
773 @end deftypefn") | |
774 { | |
775 octave_value retval; | |
776 | |
777 int nargin = args.length (); | |
778 | |
779 if (nargin == 1) | |
780 { | |
7336 | 781 octave_value arg = args(0); |
782 | |
783 if (arg.is_map () || arg.is_object ()) | |
4358 | 784 { |
7336 | 785 Octave_map m = arg.map_value (); |
786 | |
4744 | 787 string_vector keys = m.keys (); |
7336 | 788 |
4744 | 789 if (keys.length () == 0) |
790 retval = Cell (0, 1); | |
791 else | |
792 retval = Cell (m.keys ()); | |
4358 | 793 } |
794 else | |
795 gripe_wrong_type_arg ("fieldnames", args(0)); | |
796 } | |
797 else | |
5823 | 798 print_usage (); |
4358 | 799 |
800 return retval; | |
801 } | |
802 | |
803 DEFUN (isfield, args, , | |
804 "-*- texinfo -*-\n\ | |
805 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ | |
806 Return true if the expression @var{expr} is a structure and it includes an\n\ | |
807 element named @var{name}. The first argument must be a structure and\n\ | |
808 the second must be a string.\n\ | |
809 @end deftypefn") | |
810 { | |
811 octave_value retval; | |
812 | |
813 int nargin = args.length (); | |
814 | |
815 if (nargin == 2) | |
816 { | |
817 retval = false; | |
818 | |
5775 | 819 // FIXME -- should this work for all types that can do |
4358 | 820 // structure reference operations? |
821 | |
822 if (args(0).is_map () && args(1).is_string ()) | |
823 { | |
824 std::string key = args(1).string_value (); | |
825 | |
826 Octave_map m = args(0).map_value (); | |
827 | |
828 retval = m.contains (key) != 0; | |
829 } | |
830 } | |
831 else | |
5823 | 832 print_usage (); |
4358 | 833 |
834 return retval; | |
835 } | |
836 | |
4750 | 837 // Check that the dimensions of the input arguments are correct. |
838 | |
839 static bool | |
840 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, | |
841 bool is_cell, int dim) | |
842 { | |
4751 | 843 bool retval = true; |
4750 | 844 |
845 if (dim >= 0 && dim < c_dv.length ()) | |
846 { | |
847 if (is_cell) | |
848 { | |
4752 | 849 if (f_dv.numel () != c_dv(dim)) |
4750 | 850 { |
4751 | 851 error ("cell2struct: numel (FIELD) != size (CELL, DIM)"); |
4750 | 852 |
853 retval = false; | |
854 } | |
855 } | |
856 else | |
857 { | |
858 if (f_dv.length () > 2) | |
859 { | |
4751 | 860 error ("cell2struct: field array must be a 2-d matrix"); |
4750 | 861 |
862 retval = false; | |
863 } | |
864 else if (f_dv(0) != c_dv(dim)) | |
865 { | |
4751 | 866 error ("cell2struct: size (FIELD, 1) != length (C, DIM)"); |
4750 | 867 |
868 retval = false; | |
869 } | |
870 } | |
871 } | |
872 else | |
873 { | |
874 error ("cell2struct: DIM out of range"); | |
875 | |
876 retval = false; | |
877 } | |
878 | |
879 return retval; | |
880 } | |
881 | |
882 static void | |
5275 | 883 cell2struct_construct_idx (Array<octave_idx_type>& ra_idx1, |
884 const Array<octave_idx_type>& ra_idx2, | |
885 octave_idx_type dim, octave_idx_type fill_value) | |
4750 | 886 { |
5275 | 887 octave_idx_type iidx = 0; |
4750 | 888 |
5275 | 889 for (octave_idx_type idx = 0; idx < ra_idx1.length (); idx++) |
4750 | 890 { |
891 if (idx == dim) | |
892 ra_idx1.elem (idx) = fill_value; | |
893 else | |
894 ra_idx1.elem (idx) = ra_idx2(iidx++); | |
895 } | |
896 } | |
897 | |
898 DEFUN (cell2struct, args, , | |
899 "-*- texinfo -*-\n\ | |
4817 | 900 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ |
901 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ | |
902 must match the number of elements in @var{cell} along dimension @var{dim},\n\ | |
903 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ | |
4750 | 904 \n\ |
905 @example\n\ | |
906 @group\n\ | |
7031 | 907 A = cell2struct (@{'Peter', 'Hannah', 'Robert';\n\ |
908 185, 170, 168@},\n\ | |
6519 | 909 @{'Name','Height'@}, 1);\n\ |
4750 | 910 A(1)\n\ |
911 @result{} ans =\n\ | |
4780 | 912 @{\n\ |
913 Height = 185\n\ | |
914 Name = Peter\n\ | |
915 @}\n\ | |
4750 | 916 \n\ |
917 @end group\n\ | |
918 @end example\n\ | |
919 @end deftypefn") | |
920 { | |
921 octave_value retval; | |
922 | |
4751 | 923 if (args.length () == 3) |
4750 | 924 { |
4751 | 925 Cell c = args(0).cell_value (); |
4750 | 926 |
4751 | 927 if (! error_state) |
4750 | 928 { |
4751 | 929 octave_value field = args(1); |
4750 | 930 |
4751 | 931 // Field is either cell or character matrix. |
4750 | 932 |
5775 | 933 // FIXME -- this could be simplified if we had |
4752 | 934 // cellstr and iscellstr functions available. |
935 | |
4751 | 936 bool field_is_cell = field.is_cell (); |
4750 | 937 |
4751 | 938 Cell field_cell; |
939 charMatrix field_char; | |
4750 | 940 |
941 if (field_is_cell) | |
4751 | 942 field_cell = field.cell_value (); |
943 else | |
944 field_char = field.char_matrix_value (); | |
945 | |
946 if (! error_state) | |
4750 | 947 { |
4751 | 948 // Retrieve the dimension value. |
949 | |
5775 | 950 // FIXME -- int_value () should print out the |
4751 | 951 // conversions it does to be Matlab compatible. |
952 | |
5275 | 953 octave_idx_type dim = args(2).int_value () - 1; |
4751 | 954 |
955 if (! error_state) | |
956 { | |
957 dim_vector c_dv = c.dims (); | |
958 dim_vector field_dv = field.dims (); | |
959 | |
960 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, | |
961 dim)) | |
962 { | |
5275 | 963 octave_idx_type c_dv_length = c_dv.length (); |
4751 | 964 |
965 // Dimension vector for the Cell arrays to be | |
966 // put into the structure. | |
967 | |
968 dim_vector value_dv; | |
969 | |
970 // Initialize c_value_dv. | |
4750 | 971 |
4751 | 972 if (c_dv_length == 2) |
973 value_dv = dim_vector (1, 1); | |
974 else | |
975 value_dv.resize (c_dv_length - 1); | |
976 | |
5275 | 977 octave_idx_type idx_tmp = 0; |
4751 | 978 |
5275 | 979 for (octave_idx_type i = 0; i < c_dv_length; i++) |
4751 | 980 { |
981 if (i != dim) | |
982 value_dv.elem (idx_tmp++) = c_dv.elem (i); | |
983 } | |
984 | |
985 // All initializing is done, we can start moving | |
986 // values. | |
987 | |
988 Octave_map map; | |
989 | |
990 // If field is a cell array then we use all | |
991 // elements in array, on the other hand when | |
992 // field is a character array the number of | |
993 // elements is equals the number of rows. | |
994 | |
5275 | 995 octave_idx_type field_numel |
4751 | 996 = field_is_cell ? field_dv.numel (): field_dv(0); |
997 | |
998 // For matlab compatibility. | |
999 | |
1000 if (field_numel == 0) | |
1001 map.reshape (dim_vector (0, 1)); | |
4750 | 1002 |
5275 | 1003 for (octave_idx_type i = 0; i < field_numel; i++) |
4751 | 1004 { |
1005 // Construct cell array which goes into the | |
1006 // structure together with the appropriate | |
1007 // field name. | |
1008 | |
1009 Cell c_value (value_dv); | |
1010 | |
5275 | 1011 Array<octave_idx_type> value_idx (value_dv.length (), 0); |
1012 Array<octave_idx_type> c_idx (c_dv_length, 0); | |
4751 | 1013 |
5275 | 1014 for (octave_idx_type j = 0; j < value_dv.numel (); j++) |
4751 | 1015 { |
1016 // Need to do this to construct the | |
1017 // appropriate idx for getting elements | |
1018 // from the original cell array. | |
1019 | |
1020 cell2struct_construct_idx (c_idx, value_idx, | |
1021 dim, i); | |
1022 | |
1023 c_value.elem (value_idx) = c.elem (c_idx); | |
1024 | |
1025 increment_index (value_idx, value_dv); | |
1026 } | |
1027 | |
1028 std::string field_str; | |
4750 | 1029 |
4751 | 1030 if (field_is_cell) |
1031 { | |
1032 // Matlab retrieves the field values | |
1033 // column by column. | |
1034 | |
1035 octave_value field_tmp = field_cell.elem (i); | |
1036 | |
1037 field_str = field_tmp.string_value (); | |
1038 | |
1039 if (error_state) | |
1040 { | |
1041 error ("cell2struct: fields have to be of type string"); | |
1042 break; | |
1043 } | |
1044 } | |
1045 else | |
1046 { | |
1047 field_str = field_char.row_as_string (i); | |
1048 | |
1049 if (error_state) | |
1050 return retval; | |
1051 } | |
1052 | |
6811 | 1053 if (! valid_identifier (field_str)) |
1054 { | |
1055 error ("cell2struct: invalid field name `%s'", | |
1056 field_str.c_str ()); | |
1057 break; | |
1058 } | |
1059 | |
4751 | 1060 map.reshape (value_dv); |
1061 | |
1062 map.assign (field_str, c_value); | |
1063 } | |
1064 | |
1065 if (! error_state) | |
1066 retval = map; | |
1067 } | |
4750 | 1068 } |
4751 | 1069 else |
1070 error ("cell2struct: expecting third argument to be an integer"); | |
4750 | 1071 } |
1072 else | |
4751 | 1073 error ("cell2struct: expecting second argument to be a cell or character array"); |
4750 | 1074 } |
4751 | 1075 else |
1076 error ("cell2struct: expecting first argument to be a cell array"); | |
4750 | 1077 } |
4751 | 1078 else |
5823 | 1079 print_usage (); |
4750 | 1080 |
1081 return retval; | |
1082 } | |
1083 | |
4817 | 1084 // So we can call Fcellstr directly. |
1085 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
1086 | |
1087 DEFUN (rmfield, args, , | |
1088 "-*- texinfo -*-\n\ | |
1089 @deftypefn {Built-in Function} {} rmfield (@var{s}, @var{f})\n\ | |
1090 Remove field @var{f} from the structure @var{s}. If @var{f} is a\n\ | |
1091 cell array of character strings or a character array, remove the\n\ | |
1092 named fields.\n\ | |
5642 | 1093 @seealso{cellstr, iscellstr, setfield}\n\ |
1094 @end deftypefn") | |
4817 | 1095 { |
1096 octave_value retval; | |
1097 | |
1098 int nargin = args.length (); | |
1099 | |
1100 if (nargin == 2) | |
1101 { | |
1102 Octave_map m = args(0).map_value (); | |
1103 | |
1104 octave_value_list fval = Fcellstr (args(1), 1); | |
1105 | |
1106 if (! error_state) | |
1107 { | |
1108 Cell fcell = fval(0).cell_value (); | |
1109 | |
1110 for (int i = 0; i < fcell.numel (); i++) | |
1111 { | |
1112 std::string key = fcell(i).string_value (); | |
1113 | |
1114 if (m.contains (key)) | |
1115 m.del (key); | |
1116 else | |
1117 { | |
1118 error ("rmfield: structure does not contain field %s", | |
1119 key.c_str ()); | |
1120 | |
1121 break; | |
1122 } | |
1123 } | |
1124 | |
1125 if (! error_state) | |
1126 retval = m; | |
1127 } | |
1128 } | |
1129 else | |
5823 | 1130 print_usage (); |
4817 | 1131 |
1132 return retval; | |
1133 } | |
1134 | |
1135 bool | |
6974 | 1136 octave_struct::save_ascii (std::ostream& os) |
4817 | 1137 { |
1138 Octave_map m = map_value (); | |
6639 | 1139 os << "# length: " << m.nfields () << "\n"; |
4817 | 1140 |
1141 Octave_map::iterator i = m.begin (); | |
1142 while (i != m.end ()) | |
1143 { | |
5341 | 1144 octave_value val = map.contents (i); |
4817 | 1145 |
6974 | 1146 bool b = save_ascii_data (os, val, m.key (i), false, 0); |
4817 | 1147 |
1148 if (! b) | |
1149 return os; | |
1150 | |
1151 i++; | |
1152 } | |
1153 | |
1154 return true; | |
1155 } | |
1156 | |
1157 bool | |
1158 octave_struct::load_ascii (std::istream& is) | |
1159 { | |
5275 | 1160 octave_idx_type len = 0; |
4817 | 1161 bool success = true; |
1162 | |
1163 if (extract_keyword (is, "length", len) && len >= 0) | |
1164 { | |
1165 if (len > 0) | |
1166 { | |
1167 Octave_map m (map); | |
1168 | |
5275 | 1169 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1170 { |
1171 octave_value t2; | |
1172 bool dummy; | |
1173 | |
1174 // recurse to read cell elements | |
1175 std::string nm | |
5756 | 1176 = read_ascii_data (is, std::string (), dummy, t2, j); |
4817 | 1177 |
1178 if (!is) | |
1179 break; | |
1180 | |
6293 | 1181 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1182 |
6293 | 1183 if (error_state) |
1184 { | |
1185 error ("load: internal error loading struct elements"); | |
1186 return false; | |
1187 } | |
5433 | 1188 |
6293 | 1189 m.assign (nm, tcell); |
4817 | 1190 } |
1191 | |
1192 if (is) | |
1193 map = m; | |
1194 else | |
1195 { | |
1196 error ("load: failed to load structure"); | |
1197 success = false; | |
1198 } | |
1199 } | |
1200 else if (len == 0 ) | |
6292 | 1201 map = Octave_map (dim_vector (1, 1)); |
4817 | 1202 else |
1203 panic_impossible (); | |
1204 } | |
1205 else { | |
1206 error ("load: failed to extract number of elements in structure"); | |
1207 success = false; | |
1208 } | |
1209 | |
1210 return success; | |
1211 } | |
1212 | |
1213 bool | |
1214 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) | |
1215 { | |
1216 Octave_map m = map_value (); | |
1217 | |
6639 | 1218 int32_t len = m.nfields (); |
5760 | 1219 os.write (reinterpret_cast<char *> (&len), 4); |
4817 | 1220 |
1221 Octave_map::iterator i = m.begin (); | |
1222 while (i != m.end ()) | |
1223 { | |
5341 | 1224 octave_value val = map.contents (i); |
4817 | 1225 |
5341 | 1226 bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats); |
4817 | 1227 |
1228 if (! b) | |
1229 return os; | |
1230 | |
1231 i++; | |
1232 } | |
1233 | |
1234 return true; | |
1235 } | |
1236 | |
1237 bool | |
1238 octave_struct::load_binary (std::istream& is, bool swap, | |
5760 | 1239 oct_mach_info::float_format fmt) |
4817 | 1240 { |
1241 bool success = true; | |
5828 | 1242 int32_t len; |
5760 | 1243 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4817 | 1244 return false; |
1245 if (swap) | |
4944 | 1246 swap_bytes<4> (&len); |
4817 | 1247 |
1248 if (len > 0) | |
1249 { | |
1250 Octave_map m (map); | |
1251 | |
5275 | 1252 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1253 { |
1254 octave_value t2; | |
1255 bool dummy; | |
1256 std::string doc; | |
1257 | |
1258 // recurse to read cell elements | |
1259 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
1260 dummy, t2, doc); | |
1261 | |
1262 if (!is) | |
1263 break; | |
1264 | |
6293 | 1265 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1266 |
6293 | 1267 if (error_state) |
1268 { | |
1269 error ("load: internal error loading struct elements"); | |
1270 return false; | |
1271 } | |
5433 | 1272 |
6293 | 1273 m.assign (nm, tcell); |
4817 | 1274 } |
1275 | |
1276 if (is) | |
1277 map = m; | |
1278 else | |
1279 { | |
1280 error ("load: failed to load structure"); | |
1281 success = false; | |
1282 } | |
1283 } | |
1284 else if (len == 0 ) | |
6292 | 1285 map = Octave_map (dim_vector (1, 1)); |
4817 | 1286 else |
1287 panic_impossible (); | |
1288 | |
1289 return success; | |
1290 } | |
1291 | |
1292 #if defined (HAVE_HDF5) | |
1293 | |
1294 bool | |
1295 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1296 { | |
1297 hid_t data_hid = -1; | |
1298 | |
1299 data_hid = H5Gcreate (loc_id, name, 0); | |
1300 if (data_hid < 0) return false; | |
1301 | |
1302 // recursively add each element of the structure to this group | |
1303 Octave_map m = map_value (); | |
1304 Octave_map::iterator i = m.begin (); | |
1305 while (i != m.end ()) | |
1306 { | |
5341 | 1307 octave_value val = map.contents (i); |
4817 | 1308 |
5341 | 1309 bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false, |
4817 | 1310 save_as_floats); |
1311 | |
1312 if (! retval2) | |
1313 break; | |
1314 | |
1315 i++; | |
1316 } | |
1317 | |
1318 H5Gclose (data_hid); | |
4837 | 1319 |
4817 | 1320 return true; |
1321 } | |
1322 | |
1323 bool | |
1324 octave_struct::load_hdf5 (hid_t loc_id, const char *name, | |
1325 bool have_h5giterate_bug) | |
1326 { | |
1327 bool retval = false; | |
1328 | |
1329 hdf5_callback_data dsub; | |
1330 | |
1331 herr_t retval2 = 0; | |
6292 | 1332 Octave_map m (dim_vector (1, 1)); |
4817 | 1333 int current_item = 0; |
1334 #ifdef HAVE_H5GGET_NUM_OBJS | |
1335 hsize_t num_obj = 0; | |
5060 | 1336 hid_t group_id = H5Gopen (loc_id, name); |
1337 H5Gget_num_objs (group_id, &num_obj); | |
1338 H5Gclose (group_id); | |
4817 | 1339 |
1340 while (current_item < static_cast<int> (num_obj) | |
1341 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1342 hdf5_read_next_data, &dsub)) > 0) | |
1343 #else | |
1344 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1345 hdf5_read_next_data, &dsub)) > 0) | |
1346 #endif | |
1347 { | |
5342 | 1348 octave_value t2 = dsub.tc; |
1349 | |
6293 | 1350 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1351 |
6293 | 1352 if (error_state) |
1353 { | |
1354 error ("load: internal error loading struct elements"); | |
1355 return false; | |
1356 } | |
5433 | 1357 |
6293 | 1358 m.assign (dsub.name, tcell); |
5336 | 1359 |
4817 | 1360 if (have_h5giterate_bug) |
1361 current_item++; // H5Giterate returned the last index processed | |
1362 } | |
1363 | |
1364 if (retval2 >= 0) | |
1365 { | |
1366 map = m; | |
1367 retval = true; | |
1368 } | |
1369 | |
1370 return retval; | |
1371 } | |
1372 | |
4687 | 1373 #endif |
1374 | |
5900 | 1375 mxArray * |
1376 octave_struct::as_mxArray (void) const | |
1377 { | |
1378 int nf = nfields (); | |
1379 string_vector kv = map_keys (); | |
6065 | 1380 |
1381 OCTAVE_LOCAL_BUFFER (const char *, f, nf); | |
1382 | |
5900 | 1383 for (int i = 0; i < nf; i++) |
6065 | 1384 f[i] = kv[i].c_str (); |
5900 | 1385 |
1386 mxArray *retval = new mxArray (dims (), nf, f); | |
1387 | |
1388 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1389 | |
6686 | 1390 mwSize nel = numel (); |
5900 | 1391 |
6686 | 1392 mwSize ntot = nf * nel; |
5900 | 1393 |
1394 for (int i = 0; i < nf; i++) | |
1395 { | |
1396 Cell c = map.contents (kv[i]); | |
1397 | |
1398 const octave_value *p = c.data (); | |
1399 | |
6686 | 1400 mwIndex k = 0; |
1401 for (mwIndex j = i; j < ntot; j += nf) | |
5900 | 1402 elts[j] = new mxArray (p[k++]); |
1403 } | |
1404 | |
1405 return retval; | |
1406 } | |
1407 | |
2376 | 1408 /* |
1409 ;;; Local Variables: *** | |
1410 ;;; mode: C++ *** | |
1411 ;;; End: *** | |
1412 */ |