annotate libinterp/octave-value/ov-struct.cc @ 15614:f2b8f90052fd

warn instead of throwing an error for invalid structure field names * ov-struct.cc, ov-struct.h (maybe_warn_invalid_field_name): New function. (octave_struct::subsasgn, octave_scalar_struct::dotref, octave_scalar_struct::subsasgn, Fstruct): Call maybe_warn_invalid_field_name. * pt-idx.cc (tree_index_expression::get_struct_index): Don't check for valid structure field names here.
author John W. Eaton <jwe@octave.org>
date Tue, 20 Nov 2012 13:24:51 -0500
parents 126285fce876
children 18f168880226
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
1 /*
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13951
diff changeset
3 Copyright (C) 1996-2012 John W. Eaton
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
4
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
6
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6974
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6974
diff changeset
10 option) any later version.
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
11
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
15 for more details.
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
16
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6974
diff changeset
18 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6974
diff changeset
19 <http://www.gnu.org/licenses/>.
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
20
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
21 */
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
22
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
24 #include <config.h>
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
25 #endif
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
26
3503
d14c483b3c12 [project @ 2000-02-01 04:06:07 by jwe]
jwe
parents: 3219
diff changeset
27 #include <iostream>
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
28
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
29 #include "Cell.h"
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
30 #include "defun.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
31 #include "error.h"
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
32 #include "gripes.h"
15149
62a35ae7d6a2 use forward decls for mxArray in ov.h and ov-base.h
John W. Eaton <jwe@octave.org>
parents: 15057
diff changeset
33 #include "mxarray.h"
2979
a3556d2adec9 [project @ 1997-05-15 22:35:37 by jwe]
jwe
parents: 2962
diff changeset
34 #include "oct-lvalue.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
35 #include "ov-struct.h"
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
36 #include "unwind-prot.h"
6811
3fe394f8502b [project @ 2007-08-10 19:07:34 by jwe]
jwe
parents: 6686
diff changeset
37 #include "utils.h"
2948
56be458e237f [project @ 1997-05-09 13:37:35 by jwe]
jwe
parents: 2916
diff changeset
38 #include "variables.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
39
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
40 #include "Array-util.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 8150
diff changeset
41 #include "oct-locbuf.h"
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
42
4687
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
43 #include "byte-swap.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
44 #include "ls-oct-ascii.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
45 #include "ls-oct-binary.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
46 #include "ls-hdf5.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
47 #include "ls-utils.h"
5759
ce79d238f142 [project @ 2006-04-13 13:04:32 by jwe]
jwe
parents: 5756
diff changeset
48 #include "pr-output.h"
4687
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
49
3219
30770ba4457a [project @ 1998-11-13 03:44:31 by jwe]
jwe
parents: 2985
diff changeset
50 DEFINE_OCTAVE_ALLOCATOR(octave_struct);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
51
4612
d44675070f1a [project @ 2003-11-14 19:49:56 by jwe]
jwe
parents: 4604
diff changeset
52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct");
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
53
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
54 // How many levels of structure elements should we print?
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
55 static int Vstruct_levels_to_print = 2;
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
56
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
57 // TRUE means print struct array contents, up to the number of levels
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
58 // specified by struct_levels_to_print.
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
59 static bool Vprint_struct_array_contents = false;
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
60
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
61 octave_base_value *
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
62 octave_struct::try_narrowing_conversion (void)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
63 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
64 octave_base_value *retval = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
65
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
66 if (numel () == 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
67 retval = new octave_scalar_struct (map.checkelem (0));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
68
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
69 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
70 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
71
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
72 Cell
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
73 octave_struct::dotref (const octave_value_list& idx, bool auto_add)
2962
5e0fe4c5d52f [project @ 1997-05-12 03:13:57 by jwe]
jwe
parents: 2954
diff changeset
74 {
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
75 Cell retval;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
76
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
77 assert (idx.length () == 1);
2962
5e0fe4c5d52f [project @ 1997-05-12 03:13:57 by jwe]
jwe
parents: 2954
diff changeset
78
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
79 std::string nm = idx(0).string_value ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
80
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
81 octave_map::const_iterator p = map.seek (nm);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
82
4219
23d06c9e1edd [project @ 2002-12-06 21:29:17 by jwe]
jwe
parents: 4197
diff changeset
83 if (p != map.end ())
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
84 retval = map.contents (p);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
85 else if (auto_add)
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10315
diff changeset
86 retval = (numel () == 0) ? Cell (dim_vector (1, 1)) : Cell (dims ());
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
87 else
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14624
diff changeset
88 error ("structure has no member '%s'", nm.c_str ());
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
89
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
90 return retval;
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
91 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
92
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
93 #if 0
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
94 static void
10370
9c4daf174387 implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
95 gripe_invalid_index1 (void)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
96 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
97 error ("invalid index for structure array");
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
98 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
99 #endif
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
100
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
101 static void
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
102 gripe_invalid_index_for_assignment (void)
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
103 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
104 error ("invalid index for structure array assignment");
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
105 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
106
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
107 static void
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
108 gripe_invalid_index_type (const std::string& nm, char t)
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
109 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
110 error ("%s cannot be indexed with %c", nm.c_str (), t);
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
111 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
112
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
113 static void
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
114 gripe_failed_assignment (void)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
115 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
116 error ("assignment to structure element failed");
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
117 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
118
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
119 static void
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
120 maybe_warn_invalid_field_name (const std::string& key, const char *who)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
121 {
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
122 if (! valid_identifier (key))
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
123 {
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
124 if (who)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
125 warning_with_id ("Octave:matlab-incompatible",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
126 "%s: invalid structure field name '%s'",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
127 who, key.c_str ());
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
128 else
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
129 warning_with_id ("Octave:matlab-incompatible",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
130 "invalid structure field name '%s'",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
131 key.c_str ());
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
132 }
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
133 }
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
134
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
135 octave_value_list
4247
fc9a075d10fb [project @ 2002-12-30 23:05:27 by jwe]
jwe
parents: 4219
diff changeset
136 octave_struct::subsref (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
137 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
138 int nargout)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
139 {
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
140 octave_value_list retval;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
141
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
142 int skip = 1;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
143
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
144 switch (type[0])
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
145 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
146 case '(':
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
147 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
148 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
149 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
150 std::list<octave_value_list>::const_iterator p = idx.begin ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
151 octave_value_list key_idx = *++p;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
152
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
153 const Cell tmp = dotref (key_idx);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
154
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
155 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
156 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
157 const Cell t = tmp.index (idx.front ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
158
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
159 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
160
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
161 // We handled two index elements, so tell
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
162 // next_subsref to skip both of them.
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
163
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
164 skip++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
165 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
166 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
167 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
168 retval(0) = do_index_op (idx.front ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
169 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
170 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
171
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
172 case '.':
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
173 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
174 if (map.numel () > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
175 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
176 const Cell t = dotref (idx.front ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
177
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
178 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
179 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
180 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
181 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
182
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
183 case '{':
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
184 gripe_invalid_index_type (type_name (), type[0]);
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
185 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
186
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
187 default:
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
188 panic_impossible ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
189 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
190
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5760
diff changeset
191 // FIXME -- perhaps there should be an
4994
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
192 // octave_value_list::next_subsref member function? See also
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
193 // octave_user_function::subsref.
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
194
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
195 if (idx.size () > 1)
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
196 retval = retval(0).next_subsref (nargout, type, idx, skip);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
197
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
198 return retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
199 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
200
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
201 octave_value
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
202 octave_struct::subsref (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
203 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
204 bool auto_add)
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
205 {
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
206 octave_value retval;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
207
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
208 int skip = 1;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
209
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
210 switch (type[0])
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
211 {
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
212 case '(':
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
213 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
214 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
215 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
216 std::list<octave_value_list>::const_iterator p = idx.begin ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
217 octave_value_list key_idx = *++p;
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
218
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
219 const Cell tmp = dotref (key_idx, auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
220
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
221 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
222 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
223 const Cell t = tmp.index (idx.front (), auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
224
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
225 retval = (t.length () == 1) ? t(0) : octave_value (t, true);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
226
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
227 // We handled two index elements, so tell
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
228 // next_subsref to skip both of them.
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
229
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
230 skip++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
231 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
232 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
233 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
234 retval = do_index_op (idx.front (), auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
235 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
236 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
237
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
238 case '.':
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
239 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
240 if (map.numel () > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
241 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
242 const Cell t = dotref (idx.front (), auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
243
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
244 retval = (t.length () == 1) ? t(0) : octave_value (t, true);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
245 }
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
246 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
247 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
248
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
249 case '{':
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
250 gripe_invalid_index_type (type_name (), type[0]);
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
251 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
252
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
253 default:
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
254 panic_impossible ();
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
255 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
256
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
257 // FIXME -- perhaps there should be an
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
258 // octave_value_list::next_subsref member function? See also
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
259 // octave_user_function::subsref.
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
260
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
261 if (idx.size () > 1)
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
262 retval = retval.next_subsref (auto_add, type, idx, skip);
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
263
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
264 return retval;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
265 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
266
8031
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
267 /*
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
268 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
269 %! x(1).a.a = 1;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
270 %! x(2).a.a = 2;
8031
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
271 %! assert (size (x), [1, 2]);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
272 %! assert (x(1).a.a, 1);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
273 %! assert (x(2).a.a, 2);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
274 */
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
275
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
276 octave_value
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
277 octave_struct::numeric_conv (const octave_value& val,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
278 const std::string& type)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
279 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
280 octave_value retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
281
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
282 if (type.length () > 0 && type[0] == '.' && ! val.is_map ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
283 retval = octave_map ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
284 else
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
285 retval = val;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
286
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
287 return retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
288 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
289
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
290 octave_value
4247
fc9a075d10fb [project @ 2002-12-30 23:05:27 by jwe]
jwe
parents: 4219
diff changeset
291 octave_struct::subsasgn (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
292 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
293 const octave_value& rhs)
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
294 {
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
295 octave_value retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
296
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
297 int n = type.length ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
298
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
299 octave_value t_rhs = rhs;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
300
9286
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
301 if (idx.front ().empty ())
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
302 {
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
303 error ("missing index in indexed assignment");
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
304 return retval;
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
305 }
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
306
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
307 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.'))
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
308 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
309 switch (type[0])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
310 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
311 case '(':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
312 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
313 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
314 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
315 std::list<octave_value_list>::const_iterator p = idx.begin ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
316 octave_value_list t_idx = *p;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
317
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
318 octave_value_list key_idx = *++p;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
319
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
320 assert (key_idx.length () == 1);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
321
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
322 std::string key = key_idx(0).string_value ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
323
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
324 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
325
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
326 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
327 return retval;
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
328
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
329 std::list<octave_value_list> next_idx (idx);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
330
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
331 // We handled two index elements, so subsasgn to
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
332 // needs to skip both of them.
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
333
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
334 next_idx.erase (next_idx.begin ());
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
335 next_idx.erase (next_idx.begin ());
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
336
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
337 std::string next_type = type.substr (2);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
338
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
339 Cell tmpc (1, 1);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
340 octave_map::iterator pkey = map.seek (key);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
341 if (pkey != map.end ())
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
342 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
343 map.contents (pkey).make_unique ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
344 tmpc = map.contents (pkey).index (idx.front (), true);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
345 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
346
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
347 // FIXME: better code reuse? cf. octave_cell::subsasgn and the case below.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
348 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
349 {
8579
7e0f36dfefbe implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents: 8564
diff changeset
350 if (tmpc.numel () == 1)
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
351 {
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
352 octave_value& tmp = tmpc(0);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
353
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
354 bool orig_undefined = tmp.is_undefined ();
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
355
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
356 if (orig_undefined || tmp.is_zero_by_zero ())
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
357 {
8564
b0f803b5ce41 more indexing fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 8551
diff changeset
358 tmp = octave_value::empty_conv (next_type, rhs);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
359 tmp.make_unique (); // probably a no-op.
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
360 }
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
361 else
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8579
diff changeset
362 // optimization: ignore the copy still stored inside our map.
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8579
diff changeset
363 tmp.make_unique (1);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
364
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
365 if (! error_state)
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
366 t_rhs = (orig_undefined
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
367 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
368 : tmp.subsasgn (next_type, next_idx, rhs));
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
369 }
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
370 else
8579
7e0f36dfefbe implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents: 8564
diff changeset
371 gripe_indexed_cs_list ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
372 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
373 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
374 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
375 gripe_invalid_index_for_assignment ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
376 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
377 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
378
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
379 case '.':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
380 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
381 octave_value_list key_idx = idx.front ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
382
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
383 assert (key_idx.length () == 1);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
384
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
385 std::string key = key_idx(0).string_value ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
386
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
387 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
388
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
389 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
390 return retval;
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
391
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
392 std::list<octave_value_list> next_idx (idx);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
393
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
394 next_idx.erase (next_idx.begin ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
395
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
396 std::string next_type = type.substr (1);
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
397
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8579
diff changeset
398 Cell tmpc (1, 1);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
399 octave_map::iterator pkey = map.seek (key);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
400 if (pkey != map.end ())
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
401 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
402 map.contents (pkey).make_unique ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
403 tmpc = map.contents (pkey);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
404 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
405
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
406 // FIXME: better code reuse?
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
407 if (! error_state)
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
408 {
8579
7e0f36dfefbe implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents: 8564
diff changeset
409 if (tmpc.numel () == 1)
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
410 {
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
411 octave_value& tmp = tmpc(0);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
412
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
413 bool orig_undefined = tmp.is_undefined ();
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
414
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
415 if (orig_undefined || tmp.is_zero_by_zero ())
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
416 {
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8579
diff changeset
417 tmp = octave_value::empty_conv (next_type, rhs);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
418 tmp.make_unique (); // probably a no-op.
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
419 }
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
420 else
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
421 // optimization: ignore the copy still stored inside our map.
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
422 tmp.make_unique (1);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
423
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
424 if (! error_state)
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
425 t_rhs = (orig_undefined
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
426 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
427 : tmp.subsasgn (next_type, next_idx, rhs));
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
428 }
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
429 else
8579
7e0f36dfefbe implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents: 8564
diff changeset
430 gripe_indexed_cs_list ();
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
431 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
432 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
433 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
434
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
435 case '{':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
436 gripe_invalid_index_type (type_name (), type[0]);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
437 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
438
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
439 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
440 panic_impossible ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
441 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
442 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
443
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
444 if (! error_state)
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
445 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
446 switch (type[0])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
447 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
448 case '(':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
449 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
450 if (n > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
451 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
452 std::list<octave_value_list>::const_iterator p = idx.begin ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
453 octave_value_list key_idx = *++p;
8587
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
454 octave_value_list idxf = idx.front ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
455
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
456 assert (key_idx.length () == 1);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
457
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
458 std::string key = key_idx(0).string_value ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
459
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
460 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
461
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
462 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
463 return retval;
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
464
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
465 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
466 {
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
467 if (t_rhs.is_cs_list ())
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
468 {
8587
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
469 Cell tmp_cell = Cell (t_rhs.list_value ());
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
470
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
471 // Inquire the proper shape of the RHS.
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
472
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
473 dim_vector didx = dims ().redim (idxf.length ());
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
474 for (octave_idx_type k = 0; k < idxf.length (); k++)
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
475 if (! idxf(k).is_magic_colon ()) didx(k) = idxf(k).numel ();
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
476
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
477 if (didx.numel () == tmp_cell.numel ())
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
478 tmp_cell = tmp_cell.reshape (didx);
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
479
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
480
10749
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
481 map.assign (idxf, key, tmp_cell);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
482
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
483 if (! error_state)
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
484 {
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
485 count++;
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
486 retval = octave_value (this);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
487 }
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
488 else
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
489 gripe_failed_assignment ();
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
490 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
491 else
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
492 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
493 const octave_map& cmap = const_cast<const octave_map &> (map);
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
494 // cast map to const reference to avoid forced key insertion.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
495 if (idxf.all_scalars ()
8587
35656d6ad061 properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
496 || cmap.contents (key).index (idxf, true).numel () == 1)
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
497 {
10749
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
498 map.assign (idxf, key, Cell (t_rhs.storable_value ()));
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
499 if (! error_state)
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
500 {
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
501 count++;
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
502 retval = octave_value (this);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
503 }
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
504 else
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
505 gripe_failed_assignment ();
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
506 }
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
507 else if (! error_state)
10030
83bb2a78c07d improve simple assignments error checks and messages
Jaroslav Hajek <highegg@gmail.com>
parents: 9892
diff changeset
508 gripe_nonbraced_cs_list_assignment ();
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
509 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
510 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
511 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
512 gripe_failed_assignment ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
513 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
514 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
515 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
516 if (t_rhs.is_map () || t_rhs.is_object ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
517 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
518 octave_map rhs_map = t_rhs.map_value ();
4197
40f76ce7a051 [project @ 2002-11-21 18:56:25 by jwe]
jwe
parents: 4192
diff changeset
519
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
520 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
521 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
522 map.assign (idx.front (), rhs_map);
5592
61d6cebd243b [project @ 2006-01-12 17:55:22 by dbateman]
dbateman
parents: 5582
diff changeset
523
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
524 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
525 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
526 count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
527 retval = octave_value (this);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
528 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
529 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
530 gripe_failed_assignment ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
531 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
532 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
533 error ("invalid structure assignment");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
534 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
535 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
536 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
537 if (t_rhs.is_null_value ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
538 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
539 map.delete_elements (idx.front ());
5592
61d6cebd243b [project @ 2006-01-12 17:55:22 by dbateman]
dbateman
parents: 5582
diff changeset
540
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
541 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
542 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
543 count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
544 retval = octave_value (this);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
545 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
546 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
547 gripe_failed_assignment ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
548 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
549 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
550 error ("invalid structure assignment");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
551 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
552 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
553 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
554 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
555
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
556 case '.':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
557 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
558 octave_value_list key_idx = idx.front ();
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
559
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
560 assert (key_idx.length () == 1);
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
561
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
562 std::string key = key_idx(0).string_value ();
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
563
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
564 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
565
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
566 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
567 return retval;
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
568
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
569 if (t_rhs.is_cs_list ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
570 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
571 Cell tmp_cell = Cell (t_rhs.list_value ());
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
572
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
573 // The shape of the RHS is irrelevant, we just want
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
574 // the number of elements to agree and to preserve the
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
575 // shape of the left hand side of the assignment.
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
576
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
577 if (numel () == tmp_cell.numel ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
578 tmp_cell = tmp_cell.reshape (dims ());
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
579
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
580 map.setfield (key, tmp_cell);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
581 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
582 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
583 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
584 Cell tmp_cell(1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
585 tmp_cell(0) = t_rhs.storable_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
586 map.setfield (key, tmp_cell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
587 }
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
588
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
589 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
590 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
591 count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
592 retval = octave_value (this);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
593 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
594 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
595 gripe_failed_assignment ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
596 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
597 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
598
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
599 case '{':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
600 gripe_invalid_index_type (type_name (), type[0]);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
601 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
602
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
603 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
604 panic_impossible ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
605 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
606 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
607 else
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
608 gripe_failed_assignment ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
609
10754
92eb5fb58ebc fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10753
diff changeset
610 retval.maybe_mutate ();
92eb5fb58ebc fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10753
diff changeset
611
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
612 return retval;
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
613 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
614
7046
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
615 octave_value
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
616 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok)
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
617 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
618 // octave_map handles indexing itself.
8679
280fae940bb0 optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8587
diff changeset
619 return map.index (idx, resize_ok);
7046
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
620 }
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
621
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
622 size_t
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
623 octave_struct::byte_size (void) const
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
624 {
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
625 // Neglect the size of the fieldnames.
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
626
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
627 size_t retval = 0;
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
628
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
629 for (octave_map::const_iterator p = map.begin (); p != map.end (); p++)
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
630 {
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
631 std::string key = map.key (p);
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
632
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
633 octave_value val = octave_value (map.contents (p));
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
634
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
635 retval += val.byte_size ();
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
636 }
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
637
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
638 return retval;
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
639 }
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
640
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
641 void
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
642 octave_struct::print (std::ostream& os, bool) const
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
643 {
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
644 print_raw (os);
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
645 }
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
646
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
647 void
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
648 octave_struct::print_raw (std::ostream& os, bool) const
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
649 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10030
diff changeset
650 unwind_protect frame;
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
651
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10030
diff changeset
652 frame.protect_var (Vstruct_levels_to_print);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
653
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
654 if (Vstruct_levels_to_print >= 0)
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
655 {
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
656 bool max_depth_reached = Vstruct_levels_to_print-- == 0;
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
657
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
658 bool print_fieldnames_only
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
659 = (max_depth_reached || ! Vprint_struct_array_contents);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
660
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
661 increment_indent_level ();
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
662
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
663 newline (os);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
664 indent (os);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
665 dim_vector dv = dims ();
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
666 os << dv.str () << " struct array containing the fields:";
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
667 newline (os);
3932
2e2e32198722 [project @ 2002-05-07 18:10:44 by jwe]
jwe
parents: 3548
diff changeset
668
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
669 increment_indent_level ();
4604
cba347c642e2 [project @ 2003-11-13 04:38:05 by jwe]
jwe
parents: 4561
diff changeset
670
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
671 string_vector key_list = map.fieldnames ();
5880
84ca47e311b3 [project @ 2006-07-06 22:34:48 by jwe]
jwe
parents: 5828
diff changeset
672
84ca47e311b3 [project @ 2006-07-06 22:34:48 by jwe]
jwe
parents: 5828
diff changeset
673 for (octave_idx_type i = 0; i < key_list.length (); i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
674 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
675 std::string key = key_list[i];
5880
84ca47e311b3 [project @ 2006-07-06 22:34:48 by jwe]
jwe
parents: 5828
diff changeset
676
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
677 Cell val = map.contents (key);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
678
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
679 newline (os);
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
680
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
681 if (print_fieldnames_only)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
682 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
683 indent (os);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
684 os << key;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
685 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
686 else
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
687 {
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
688 octave_value tmp (val);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
689 tmp.print_with_name (os, key);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
690 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
691 }
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
692
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
693 if (print_fieldnames_only)
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
694 newline (os);
4604
cba347c642e2 [project @ 2003-11-13 04:38:05 by jwe]
jwe
parents: 4561
diff changeset
695
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
696 decrement_indent_level ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
697 decrement_indent_level ();
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
698 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
699 else
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
700 {
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
701 indent (os);
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
702 os << "<structure>";
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
703 newline (os);
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
704 }
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
705 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
706
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
707 bool
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
708 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
709 {
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
710 bool retval = false;
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
711
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
712 indent (os);
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
713
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
714 if (Vstruct_levels_to_print < 0)
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
715 os << name << " = ";
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
716 else
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
717 {
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
718 os << name << " =";
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
719 newline (os);
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
720 retval = true;
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
721 }
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
722
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
723 return retval;
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
724 }
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
725
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
726 static bool
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
727 scalar (const dim_vector& dims)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
728 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
729 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1;
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
730 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
731
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
732
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
733 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
734 octave_struct::save_ascii (std::ostream& os)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
735 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
736 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
737
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
738 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
739
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
740 const dim_vector dv = dims ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
741
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
742 os << "# ndims: " << dv.length () << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
743
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
744 for (int i = 0; i < dv.length (); i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
745 os << " " << dv (i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
746 os << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
747
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
748 os << "# length: " << nf << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
749
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
750 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
751 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
752 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
753
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
754 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
755 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
756 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
757
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
758 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
759
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
760 bool b = save_ascii_data (os, val, key, false, 0);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
761
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
762 if (! b)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
763 return os;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
764 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
765
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
766 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
767 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
768
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
769 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
770 octave_struct::load_ascii (std::istream& is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
771 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
772 octave_idx_type len = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
773 dim_vector dv (1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
774 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
775
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
776 // KLUGE: earlier Octave versions did not save extra dimensions with struct,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
777 // and as a result did not preserve dimensions for empty structs.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
778 // The default dimensions were 1x1, which we want to preserve.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
779 string_vector keywords(2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
780
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
781 keywords[0] = "ndims";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
782 keywords[1] = "length";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
783
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
784 std::string kw;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
785
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
786 if (extract_keyword (is, keywords, kw, len, true))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
787 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
788 if (kw == keywords[0])
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
789 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
790 int mdims = std::max (static_cast<int> (len), 2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
791 dv.resize (mdims);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
792 for (int i = 0; i < mdims; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
793 is >> dv(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
794
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
795 success = extract_keyword (is, keywords[1], len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
796 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
797 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
798 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
799 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
800
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
801 if (success && len >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
802 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
803 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
804 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
805 octave_map m (dv);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
806
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
807 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
808 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
809 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
810 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
811
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
812 // recurse to read cell elements
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
813 std::string nm
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
814 = read_ascii_data (is, std::string (), dummy, t2, j);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
815
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
816 if (!is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
817 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
818
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
819 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
820
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
821 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
822 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
823 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
824 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
825 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
826
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
827 m.setfield (nm, tcell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
828 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
829
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
830 if (is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
831 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
832 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
833 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
834 error ("load: failed to load structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
835 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
836 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
837 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
838 else if (len == 0 )
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
839 map = octave_map (dv);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
840 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
841 panic_impossible ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
842 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
843 else {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
844 error ("load: failed to extract number of elements in structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
845 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
846 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
847
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
848 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
849 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
850
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
851 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
852 octave_struct::save_binary (std::ostream& os, bool& save_as_floats)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
853 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
854 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
855
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
856 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
857
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
858 dim_vector d = dims ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
859 if (d.length () < 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
860 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
861
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
862 // Use negative value for ndims
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
863 int32_t di = - d.length ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
864 os.write (reinterpret_cast<char *> (&di), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
865 for (int i = 0; i < d.length (); i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
866 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
867 di = d(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
868 os.write (reinterpret_cast<char *> (&di), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
869 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
870
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
871 int32_t len = nf;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
872 os.write (reinterpret_cast<char *> (&len), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
873
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
874 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
875 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
876 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
877
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
878 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
879 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
880 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
881
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
882 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
883
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
884 bool b = save_binary_data (os, val, key, "", 0, save_as_floats);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
885
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
886 if (! b)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
887 return os;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
888 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
889
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
890 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
891 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
892
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
893 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
894 octave_struct::load_binary (std::istream& is, bool swap,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
895 oct_mach_info::float_format fmt)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
896 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
897 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
898 int32_t len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
899 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
900 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
901 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
902 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
903
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
904 dim_vector dv (1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
905
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
906 if (len < 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
907 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
908 // We have explicit dimensions.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
909 int mdims = -len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
910
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
911 int32_t di;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
912 dv.resize (mdims);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
913
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
914 for (int i = 0; i < mdims; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
915 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
916 if (! is.read (reinterpret_cast<char *> (&di), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
917 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
918 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
919 swap_bytes<4> (&di);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
920 dv(i) = di;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
921 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
922
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
923 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
924 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
925 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
926 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
927 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
928
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
929 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
930 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
931 octave_map m (dv);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
932
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
933 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
934 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
935 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
936 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
937 std::string doc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
938
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
939 // recurse to read cell elements
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
940 std::string nm = read_binary_data (is, swap, fmt, std::string (),
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
941 dummy, t2, doc);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
942
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
943 if (!is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
944 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
945
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
946 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
947
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
948 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
949 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
950 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
951 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
952 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
953
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
954 m.setfield (nm, tcell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
955 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
956
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
957 if (is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
958 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
959 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
960 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
961 error ("load: failed to load structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
962 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
963 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
964 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
965 else if (len == 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
966 map = octave_map (dv);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
967 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
968 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
969
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
970 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
971 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
972
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
973 #if defined (HAVE_HDF5)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
974
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
975 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
976 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
977 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
978 hid_t data_hid = -1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
979
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
980 #if HAVE_HDF5_18
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
981 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
982 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
983 data_hid = H5Gcreate (loc_id, name, 0);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
984 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
985 if (data_hid < 0) return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
986
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
987 // recursively add each element of the structure to this group
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
988 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
989
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
990 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
991
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
992 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
993 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
994 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
995
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
996 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
997 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
998 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
999
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1000 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1001
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1002 bool retval2 = add_hdf5_data (data_hid, val, key, "", false,
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1003 save_as_floats);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1004
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1005 if (! retval2)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1006 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1007 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1008
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1009 H5Gclose (data_hid);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1010
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1011 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1012 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1013
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1014 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1015 octave_struct::load_hdf5 (hid_t loc_id, const char *name)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1016 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1017 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1018
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1019 hdf5_callback_data dsub;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1020
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1021 herr_t retval2 = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1022 octave_map m (dim_vector (1, 1));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1023 int current_item = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1024 hsize_t num_obj = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1025 #if HAVE_HDF5_18
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1026 hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1027 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1028 hid_t group_id = H5Gopen (loc_id, name);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1029 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1030 H5Gget_num_objs (group_id, &num_obj);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1031 H5Gclose (group_id);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1032
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1033 // FIXME -- fields appear to be sorted alphabetically on loading.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1034 // Why is that happening?
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1035
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1036 while (current_item < static_cast<int> (num_obj)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1037 && (retval2 = H5Giterate (loc_id, name, &current_item,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1038 hdf5_read_next_data, &dsub)) > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1039 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1040 octave_value t2 = dsub.tc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1041
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1042 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1043
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1044 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1045 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1046 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1047 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1048 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1049
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1050 m.setfield (dsub.name, tcell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1051
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1052 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1053
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1054 if (retval2 >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1055 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1056 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1057 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1058 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1059
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1060 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1061 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1062
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1063 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1064
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1065 mxArray *
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1066 octave_struct::as_mxArray (void) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1067 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1068 int nf = nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1069 string_vector kv = map_keys ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1070
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1071 OCTAVE_LOCAL_BUFFER (const char *, f, nf);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1072
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1073 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1074 f[i] = kv[i].c_str ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1075
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1076 mxArray *retval = new mxArray (dims (), nf, f);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1077
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1078 mxArray **elts = static_cast<mxArray **> (retval->get_data ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1079
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1080 mwSize nel = numel ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1081
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1082 mwSize ntot = nf * nel;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1083
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1084 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1085 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1086 Cell c = map.contents (kv[i]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1087
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1088 const octave_value *p = c.data ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1089
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1090 mwIndex k = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1091 for (mwIndex j = i; j < ntot; j += nf)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1092 elts[j] = new mxArray (p[k++]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1093 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1094
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1095 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1096 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1097
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1098 octave_value
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1099 octave_struct::fast_elem_extract (octave_idx_type n) const
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1100 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1101 if (n < map.numel ())
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1102 return map.checkelem (n);
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1103 else
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1104 return octave_value ();
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1105 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1106
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1107 bool
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1108 octave_struct::fast_elem_insert (octave_idx_type n,
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1109 const octave_value& x)
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1110 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1111 bool retval = false;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1112
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1113 if (n < map.numel ())
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1114 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1115 // To avoid copying the scalar struct, it just stores a pointer to
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1116 // itself.
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1117 const octave_scalar_map *sm_ptr;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1118 void *here = reinterpret_cast<void *>(&sm_ptr);
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
1119 return (x.get_rep ().fast_elem_insert_self (here, btyp_struct)
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1120 && map.fast_elem_insert (n, *sm_ptr));
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1121 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1122
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1123 return retval;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1124 }
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1125 DEFINE_OCTAVE_ALLOCATOR(octave_scalar_struct);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1126
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1127 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_scalar_struct, "scalar struct", "struct");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1128
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1129 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1130 octave_scalar_struct::dotref (const octave_value_list& idx, bool auto_add)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1131 {
15613
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1132 octave_value retval;
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1133
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1134 assert (idx.length () == 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1135
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1136 std::string nm = idx(0).string_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1137
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1138 maybe_warn_invalid_field_name (nm, "subsref");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1139
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1140 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1141 return retval;
15613
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1142
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1143 retval = map.getfield (nm);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1144
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1145 if (! auto_add && retval.is_undefined ())
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14624
diff changeset
1146 error ("structure has no member '%s'", nm.c_str ());
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1147
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1148 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1149 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1150
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1151 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1152 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1153 const std::list<octave_value_list>& idx)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1154 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1155 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1156
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1157 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1158 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1159 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1160
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1161 retval = dotref (idx.front ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1162
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1163 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1164 retval = retval.next_subsref (type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1165 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1166 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1167 retval = to_array ().subsref (type, idx);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1168
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1169 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1170 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1171
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1172 octave_value_list
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1173 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1174 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1175 int nargout)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1176 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1177 octave_value_list retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1178
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1179 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1180 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1181 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1182
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1183 retval(0) = dotref (idx.front ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1184
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1185 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1186 retval = retval(0).next_subsref (nargout, type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1187 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1188 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1189 retval = to_array ().subsref (type, idx, nargout);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1190
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1191 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1192 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1193
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1194 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1195 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1196 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1197 bool auto_add)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1198 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1199 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1200
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1201 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1202 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1203 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1204
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1205 retval = dotref (idx.front (), auto_add);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1206
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1207 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1208 retval = retval.next_subsref (auto_add, type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1209 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1210 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1211 retval = to_array ().subsref (type, idx, auto_add);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1212
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1213 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1214 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1215
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1216 /*
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1217 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1218 %! x(1).a.a = 1;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1219 %! x(2).a.a = 2;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1220 %! assert (size (x), [1, 2]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1221 %! assert (x(1).a.a, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1222 %! assert (x(2).a.a, 2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1223 */
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1224
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1225 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1226 octave_scalar_struct::numeric_conv (const octave_value& val,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1227 const std::string& type)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1228 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1229 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1230
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1231 if (type.length () > 0 && type[0] == '.' && ! val.is_map ())
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1232 retval = octave_map ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1233 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1234 retval = val;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1235
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1236 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1237 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1238
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1239 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1240 octave_scalar_struct::subsasgn (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1241 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1242 const octave_value& rhs)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1243 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1244 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1245
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1246 if (idx.front ().empty ())
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1247 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1248 error ("missing index in indexed assignment");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1249 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1250 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1251
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1252 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1253 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1254 int n = type.length ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1255
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1256 octave_value t_rhs = rhs;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1257
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1258 octave_value_list key_idx = idx.front ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1259
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1260 assert (key_idx.length () == 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1261
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1262 std::string key = key_idx(0).string_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1263
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1264 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1265
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1266 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1267 return retval;
15613
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1268
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1269 if (n > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1270 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1271 std::list<octave_value_list> next_idx (idx);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1272
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1273 next_idx.erase (next_idx.begin ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1274
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1275 std::string next_type = type.substr (1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1276
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1277 octave_value tmp;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1278 octave_map::iterator pkey = map.seek (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1279 if (pkey != map.end ())
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1280 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1281 map.contents (pkey).make_unique ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1282 tmp = map.contents (pkey);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1283 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1284
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1285 if (! error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1286 {
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1287 bool orig_undefined = tmp.is_undefined ();
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1288
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1289 if (orig_undefined || tmp.is_zero_by_zero ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1290 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1291 tmp = octave_value::empty_conv (next_type, rhs);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1292 tmp.make_unique (); // probably a no-op.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1293 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1294 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1295 // optimization: ignore the copy still stored inside our map.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1296 tmp.make_unique (1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1297
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1298 if (! error_state)
12181
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1299 t_rhs = (orig_undefined
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1300 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
88ff30dcc048 fix another bug in class assignment to undefined object with index
John W. Eaton <jwe@octave.org>
parents: 12143
diff changeset
1301 : tmp.subsasgn (next_type, next_idx, rhs));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1302 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1303 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1304
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1305 if (! error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1306 map.setfield (key, t_rhs.storable_value ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1307 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1308 gripe_failed_assignment ();
10749
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
1309
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
1310 count++;
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
1311 retval = this;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1312 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1313 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1314 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1315 // Forward this case to octave_struct.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1316 octave_value tmp (new octave_struct (octave_map (map)));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1317 retval = tmp.subsasgn (type, idx, rhs);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1318 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1319
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1320 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1321 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1322
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1323 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1324 octave_scalar_struct::do_index_op (const octave_value_list& idx, bool resize_ok)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1325 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1326 // octave_map handles indexing itself.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1327 return octave_map (map).index (idx, resize_ok);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1328 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1329
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1330 size_t
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1331 octave_scalar_struct::byte_size (void) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1332 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1333 // Neglect the size of the fieldnames.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1334
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1335 size_t retval = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1336
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1337 for (octave_map::const_iterator p = map.begin (); p != map.end (); p++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1338 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1339 std::string key = map.key (p);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1340
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1341 octave_value val = octave_value (map.contents (p));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1342
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1343 retval += val.byte_size ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1344 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1345
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1346 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1347 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1348
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1349 void
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1350 octave_scalar_struct::print (std::ostream& os, bool) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1351 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1352 print_raw (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1353 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1354
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1355 void
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1356 octave_scalar_struct::print_raw (std::ostream& os, bool) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1357 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1358 unwind_protect frame;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1359
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1360 frame.protect_var (Vstruct_levels_to_print);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1361
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1362 if (Vstruct_levels_to_print >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1363 {
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1364 bool max_depth_reached = Vstruct_levels_to_print-- == 0;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1365
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1366 bool print_fieldnames_only = max_depth_reached;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1367
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1368 increment_indent_level ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1369
13112
969ed305dde5 Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12483
diff changeset
1370 if (! Vcompact_format)
969ed305dde5 Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12483
diff changeset
1371 newline (os);
969ed305dde5 Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12483
diff changeset
1372
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1373 indent (os);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1374 os << "scalar structure containing the fields:";
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1375 newline (os);
13112
969ed305dde5 Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12483
diff changeset
1376 if (! Vcompact_format)
969ed305dde5 Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12483
diff changeset
1377 newline (os);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1378
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1379 increment_indent_level ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1380
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1381 string_vector key_list = map.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1382
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1383 for (octave_idx_type i = 0; i < key_list.length (); i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1384 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1385 std::string key = key_list[i];
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1386
12143
ed129a03f93c octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1387 octave_value val = map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1388
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1389 if (print_fieldnames_only)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1390 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1391 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1392 os << key;
12143
ed129a03f93c octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1393 dim_vector dv = val.dims ();
ed129a03f93c octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1394 os << ": " << dv.str () << " " << val.type_name ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1395 newline (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1396 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1397 else
12143
ed129a03f93c octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1398 val.print_with_name (os, key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1399 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1400
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1401 decrement_indent_level ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1402 decrement_indent_level ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1403 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1404 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1405 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1406 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1407 os << "<structure>";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1408 newline (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1409 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1410 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1411
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1412 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1413 octave_scalar_struct::print_name_tag (std::ostream& os, const std::string& name) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1414 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1415 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1416
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1417 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1418
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1419 if (Vstruct_levels_to_print < 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1420 os << name << " = ";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1421 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1422 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1423 os << name << " =";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1424 newline (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1425 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1426 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1427
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1428 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1429 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1430
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1431 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1432 octave_scalar_struct::save_ascii (std::ostream& os)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1433 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1434 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1435
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1436 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1437
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1438 const dim_vector dv = dims ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1439
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1440 os << "# ndims: " << dv.length () << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1441
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1442 for (int i = 0; i < dv.length (); i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1443 os << " " << dv (i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1444 os << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1445
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1446 os << "# length: " << nf << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1447
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1448 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1449 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1450 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1451
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1452 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1453 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1454 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1455
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1456 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1457
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1458 bool b = save_ascii_data (os, val, key, false, 0);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1459
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1460 if (! b)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1461 return os;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1462 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1463
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1464 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1465 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1466
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1467 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1468 octave_scalar_struct::load_ascii (std::istream& is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1469 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1470 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1471 octave_idx_type len = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1472
10753
bee1b1a2e29a yield compatible dims from cell2struct + more fixes. build & tests OK
Jaroslav Hajek <highegg@gmail.com>
parents: 10751
diff changeset
1473 if (extract_keyword (is, "length", len) && len >= 0)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1474 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1475 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1476 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1477 octave_scalar_map m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1478
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1479 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1480 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1481 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1482 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1483
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1484 // recurse to read cell elements
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1485 std::string nm
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1486 = read_ascii_data (is, std::string (), dummy, t2, j);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1487
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1488 if (!is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1489 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1490
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1491 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1492 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1493 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1494 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1495 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1496
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1497 m.setfield (nm, t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1498 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1499
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1500 if (is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1501 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1502 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1503 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1504 error ("load: failed to load structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1505 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1506 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1507 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1508 else if (len == 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1509 map = octave_scalar_map ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1510 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1511 panic_impossible ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1512 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1513 else {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1514 error ("load: failed to extract number of elements in structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1515 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1516 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1517
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1518 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1519 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1520
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1521 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1522 octave_scalar_struct::save_binary (std::ostream& os, bool& save_as_floats)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1523 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1524 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1525
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1526 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1527
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1528 int32_t len = nf;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1529 os.write (reinterpret_cast<char *> (&len), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1530
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1531 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1532 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1533 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1534
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1535 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1536 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1537 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1538
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1539 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1540
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1541 bool b = save_binary_data (os, val, key, "", 0, save_as_floats);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1542
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1543 if (! b)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1544 return os;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1545 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1546
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1547 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1548 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1549
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1550 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1551 octave_scalar_struct::load_binary (std::istream& is, bool swap,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1552 oct_mach_info::float_format fmt)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1553 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1554 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1555 int32_t len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1556 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1557 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1558 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1559 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1560
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1561 dim_vector dv (1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1562
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1563 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1564 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1565 octave_scalar_map m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1566
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1567 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1568 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1569 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1570 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1571 std::string doc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1572
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1573 // recurse to read cell elements
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1574 std::string nm = read_binary_data (is, swap, fmt, std::string (),
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1575 dummy, t2, doc);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1576
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1577 if (!is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1578 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1579
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1580 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1581 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1582 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1583 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1584 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1585
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1586 m.setfield (nm, t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1587 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1588
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1589 if (is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1590 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1591 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1592 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1593 error ("load: failed to load structure");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1594 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1595 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1596 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1597 else if (len == 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1598 map = octave_scalar_map ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1599 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1600 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1601
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1602 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1603 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1604
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1605 #if defined (HAVE_HDF5)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1606
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1607 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1608 octave_scalar_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1609 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1610 hid_t data_hid = -1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1611
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1612 #if HAVE_HDF5_18
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1613 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1614 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1615 data_hid = H5Gcreate (loc_id, name, 0);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1616 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1617 if (data_hid < 0) return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1618
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1619 // recursively add each element of the structure to this group
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1620 octave_scalar_map m = scalar_map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1621
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1622 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1623
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1624 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1625 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1626 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1627
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1628 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1629 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1630 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1631
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1632 octave_value val = map.contents (key);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1633
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1634 bool retval2 = add_hdf5_data (data_hid, val, key, "", false,
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1635 save_as_floats);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1636
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1637 if (! retval2)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1638 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1639 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1640
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1641 H5Gclose (data_hid);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1642
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1643 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1644 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1645
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1646 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1647 octave_scalar_struct::load_hdf5 (hid_t loc_id, const char *name)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1648 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1649 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1650
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1651 hdf5_callback_data dsub;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1652
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1653 herr_t retval2 = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1654 octave_scalar_map m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1655 int current_item = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1656 hsize_t num_obj = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1657 #if HAVE_HDF5_18
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1658 hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1659 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1660 hid_t group_id = H5Gopen (loc_id, name);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1661 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1662 H5Gget_num_objs (group_id, &num_obj);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1663 H5Gclose (group_id);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1664
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1665 // FIXME -- fields appear to be sorted alphabetically on loading.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1666 // Why is that happening?
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1667
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1668 while (current_item < static_cast<int> (num_obj)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1669 && (retval2 = H5Giterate (loc_id, name, &current_item,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1670 hdf5_read_next_data, &dsub)) > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1671 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1672 octave_value t2 = dsub.tc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1673
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1674 if (error_state)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1675 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1676 error ("load: internal error loading struct elements");
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1677 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1678 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1679
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1680 m.setfield (dsub.name, t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1681
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1682 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1683
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1684 if (retval2 >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1685 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1686 map = m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1687 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1688 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1689
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1690 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1691 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1692
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1693 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1694
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1695 mxArray *
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1696 octave_scalar_struct::as_mxArray (void) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1697 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1698 int nf = nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1699 string_vector kv = map_keys ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1700
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1701 OCTAVE_LOCAL_BUFFER (const char *, f, nf);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1702
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1703 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1704 f[i] = kv[i].c_str ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1705
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1706 mxArray *retval = new mxArray (dims (), nf, f);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1707
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1708 mxArray **elts = static_cast<mxArray **> (retval->get_data ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1709
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1710 mwSize nel = numel ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1711
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1712 mwSize ntot = nf * nel;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1713
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1714 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1715 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1716 Cell c = map.contents (kv[i]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1717
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1718 const octave_value *p = c.data ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1719
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1720 mwIndex k = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1721 for (mwIndex j = i; j < ntot; j += nf)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1722 elts[j] = new mxArray (p[k++]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1723 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1724
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1725 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1726 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1727
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1728
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1729 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1730 octave_scalar_struct::to_array (void)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1731 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1732 return new octave_struct (octave_map (map));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1733 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1734
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1735 bool
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1736 octave_scalar_struct::fast_elem_insert_self (void *where, builtin_type_t btyp) const
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1737 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1738
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1739 if (btyp == btyp_struct)
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1740 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1741 *(reinterpret_cast<const octave_scalar_map **>(where)) = &map;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1742 return true;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1743 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1744 else
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1745 return false;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1746 }
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1747
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1748 DEFUN (struct, args, ,
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1749 "-*- texinfo -*-\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1750 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1751 \n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1752 Create a structure and initialize its value.\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1753 \n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1754 If the values are cell arrays, create a structure array and initialize\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1755 its values. The dimensions of each cell array of values must match.\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1756 Singleton cells and non-cell values are repeated so that they fill\n\
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1757 the entire array. If the cells are empty, create an empty structure\n\
4911
14027e0bafa4 [project @ 2004-07-22 19:58:06 by jwe]
jwe
parents: 4837
diff changeset
1758 array with the specified field names.\n\
9190
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1759 \n\
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1760 If the argument is an object, return the underlying struct.\n\
4911
14027e0bafa4 [project @ 2004-07-22 19:58:06 by jwe]
jwe
parents: 4837
diff changeset
1761 @end deftypefn")
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1762 {
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1763 octave_value retval;
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1764
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1765 int nargin = args.length ();
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1766
5444
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1767 // struct ([]) returns an empty struct.
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1768
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1769 // struct (empty_matrix) returns an empty struct with the same
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1770 // dimensions as the empty matrix.
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1771
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1772 // Note that struct () creates a 1x1 struct with no fields for
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1773 // compatibility with Matlab.
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1774
10747
58c1b5402588 fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents: 10742
diff changeset
1775 if (nargin == 1 && args(0).is_map ())
58c1b5402588 fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents: 10742
diff changeset
1776 return args(0);
58c1b5402588 fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents: 10742
diff changeset
1777
9190
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1778 if (nargin == 1 && args(0).is_object ())
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1779 {
10747
58c1b5402588 fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents: 10742
diff changeset
1780 retval = args(0).map_value ();
9190
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1781
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1782 return retval;
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1783 }
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1784
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1785 if ((nargin == 1 || nargin == 2)
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1786 && args(0).is_empty () && args(0).is_real_matrix ())
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1787 {
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1788 Cell fields;
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1789
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1790 if (nargin == 2)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1791 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1792 if (args(1).is_cellstr ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1793 retval = octave_map (args(0).dims (), args(1).cellstr_value ());
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1794 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1795 error ("struct: expecting cell array of field names as second argument");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1796 }
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1797 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1798 retval = octave_map (args(0).dims ());
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1799
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1800 return retval;
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1801 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1802
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1803 // Check for "field", VALUE pairs.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1804
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1805 for (int i = 0; i < nargin; i += 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1806 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1807 if (! args(i).is_string () || i + 1 >= nargin)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1808 {
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 12181
diff changeset
1809 error ("struct: expecting alternating \"field\", VALUE pairs");
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1810 return retval;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1811 }
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1812 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1813
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1814 // Check that the dimensions of the values correspond.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1815
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1816 dim_vector dims (1, 1);
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1817
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1818 int first_dimensioned_value = 0;
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1819
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1820 for (int i = 1; i < nargin; i += 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1821 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1822 if (args(i).is_cell ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1823 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1824 dim_vector argdims (args(i).dims ());
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1825
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1826 if (! scalar (argdims))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1827 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1828 if (! first_dimensioned_value)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1829 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1830 dims = argdims;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1831 first_dimensioned_value = i + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1832 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1833 else if (dims != argdims)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1834 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1835 error ("struct: dimensions of parameter %d do not match those of parameter %d",
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1836 first_dimensioned_value, i+1);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1837 return retval;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1838 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1839 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1840 }
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1841 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1842
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1843 // Create the return value.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1844
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1845 octave_map map (dims);
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1846
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1847 for (int i = 0; i < nargin; i+= 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1848 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1849 // Get key.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1850
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1851 std::string key (args(i).string_value ());
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1852
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1853 if (error_state)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1854 return retval;
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1855
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1856 maybe_warn_invalid_field_name (key, "struct");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1857
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1858 if (error_state)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1859 return retval;
6811
3fe394f8502b [project @ 2007-08-10 19:07:34 by jwe]
jwe
parents: 6686
diff changeset
1860
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1861 // Value may be v, { v }, or { v1, v2, ... }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1862 // In the first two cases, we need to create a cell array of
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1863 // the appropriate dimensions filled with v. In the last case,
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1864 // the cell array has already been determined to be of the
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1865 // correct dimensions.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1866
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1867 if (args(i+1).is_cell ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1868 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1869 const Cell c (args(i+1).cell_value ());
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1870
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1871 if (error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1872 return retval;
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1873
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1874 if (scalar (c.dims ()))
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1875 map.setfield (key, Cell (dims, c(0)));
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1876 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1877 map.setfield (key, c);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1878 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1879 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1880 map.setfield (key, Cell (dims, args(i+1)));
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1881
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1882 if (error_state)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1883 return retval;
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1884 }
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1885
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1886 return octave_value (map);
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1887 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1888
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1889 /*
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1890 %!shared x
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1891 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1892 %!assert (struct ("a",1, "b",3), x(1))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1893 %!assert (isempty (x([])))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1894 %!assert (isempty (struct ("a",{}, "b",{})))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1895 %!assert (struct ("a",{1,2}, "b",{3,3}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1896 %!assert (struct ("a",{1,2}, "b",3), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1897 %!assert (struct ("a",{1,2}, "b",{3}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1898 %!assert (struct ("b",3, "a",{1,2}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1899 %!assert (struct ("b",{3}, "a",{1,2}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1900 %!test x = struct ([]);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1901 %!assert (size (x), [0,0])
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1902 %!assert (isstruct (x))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1903 %!assert (isempty (fieldnames (x)))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1904 %!fail ('struct ("a",{1,2},"b",{1,2,3})', 'dimensions of parameter 2 do not match those of parameter 4')
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1905 %!fail ('struct (1,2,3,4)', 'struct: expecting alternating "field", VALUE pairs')
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1906 %!fail ('struct ("1",2,"3")', 'struct: expecting alternating "field", VALUE pairs')
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1907 */
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1908
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1909 DEFUN (isstruct, args, ,
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1910 "-*- texinfo -*-\n\
11431
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1911 @deftypefn {Built-in Function} {} isstruct (@var{x})\n\
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1912 Return true if @var{x} is a structure or a structure array.\n\
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1913 @seealso{ismatrix, iscell, isa}\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1914 @end deftypefn")
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1915 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1916 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1917
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1918 if (args.length () == 1)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1919 retval = args(0).is_map ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1920 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
1921 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1922
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1923 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1924 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1925
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1926 DEFUN (fieldnames, args, ,
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1927 "-*- texinfo -*-\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1928 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1929 Return a cell array of strings naming the elements of the structure\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1930 @var{struct}. It is an error to call @code{fieldnames} with an\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1931 argument that is not a structure.\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1932 @end deftypefn")
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1933 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1934 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1935
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1936 int nargin = args.length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1937
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1938 if (nargin == 1)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1939 {
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7046
diff changeset
1940 octave_value arg = args(0);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7046
diff changeset
1941
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7046
diff changeset
1942 if (arg.is_map () || arg.is_object ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1943 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1944 octave_map m = arg.map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1945
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1946 string_vector keys = m.fieldnames ();
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7046
diff changeset
1947
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1948 if (keys.length () == 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1949 retval = Cell (0, 1);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1950 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1951 retval = Cell (keys);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1952 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1953 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1954 gripe_wrong_type_arg ("fieldnames", args(0));
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1955 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1956 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
1957 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1958
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1959 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1960 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1961
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
1962 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1963 ## test preservation of fieldname order
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
1964 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1965 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1966 %! assert (fieldnames (x), {"d"; "a"; "b"; "c"});
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
1967 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
1968
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1969 DEFUN (isfield, args, ,
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1970 "-*- texinfo -*-\n\
11431
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1971 @deftypefn {Built-in Function} {} isfield (@var{x}, @var{name})\n\
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1972 Return true if the @var{x} is a structure and it\n\
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1973 includes an element named @var{name}. If @var{name} is a cell\n\
11431
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11359
diff changeset
1974 array of strings then a logical array of equal dimension is returned.\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1975 @end deftypefn")
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1976 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1977 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1978
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1979 int nargin = args.length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1980
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1981 if (nargin == 2)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1982 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1983 retval = false;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1984
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1985 if (args(0).is_map ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1986 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1987 octave_map m = args(0).map_value ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1988
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1989 // FIXME -- should this work for all types that can do
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1990 // structure reference operations?
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1991
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1992 if (args(1).is_string ())
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1993 {
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1994 std::string key = args(1).string_value ();
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1995
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1996 retval = m.isfield (key);
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1997 }
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1998 else if (args(1).is_cell ())
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
1999 {
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2000 Cell c = args(1).cell_value ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
2001 boolNDArray bm (c.dims ());
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2002 octave_idx_type n = bm.numel ();
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2003
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2004 for (octave_idx_type i = 0; i < n; i++)
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2005 {
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2006 if (c(i).is_string ())
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2007 {
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2008 std::string key = c(i).string_value ();
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2009
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
2010 bm(i) = m.isfield (key);
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2011 }
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2012 else
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2013 bm(i) = false;
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2014 }
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2015
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2016 retval = bm;
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2017 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2018 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2019 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2020 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2021 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2022
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2023 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2024 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2025
9784
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2026 DEFUN (nfields, args, ,
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2027 "-*- texinfo -*-\n\
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2028 @deftypefn {Built-in Function} {} nfields (@var{s})\n\
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2029 Return the number of fields of the structure @var{s}.\n\
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2030 @end deftypefn")
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2031 {
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2032 octave_value retval;
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2033
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2034 int nargin = args.length ();
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2035
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2036 if (nargin == 1 && args(0).is_map ())
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2037 {
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2038 retval = static_cast<double> (args(0).nfields ());
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2039 }
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2040 else
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2041 print_usage ();
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2042
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2043 return retval;
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2044 }
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2045
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2046 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2047 ## test isfield
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2048 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2049 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2050 %! assert (isfield (x, "b"));
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2051 %!assert (isfield (struct ("a", "1"), "a"))
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2052 %!assert (isfield ({1}, "c"), false)
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2053 %!assert (isfield (struct ("a", "1"), 10), false)
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2054 %!assert (isfield (struct ("a", "b"), "a "), false)
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2055 %!assert (isfield (struct ("a", 1, "b", 2), {"a", "c"}), [true, false])
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2056 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2057
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2058 DEFUN (cell2struct, args, ,
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2059 "-*- texinfo -*-\n\
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2060 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
2061 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2062 must match the number of elements in @var{cell} along dimension @var{dim},\n\
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2063 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2064 If @var{dim} is omitted, a value of 1 is assumed.\n\
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2065 \n\
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2066 @example\n\
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2067 @group\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2068 A = cell2struct (@{\"Peter\", \"Hannah\", \"Robert\";\n\
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7017
diff changeset
2069 185, 170, 168@},\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2070 @{\"Name\",\"Height\"@}, 1);\n\
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2071 A(1)\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2072 @result{}\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2073 @{\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2074 Name = Peter\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2075 Height = 185\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
2076 @}\n\
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2077 \n\
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2078 @end group\n\
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2079 @end example\n\
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2080 @end deftypefn")
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2081 {
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2082 octave_value retval;
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2083
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2084 int nargin = args.length ();
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2085
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2086 if (nargin == 2 || nargin == 3)
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2087 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
2088 if (! args(0).is_cell ())
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2089 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2090 error ("cell2struct: argument CELL must be of type cell");
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2091 return retval;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2092 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2093
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2094 if (! (args(1).is_cellstr () || args(1).is_char_matrix ()))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2095 {
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2096 error ("cell2struct: FIELDS must be a cell array of strings or a character matrix");
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2097 return retval;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2098 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2099
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2100 const Cell vals = args(0).cell_value ();
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2101 const Array<std::string> fields = args(1).cellstr_value ();
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2102
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2103 octave_idx_type ext = 0;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2104
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2105 int dim = 0;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2106
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2107 if (nargin == 3)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2108 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2109 if (args(2).is_real_scalar ())
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2110 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2111 dim = nargin == 2 ? 0 : args(2).int_value () - 1;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2112
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2113 if (error_state)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2114 return retval;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2115 }
10751
717ba2c3eef1 rewrite cell2struct, 1 failing test
Jaroslav Hajek <highegg@gmail.com>
parents: 10749
diff changeset
2116 else
717ba2c3eef1 rewrite cell2struct, 1 failing test
Jaroslav Hajek <highegg@gmail.com>
parents: 10749
diff changeset
2117 {
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2118 error ("cell2struct: DIM must be a real scalar");
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2119 return retval;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2120 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2121 }
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2122
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2123 if (dim < 0)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2124 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2125 error ("cell2struct: DIM must be a valid dimension");
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2126 return retval;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2127 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2128
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2129 ext = vals.ndims () > dim ? vals.dims ()(dim) : 1;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2130
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2131 if (ext != fields.numel ())
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2132 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2133 error ("cell2struct: number of FIELDS does not match dimension");
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2134 return retval;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2135 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2136
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2137 int nd = std::max (dim+1, vals.ndims ());
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2138 // result dimensions.
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2139 dim_vector rdv = vals.dims ().redim (nd);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2140
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2141 assert (ext == rdv(dim));
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2142 if (nd == 2)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2143 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2144 rdv(0) = rdv(1-dim);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2145 rdv(1) = 1;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2146 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2147 else
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2148 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2149 for (int i = dim + 1; i < nd; i++)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2150 rdv(i-1) = rdv(i);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2151
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2152 rdv.resize (nd-1);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2153 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2154
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2155 octave_map map (rdv);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2156 Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon);
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2157
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2158 for (octave_idx_type i = 0; i < ext; i++)
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2159 {
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2160 ia(dim) = i;
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2161 map.setfield (fields(i), vals.index (ia).reshape (rdv));
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2162 }
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2163
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2164 retval = map;
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2165 }
4751
7cb3b220d0f8 [project @ 2004-02-07 22:42:04 by jwe]
jwe
parents: 4750
diff changeset
2166 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2167 print_usage ();
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2168
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2169 return retval;
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2170 }
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2171
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2172 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2173 ## test cell2struct versus struct2cell
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2174 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2175 %! keys = cellstr (char (floor (rand (100,10)*24+65)))';
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2176 %! vals = mat2cell (rand (100,1), ones (100,1), 1)';
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2177 %! s = struct ([keys; vals]{:});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2178 %! t = cell2struct (vals, keys, 2);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2179 %! assert (s, t);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2180 %! assert (struct2cell (s), vals');
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2181 %! assert (fieldnames (s), keys');
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2182
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2183 %!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2));
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
2184
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
2185 %!assert (cell2struct ({}, {"f"}, 3), struct ("f", {}));
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2186 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2187
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2188
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2189 // So we can call Fcellstr directly.
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2190 extern octave_value_list Fcellstr (const octave_value_list& args, int);
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2191
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2192 DEFUN (rmfield, args, ,
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2193 "-*- texinfo -*-\n\
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2194 @deftypefn {Built-in Function} {@var{s} =} rmfield (@var{s}, \"@var{f}\")\n\
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2195 @deftypefnx {Built-in Function} {@var{s} =} rmfield (@var{s}, @var{f})\n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
2196 Return a copy of the structure (array) @var{s} with the field @var{f}\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
2197 removed. If @var{f} is a cell array of strings or a character array, remove\n\
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2198 each of the named fields.\n\
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5598
diff changeset
2199 @seealso{cellstr, iscellstr, setfield}\n\
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5598
diff changeset
2200 @end deftypefn")
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2201 {
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2202 octave_value retval;
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2203
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2204 int nargin = args.length ();
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2205
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2206 if (nargin == 2)
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2207 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
2208 octave_map m = args(0).map_value ();
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2209
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2210 octave_value_list fval = Fcellstr (args(1), 1);
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2211
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2212 if (! error_state)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2213 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2214 Cell fcell = fval(0).cell_value ();
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2215
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2216 for (int i = 0; i < fcell.numel (); i++)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2217 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2218 std::string key = fcell(i).string_value ();
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2219
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
2220 if (m.isfield (key))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
2221 m.rmfield (key);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2222 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2223 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2224 error ("rmfield: structure does not contain field %s",
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2225 key.c_str ());
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2226
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2227 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2228 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2229 }
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2230
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2231 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2232 retval = m;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2233 }
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2234 }
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2235 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2236 print_usage ();
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2237
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2238 return retval;
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2239 }
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2240
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2241 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2242 ## test rmfield
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2243 %!shared x
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2244 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123";
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2245 %!
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2246 %!test
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2247 %! y = rmfield (x, "c");
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2248 %! assert (fieldnames (y), {"d"; "a"; "b"; "f"});
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2249 %! assert (size (y), [1, 6]);
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2250 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2251 %! y = rmfield (x, {"a", "f"});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2252 %! assert (fieldnames (y), {"d"; "b"; "c"});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2253 %! assert (size (y), [1, 6]);
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2254 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2255
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2256 DEFUN (struct_levels_to_print, args, nargout,
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2257 "-*- texinfo -*-\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2258 @deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2259 @deftypefnx {Built-in Function} {@var{old_val} =} struct_levels_to_print (@var{new_val})\n\
13951
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2260 @deftypefnx {Built-in Function} {} struct_levels_to_print (@var{new_val}, \"local\")\n\
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2261 Query or set the internal variable that specifies the number of\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2262 structure levels to display.\n\
13951
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2263 \n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2264 When called from inside a function with the \"local\" option, the variable is\n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2265 changed locally for the function and any subroutines it calls. The original\n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2266 variable value is restored when exiting the function.\n\
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2267 @end deftypefn")
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2268 {
15215
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
2269 return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print, -1,
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
2270 std::numeric_limits<int>::max ());
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2271 }
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2272
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2273 DEFUN (print_struct_array_contents, args, nargout,
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2274 "-*- texinfo -*-\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2275 @deftypefn {Built-in Function} {@var{val} =} print_struct_array_contents ()\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2276 @deftypefnx {Built-in Function} {@var{old_val} =} print_struct_array_contents (@var{new_val})\n\
13951
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2277 @deftypefnx {Built-in Function} {} print_struct_array_contents (@var{new_val}, \"local\")\n\
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2278 Query or set the internal variable that specifies whether to print struct\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2279 array contents. If true, values of struct array elements are printed.\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2280 This variable does not affect scalar structures. Their elements\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2281 are always printed. In both cases, however, printing will be limited to\n\
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2282 the number of levels specified by @var{struct_levels_to_print}.\n\
13951
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2283 \n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2284 When called from inside a function with the \"local\" option, the variable is\n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2285 changed locally for the function and any subroutines it calls. The original\n\
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13868
diff changeset
2286 variable value is restored when exiting the function.\n\
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2287 @end deftypefn")
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2288 {
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2289 return SET_INTERNAL_VARIABLE (print_struct_array_contents);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2290 }