sub(A::AbstractArray, I::Union(RangeIndex, Colon)...) = sub(A, ntuple(length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...)
julia> getindex(A::FArray, ::Colon) = A[start(A) : endof(A)]
getindex (generic function with 177 methods)
julia> getindex2(A::FArray, ::Colon) = A[start(A) : endof(A)]
getindex2 (generic function with 1 method)
julia> A = FZeros(Int, -2:8)
11-element FArray{Int64,1} (-2:8,)
julia> A[:]
8-element Array{Int64,1}:
0
0
0
0
0
0
0
0
julia> @which A[:]
getindex(A::AbstractArray{T,N},I::AbstractArray{T,N}) at abstractarray.jl:380
julia> getindex2(A, :)
11-element Array{Int64,1}:
0
0
0
0
0
0
0
0
0
0
0
julia>
I recently implemented a similar transformation for array-like objects, in which I decided to create functions bounds(A, dim) = (lbound(A, dim), ubound(A, dim))
which return the end points of the indexing axes; without those, it was really hard to deduce what indices things should map to.
It might be that these should actually be included in Base and used by the colon indexing implementation for AbstractArray
s (bounds(A, dim) = (1, size(A, dim))
for all Array
s, so could probably be inlined at no runtime cost). If so, you could use them too and have your FArray
type return e.g. (-2, 8)
, which would allow the default colon indexing implementation to Just Work™.
// T
Colon lowering changed in 0.4,
sub(A::AbstractArray, I::Union(RangeIndex, Colon)...) = sub(A, ntuple(length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...)
Thanks again,Greg
julia> :
Colon()
julia> f(x::Colon) = x
f(:)
Colon()
julia> expand(:(A[end]))
:(getindex(A,(top(endof))(A)))
julia> expand(:(A[1, end]))
:(getindex(A,1,(top(trailingsize))(A,2)))
julia> expand(:(A[1, end, 3]))
:(getindex(A,1,(top(size))(A,2),3))
# Julia 0.3:
julia> expand(:(A[:]))
:(getindex(A,colon(1,(top(endof))(A))))
# Julia 0.4:
julia> expand(:(A[:]))
:(getindex(A,:))