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