Mercurial > hg > octave-lyh
annotate src/ov-struct.cc @ 9522:e79470be3ecb
implement subsasgn this-arg optimization
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 13 Aug 2009 15:51:57 +0200 |
parents | 610bf90fce2a |
children | 8e5009334661 |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
2376 | 29 |
3933 | 30 #include "Cell.h" |
4358 | 31 #include "defun.h" |
2376 | 32 #include "error.h" |
4358 | 33 #include "gripes.h" |
2979 | 34 #include "oct-lvalue.h" |
3932 | 35 #include "ov-list.h" |
2376 | 36 #include "ov-struct.h" |
37 #include "unwind-prot.h" | |
6811 | 38 #include "utils.h" |
2948 | 39 #include "variables.h" |
2376 | 40 |
4750 | 41 #include "Array-util.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
42 #include "oct-locbuf.h" |
4750 | 43 |
4687 | 44 #include "byte-swap.h" |
45 #include "ls-oct-ascii.h" | |
46 #include "ls-oct-binary.h" | |
47 #include "ls-hdf5.h" | |
48 #include "ls-utils.h" | |
5759 | 49 #include "pr-output.h" |
4687 | 50 |
3219 | 51 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376 | 52 |
4612 | 53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376 | 54 |
4513 | 55 Cell |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
56 octave_struct::dotref (const octave_value_list& idx, bool auto_add) |
2962 | 57 { |
4513 | 58 Cell retval; |
3933 | 59 |
60 assert (idx.length () == 1); | |
2962 | 61 |
3933 | 62 std::string nm = idx(0).string_value (); |
63 | |
4219 | 64 Octave_map::const_iterator p = map.seek (nm); |
2376 | 65 |
4219 | 66 if (p != map.end ()) |
3933 | 67 retval = map.contents (p); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
68 else if (auto_add) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
69 retval = (numel () == 0) ? Cell (dim_vector (1)) : Cell (dims ()); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
70 else |
2376 | 71 error ("structure has no member `%s'", nm.c_str ()); |
72 | |
73 return retval; | |
74 } | |
75 | |
4513 | 76 #if 0 |
3933 | 77 static void |
78 gripe_invalid_index (void) | |
79 { | |
80 error ("invalid index for structure array"); | |
81 } | |
4513 | 82 #endif |
3933 | 83 |
84 static void | |
85 gripe_invalid_index_for_assignment (void) | |
86 { | |
87 error ("invalid index for structure array assignment"); | |
88 } | |
89 | |
90 static void | |
91 gripe_invalid_index_type (const std::string& nm, char t) | |
92 { | |
93 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
94 } | |
95 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
96 void |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
97 octave_struct::gripe_failed_assignment (void) |
3933 | 98 { |
99 error ("assignment to structure element failed"); | |
100 } | |
101 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
102 octave_value_list |
4247 | 103 octave_struct::subsref (const std::string& type, |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
104 const std::list<octave_value_list>& idx, |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
105 int nargout) |
3933 | 106 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
107 octave_value_list retval; |
3933 | 108 |
109 int skip = 1; | |
110 | |
111 switch (type[0]) | |
112 { | |
113 case '(': | |
114 { | |
115 if (type.length () > 1 && type[1] == '.') | |
116 { | |
4219 | 117 std::list<octave_value_list>::const_iterator p = idx.begin (); |
118 octave_value_list key_idx = *++p; | |
3933 | 119 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
120 const Cell tmp = dotref (key_idx); |
3933 | 121 |
122 if (! error_state) | |
123 { | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
124 const Cell t = tmp.index (idx.front ()); |
3933 | 125 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
126 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933 | 127 |
4513 | 128 // We handled two index elements, so tell |
129 // next_subsref to skip both of them. | |
3933 | 130 |
4513 | 131 skip++; |
3933 | 132 } |
133 } | |
134 else | |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
135 retval(0) = map.index (idx.front ()); |
3933 | 136 } |
137 break; | |
138 | |
139 case '.': | |
140 { | |
5592 | 141 if (map.numel() > 0) |
142 { | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
143 const Cell t = dotref (idx.front ()); |
3933 | 144 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
145 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
5592 | 146 } |
3933 | 147 } |
148 break; | |
149 | |
150 case '{': | |
151 gripe_invalid_index_type (type_name (), type[0]); | |
152 break; | |
153 | |
154 default: | |
155 panic_impossible (); | |
156 } | |
157 | |
5775 | 158 // FIXME -- perhaps there should be an |
4994 | 159 // octave_value_list::next_subsref member function? See also |
160 // octave_user_function::subsref. | |
161 | |
162 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
|
163 retval = retval(0).next_subsref (nargout, type, idx, skip); |
3933 | 164 |
165 return retval; | |
166 } | |
167 | |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
168 octave_value |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
169 octave_struct::subsref (const std::string& type, |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
170 const std::list<octave_value_list>& idx, |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
171 bool auto_add) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
172 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
173 octave_value retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
174 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
175 int skip = 1; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
176 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
177 switch (type[0]) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
178 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
179 case '(': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
180 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
181 if (type.length () > 1 && type[1] == '.') |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
182 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
183 std::list<octave_value_list>::const_iterator p = idx.begin (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
184 octave_value_list key_idx = *++p; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
185 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
186 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
|
187 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
188 if (! error_state) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
189 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
190 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
|
191 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
192 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
193 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
194 // We handled two index elements, so tell |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
195 // next_subsref to skip both of them. |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
196 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
197 skip++; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
198 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
199 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
200 else |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
201 retval = map.index (idx.front (), auto_add); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
202 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
203 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
204 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
205 case '.': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
206 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
207 if (map.numel() > 0) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
208 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
209 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
|
210 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
211 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
212 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
213 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
214 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
215 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
216 case '{': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
217 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
|
218 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
219 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
220 default: |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
221 panic_impossible (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
222 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
223 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
224 // FIXME -- perhaps there should be an |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
225 // 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
|
226 // octave_user_function::subsref. |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
227 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
228 if (idx.size () > 1) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
229 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
|
230 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
231 return retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
232 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
233 |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
234 /* |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
235 %!test |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
236 %! x(1).a.a = 1; 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
|
237 %! 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
|
238 %! 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
|
239 %! 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
|
240 */ |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
241 |
3933 | 242 octave_value |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
243 octave_struct::numeric_conv (const octave_value& val, |
3933 | 244 const std::string& type) |
245 { | |
246 octave_value retval; | |
247 | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
248 if (type.length () > 0 && type[0] == '.' && ! val.is_map ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
249 retval = Octave_map (); |
3933 | 250 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
251 retval = val; |
3933 | 252 |
253 return retval; | |
254 } | |
255 | |
256 octave_value | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
257 octave_struct::dotasgn (const octave_value_list& idx, const octave_value& rhs) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
258 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
259 octave_value retval; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
260 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
261 assert (idx.length () == 1); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
262 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
263 std::string key = idx(0).string_value (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
264 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
265 if (rhs.is_cs_list ()) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
266 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
267 Cell tmp_cell = Cell (rhs.list_value ()); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
268 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
269 // The shape of the RHS is irrelevant, we just want |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
270 // the number of elements to agree and to preserve the |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
271 // shape of the left hand side of the assignment. |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
272 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
273 if (numel () == tmp_cell.numel ()) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
274 tmp_cell = tmp_cell.reshape (dims ()); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
275 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
276 map.assign (key, tmp_cell); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
277 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
278 else |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
279 // Regularize a null matrix if stored into a struct component. |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
280 map.assign (key, rhs.storable_value ()); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
281 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
282 if (! error_state) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
283 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
284 count++; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
285 retval = octave_value (this); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
286 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
287 else |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
288 gripe_failed_assignment (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
289 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
290 return retval; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
291 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
292 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
293 octave_value |
4247 | 294 octave_struct::subsasgn (const std::string& type, |
4219 | 295 const std::list<octave_value_list>& idx, |
3933 | 296 const octave_value& rhs) |
2376 | 297 { |
3933 | 298 octave_value retval; |
299 | |
300 int n = type.length (); | |
301 | |
302 octave_value t_rhs = rhs; | |
303 | |
9286
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
304 if (idx.front ().empty ()) |
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 error ("missing index in indexed assignment"); |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
307 return retval; |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
308 } |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
309 |
3933 | 310 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
311 { | |
312 switch (type[0]) | |
313 { | |
314 case '(': | |
315 { | |
316 if (type.length () > 1 && type[1] == '.') | |
317 { | |
4219 | 318 std::list<octave_value_list>::const_iterator p = idx.begin (); |
319 octave_value_list t_idx = *p; | |
3933 | 320 |
4513 | 321 octave_value_list key_idx = *++p; |
322 | |
323 assert (key_idx.length () == 1); | |
3933 | 324 |
4513 | 325 std::string key = key_idx(0).string_value (); |
3933 | 326 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
327 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
328 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
329 // We handled two index elements, so subsasgn to |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
330 // needs to skip both of them. |
3933 | 331 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
332 next_idx.erase (next_idx.begin ()); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
333 next_idx.erase (next_idx.begin ()); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
334 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
335 std::string next_type = type.substr (2); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
336 |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
337 Cell tmpc (1, 1); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
338 Octave_map::iterator pkey = map.seek (key); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
339 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
340 { |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
341 pkey->second.make_unique (); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
342 tmpc = pkey->second.index (idx.front (), true); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
343 } |
3933 | 344 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
345 // FIXME: better code reuse? cf. octave_cell::subsasgn and the case below. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
346 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
347 { |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
348 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
349 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
350 octave_value& tmp = tmpc(0); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
351 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
352 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
353 { |
8564 | 354 tmp = octave_value::empty_conv (next_type, rhs); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
355 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
356 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
357 else |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
358 // optimization: ignore the copy still stored inside our map. |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
359 tmp.make_unique (1); |
8546
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 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
362 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
363 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
364 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
365 gripe_indexed_cs_list (); |
3933 | 366 } |
367 } | |
368 else | |
369 gripe_invalid_index_for_assignment (); | |
370 } | |
371 break; | |
372 | |
373 case '.': | |
374 { | |
375 octave_value_list key_idx = idx.front (); | |
376 | |
377 assert (key_idx.length () == 1); | |
378 | |
379 std::string key = key_idx(0).string_value (); | |
380 | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
381 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
382 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
383 next_idx.erase (next_idx.begin ()); |
3933 | 384 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
385 std::string next_type = type.substr (1); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
386 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
387 Cell tmpc (1, 1); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
388 Octave_map::iterator pkey = map.seek (key); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
389 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
390 { |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
391 pkey->second.make_unique (); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
392 tmpc = pkey->second; |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
393 } |
3933 | 394 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
395 // FIXME: better code reuse? |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
396 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
397 { |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
398 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
399 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
400 octave_value& tmp = tmpc(0); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
401 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
402 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
403 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
404 tmp = octave_value::empty_conv (next_type, rhs); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
405 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
406 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
407 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
408 // optimization: ignore the copy still stored inside our map. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
409 tmp.make_unique (1); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
410 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
411 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
412 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
413 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
414 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
415 gripe_indexed_cs_list (); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
416 } |
3933 | 417 } |
418 break; | |
419 | |
420 case '{': | |
421 gripe_invalid_index_type (type_name (), type[0]); | |
422 break; | |
423 | |
424 default: | |
425 panic_impossible (); | |
426 } | |
427 } | |
428 | |
429 if (! error_state) | |
430 { | |
431 switch (type[0]) | |
432 { | |
433 case '(': | |
434 { | |
435 if (n > 1 && type[1] == '.') | |
436 { | |
4219 | 437 std::list<octave_value_list>::const_iterator p = idx.begin (); |
438 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
|
439 octave_value_list idxf = idx.front (); |
3933 | 440 |
441 assert (key_idx.length () == 1); | |
442 | |
443 std::string key = key_idx(0).string_value (); | |
444 | |
445 if (! error_state) | |
446 { | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
447 if (t_rhs.is_cs_list ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
448 { |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
449 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
|
450 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
451 // 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
|
452 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
453 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
|
454 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
|
455 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
|
456 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
457 if (didx.numel () == tmp_cell.numel ()) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
458 tmp_cell = tmp_cell.reshape (didx); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
459 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
460 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
461 map.assign (idxf, key, tmp_cell); |
3933 | 462 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
463 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
464 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
465 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
466 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
467 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
468 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
469 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
470 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
471 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
472 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
473 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
|
474 // cast map to const reference to avoid forced key insertion. |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
475 if (idxf.all_scalars () |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
476 || cmap.contents (key).index (idxf, true).numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
477 { |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
478 map.assign (idxf, key, t_rhs.storable_value ()); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
479 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
480 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
481 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
482 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
483 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
484 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
485 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
486 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
487 else if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
488 error ("invalid assignment to cs-list outside multiple assignment."); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
489 } |
3933 | 490 } |
491 else | |
492 gripe_failed_assignment (); | |
493 } | |
494 else | |
4197 | 495 { |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
496 if (t_rhs.is_map() |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
497 || (is_object () && t_rhs.is_object ())) |
4197 | 498 { |
5592 | 499 Octave_map rhs_map = t_rhs.map_value (); |
4197 | 500 |
501 if (! error_state) | |
5592 | 502 { |
503 map.assign (idx.front (), rhs_map); | |
504 | |
505 if (! error_state) | |
5759 | 506 { |
507 count++; | |
508 retval = octave_value (this); | |
509 } | |
5592 | 510 else |
511 gripe_failed_assignment (); | |
512 } | |
4197 | 513 else |
5592 | 514 error ("invalid structure assignment"); |
4197 | 515 } |
4513 | 516 else |
5592 | 517 { |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
518 if (t_rhs.is_null_value()) |
5592 | 519 { |
520 map.maybe_delete_elements (idx.front()); | |
521 | |
522 if (! error_state) | |
5759 | 523 { |
524 count++; | |
525 retval = octave_value (this); | |
526 } | |
5592 | 527 else |
528 gripe_failed_assignment (); | |
529 } | |
530 else | |
531 error ("invalid structure assignment"); | |
532 } | |
4197 | 533 } |
3933 | 534 } |
535 break; | |
536 | |
537 case '.': | |
538 { | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
539 retval = dotasgn (idx.front (), t_rhs); |
3933 | 540 } |
541 break; | |
542 | |
543 case '{': | |
544 gripe_invalid_index_type (type_name (), type[0]); | |
545 break; | |
546 | |
547 default: | |
548 panic_impossible (); | |
549 } | |
550 } | |
551 else | |
552 gripe_failed_assignment (); | |
553 | |
554 return retval; | |
2376 | 555 } |
556 | |
7046 | 557 octave_value |
558 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
559 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8587
diff
changeset
|
560 // Octave_map handles indexing itself. |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8587
diff
changeset
|
561 return map.index (idx, resize_ok); |
7046 | 562 } |
563 | |
4791 | 564 size_t |
565 octave_struct::byte_size (void) const | |
566 { | |
567 // Neglect the size of the fieldnames. | |
568 | |
569 size_t retval = 0; | |
570 | |
571 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
572 { | |
573 std::string key = map.key (p); | |
574 | |
575 octave_value val = octave_value (map.contents (p)); | |
576 | |
577 retval += val.byte_size (); | |
578 } | |
579 | |
580 return retval; | |
581 } | |
582 | |
2376 | 583 void |
3523 | 584 octave_struct::print (std::ostream& os, bool) const |
2901 | 585 { |
586 print_raw (os); | |
587 } | |
588 | |
589 void | |
3523 | 590 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 591 { |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9286
diff
changeset
|
592 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
2376 | 593 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9286
diff
changeset
|
594 unwind_protect::protect_var (Vstruct_levels_to_print); |
2376 | 595 |
3961 | 596 if (Vstruct_levels_to_print >= 0) |
2376 | 597 { |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
598 bool print_keys_only = Vstruct_levels_to_print-- == 0; |
3961 | 599 |
2901 | 600 indent (os); |
601 os << "{"; | |
602 newline (os); | |
2376 | 603 |
2901 | 604 increment_indent_level (); |
2376 | 605 |
5598 | 606 octave_idx_type n = map.numel (); |
3932 | 607 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
608 if (n != 1 || print_keys_only) |
4604 | 609 { |
610 indent (os); | |
611 dim_vector dv = dims (); | |
612 os << dv.str () << " struct array containing the fields:"; | |
613 newline (os); | |
614 newline (os); | |
615 | |
616 increment_indent_level (); | |
617 } | |
618 | |
5880 | 619 string_vector key_list = map.keys (); |
620 | |
621 for (octave_idx_type i = 0; i < key_list.length (); i++) | |
2376 | 622 { |
5880 | 623 std::string key = key_list[i]; |
624 | |
625 Cell val = map.contents (key); | |
2376 | 626 |
4499 | 627 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961 | 628 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
629 if (n != 1 || print_keys_only) |
3932 | 630 { |
3961 | 631 indent (os); |
4604 | 632 os << key; |
633 if (n == 1) | |
634 { | |
635 dim_vector dv = tmp.dims (); | |
636 os << ": " << dv.str () << " " << tmp.type_name (); | |
637 } | |
3961 | 638 newline (os); |
3932 | 639 } |
3961 | 640 else |
4121 | 641 tmp.print_with_name (os, key); |
2376 | 642 } |
643 | |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
644 if (n != 1 || print_keys_only) |
4604 | 645 decrement_indent_level (); |
646 | |
2901 | 647 decrement_indent_level (); |
2376 | 648 |
2901 | 649 indent (os); |
650 os << "}"; | |
651 newline (os); | |
2376 | 652 } |
653 else | |
2901 | 654 { |
3961 | 655 indent (os); |
656 os << "<structure>"; | |
2901 | 657 newline (os); |
658 } | |
2376 | 659 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9286
diff
changeset
|
660 unwind_protect::run_frame (uwp_frame); |
2376 | 661 } |
662 | |
2901 | 663 bool |
3523 | 664 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 665 { |
3961 | 666 bool retval = false; |
667 | |
2901 | 668 indent (os); |
3961 | 669 |
670 if (Vstruct_levels_to_print < 0) | |
671 os << name << " = "; | |
672 else | |
673 { | |
674 os << name << " ="; | |
675 newline (os); | |
676 retval = true; | |
677 } | |
678 | |
679 return retval; | |
2901 | 680 } |
681 | |
4744 | 682 static bool |
683 scalar (const dim_vector& dims) | |
684 { | |
685 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; | |
686 } | |
687 | |
688 /* | |
689 %!shared x | |
690 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; | |
691 %!assert(struct('a',1,'b',3),x(1)) | |
5592 | 692 %!assert(isempty(x([]))) |
693 %!assert(isempty(struct('a',{},'b',{}))) | |
4744 | 694 %!assert(struct('a',{1,2},'b',{3,3}),x) |
695 %!assert(struct('a',{1,2},'b',3),x) | |
696 %!assert(struct('a',{1,2},'b',{3}),x) | |
697 %!assert(struct('b',3,'a',{1,2}),x) | |
698 %!assert(struct('b',{3},'a',{1,2}),x) | |
699 %!test x=struct([]); | |
700 %!assert(size(x),[0,0]); | |
701 %!assert(isstruct(x)); | |
702 %!assert(isempty(fieldnames(x))); | |
703 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") | |
5582 | 704 %!fail("struct(1,2,3,4)","struct expects alternating \"field\", VALUE pairs"); |
705 %!fail("struct('1',2,'3')","struct expects alternating \"field\", VALUE pairs"); | |
4744 | 706 */ |
707 | |
708 DEFUN (struct, args, , | |
709 "-*- texinfo -*-\n\ | |
710 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ | |
711 \n\ | |
712 Create a structure and initialize its value.\n\ | |
713 \n\ | |
714 If the values are cell arrays, create a structure array and initialize\n\ | |
715 its values. The dimensions of each cell array of values must match.\n\ | |
716 Singleton cells and non-cell values are repeated so that they fill\n\ | |
717 the entire array. If the cells are empty, create an empty structure\n\ | |
4911 | 718 array with the specified field names.\n\ |
9190 | 719 \n\ |
720 If the argument is an object, return the underlying struct.\n\ | |
4911 | 721 @end deftypefn") |
4744 | 722 { |
6946 | 723 octave_value retval; |
4744 | 724 |
725 int nargin = args.length (); | |
726 | |
5444 | 727 // struct ([]) returns an empty struct. |
728 | |
729 // struct (empty_matrix) returns an empty struct with the same | |
730 // dimensions as the empty matrix. | |
731 | |
732 // Note that struct () creates a 1x1 struct with no fields for | |
733 // compatibility with Matlab. | |
4744 | 734 |
9190 | 735 if (nargin == 1 && args(0).is_object ()) |
736 { | |
737 Octave_map m = args(0).map_value (); | |
738 retval = octave_value (new octave_struct (m)); | |
739 | |
740 return retval; | |
741 } | |
742 | |
6946 | 743 if ((nargin == 1 || nargin == 2) |
744 && args(0).is_empty () && args(0).is_real_matrix ()) | |
745 { | |
746 Cell fields; | |
747 | |
748 if (nargin == 2) | |
749 { | |
750 if (args(1).is_cellstr ()) | |
751 retval = Octave_map (args(0).dims (), args(1).cell_value ()); | |
752 else | |
753 error ("struct: expecting cell array of field names as second argument"); | |
754 } | |
755 else | |
756 retval = Octave_map (args(0).dims ()); | |
757 | |
758 return retval; | |
759 } | |
4744 | 760 |
761 // Check for "field", VALUE pairs. | |
762 | |
763 for (int i = 0; i < nargin; i += 2) | |
764 { | |
765 if (! args(i).is_string () || i + 1 >= nargin) | |
766 { | |
767 error ("struct expects alternating \"field\", VALUE pairs"); | |
768 return retval; | |
769 } | |
770 } | |
771 | |
772 // Check that the dimensions of the values correspond. | |
773 | |
774 dim_vector dims (1, 1); | |
775 | |
776 int first_dimensioned_value = 0; | |
777 | |
778 for (int i = 1; i < nargin; i += 2) | |
779 { | |
780 if (args(i).is_cell ()) | |
781 { | |
782 dim_vector argdims (args(i).dims ()); | |
783 | |
784 if (! scalar (argdims)) | |
785 { | |
786 if (! first_dimensioned_value) | |
787 { | |
788 dims = argdims; | |
789 first_dimensioned_value = i + 1; | |
790 } | |
791 else if (dims != argdims) | |
792 { | |
793 error ("struct: dimensions of parameter %d do not match those of parameter %d", | |
794 first_dimensioned_value, i+1); | |
795 return retval; | |
796 } | |
797 } | |
798 } | |
799 } | |
800 | |
801 // Create the return value. | |
802 | |
803 Octave_map map (dims); | |
804 | |
805 for (int i = 0; i < nargin; i+= 2) | |
806 { | |
807 // Get key. | |
808 | |
809 std::string key (args(i).string_value ()); | |
810 | |
811 if (error_state) | |
812 return retval; | |
813 | |
6811 | 814 if (! valid_identifier (key)) |
815 { | |
816 error ("struct: invalid structure field name `%s'", key.c_str ()); | |
817 return retval; | |
818 } | |
819 | |
4744 | 820 // Value may be v, { v }, or { v1, v2, ... } |
821 // In the first two cases, we need to create a cell array of | |
822 // the appropriate dimensions filled with v. In the last case, | |
823 // the cell array has already been determined to be of the | |
824 // correct dimensions. | |
825 | |
826 if (args(i+1).is_cell ()) | |
827 { | |
828 const Cell c (args(i+1).cell_value ()); | |
829 | |
830 if (error_state) | |
831 return retval; | |
832 | |
833 if (scalar (c.dims ())) | |
834 map.assign (key, Cell (dims, c(0))); | |
835 else | |
836 map.assign (key, c); | |
837 } | |
838 else | |
839 map.assign (key, Cell (dims, args(i+1))); | |
840 | |
841 if (error_state) | |
842 return retval; | |
843 } | |
6946 | 844 |
4744 | 845 return octave_value (map); |
846 } | |
847 | |
4358 | 848 DEFUN (isstruct, args, , |
849 "-*- texinfo -*-\n\ | |
850 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ | |
851 Return 1 if the value of the expression @var{expr} is a structure.\n\ | |
852 @end deftypefn") | |
853 { | |
854 octave_value retval; | |
855 | |
856 if (args.length () == 1) | |
857 retval = args(0).is_map (); | |
858 else | |
5823 | 859 print_usage (); |
4358 | 860 |
861 return retval; | |
862 } | |
863 | |
864 DEFUN (fieldnames, args, , | |
865 "-*- texinfo -*-\n\ | |
866 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ | |
867 Return a cell array of strings naming the elements of the structure\n\ | |
868 @var{struct}. It is an error to call @code{fieldnames} with an\n\ | |
869 argument that is not a structure.\n\ | |
870 @end deftypefn") | |
871 { | |
872 octave_value retval; | |
873 | |
874 int nargin = args.length (); | |
875 | |
876 if (nargin == 1) | |
877 { | |
7336 | 878 octave_value arg = args(0); |
879 | |
880 if (arg.is_map () || arg.is_object ()) | |
4358 | 881 { |
7336 | 882 Octave_map m = arg.map_value (); |
883 | |
4744 | 884 string_vector keys = m.keys (); |
7336 | 885 |
4744 | 886 if (keys.length () == 0) |
887 retval = Cell (0, 1); | |
888 else | |
889 retval = Cell (m.keys ()); | |
4358 | 890 } |
891 else | |
892 gripe_wrong_type_arg ("fieldnames", args(0)); | |
893 } | |
894 else | |
5823 | 895 print_usage (); |
4358 | 896 |
897 return retval; | |
898 } | |
899 | |
900 DEFUN (isfield, args, , | |
901 "-*- texinfo -*-\n\ | |
902 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ | |
903 Return true if the expression @var{expr} is a structure and it includes an\n\ | |
904 element named @var{name}. The first argument must be a structure and\n\ | |
905 the second must be a string.\n\ | |
906 @end deftypefn") | |
907 { | |
908 octave_value retval; | |
909 | |
910 int nargin = args.length (); | |
911 | |
912 if (nargin == 2) | |
913 { | |
914 retval = false; | |
915 | |
5775 | 916 // FIXME -- should this work for all types that can do |
4358 | 917 // structure reference operations? |
918 | |
919 if (args(0).is_map () && args(1).is_string ()) | |
920 { | |
921 std::string key = args(1).string_value (); | |
922 | |
923 Octave_map m = args(0).map_value (); | |
924 | |
925 retval = m.contains (key) != 0; | |
926 } | |
927 } | |
928 else | |
5823 | 929 print_usage (); |
4358 | 930 |
931 return retval; | |
932 } | |
933 | |
4750 | 934 // Check that the dimensions of the input arguments are correct. |
935 | |
936 static bool | |
937 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, | |
938 bool is_cell, int dim) | |
939 { | |
4751 | 940 bool retval = true; |
4750 | 941 |
942 if (dim >= 0 && dim < c_dv.length ()) | |
943 { | |
944 if (is_cell) | |
945 { | |
4752 | 946 if (f_dv.numel () != c_dv(dim)) |
4750 | 947 { |
4751 | 948 error ("cell2struct: numel (FIELD) != size (CELL, DIM)"); |
4750 | 949 |
950 retval = false; | |
951 } | |
952 } | |
953 else | |
954 { | |
955 if (f_dv.length () > 2) | |
956 { | |
4751 | 957 error ("cell2struct: field array must be a 2-d matrix"); |
4750 | 958 |
959 retval = false; | |
960 } | |
961 else if (f_dv(0) != c_dv(dim)) | |
962 { | |
4751 | 963 error ("cell2struct: size (FIELD, 1) != length (C, DIM)"); |
4750 | 964 |
965 retval = false; | |
966 } | |
967 } | |
968 } | |
969 else | |
970 { | |
971 error ("cell2struct: DIM out of range"); | |
972 | |
973 retval = false; | |
974 } | |
975 | |
976 return retval; | |
977 } | |
978 | |
979 static void | |
5275 | 980 cell2struct_construct_idx (Array<octave_idx_type>& ra_idx1, |
981 const Array<octave_idx_type>& ra_idx2, | |
982 octave_idx_type dim, octave_idx_type fill_value) | |
4750 | 983 { |
5275 | 984 octave_idx_type iidx = 0; |
4750 | 985 |
5275 | 986 for (octave_idx_type idx = 0; idx < ra_idx1.length (); idx++) |
4750 | 987 { |
988 if (idx == dim) | |
989 ra_idx1.elem (idx) = fill_value; | |
990 else | |
991 ra_idx1.elem (idx) = ra_idx2(iidx++); | |
992 } | |
993 } | |
994 | |
995 DEFUN (cell2struct, args, , | |
996 "-*- texinfo -*-\n\ | |
4817 | 997 @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
|
998 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ |
4817 | 999 must match the number of elements in @var{cell} along dimension @var{dim},\n\ |
1000 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ | |
4750 | 1001 \n\ |
1002 @example\n\ | |
1003 @group\n\ | |
7031 | 1004 A = cell2struct (@{'Peter', 'Hannah', 'Robert';\n\ |
1005 185, 170, 168@},\n\ | |
6519 | 1006 @{'Name','Height'@}, 1);\n\ |
4750 | 1007 A(1)\n\ |
1008 @result{} ans =\n\ | |
4780 | 1009 @{\n\ |
1010 Height = 185\n\ | |
1011 Name = Peter\n\ | |
1012 @}\n\ | |
4750 | 1013 \n\ |
1014 @end group\n\ | |
1015 @end example\n\ | |
1016 @end deftypefn") | |
1017 { | |
1018 octave_value retval; | |
1019 | |
4751 | 1020 if (args.length () == 3) |
4750 | 1021 { |
4751 | 1022 Cell c = args(0).cell_value (); |
4750 | 1023 |
4751 | 1024 if (! error_state) |
4750 | 1025 { |
4751 | 1026 octave_value field = args(1); |
4750 | 1027 |
4751 | 1028 // Field is either cell or character matrix. |
4750 | 1029 |
5775 | 1030 // FIXME -- this could be simplified if we had |
4752 | 1031 // cellstr and iscellstr functions available. |
1032 | |
4751 | 1033 bool field_is_cell = field.is_cell (); |
4750 | 1034 |
4751 | 1035 Cell field_cell; |
1036 charMatrix field_char; | |
4750 | 1037 |
1038 if (field_is_cell) | |
4751 | 1039 field_cell = field.cell_value (); |
1040 else | |
1041 field_char = field.char_matrix_value (); | |
1042 | |
1043 if (! error_state) | |
4750 | 1044 { |
4751 | 1045 // Retrieve the dimension value. |
1046 | |
5775 | 1047 // FIXME -- int_value () should print out the |
4751 | 1048 // conversions it does to be Matlab compatible. |
1049 | |
5275 | 1050 octave_idx_type dim = args(2).int_value () - 1; |
4751 | 1051 |
1052 if (! error_state) | |
1053 { | |
1054 dim_vector c_dv = c.dims (); | |
1055 dim_vector field_dv = field.dims (); | |
1056 | |
1057 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, | |
1058 dim)) | |
1059 { | |
5275 | 1060 octave_idx_type c_dv_length = c_dv.length (); |
4751 | 1061 |
1062 // Dimension vector for the Cell arrays to be | |
1063 // put into the structure. | |
1064 | |
1065 dim_vector value_dv; | |
1066 | |
1067 // Initialize c_value_dv. | |
4750 | 1068 |
4751 | 1069 if (c_dv_length == 2) |
1070 value_dv = dim_vector (1, 1); | |
1071 else | |
1072 value_dv.resize (c_dv_length - 1); | |
1073 | |
5275 | 1074 octave_idx_type idx_tmp = 0; |
4751 | 1075 |
5275 | 1076 for (octave_idx_type i = 0; i < c_dv_length; i++) |
4751 | 1077 { |
1078 if (i != dim) | |
1079 value_dv.elem (idx_tmp++) = c_dv.elem (i); | |
1080 } | |
1081 | |
1082 // All initializing is done, we can start moving | |
1083 // values. | |
1084 | |
1085 Octave_map map; | |
1086 | |
1087 // If field is a cell array then we use all | |
1088 // elements in array, on the other hand when | |
1089 // field is a character array the number of | |
1090 // elements is equals the number of rows. | |
1091 | |
5275 | 1092 octave_idx_type field_numel |
4751 | 1093 = field_is_cell ? field_dv.numel (): field_dv(0); |
1094 | |
1095 // For matlab compatibility. | |
1096 | |
1097 if (field_numel == 0) | |
1098 map.reshape (dim_vector (0, 1)); | |
4750 | 1099 |
5275 | 1100 for (octave_idx_type i = 0; i < field_numel; i++) |
4751 | 1101 { |
1102 // Construct cell array which goes into the | |
1103 // structure together with the appropriate | |
1104 // field name. | |
1105 | |
1106 Cell c_value (value_dv); | |
1107 | |
5275 | 1108 Array<octave_idx_type> value_idx (value_dv.length (), 0); |
1109 Array<octave_idx_type> c_idx (c_dv_length, 0); | |
4751 | 1110 |
5275 | 1111 for (octave_idx_type j = 0; j < value_dv.numel (); j++) |
4751 | 1112 { |
1113 // Need to do this to construct the | |
1114 // appropriate idx for getting elements | |
1115 // from the original cell array. | |
1116 | |
1117 cell2struct_construct_idx (c_idx, value_idx, | |
1118 dim, i); | |
1119 | |
1120 c_value.elem (value_idx) = c.elem (c_idx); | |
1121 | |
1122 increment_index (value_idx, value_dv); | |
1123 } | |
1124 | |
1125 std::string field_str; | |
4750 | 1126 |
4751 | 1127 if (field_is_cell) |
1128 { | |
1129 // Matlab retrieves the field values | |
1130 // column by column. | |
1131 | |
1132 octave_value field_tmp = field_cell.elem (i); | |
1133 | |
1134 field_str = field_tmp.string_value (); | |
1135 | |
1136 if (error_state) | |
1137 { | |
1138 error ("cell2struct: fields have to be of type string"); | |
1139 break; | |
1140 } | |
1141 } | |
1142 else | |
1143 { | |
1144 field_str = field_char.row_as_string (i); | |
1145 | |
1146 if (error_state) | |
1147 return retval; | |
1148 } | |
1149 | |
6811 | 1150 if (! valid_identifier (field_str)) |
1151 { | |
1152 error ("cell2struct: invalid field name `%s'", | |
1153 field_str.c_str ()); | |
1154 break; | |
1155 } | |
1156 | |
4751 | 1157 map.reshape (value_dv); |
1158 | |
1159 map.assign (field_str, c_value); | |
1160 } | |
1161 | |
1162 if (! error_state) | |
1163 retval = map; | |
1164 } | |
4750 | 1165 } |
4751 | 1166 else |
1167 error ("cell2struct: expecting third argument to be an integer"); | |
4750 | 1168 } |
1169 else | |
4751 | 1170 error ("cell2struct: expecting second argument to be a cell or character array"); |
4750 | 1171 } |
4751 | 1172 else |
1173 error ("cell2struct: expecting first argument to be a cell array"); | |
4750 | 1174 } |
4751 | 1175 else |
5823 | 1176 print_usage (); |
4750 | 1177 |
1178 return retval; | |
1179 } | |
1180 | |
4817 | 1181 // So we can call Fcellstr directly. |
1182 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
1183 | |
1184 DEFUN (rmfield, args, , | |
1185 "-*- texinfo -*-\n\ | |
1186 @deftypefn {Built-in Function} {} rmfield (@var{s}, @var{f})\n\ | |
1187 Remove field @var{f} from the structure @var{s}. If @var{f} is a\n\ | |
1188 cell array of character strings or a character array, remove the\n\ | |
1189 named fields.\n\ | |
5642 | 1190 @seealso{cellstr, iscellstr, setfield}\n\ |
1191 @end deftypefn") | |
4817 | 1192 { |
1193 octave_value retval; | |
1194 | |
1195 int nargin = args.length (); | |
1196 | |
1197 if (nargin == 2) | |
1198 { | |
1199 Octave_map m = args(0).map_value (); | |
1200 | |
1201 octave_value_list fval = Fcellstr (args(1), 1); | |
1202 | |
1203 if (! error_state) | |
1204 { | |
1205 Cell fcell = fval(0).cell_value (); | |
1206 | |
1207 for (int i = 0; i < fcell.numel (); i++) | |
1208 { | |
1209 std::string key = fcell(i).string_value (); | |
1210 | |
1211 if (m.contains (key)) | |
1212 m.del (key); | |
1213 else | |
1214 { | |
1215 error ("rmfield: structure does not contain field %s", | |
1216 key.c_str ()); | |
1217 | |
1218 break; | |
1219 } | |
1220 } | |
1221 | |
1222 if (! error_state) | |
1223 retval = m; | |
1224 } | |
1225 } | |
1226 else | |
5823 | 1227 print_usage (); |
4817 | 1228 |
1229 return retval; | |
1230 } | |
1231 | |
1232 bool | |
6974 | 1233 octave_struct::save_ascii (std::ostream& os) |
4817 | 1234 { |
1235 Octave_map m = map_value (); | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1236 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1237 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1238 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1239 os << "# length: " << nf << "\n"; |
4817 | 1240 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1241 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1242 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1243 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1244 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1245 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1246 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1247 std::string key = keys(i); |
4817 | 1248 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1249 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1250 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1251 bool b = save_ascii_data (os, val, key, false, 0); |
4817 | 1252 |
1253 if (! b) | |
1254 return os; | |
1255 } | |
1256 | |
1257 return true; | |
1258 } | |
1259 | |
1260 bool | |
1261 octave_struct::load_ascii (std::istream& is) | |
1262 { | |
5275 | 1263 octave_idx_type len = 0; |
4817 | 1264 bool success = true; |
1265 | |
1266 if (extract_keyword (is, "length", len) && len >= 0) | |
1267 { | |
1268 if (len > 0) | |
1269 { | |
1270 Octave_map m (map); | |
1271 | |
5275 | 1272 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1273 { |
1274 octave_value t2; | |
1275 bool dummy; | |
1276 | |
1277 // recurse to read cell elements | |
1278 std::string nm | |
5756 | 1279 = read_ascii_data (is, std::string (), dummy, t2, j); |
4817 | 1280 |
1281 if (!is) | |
1282 break; | |
1283 | |
6293 | 1284 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1285 |
6293 | 1286 if (error_state) |
1287 { | |
1288 error ("load: internal error loading struct elements"); | |
1289 return false; | |
1290 } | |
5433 | 1291 |
6293 | 1292 m.assign (nm, tcell); |
4817 | 1293 } |
1294 | |
1295 if (is) | |
1296 map = m; | |
1297 else | |
1298 { | |
1299 error ("load: failed to load structure"); | |
1300 success = false; | |
1301 } | |
1302 } | |
1303 else if (len == 0 ) | |
6292 | 1304 map = Octave_map (dim_vector (1, 1)); |
4817 | 1305 else |
1306 panic_impossible (); | |
1307 } | |
1308 else { | |
1309 error ("load: failed to extract number of elements in structure"); | |
1310 success = false; | |
1311 } | |
1312 | |
1313 return success; | |
1314 } | |
1315 | |
1316 bool | |
1317 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) | |
1318 { | |
1319 Octave_map m = map_value (); | |
1320 | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1321 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1322 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1323 int32_t len = nf; |
5760 | 1324 os.write (reinterpret_cast<char *> (&len), 4); |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1325 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1326 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1327 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1328 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1329 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1330 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1331 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1332 std::string key = keys(i); |
4817 | 1333 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1334 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1335 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1336 bool b = save_binary_data (os, val, key, "", 0, save_as_floats); |
4817 | 1337 |
1338 if (! b) | |
1339 return os; | |
1340 } | |
1341 | |
1342 return true; | |
1343 } | |
1344 | |
1345 bool | |
1346 octave_struct::load_binary (std::istream& is, bool swap, | |
5760 | 1347 oct_mach_info::float_format fmt) |
4817 | 1348 { |
1349 bool success = true; | |
5828 | 1350 int32_t len; |
5760 | 1351 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4817 | 1352 return false; |
1353 if (swap) | |
4944 | 1354 swap_bytes<4> (&len); |
4817 | 1355 |
1356 if (len > 0) | |
1357 { | |
1358 Octave_map m (map); | |
1359 | |
5275 | 1360 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1361 { |
1362 octave_value t2; | |
1363 bool dummy; | |
1364 std::string doc; | |
1365 | |
1366 // recurse to read cell elements | |
1367 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
1368 dummy, t2, doc); | |
1369 | |
1370 if (!is) | |
1371 break; | |
1372 | |
6293 | 1373 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1374 |
6293 | 1375 if (error_state) |
1376 { | |
1377 error ("load: internal error loading struct elements"); | |
1378 return false; | |
1379 } | |
5433 | 1380 |
6293 | 1381 m.assign (nm, tcell); |
4817 | 1382 } |
1383 | |
1384 if (is) | |
1385 map = m; | |
1386 else | |
1387 { | |
1388 error ("load: failed to load structure"); | |
1389 success = false; | |
1390 } | |
1391 } | |
1392 else if (len == 0 ) | |
6292 | 1393 map = Octave_map (dim_vector (1, 1)); |
4817 | 1394 else |
1395 panic_impossible (); | |
1396 | |
1397 return success; | |
1398 } | |
1399 | |
1400 #if defined (HAVE_HDF5) | |
1401 | |
1402 bool | |
1403 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1404 { | |
1405 hid_t data_hid = -1; | |
1406 | |
1407 data_hid = H5Gcreate (loc_id, name, 0); | |
1408 if (data_hid < 0) return false; | |
1409 | |
1410 // recursively add each element of the structure to this group | |
1411 Octave_map m = map_value (); | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1412 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1413 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1414 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1415 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1416 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1417 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1418 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1419 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1420 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1421 std::string key = keys(i); |
4817 | 1422 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1423 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1424 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1425 bool retval2 = add_hdf5_data (data_hid, val, key, "", false, |
4817 | 1426 save_as_floats); |
1427 | |
1428 if (! retval2) | |
1429 break; | |
1430 } | |
1431 | |
1432 H5Gclose (data_hid); | |
4837 | 1433 |
4817 | 1434 return true; |
1435 } | |
1436 | |
1437 bool | |
1438 octave_struct::load_hdf5 (hid_t loc_id, const char *name, | |
1439 bool have_h5giterate_bug) | |
1440 { | |
1441 bool retval = false; | |
1442 | |
1443 hdf5_callback_data dsub; | |
1444 | |
1445 herr_t retval2 = 0; | |
6292 | 1446 Octave_map m (dim_vector (1, 1)); |
4817 | 1447 int current_item = 0; |
1448 #ifdef HAVE_H5GGET_NUM_OBJS | |
1449 hsize_t num_obj = 0; | |
5060 | 1450 hid_t group_id = H5Gopen (loc_id, name); |
1451 H5Gget_num_objs (group_id, &num_obj); | |
1452 H5Gclose (group_id); | |
4817 | 1453 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1454 // FIXME -- fields appear to be sorted alphabetically on loading. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1455 // Why is that happening? |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1456 |
4817 | 1457 while (current_item < static_cast<int> (num_obj) |
1458 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1459 hdf5_read_next_data, &dsub)) > 0) | |
1460 #else | |
1461 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1462 hdf5_read_next_data, &dsub)) > 0) | |
1463 #endif | |
1464 { | |
5342 | 1465 octave_value t2 = dsub.tc; |
1466 | |
6293 | 1467 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1468 |
6293 | 1469 if (error_state) |
1470 { | |
1471 error ("load: internal error loading struct elements"); | |
1472 return false; | |
1473 } | |
5433 | 1474 |
6293 | 1475 m.assign (dsub.name, tcell); |
5336 | 1476 |
4817 | 1477 if (have_h5giterate_bug) |
1478 current_item++; // H5Giterate returned the last index processed | |
1479 } | |
1480 | |
1481 if (retval2 >= 0) | |
1482 { | |
1483 map = m; | |
1484 retval = true; | |
1485 } | |
1486 | |
1487 return retval; | |
1488 } | |
1489 | |
4687 | 1490 #endif |
1491 | |
5900 | 1492 mxArray * |
1493 octave_struct::as_mxArray (void) const | |
1494 { | |
1495 int nf = nfields (); | |
1496 string_vector kv = map_keys (); | |
6065 | 1497 |
1498 OCTAVE_LOCAL_BUFFER (const char *, f, nf); | |
1499 | |
5900 | 1500 for (int i = 0; i < nf; i++) |
6065 | 1501 f[i] = kv[i].c_str (); |
5900 | 1502 |
1503 mxArray *retval = new mxArray (dims (), nf, f); | |
1504 | |
1505 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1506 | |
6686 | 1507 mwSize nel = numel (); |
5900 | 1508 |
6686 | 1509 mwSize ntot = nf * nel; |
5900 | 1510 |
1511 for (int i = 0; i < nf; i++) | |
1512 { | |
1513 Cell c = map.contents (kv[i]); | |
1514 | |
1515 const octave_value *p = c.data (); | |
1516 | |
6686 | 1517 mwIndex k = 0; |
1518 for (mwIndex j = i; j < ntot; j += nf) | |
5900 | 1519 elts[j] = new mxArray (p[k++]); |
1520 } | |
1521 | |
1522 return retval; | |
1523 } | |
1524 | |
2376 | 1525 /* |
1526 ;;; Local Variables: *** | |
1527 ;;; mode: C++ *** | |
1528 ;;; End: *** | |
1529 */ |