@@ -205,53 +205,45 @@ function getindex{T,S<:Integer}(s::SubArray{T,1}, I::AbstractVector{S})
205
205
end
206
206
207
207
function translate_indexes (s:: SubArray , I:: Union(Real,AbstractArray) ...)
208
- I = indices (I)
209
- nds = ndims (s)
210
208
n = length (I)
211
- if n > nds
212
- throw (BoundsError ())
209
+ newindexes = Any[s. indexes... ]
210
+ pdims = parentdims (s)
211
+ havelinear = n < ndims (s)
212
+ for i = 1 : n- havelinear
213
+ newindexes[pdims[i]] = s. indexes[pdims[i]][I[i]]
213
214
end
214
- ndp = ndims (s. parent) - (nds- n)
215
- newindexes = Array (Any, ndp)
216
- sp = strides (s. parent)
217
- j = 1
218
- for i = 1 : ndp
219
- t = s. indexes[i]
220
- if j <= nds && s. strides[j] == sp[i]
221
- # TODO : don't generate the dense vector indexes if they can be ranges
222
- if j== n && n < nds
223
- newindexes[i] = translate_linear_indexes (s, j, I[j])
224
- else
225
- newindexes[i] = isa (t, Int) ? t : t[I[j]]
226
- end
227
- j += 1
228
- else
229
- newindexes[i] = t
230
- end
215
+ lastdim = pdims[n]
216
+ if havelinear
217
+ newindexes = newindexes[1 : lastdim]
218
+ newindexes[pdims[n]] = translate_linear_indexes (s, n, I[end ], pdims)
231
219
end
232
220
newindexes
233
221
end
234
222
235
223
# translate a linear index vector I for dim n to a linear index vector for
236
224
# the parent array
237
- function translate_linear_indexes (s, n, I)
225
+ function translate_linear_indexes (s, n, I, pdims )
238
226
idx = Array (Int, length (I))
239
227
ssztail = size (s)[n: ]
240
- pdims = parentdims (s)
241
228
psztail = size (s. parent)[pdims[n: ]]
229
+ taildimsoffset = 0
230
+ for i = pdims[end ]+ 1 : ndims (s. parent)
231
+ taildimsoffset += (s. indexes[i]- 1 )* stride (s. parent, i)
232
+ end
242
233
for j= 1 : length (I)
243
234
su = ind2sub (ssztail,I[j])
244
- idx[j] = sub2ind (psztail, [ s. indexes[pdims[n+ k- 1 ]][su[k]] for k= 1 : length (su) ]. .. )
235
+ idx[j] = sub2ind (psztail, [ s. indexes[pdims[n+ k- 1 ]][su[k]] for k= 1 : length (su) ]. .. ) + taildimsoffset
245
236
end
246
237
idx
247
238
end
248
239
249
240
function parentdims (s:: SubArray )
250
- dimindex = Array (Int, ndims (s))
241
+ nd = ndims (s)
242
+ dimindex = Array (Int, nd)
251
243
sp = strides (s. parent)
252
244
j = 1
253
245
for i = 1 : ndims (s. parent)
254
- if sp[i] == s. strides[j]
246
+ if j <= nd && sp[i] == s. strides[j]
255
247
dimindex[j] = i
256
248
j += 1
257
249
end
1 commit comments
JeffBezanson commentedon Jul 18, 2013
Much appreciated! Index gymnastics routinely make by brain freeze. The new code does look much nicer too.