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