Mercurial > hg > octave-max
comparison liboctave/Array.cc @ 9886:cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 29 Nov 2009 07:51:15 +0100 |
parents | ead4f9c82a9a |
children | 56fbe170d354 |
comparison
equal
deleted
inserted
replaced
9885:dd3fc8ba4796 | 9886:cddd5c3d5f04 |
---|---|
1178 octave_idx_type n = numel (), rhl = rhs.numel (); | 1178 octave_idx_type n = numel (), rhl = rhs.numel (); |
1179 | 1179 |
1180 if (rhl == 1 || i.length (n) == rhl) | 1180 if (rhl == 1 || i.length (n) == rhl) |
1181 { | 1181 { |
1182 octave_idx_type nx = i.extent (n); | 1182 octave_idx_type nx = i.extent (n); |
1183 bool colon = i.is_colon_equiv (nx); | |
1183 // Try to resize first if necessary. | 1184 // Try to resize first if necessary. |
1184 if (nx != n) | 1185 if (nx != n) |
1185 { | 1186 { |
1186 // Optimize case A = []; A(1:n) = X with A empty. | 1187 // Optimize case A = []; A(1:n) = X with A empty. |
1187 if (rows () == 0 && columns () == 0 && ndims () == 2 | 1188 if (dimensions.zero_by_zero () && colon) |
1188 && i.is_colon_equiv (nx)) | |
1189 { | 1189 { |
1190 if (rhl == 1) | 1190 if (rhl == 1) |
1191 *this = Array<T> (dim_vector (1, nx), rhs(0)); | 1191 *this = Array<T> (dim_vector (1, nx), rhs(0)); |
1192 else | 1192 else |
1193 *this = Array<T> (rhs, dim_vector (1, nx)); | 1193 *this = Array<T> (rhs, dim_vector (1, nx)); |
1196 | 1196 |
1197 resize_fill (nx, rfv); | 1197 resize_fill (nx, rfv); |
1198 n = numel (); | 1198 n = numel (); |
1199 } | 1199 } |
1200 | 1200 |
1201 if (i.is_colon ()) | 1201 if (colon) |
1202 { | 1202 { |
1203 // A(:) = X makes a full fill or a shallow copy. | 1203 // A(:) = X makes a full fill or a shallow copy. |
1204 if (rhl == 1) | 1204 if (rhl == 1) |
1205 fill (rhs(0)); | 1205 fill (rhs(0)); |
1206 else | 1206 else |
1247 || (rhdv.length () == 2 && il == rhdv(0) && jl == rhdv(1))); | 1247 || (rhdv.length () == 2 && il == rhdv(0) && jl == rhdv(1))); |
1248 match = match || (il == 1 && jl == rhdv(0) && rhdv(1) == 1); | 1248 match = match || (il == 1 && jl == rhdv(0) && rhdv(1) == 1); |
1249 | 1249 |
1250 if (match) | 1250 if (match) |
1251 { | 1251 { |
1252 bool all_colons = (i.is_colon_equiv (rdv(0)) | |
1253 && j.is_colon_equiv (rdv(1))); | |
1252 // Resize if requested. | 1254 // Resize if requested. |
1253 if (rdv != dv) | 1255 if (rdv != dv) |
1254 { | 1256 { |
1255 // Optimize case A = []; A(1:m, 1:n) = X | 1257 // Optimize case A = []; A(1:m, 1:n) = X |
1256 if (dv.all_zero () && i.is_colon_equiv (rdv(0)) | 1258 if (dv.zero_by_zero () && all_colons) |
1257 && j.is_colon_equiv (rdv(1))) | |
1258 { | 1259 { |
1259 if (isfill) | 1260 if (isfill) |
1260 *this = Array<T> (rdv, rhs(0)); | 1261 *this = Array<T> (rdv, rhs(0)); |
1261 else | 1262 else |
1262 *this = Array<T> (rhs, rdv); | 1263 *this = Array<T> (rhs, rdv); |
1265 | 1266 |
1266 resize (rdv, rfv); | 1267 resize (rdv, rfv); |
1267 dv = dimensions; | 1268 dv = dimensions; |
1268 } | 1269 } |
1269 | 1270 |
1270 if (i.is_colon () && j.is_colon ()) | 1271 if (all_colons) |
1271 { | 1272 { |
1272 // A(:,:) = X makes a full fill or a shallow copy | 1273 // A(:,:) = X makes a full fill or a shallow copy |
1273 if (isfill) | 1274 if (isfill) |
1274 fill (rhs(0)); | 1275 fill (rhs(0)); |
1275 else | 1276 else |
1351 | 1352 |
1352 rhdv.chop_all_singletons (); | 1353 rhdv.chop_all_singletons (); |
1353 int j = 0, rhdvl = rhdv.length (); | 1354 int j = 0, rhdvl = rhdv.length (); |
1354 for (int i = 0; i < ial; i++) | 1355 for (int i = 0; i < ial; i++) |
1355 { | 1356 { |
1356 all_colons = all_colons && ia(i).is_colon (); | 1357 all_colons = all_colons && ia(i).is_colon_equiv (rdv(i)); |
1357 octave_idx_type l = ia(i).length (rdv(i)); | 1358 octave_idx_type l = ia(i).length (rdv(i)); |
1358 if (l == 1) continue; | 1359 if (l == 1) continue; |
1359 match = match && j < rhdvl && l == rhdv(j++); | 1360 match = match && j < rhdvl && l == rhdv(j++); |
1360 } | 1361 } |
1361 | 1362 |
1365 if (match) | 1366 if (match) |
1366 { | 1367 { |
1367 // Resize first if necessary. | 1368 // Resize first if necessary. |
1368 if (rdv != dv) | 1369 if (rdv != dv) |
1369 { | 1370 { |
1371 // Optimize case A = []; A(1:m, 1:n) = X | |
1372 if (dv.zero_by_zero () && all_colons) | |
1373 { | |
1374 rdv.chop_trailing_singletons (); | |
1375 if (isfill) | |
1376 *this = Array<T> (rdv, rhs(0)); | |
1377 else | |
1378 *this = Array<T> (rhs, rdv); | |
1379 return; | |
1380 } | |
1381 | |
1370 resize_fill (rdv, rfv); | 1382 resize_fill (rdv, rfv); |
1371 dv = dimensions; | 1383 dv = dimensions; |
1372 chop_trailing_singletons (); | 1384 chop_trailing_singletons (); |
1373 } | 1385 } |
1374 | 1386 |