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