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