Mercurial > hg > octave-lyh
annotate src/ov-struct.cc @ 8031:d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 12 Aug 2008 13:20:27 -0400 |
parents | 443a8f5a50fd |
children | 283989f2da9b |
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 { |
347 if (t_rhs.is_empty()) | |
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 | |
388 map.assign (key, t_rhs); | |
3933 | 389 |
390 if (! error_state) | |
5759 | 391 { |
392 count++; | |
393 retval = octave_value (this); | |
394 } | |
3933 | 395 else |
396 gripe_failed_assignment (); | |
397 } | |
398 break; | |
399 | |
400 case '{': | |
401 gripe_invalid_index_type (type_name (), type[0]); | |
402 break; | |
403 | |
404 default: | |
405 panic_impossible (); | |
406 } | |
407 } | |
408 else | |
409 gripe_failed_assignment (); | |
410 | |
411 return retval; | |
2376 | 412 } |
413 | |
7046 | 414 octave_value |
415 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
416 { | |
417 octave_value retval; | |
418 | |
419 octave_idx_type n_idx = idx.length (); | |
420 | |
421 int nd = map.ndims (); | |
422 | |
423 switch (n_idx) | |
424 { | |
425 case 0: | |
426 retval = map; | |
427 break; | |
428 | |
429 case 1: | |
430 { | |
431 idx_vector i = idx (0).index_vector (); | |
432 | |
433 if (! error_state) | |
434 retval = map.index (i, resize_ok, Cell::resize_fill_value ()); | |
435 } | |
436 break; | |
437 | |
438 default: | |
439 { | |
440 if (n_idx == 2 && nd == 2) | |
441 { | |
442 idx_vector i = idx (0).index_vector (); | |
443 | |
444 if (! error_state) | |
445 { | |
446 idx_vector j = idx (1).index_vector (); | |
447 | |
448 if (! error_state) | |
449 retval = map.index (i, j, resize_ok, | |
450 Cell::resize_fill_value ()); | |
451 } | |
452 } | |
453 else | |
454 { | |
455 Array<idx_vector> idx_vec (n_idx); | |
456 | |
457 for (octave_idx_type i = 0; i < n_idx; i++) | |
458 { | |
459 idx_vec(i) = idx(i).index_vector (); | |
460 | |
461 if (error_state) | |
462 break; | |
463 } | |
464 | |
465 if (! error_state) | |
466 retval = map.index (idx_vec, resize_ok, | |
467 Cell::resize_fill_value ()); | |
468 } | |
469 } | |
470 break; | |
471 } | |
472 | |
473 return retval; | |
474 } | |
475 | |
4791 | 476 size_t |
477 octave_struct::byte_size (void) const | |
478 { | |
479 // Neglect the size of the fieldnames. | |
480 | |
481 size_t retval = 0; | |
482 | |
483 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
484 { | |
485 std::string key = map.key (p); | |
486 | |
487 octave_value val = octave_value (map.contents (p)); | |
488 | |
489 retval += val.byte_size (); | |
490 } | |
491 | |
492 return retval; | |
493 } | |
494 | |
2376 | 495 void |
3523 | 496 octave_struct::print (std::ostream& os, bool) const |
2901 | 497 { |
498 print_raw (os); | |
499 } | |
500 | |
501 void | |
3523 | 502 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 503 { |
2985 | 504 unwind_protect::begin_frame ("octave_struct_print"); |
2376 | 505 |
506 unwind_protect_int (Vstruct_levels_to_print); | |
507 | |
3961 | 508 if (Vstruct_levels_to_print >= 0) |
2376 | 509 { |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
510 bool print_keys_only = Vstruct_levels_to_print-- == 0; |
3961 | 511 |
2901 | 512 indent (os); |
513 os << "{"; | |
514 newline (os); | |
2376 | 515 |
2901 | 516 increment_indent_level (); |
2376 | 517 |
5598 | 518 octave_idx_type n = map.numel (); |
3932 | 519 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
520 if (n != 1 || print_keys_only) |
4604 | 521 { |
522 indent (os); | |
523 dim_vector dv = dims (); | |
524 os << dv.str () << " struct array containing the fields:"; | |
525 newline (os); | |
526 newline (os); | |
527 | |
528 increment_indent_level (); | |
529 } | |
530 | |
5880 | 531 string_vector key_list = map.keys (); |
532 | |
533 for (octave_idx_type i = 0; i < key_list.length (); i++) | |
2376 | 534 { |
5880 | 535 std::string key = key_list[i]; |
536 | |
537 Cell val = map.contents (key); | |
2376 | 538 |
4499 | 539 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961 | 540 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
541 if (n != 1 || print_keys_only) |
3932 | 542 { |
3961 | 543 indent (os); |
4604 | 544 os << key; |
545 if (n == 1) | |
546 { | |
547 dim_vector dv = tmp.dims (); | |
548 os << ": " << dv.str () << " " << tmp.type_name (); | |
549 } | |
3961 | 550 newline (os); |
3932 | 551 } |
3961 | 552 else |
4121 | 553 tmp.print_with_name (os, key); |
2376 | 554 } |
555 | |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
556 if (n != 1 || print_keys_only) |
4604 | 557 decrement_indent_level (); |
558 | |
2901 | 559 decrement_indent_level (); |
2376 | 560 |
2901 | 561 indent (os); |
562 os << "}"; | |
563 newline (os); | |
2376 | 564 } |
565 else | |
2901 | 566 { |
3961 | 567 indent (os); |
568 os << "<structure>"; | |
2901 | 569 newline (os); |
570 } | |
2376 | 571 |
2985 | 572 unwind_protect::run_frame ("octave_struct_print"); |
2376 | 573 } |
574 | |
2901 | 575 bool |
3523 | 576 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 577 { |
3961 | 578 bool retval = false; |
579 | |
2901 | 580 indent (os); |
3961 | 581 |
582 if (Vstruct_levels_to_print < 0) | |
583 os << name << " = "; | |
584 else | |
585 { | |
586 os << name << " ="; | |
587 newline (os); | |
588 retval = true; | |
589 } | |
590 | |
591 return retval; | |
2901 | 592 } |
593 | |
4744 | 594 static bool |
595 scalar (const dim_vector& dims) | |
596 { | |
597 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; | |
598 } | |
599 | |
600 /* | |
601 %!shared x | |
602 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; | |
603 %!assert(struct('a',1,'b',3),x(1)) | |
5592 | 604 %!assert(isempty(x([]))) |
605 %!assert(isempty(struct('a',{},'b',{}))) | |
4744 | 606 %!assert(struct('a',{1,2},'b',{3,3}),x) |
607 %!assert(struct('a',{1,2},'b',3),x) | |
608 %!assert(struct('a',{1,2},'b',{3}),x) | |
609 %!assert(struct('b',3,'a',{1,2}),x) | |
610 %!assert(struct('b',{3},'a',{1,2}),x) | |
611 %!test x=struct([]); | |
612 %!assert(size(x),[0,0]); | |
613 %!assert(isstruct(x)); | |
614 %!assert(isempty(fieldnames(x))); | |
615 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") | |
5582 | 616 %!fail("struct(1,2,3,4)","struct expects alternating \"field\", VALUE pairs"); |
617 %!fail("struct('1',2,'3')","struct expects alternating \"field\", VALUE pairs"); | |
4744 | 618 */ |
619 | |
620 DEFUN (struct, args, , | |
621 "-*- texinfo -*-\n\ | |
622 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ | |
623 \n\ | |
624 Create a structure and initialize its value.\n\ | |
625 \n\ | |
626 If the values are cell arrays, create a structure array and initialize\n\ | |
627 its values. The dimensions of each cell array of values must match.\n\ | |
628 Singleton cells and non-cell values are repeated so that they fill\n\ | |
629 the entire array. If the cells are empty, create an empty structure\n\ | |
4911 | 630 array with the specified field names.\n\ |
631 @end deftypefn") | |
4744 | 632 { |
6946 | 633 octave_value retval; |
4744 | 634 |
635 int nargin = args.length (); | |
636 | |
5444 | 637 // struct ([]) returns an empty struct. |
638 | |
639 // struct (empty_matrix) returns an empty struct with the same | |
640 // dimensions as the empty matrix. | |
641 | |
642 // Note that struct () creates a 1x1 struct with no fields for | |
643 // compatibility with Matlab. | |
4744 | 644 |
6946 | 645 if ((nargin == 1 || nargin == 2) |
646 && args(0).is_empty () && args(0).is_real_matrix ()) | |
647 { | |
648 Cell fields; | |
649 | |
650 if (nargin == 2) | |
651 { | |
652 if (args(1).is_cellstr ()) | |
653 retval = Octave_map (args(0).dims (), args(1).cell_value ()); | |
654 else | |
655 error ("struct: expecting cell array of field names as second argument"); | |
656 } | |
657 else | |
658 retval = Octave_map (args(0).dims ()); | |
659 | |
660 return retval; | |
661 } | |
4744 | 662 |
663 // Check for "field", VALUE pairs. | |
664 | |
665 for (int i = 0; i < nargin; i += 2) | |
666 { | |
667 if (! args(i).is_string () || i + 1 >= nargin) | |
668 { | |
669 error ("struct expects alternating \"field\", VALUE pairs"); | |
670 return retval; | |
671 } | |
672 } | |
673 | |
674 // Check that the dimensions of the values correspond. | |
675 | |
676 dim_vector dims (1, 1); | |
677 | |
678 int first_dimensioned_value = 0; | |
679 | |
680 for (int i = 1; i < nargin; i += 2) | |
681 { | |
682 if (args(i).is_cell ()) | |
683 { | |
684 dim_vector argdims (args(i).dims ()); | |
685 | |
686 if (! scalar (argdims)) | |
687 { | |
688 if (! first_dimensioned_value) | |
689 { | |
690 dims = argdims; | |
691 first_dimensioned_value = i + 1; | |
692 } | |
693 else if (dims != argdims) | |
694 { | |
695 error ("struct: dimensions of parameter %d do not match those of parameter %d", | |
696 first_dimensioned_value, i+1); | |
697 return retval; | |
698 } | |
699 } | |
700 } | |
701 } | |
702 | |
703 // Create the return value. | |
704 | |
705 Octave_map map (dims); | |
706 | |
707 for (int i = 0; i < nargin; i+= 2) | |
708 { | |
709 // Get key. | |
710 | |
711 std::string key (args(i).string_value ()); | |
712 | |
713 if (error_state) | |
714 return retval; | |
715 | |
6811 | 716 if (! valid_identifier (key)) |
717 { | |
718 error ("struct: invalid structure field name `%s'", key.c_str ()); | |
719 return retval; | |
720 } | |
721 | |
4744 | 722 // Value may be v, { v }, or { v1, v2, ... } |
723 // In the first two cases, we need to create a cell array of | |
724 // the appropriate dimensions filled with v. In the last case, | |
725 // the cell array has already been determined to be of the | |
726 // correct dimensions. | |
727 | |
728 if (args(i+1).is_cell ()) | |
729 { | |
730 const Cell c (args(i+1).cell_value ()); | |
731 | |
732 if (error_state) | |
733 return retval; | |
734 | |
735 if (scalar (c.dims ())) | |
736 map.assign (key, Cell (dims, c(0))); | |
737 else | |
738 map.assign (key, c); | |
739 } | |
740 else | |
741 map.assign (key, Cell (dims, args(i+1))); | |
742 | |
743 if (error_state) | |
744 return retval; | |
745 } | |
6946 | 746 |
4744 | 747 return octave_value (map); |
748 } | |
749 | |
4358 | 750 DEFUN (isstruct, args, , |
751 "-*- texinfo -*-\n\ | |
752 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ | |
753 Return 1 if the value of the expression @var{expr} is a structure.\n\ | |
754 @end deftypefn") | |
755 { | |
756 octave_value retval; | |
757 | |
758 if (args.length () == 1) | |
759 retval = args(0).is_map (); | |
760 else | |
5823 | 761 print_usage (); |
4358 | 762 |
763 return retval; | |
764 } | |
765 | |
766 DEFUN (fieldnames, args, , | |
767 "-*- texinfo -*-\n\ | |
768 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ | |
769 Return a cell array of strings naming the elements of the structure\n\ | |
770 @var{struct}. It is an error to call @code{fieldnames} with an\n\ | |
771 argument that is not a structure.\n\ | |
772 @end deftypefn") | |
773 { | |
774 octave_value retval; | |
775 | |
776 int nargin = args.length (); | |
777 | |
778 if (nargin == 1) | |
779 { | |
7336 | 780 octave_value arg = args(0); |
781 | |
782 if (arg.is_map () || arg.is_object ()) | |
4358 | 783 { |
7336 | 784 Octave_map m = arg.map_value (); |
785 | |
4744 | 786 string_vector keys = m.keys (); |
7336 | 787 |
4744 | 788 if (keys.length () == 0) |
789 retval = Cell (0, 1); | |
790 else | |
791 retval = Cell (m.keys ()); | |
4358 | 792 } |
793 else | |
794 gripe_wrong_type_arg ("fieldnames", args(0)); | |
795 } | |
796 else | |
5823 | 797 print_usage (); |
4358 | 798 |
799 return retval; | |
800 } | |
801 | |
802 DEFUN (isfield, args, , | |
803 "-*- texinfo -*-\n\ | |
804 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ | |
805 Return true if the expression @var{expr} is a structure and it includes an\n\ | |
806 element named @var{name}. The first argument must be a structure and\n\ | |
807 the second must be a string.\n\ | |
808 @end deftypefn") | |
809 { | |
810 octave_value retval; | |
811 | |
812 int nargin = args.length (); | |
813 | |
814 if (nargin == 2) | |
815 { | |
816 retval = false; | |
817 | |
5775 | 818 // FIXME -- should this work for all types that can do |
4358 | 819 // structure reference operations? |
820 | |
821 if (args(0).is_map () && args(1).is_string ()) | |
822 { | |
823 std::string key = args(1).string_value (); | |
824 | |
825 Octave_map m = args(0).map_value (); | |
826 | |
827 retval = m.contains (key) != 0; | |
828 } | |
829 } | |
830 else | |
5823 | 831 print_usage (); |
4358 | 832 |
833 return retval; | |
834 } | |
835 | |
4750 | 836 // Check that the dimensions of the input arguments are correct. |
837 | |
838 static bool | |
839 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, | |
840 bool is_cell, int dim) | |
841 { | |
4751 | 842 bool retval = true; |
4750 | 843 |
844 if (dim >= 0 && dim < c_dv.length ()) | |
845 { | |
846 if (is_cell) | |
847 { | |
4752 | 848 if (f_dv.numel () != c_dv(dim)) |
4750 | 849 { |
4751 | 850 error ("cell2struct: numel (FIELD) != size (CELL, DIM)"); |
4750 | 851 |
852 retval = false; | |
853 } | |
854 } | |
855 else | |
856 { | |
857 if (f_dv.length () > 2) | |
858 { | |
4751 | 859 error ("cell2struct: field array must be a 2-d matrix"); |
4750 | 860 |
861 retval = false; | |
862 } | |
863 else if (f_dv(0) != c_dv(dim)) | |
864 { | |
4751 | 865 error ("cell2struct: size (FIELD, 1) != length (C, DIM)"); |
4750 | 866 |
867 retval = false; | |
868 } | |
869 } | |
870 } | |
871 else | |
872 { | |
873 error ("cell2struct: DIM out of range"); | |
874 | |
875 retval = false; | |
876 } | |
877 | |
878 return retval; | |
879 } | |
880 | |
881 static void | |
5275 | 882 cell2struct_construct_idx (Array<octave_idx_type>& ra_idx1, |
883 const Array<octave_idx_type>& ra_idx2, | |
884 octave_idx_type dim, octave_idx_type fill_value) | |
4750 | 885 { |
5275 | 886 octave_idx_type iidx = 0; |
4750 | 887 |
5275 | 888 for (octave_idx_type idx = 0; idx < ra_idx1.length (); idx++) |
4750 | 889 { |
890 if (idx == dim) | |
891 ra_idx1.elem (idx) = fill_value; | |
892 else | |
893 ra_idx1.elem (idx) = ra_idx2(iidx++); | |
894 } | |
895 } | |
896 | |
897 DEFUN (cell2struct, args, , | |
898 "-*- texinfo -*-\n\ | |
4817 | 899 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ |
900 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ | |
901 must match the number of elements in @var{cell} along dimension @var{dim},\n\ | |
902 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ | |
4750 | 903 \n\ |
904 @example\n\ | |
905 @group\n\ | |
7031 | 906 A = cell2struct (@{'Peter', 'Hannah', 'Robert';\n\ |
907 185, 170, 168@},\n\ | |
6519 | 908 @{'Name','Height'@}, 1);\n\ |
4750 | 909 A(1)\n\ |
910 @result{} ans =\n\ | |
4780 | 911 @{\n\ |
912 Height = 185\n\ | |
913 Name = Peter\n\ | |
914 @}\n\ | |
4750 | 915 \n\ |
916 @end group\n\ | |
917 @end example\n\ | |
918 @end deftypefn") | |
919 { | |
920 octave_value retval; | |
921 | |
4751 | 922 if (args.length () == 3) |
4750 | 923 { |
4751 | 924 Cell c = args(0).cell_value (); |
4750 | 925 |
4751 | 926 if (! error_state) |
4750 | 927 { |
4751 | 928 octave_value field = args(1); |
4750 | 929 |
4751 | 930 // Field is either cell or character matrix. |
4750 | 931 |
5775 | 932 // FIXME -- this could be simplified if we had |
4752 | 933 // cellstr and iscellstr functions available. |
934 | |
4751 | 935 bool field_is_cell = field.is_cell (); |
4750 | 936 |
4751 | 937 Cell field_cell; |
938 charMatrix field_char; | |
4750 | 939 |
940 if (field_is_cell) | |
4751 | 941 field_cell = field.cell_value (); |
942 else | |
943 field_char = field.char_matrix_value (); | |
944 | |
945 if (! error_state) | |
4750 | 946 { |
4751 | 947 // Retrieve the dimension value. |
948 | |
5775 | 949 // FIXME -- int_value () should print out the |
4751 | 950 // conversions it does to be Matlab compatible. |
951 | |
5275 | 952 octave_idx_type dim = args(2).int_value () - 1; |
4751 | 953 |
954 if (! error_state) | |
955 { | |
956 dim_vector c_dv = c.dims (); | |
957 dim_vector field_dv = field.dims (); | |
958 | |
959 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, | |
960 dim)) | |
961 { | |
5275 | 962 octave_idx_type c_dv_length = c_dv.length (); |
4751 | 963 |
964 // Dimension vector for the Cell arrays to be | |
965 // put into the structure. | |
966 | |
967 dim_vector value_dv; | |
968 | |
969 // Initialize c_value_dv. | |
4750 | 970 |
4751 | 971 if (c_dv_length == 2) |
972 value_dv = dim_vector (1, 1); | |
973 else | |
974 value_dv.resize (c_dv_length - 1); | |
975 | |
5275 | 976 octave_idx_type idx_tmp = 0; |
4751 | 977 |
5275 | 978 for (octave_idx_type i = 0; i < c_dv_length; i++) |
4751 | 979 { |
980 if (i != dim) | |
981 value_dv.elem (idx_tmp++) = c_dv.elem (i); | |
982 } | |
983 | |
984 // All initializing is done, we can start moving | |
985 // values. | |
986 | |
987 Octave_map map; | |
988 | |
989 // If field is a cell array then we use all | |
990 // elements in array, on the other hand when | |
991 // field is a character array the number of | |
992 // elements is equals the number of rows. | |
993 | |
5275 | 994 octave_idx_type field_numel |
4751 | 995 = field_is_cell ? field_dv.numel (): field_dv(0); |
996 | |
997 // For matlab compatibility. | |
998 | |
999 if (field_numel == 0) | |
1000 map.reshape (dim_vector (0, 1)); | |
4750 | 1001 |
5275 | 1002 for (octave_idx_type i = 0; i < field_numel; i++) |
4751 | 1003 { |
1004 // Construct cell array which goes into the | |
1005 // structure together with the appropriate | |
1006 // field name. | |
1007 | |
1008 Cell c_value (value_dv); | |
1009 | |
5275 | 1010 Array<octave_idx_type> value_idx (value_dv.length (), 0); |
1011 Array<octave_idx_type> c_idx (c_dv_length, 0); | |
4751 | 1012 |
5275 | 1013 for (octave_idx_type j = 0; j < value_dv.numel (); j++) |
4751 | 1014 { |
1015 // Need to do this to construct the | |
1016 // appropriate idx for getting elements | |
1017 // from the original cell array. | |
1018 | |
1019 cell2struct_construct_idx (c_idx, value_idx, | |
1020 dim, i); | |
1021 | |
1022 c_value.elem (value_idx) = c.elem (c_idx); | |
1023 | |
1024 increment_index (value_idx, value_dv); | |
1025 } | |
1026 | |
1027 std::string field_str; | |
4750 | 1028 |
4751 | 1029 if (field_is_cell) |
1030 { | |
1031 // Matlab retrieves the field values | |
1032 // column by column. | |
1033 | |
1034 octave_value field_tmp = field_cell.elem (i); | |
1035 | |
1036 field_str = field_tmp.string_value (); | |
1037 | |
1038 if (error_state) | |
1039 { | |
1040 error ("cell2struct: fields have to be of type string"); | |
1041 break; | |
1042 } | |
1043 } | |
1044 else | |
1045 { | |
1046 field_str = field_char.row_as_string (i); | |
1047 | |
1048 if (error_state) | |
1049 return retval; | |
1050 } | |
1051 | |
6811 | 1052 if (! valid_identifier (field_str)) |
1053 { | |
1054 error ("cell2struct: invalid field name `%s'", | |
1055 field_str.c_str ()); | |
1056 break; | |
1057 } | |
1058 | |
4751 | 1059 map.reshape (value_dv); |
1060 | |
1061 map.assign (field_str, c_value); | |
1062 } | |
1063 | |
1064 if (! error_state) | |
1065 retval = map; | |
1066 } | |
4750 | 1067 } |
4751 | 1068 else |
1069 error ("cell2struct: expecting third argument to be an integer"); | |
4750 | 1070 } |
1071 else | |
4751 | 1072 error ("cell2struct: expecting second argument to be a cell or character array"); |
4750 | 1073 } |
4751 | 1074 else |
1075 error ("cell2struct: expecting first argument to be a cell array"); | |
4750 | 1076 } |
4751 | 1077 else |
5823 | 1078 print_usage (); |
4750 | 1079 |
1080 return retval; | |
1081 } | |
1082 | |
4817 | 1083 // So we can call Fcellstr directly. |
1084 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
1085 | |
1086 DEFUN (rmfield, args, , | |
1087 "-*- texinfo -*-\n\ | |
1088 @deftypefn {Built-in Function} {} rmfield (@var{s}, @var{f})\n\ | |
1089 Remove field @var{f} from the structure @var{s}. If @var{f} is a\n\ | |
1090 cell array of character strings or a character array, remove the\n\ | |
1091 named fields.\n\ | |
5642 | 1092 @seealso{cellstr, iscellstr, setfield}\n\ |
1093 @end deftypefn") | |
4817 | 1094 { |
1095 octave_value retval; | |
1096 | |
1097 int nargin = args.length (); | |
1098 | |
1099 if (nargin == 2) | |
1100 { | |
1101 Octave_map m = args(0).map_value (); | |
1102 | |
1103 octave_value_list fval = Fcellstr (args(1), 1); | |
1104 | |
1105 if (! error_state) | |
1106 { | |
1107 Cell fcell = fval(0).cell_value (); | |
1108 | |
1109 for (int i = 0; i < fcell.numel (); i++) | |
1110 { | |
1111 std::string key = fcell(i).string_value (); | |
1112 | |
1113 if (m.contains (key)) | |
1114 m.del (key); | |
1115 else | |
1116 { | |
1117 error ("rmfield: structure does not contain field %s", | |
1118 key.c_str ()); | |
1119 | |
1120 break; | |
1121 } | |
1122 } | |
1123 | |
1124 if (! error_state) | |
1125 retval = m; | |
1126 } | |
1127 } | |
1128 else | |
5823 | 1129 print_usage (); |
4817 | 1130 |
1131 return retval; | |
1132 } | |
1133 | |
1134 bool | |
6974 | 1135 octave_struct::save_ascii (std::ostream& os) |
4817 | 1136 { |
1137 Octave_map m = map_value (); | |
6639 | 1138 os << "# length: " << m.nfields () << "\n"; |
4817 | 1139 |
1140 Octave_map::iterator i = m.begin (); | |
1141 while (i != m.end ()) | |
1142 { | |
5341 | 1143 octave_value val = map.contents (i); |
4817 | 1144 |
6974 | 1145 bool b = save_ascii_data (os, val, m.key (i), false, 0); |
4817 | 1146 |
1147 if (! b) | |
1148 return os; | |
1149 | |
1150 i++; | |
1151 } | |
1152 | |
1153 return true; | |
1154 } | |
1155 | |
1156 bool | |
1157 octave_struct::load_ascii (std::istream& is) | |
1158 { | |
5275 | 1159 octave_idx_type len = 0; |
4817 | 1160 bool success = true; |
1161 | |
1162 if (extract_keyword (is, "length", len) && len >= 0) | |
1163 { | |
1164 if (len > 0) | |
1165 { | |
1166 Octave_map m (map); | |
1167 | |
5275 | 1168 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1169 { |
1170 octave_value t2; | |
1171 bool dummy; | |
1172 | |
1173 // recurse to read cell elements | |
1174 std::string nm | |
5756 | 1175 = read_ascii_data (is, std::string (), dummy, t2, j); |
4817 | 1176 |
1177 if (!is) | |
1178 break; | |
1179 | |
6293 | 1180 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1181 |
6293 | 1182 if (error_state) |
1183 { | |
1184 error ("load: internal error loading struct elements"); | |
1185 return false; | |
1186 } | |
5433 | 1187 |
6293 | 1188 m.assign (nm, tcell); |
4817 | 1189 } |
1190 | |
1191 if (is) | |
1192 map = m; | |
1193 else | |
1194 { | |
1195 error ("load: failed to load structure"); | |
1196 success = false; | |
1197 } | |
1198 } | |
1199 else if (len == 0 ) | |
6292 | 1200 map = Octave_map (dim_vector (1, 1)); |
4817 | 1201 else |
1202 panic_impossible (); | |
1203 } | |
1204 else { | |
1205 error ("load: failed to extract number of elements in structure"); | |
1206 success = false; | |
1207 } | |
1208 | |
1209 return success; | |
1210 } | |
1211 | |
1212 bool | |
1213 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) | |
1214 { | |
1215 Octave_map m = map_value (); | |
1216 | |
6639 | 1217 int32_t len = m.nfields (); |
5760 | 1218 os.write (reinterpret_cast<char *> (&len), 4); |
4817 | 1219 |
1220 Octave_map::iterator i = m.begin (); | |
1221 while (i != m.end ()) | |
1222 { | |
5341 | 1223 octave_value val = map.contents (i); |
4817 | 1224 |
5341 | 1225 bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats); |
4817 | 1226 |
1227 if (! b) | |
1228 return os; | |
1229 | |
1230 i++; | |
1231 } | |
1232 | |
1233 return true; | |
1234 } | |
1235 | |
1236 bool | |
1237 octave_struct::load_binary (std::istream& is, bool swap, | |
5760 | 1238 oct_mach_info::float_format fmt) |
4817 | 1239 { |
1240 bool success = true; | |
5828 | 1241 int32_t len; |
5760 | 1242 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4817 | 1243 return false; |
1244 if (swap) | |
4944 | 1245 swap_bytes<4> (&len); |
4817 | 1246 |
1247 if (len > 0) | |
1248 { | |
1249 Octave_map m (map); | |
1250 | |
5275 | 1251 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1252 { |
1253 octave_value t2; | |
1254 bool dummy; | |
1255 std::string doc; | |
1256 | |
1257 // recurse to read cell elements | |
1258 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
1259 dummy, t2, doc); | |
1260 | |
1261 if (!is) | |
1262 break; | |
1263 | |
6293 | 1264 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1265 |
6293 | 1266 if (error_state) |
1267 { | |
1268 error ("load: internal error loading struct elements"); | |
1269 return false; | |
1270 } | |
5433 | 1271 |
6293 | 1272 m.assign (nm, tcell); |
4817 | 1273 } |
1274 | |
1275 if (is) | |
1276 map = m; | |
1277 else | |
1278 { | |
1279 error ("load: failed to load structure"); | |
1280 success = false; | |
1281 } | |
1282 } | |
1283 else if (len == 0 ) | |
6292 | 1284 map = Octave_map (dim_vector (1, 1)); |
4817 | 1285 else |
1286 panic_impossible (); | |
1287 | |
1288 return success; | |
1289 } | |
1290 | |
1291 #if defined (HAVE_HDF5) | |
1292 | |
1293 bool | |
1294 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1295 { | |
1296 hid_t data_hid = -1; | |
1297 | |
1298 data_hid = H5Gcreate (loc_id, name, 0); | |
1299 if (data_hid < 0) return false; | |
1300 | |
1301 // recursively add each element of the structure to this group | |
1302 Octave_map m = map_value (); | |
1303 Octave_map::iterator i = m.begin (); | |
1304 while (i != m.end ()) | |
1305 { | |
5341 | 1306 octave_value val = map.contents (i); |
4817 | 1307 |
5341 | 1308 bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false, |
4817 | 1309 save_as_floats); |
1310 | |
1311 if (! retval2) | |
1312 break; | |
1313 | |
1314 i++; | |
1315 } | |
1316 | |
1317 H5Gclose (data_hid); | |
4837 | 1318 |
4817 | 1319 return true; |
1320 } | |
1321 | |
1322 bool | |
1323 octave_struct::load_hdf5 (hid_t loc_id, const char *name, | |
1324 bool have_h5giterate_bug) | |
1325 { | |
1326 bool retval = false; | |
1327 | |
1328 hdf5_callback_data dsub; | |
1329 | |
1330 herr_t retval2 = 0; | |
6292 | 1331 Octave_map m (dim_vector (1, 1)); |
4817 | 1332 int current_item = 0; |
1333 #ifdef HAVE_H5GGET_NUM_OBJS | |
1334 hsize_t num_obj = 0; | |
5060 | 1335 hid_t group_id = H5Gopen (loc_id, name); |
1336 H5Gget_num_objs (group_id, &num_obj); | |
1337 H5Gclose (group_id); | |
4817 | 1338 |
1339 while (current_item < static_cast<int> (num_obj) | |
1340 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1341 hdf5_read_next_data, &dsub)) > 0) | |
1342 #else | |
1343 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1344 hdf5_read_next_data, &dsub)) > 0) | |
1345 #endif | |
1346 { | |
5342 | 1347 octave_value t2 = dsub.tc; |
1348 | |
6293 | 1349 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1350 |
6293 | 1351 if (error_state) |
1352 { | |
1353 error ("load: internal error loading struct elements"); | |
1354 return false; | |
1355 } | |
5433 | 1356 |
6293 | 1357 m.assign (dsub.name, tcell); |
5336 | 1358 |
4817 | 1359 if (have_h5giterate_bug) |
1360 current_item++; // H5Giterate returned the last index processed | |
1361 } | |
1362 | |
1363 if (retval2 >= 0) | |
1364 { | |
1365 map = m; | |
1366 retval = true; | |
1367 } | |
1368 | |
1369 return retval; | |
1370 } | |
1371 | |
4687 | 1372 #endif |
1373 | |
5900 | 1374 mxArray * |
1375 octave_struct::as_mxArray (void) const | |
1376 { | |
1377 int nf = nfields (); | |
1378 string_vector kv = map_keys (); | |
6065 | 1379 |
1380 OCTAVE_LOCAL_BUFFER (const char *, f, nf); | |
1381 | |
5900 | 1382 for (int i = 0; i < nf; i++) |
6065 | 1383 f[i] = kv[i].c_str (); |
5900 | 1384 |
1385 mxArray *retval = new mxArray (dims (), nf, f); | |
1386 | |
1387 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1388 | |
6686 | 1389 mwSize nel = numel (); |
5900 | 1390 |
6686 | 1391 mwSize ntot = nf * nel; |
5900 | 1392 |
1393 for (int i = 0; i < nf; i++) | |
1394 { | |
1395 Cell c = map.contents (kv[i]); | |
1396 | |
1397 const octave_value *p = c.data (); | |
1398 | |
6686 | 1399 mwIndex k = 0; |
1400 for (mwIndex j = i; j < ntot; j += nf) | |
5900 | 1401 elts[j] = new mxArray (p[k++]); |
1402 } | |
1403 | |
1404 return retval; | |
1405 } | |
1406 | |
2376 | 1407 /* |
1408 ;;; Local Variables: *** | |
1409 ;;; mode: C++ *** | |
1410 ;;; End: *** | |
1411 */ |