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