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