comparison src/ov-cell.cc @ 8546:3d8a914c580e

improve parser indexed assigment code
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 20 Jan 2009 21:15:17 +0100
parents ad3afaaa19c1
children 906f976d35a8
comparison
equal deleted inserted replaced
8545:faccdb98d953 8546:3d8a914c580e
88 Cell tcell = tmp.cell_value (); 88 Cell tcell = tmp.cell_value ();
89 89
90 if (tcell.length () == 1) 90 if (tcell.length () == 1)
91 retval(0) = tcell(0,0); 91 retval(0) = tcell(0,0);
92 else 92 else
93 { 93 retval = octave_value (octave_value_list (tcell), true);
94 octave_idx_type n = tcell.numel ();
95
96 octave_value_list lst (n, octave_value ());
97
98 for (octave_idx_type i = 0; i < n; i++)
99 {
100 OCTAVE_QUIT;
101 lst(i) = tcell(i);
102 }
103
104 retval(0) = octave_value (lst, true);
105 }
106 } 94 }
107 } 95 }
108 break; 96 break;
109 97
110 case '.': 98 case '.':
177 } 165 }
178 break; 166 break;
179 167
180 case '{': 168 case '{':
181 { 169 {
182 octave_value tmp = do_index_op (idx.front (), true); 170 Cell tmpc = matrix.index (idx.front (), true);
183 171
184 if (! error_state) 172 if (! error_state)
185 { 173 {
186 if (tmp.numel () == 1) 174 std::list<octave_value_list> next_idx (idx);
175
176 next_idx.erase (next_idx.begin ());
177
178 std::string next_type = type.substr (1);
179
180 if (rhs.is_cs_list ())
181 {
182 const octave_value_list rhsl = rhs.list_value ();
183 if (tmpc.numel () == rhsl.length ())
184 {
185 for (octave_idx_type k = 0; k < tmpc.numel () && ! error_state; k++)
186 {
187 octave_value tmp = tmpc (k);
188 if (! tmp.is_defined () || tmp.is_zero_by_zero ())
189 {
190 tmp = octave_value::empty_conv (next_type, rhs);
191 tmp.make_unique (); // probably a no-op.
192 }
193 else
194 // optimization: ignore the copy still stored inside our array and in tmpc.
195 tmp.make_unique (2);
196
197 tmpc(k) = tmp.subsasgn (next_type, next_idx, rhsl(k));
198 }
199
200 t_rhs = octave_value (octave_value_list (tmpc), true);
201 }
202 else
203 error ("invalid cs-list length in assignment");
204 }
205 else if (tmpc.numel () == 1)
187 { 206 {
188 tmp = tmp.cell_value ()(0,0); 207 octave_value tmp = tmpc(0);
189
190 std::list<octave_value_list> next_idx (idx);
191
192 next_idx.erase (next_idx.begin ());
193 208
194 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) 209 if (! tmp.is_defined () || tmp.is_zero_by_zero ())
195 tmp = octave_value::empty_conv (type.substr (1), rhs); 210 {
211 tmp = octave_value::empty_conv (type.substr (1), rhs);
212 tmp.make_unique (); // probably a no-op.
213 }
196 else 214 else
197 { 215 // optimization: ignore the copy still stored inside our array and in tmpc.
198 // This is a bit of black magic. tmp is a shallow copy 216 tmp.make_unique (2);
199 // of an element inside this cell, and maybe more. To
200 // prevent make_unique from always forcing a copy, we
201 // temporarily delete the stored value.
202 assign (idx.front (), octave_value ());
203 tmp.make_unique ();
204 assign (idx.front (), Cell (tmp));
205 }
206 217
207 if (! error_state) 218 if (! error_state)
208 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); 219 t_rhs = tmp.subsasgn (next_type, next_idx, rhs);
209 } 220 }
210 else 221 else
211 error ("scalar indices required for {} in assignment."); 222 error ("invalid assignment to cs-list outside multiple assignment.");
212 } 223 }
213 } 224 }
214 break; 225 break;
215 226
216 case '.': 227 case '.':
270 } 281 }
271 else if (i.all_scalars () || do_index_op (i, true).numel () == 1) 282 else if (i.all_scalars () || do_index_op (i, true).numel () == 1)
272 // Regularize a null matrix if stored into a cell. 283 // Regularize a null matrix if stored into a cell.
273 octave_base_matrix<Cell>::assign (i, Cell (t_rhs.storable_value ())); 284 octave_base_matrix<Cell>::assign (i, Cell (t_rhs.storable_value ()));
274 else if (! error_state) 285 else if (! error_state)
275 error ("scalar indices required for {} in assignment."); 286 error ("invalid assignment to cs-list outside multiple assignment.");
276 287
277 if (! error_state) 288 if (! error_state)
278 { 289 {
279 count++; 290 count++;
280 retval = octave_value (this); 291 retval = octave_value (this);