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