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