In julia 0.5 I plan to add eachindex(A, d) which can return the appropriate range for your object along axis d.
--Tim On Monday, September 21, 2015 06:20:02 AM Matt Bauman wrote: > Here's a quick rundown on the status quo: > > * Colon lowering changed in 0.4, as did the AbstractArray's interface. > It's *much* easier to make your own arrays in 0.4. I suggest updating to > the latest RC. > > * There is still a very strong assumption in base Julia that the valid > indices in an AbstractArray `A` for dimension `d` are in `1:size(A, d)`. > * This means that once a base method has ensured that all the indices it > visits are within `1:size(A,d)`, it might wrap its algorithm with > `@inbounds`. > * This means that if you break that assumption, using a base function could > result in the wrong answer and/or memory corruption and/or segfaults. > * It also means that the `end` keyword won't lower correctly for your array > > Now, it is possible to break this assumption and still have things work, > but for right now, it's undocumented magic. There's several of us very > interested in doing this, so it'll get better. Also new in 0.4 is the > eachindex method, which helps here, too. The key is to aggressively check > bounds and explicitly error on any indexing method that you know might be > ambiguous in its meaning (e.g., does the caller know that your array has > funny indexing semantics or not?). > > Related > discussion: > https://groups.google.com/forum/#!searchin/julia-users/offsetarray%7Csort:d > ate/julia-users/fNisYpMdZ6o/tahp5AXaAwAJ > On Sunday, September 20, 2015 at 9:34:24 PM UTC-4, Greg Plowman wrote: > > Hi, > > > > I'm trying to define a custom Array type that can be indexed using > > arbitrary ranges. > > > > e.g. A = MyArray(Int, 3:8) would define a 6-element vector with indexes > > ranging from 3 to 8, rather than the default 1 to 6. > > > > I've made some progress, but am now stuck on how to handle colon indexing. > > > > A[4:6] works by defining appropriate getindex and setindex! > > > > e.g. setindex!{T,S<:Real}(A::MyArray{T,1}, value, I::AbstractVector{S}) > > = ... > > > > but A[:] = 0 seems to get translated to A[1:6] before dispatch on > > setindex!, so I can't hijack the call. > > > > From subarray.jl, the code below suggests I can specialise on the Colon > > type, but this doesn't seem to work for me. Colon appears to be converted > > to UnitRange *before* calling setindex! > > > > sub(A::AbstractArray, I::Union(RangeIndex, Colon)...) = sub(A, ntuple( > > length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...) > > > > > > Is there a way around this? > > Should I be able to specialise on the colon argument? > > > > -- Greg