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