Mercurial > hg > octave-lyh
comparison src/ov-struct.cc @ 4513:508238e65af7
[project @ 2003-09-19 21:40:57 by jwe]
author | jwe |
---|---|
date | Fri, 19 Sep 2003 21:41:21 +0000 |
parents | 55695bf73797 |
children | e84592dc70b9 |
comparison
equal
deleted
inserted
replaced
4512:b55eaa010770 | 4513:508238e65af7 |
---|---|
42 | 42 |
43 DEFINE_OCTAVE_ALLOCATOR(octave_struct); | 43 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
44 | 44 |
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct"); | 45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct"); |
46 | 46 |
47 octave_value_list | 47 Cell |
48 octave_struct::dotref (const octave_value_list& idx) | 48 octave_struct::dotref (const octave_value_list& idx) |
49 { | 49 { |
50 octave_value_list retval; | 50 Cell retval; |
51 | 51 |
52 assert (idx.length () == 1); | 52 assert (idx.length () == 1); |
53 | 53 |
54 std::string nm = idx(0).string_value (); | 54 std::string nm = idx(0).string_value (); |
55 | 55 |
61 error ("structure has no member `%s'", nm.c_str ()); | 61 error ("structure has no member `%s'", nm.c_str ()); |
62 | 62 |
63 return retval; | 63 return retval; |
64 } | 64 } |
65 | 65 |
66 #if 0 | |
66 static void | 67 static void |
67 gripe_invalid_index (void) | 68 gripe_invalid_index (void) |
68 { | 69 { |
69 error ("invalid index for structure array"); | 70 error ("invalid index for structure array"); |
70 } | 71 } |
72 #endif | |
71 | 73 |
72 static void | 74 static void |
73 gripe_invalid_index_for_assignment (void) | 75 gripe_invalid_index_for_assignment (void) |
74 { | 76 { |
75 error ("invalid index for structure array assignment"); | 77 error ("invalid index for structure array assignment"); |
102 if (type.length () > 1 && type[1] == '.') | 104 if (type.length () > 1 && type[1] == '.') |
103 { | 105 { |
104 std::list<octave_value_list>::const_iterator p = idx.begin (); | 106 std::list<octave_value_list>::const_iterator p = idx.begin (); |
105 octave_value_list key_idx = *++p; | 107 octave_value_list key_idx = *++p; |
106 | 108 |
107 octave_value_list tmp = dotref (key_idx); | 109 Cell tmp = dotref (key_idx); |
108 | 110 |
109 if (! error_state) | 111 if (! error_state) |
110 { | 112 { |
111 octave_value_list t_idx = idx.front (); | 113 Cell t = tmp.index (idx.front ()); |
112 | 114 |
113 if (t_idx.length () == 1) | 115 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
114 { | 116 |
115 idx_vector i = t_idx(0).index_vector (); | 117 // We handled two index elements, so tell |
116 octave_value_list t = tmp.index (i); | 118 // next_subsref to skip both of them. |
117 | 119 |
118 retval = (t.length () == 1) ? t(0) : octave_value (t, true); | 120 skip++; |
119 | |
120 // We handled two index elements, so tell | |
121 // next_subsref to skip both of them. | |
122 | |
123 skip++; | |
124 } | |
125 else | |
126 gripe_invalid_index (); | |
127 } | 121 } |
128 } | 122 } |
129 else | 123 else |
130 { | 124 retval = map.index (idx.front ()); |
131 octave_value_list t_idx = idx.front (); | |
132 | |
133 if (t_idx.length () == 1) | |
134 { | |
135 idx_vector i = t_idx(0).index_vector (); | |
136 retval = map.index (i); | |
137 } | |
138 else | |
139 gripe_invalid_index (); | |
140 } | |
141 } | 125 } |
142 break; | 126 break; |
143 | 127 |
144 case '.': | 128 case '.': |
145 { | 129 { |
146 octave_value_list t = dotref (idx.front ()); | 130 Cell t = dotref (idx.front ()); |
147 | 131 |
148 retval = (t.length () == 1) ? t(0) : octave_value (t, true); | 132 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
149 } | 133 } |
150 break; | 134 break; |
151 | 135 |
162 | 146 |
163 return retval; | 147 return retval; |
164 } | 148 } |
165 | 149 |
166 octave_value | 150 octave_value |
167 octave_struct::numeric_conv (const octave_value_list& val, | 151 octave_struct::numeric_conv (const Cell& val, |
168 const std::string& type) | 152 const std::string& type) |
169 { | 153 { |
170 octave_value retval; | 154 octave_value retval; |
171 | 155 |
172 if (val.length () == 1) | 156 if (val.length () == 1) |
202 if (type.length () > 1 && type[1] == '.') | 186 if (type.length () > 1 && type[1] == '.') |
203 { | 187 { |
204 std::list<octave_value_list>::const_iterator p = idx.begin (); | 188 std::list<octave_value_list>::const_iterator p = idx.begin (); |
205 octave_value_list t_idx = *p; | 189 octave_value_list t_idx = *p; |
206 | 190 |
207 if (t_idx.length () == 1) | 191 octave_value_list key_idx = *++p; |
192 | |
193 assert (key_idx.length () == 1); | |
194 | |
195 std::string key = key_idx(0).string_value (); | |
196 | |
197 octave_value u; | |
198 | |
199 if (! map.contains (key)) | |
200 u = octave_value::empty_conv (type.substr (2), rhs); | |
201 else | |
208 { | 202 { |
209 octave_value_list key_idx = *++p; | 203 Cell map_val = map[key]; |
210 | 204 |
211 assert (key_idx.length () == 1); | 205 Cell map_elt = map_val.index (idx.front (), true); |
212 | 206 |
213 std::string key = key_idx(0).string_value (); | 207 u = numeric_conv (map_elt, type.substr (2)); |
214 | |
215 octave_value u; | |
216 | |
217 if (! map.contains (key)) | |
218 u = octave_value::empty_conv (type.substr (2), rhs); | |
219 else | |
220 { | |
221 octave_value_list map_val = map[key]; | |
222 | |
223 octave_value_list t_idx = idx.front (); | |
224 | |
225 idx_vector i = t_idx(0).index_vector (); | |
226 | |
227 octave_value_list map_elt = map_val.index (i, true); | |
228 | |
229 u = numeric_conv (map_elt, type.substr (2)); | |
230 } | |
231 | |
232 if (! error_state) | |
233 { | |
234 std::list<octave_value_list> next_idx (idx); | |
235 | |
236 // We handled two index elements, so subsasgn to | |
237 // needs to skip both of them. | |
238 | |
239 next_idx.erase (next_idx.begin ()); | |
240 next_idx.erase (next_idx.begin ()); | |
241 | |
242 u.make_unique (); | |
243 | |
244 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); | |
245 } | |
246 } | 208 } |
247 else | 209 |
248 gripe_invalid_index_for_assignment (); | 210 if (! error_state) |
211 { | |
212 std::list<octave_value_list> next_idx (idx); | |
213 | |
214 // We handled two index elements, so subsasgn to | |
215 // needs to skip both of them. | |
216 | |
217 next_idx.erase (next_idx.begin ()); | |
218 next_idx.erase (next_idx.begin ()); | |
219 | |
220 u.make_unique (); | |
221 | |
222 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); | |
223 } | |
249 } | 224 } |
250 else | 225 else |
251 gripe_invalid_index_for_assignment (); | 226 gripe_invalid_index_for_assignment (); |
252 } | 227 } |
253 break; | 228 break; |
264 | 239 |
265 if (! map.contains (key)) | 240 if (! map.contains (key)) |
266 u = octave_value::empty_conv (type.substr (1), rhs); | 241 u = octave_value::empty_conv (type.substr (1), rhs); |
267 else | 242 else |
268 { | 243 { |
269 octave_value_list map_val = map[key]; | 244 Cell map_val = map[key]; |
270 | 245 |
271 u = numeric_conv (map_val, type.substr (1)); | 246 u = numeric_conv (map_val, type.substr (1)); |
272 } | 247 } |
273 | 248 |
274 if (! error_state) | 249 if (! error_state) |
308 | 283 |
309 std::string key = key_idx(0).string_value (); | 284 std::string key = key_idx(0).string_value (); |
310 | 285 |
311 if (! error_state) | 286 if (! error_state) |
312 { | 287 { |
313 octave_value_list t_idx = idx.front (); | 288 map.assign (idx.front (), key, t_rhs); |
314 | 289 |
315 if (t_idx.length () == 1) | 290 if (! error_state) |
316 { | 291 retval = octave_value (this, count + 1); |
317 idx_vector i = t_idx(0).index_vector (); | |
318 | |
319 map.assign (i, key, t_rhs); | |
320 | |
321 if (! error_state) | |
322 retval = octave_value (this, count + 1); | |
323 else | |
324 gripe_failed_assignment (); | |
325 } | |
326 else | 292 else |
327 gripe_invalid_index_for_assignment (); | 293 gripe_failed_assignment (); |
328 } | 294 } |
329 else | 295 else |
330 gripe_failed_assignment (); | 296 gripe_failed_assignment (); |
331 } | 297 } |
332 else | 298 else |
333 { | 299 { |
334 octave_value_list t_idx = idx.front (); | 300 Octave_map rhs_map = t_rhs.map_value (); |
335 | 301 |
336 if (t_idx.length () == 1) | 302 if (! error_state) |
337 { | 303 { |
338 idx_vector i = t_idx(0).index_vector (); | 304 map.assign (idx.front (), rhs_map); |
339 | |
340 Octave_map rhs_map = t_rhs.map_value (); | |
341 | 305 |
342 if (! error_state) | 306 if (! error_state) |
343 { | 307 retval = octave_value (this, count + 1); |
344 map.assign (i, rhs_map); | |
345 | |
346 if (! error_state) | |
347 retval = octave_value (this, count + 1); | |
348 else | |
349 gripe_failed_assignment (); | |
350 } | |
351 else | 308 else |
352 error ("invalid structure assignment"); | 309 gripe_failed_assignment (); |
353 } | 310 } |
311 else | |
312 error ("invalid structure assignment"); | |
354 } | 313 } |
355 } | 314 } |
356 break; | 315 break; |
357 | 316 |
358 case '.': | 317 case '.': |
418 int n = map.array_length (); | 377 int n = map.array_length (); |
419 | 378 |
420 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | 379 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
421 { | 380 { |
422 std::string key = map.key (p); | 381 std::string key = map.key (p); |
423 octave_value_list val = map.contents (p); | 382 Cell val = map.contents (p); |
424 | 383 |
425 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); | 384 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
426 | 385 |
427 if (print_keys_only) | 386 if (print_keys_only) |
428 { | 387 { |