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