Hi Spencer, Thanks for your reply.
Actually I'm trying to follow the Interfaces chapter. I'm using Julia v0.3, so that might be a problem. Also I think my problem is mainly for vectors (1-dimensional array), because linear/subscript index has same syntax. For multi-dimensional arrays, I can map subscript indexes to the default 1:length linear indexes. A = MyArray(Int, -2:8, -2:6) # defines 11x9 Matrix A[:] gets translated to A[1:99], which can be handled by linear indexing B = MyArray(Int, -2:8) # defines 11-element Vector B[:] gets translated to B[1:8], I need it to be translated to B[-2:8] So I'm trying to find out where this conversion happens, so I can redefine it. -- Greg PS Not really sure what to do about email name. Name on julia-users shows: me (Greg Plowman change <javascript:;>) On Monday, September 21, 2015 at 1:15:42 PM UTC+10, Spencer Russell wrote: > Hi Greg, > > This doesn’t answer your question directly, but I recommend you check out > the Interfaces > <http://docs.julialang.org/en/latest/manual/interfaces/#indexing> chapter > of the manual for some good info on creating your own array type. To get > indexing working the most important parts are: > > 1. subtype AbstractArray > 2. implement the Base.linearindexing(Type) method > 3. implement either getindex(A, i::Int) or > getindex(A, i1::Int, ..., iN::Int), (and the setindex! versions) depending > on step 2. > > Then the various indexing behaviors (ranges, etc.) should work for your > type, as under-the-hood they boil down to the more basic indexing method > that you define in step 3. One thing to note that’s not spelled out super > clearly in the manual yet is that if you want the results of range indexing > to be wrapped in your type, you should also implement `similar`. There’s > some more details on that in this pr > <https://github.com/JuliaLang/julia/pull/13212/files>. > > OT: something about the way your email client is configured is causing you > to show up as [email protected] <javascript:> where for other > users I see their names (at least in my email client), so it’s a bit hard > to see who you are in lists of thread participant names. > > -s > > > > > On Sep 20, 2015, at 9:34 PM, 'Greg Plowman' via julia-users < > [email protected] <javascript:>> 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 > > >
