Hi,
    I saw in the release notes that Julia added support for different array 
indexing methods. I decided to try my hand at implementing zero indexed 
vectors, and started with the instructions 
here http://docs.julialang.org/en/latest/devdocs/offset-arrays/ I found 
this part of the documentation to be unhelpful compared to other parts of 
the Julia docs (is that just me?), but with a bit of hacking I came up with 
this

immutable ZeroIndexedVector{T} <: AbstractArray{T, 1}
    data::Array{T,1}
end

Base.linearindices{T}(A::ZeroIndexedVector{T}) = 0:(length(A.data)-1)
Base.getindex{T}(A::ZeroIndexedVector{T}, i::Int) = A.data[i + 1]
Base.setindex!{T}(A::ZeroIndexedVector{T}, v, i::Int) = (A.data[i + 1] = v)
Base.indices{T}(A::ZeroIndexedVector{T}) =
    (0:(length(A.data)-1),)

Base.endof{T}(A::ZeroIndexedVector{T}) = length(A.data) - 1
Base.show{T}(A::ZeroIndexedVector{T}) = show(A.data)
function Base.display{T}(A::ZeroIndexedVector{T})
    @printf("%d-element ZeroIndexedVector{%s, 1}:\n",
            length(A.data),
            string(T))
    for elt in A.data
       @printf("%s\n", string(elt))
    end
end

Apart from some obvious failings (eg, not copying he backing array) it 
seems to work. What's the best way to write this code? How does one extend 
this to multidimensional arrays? IMO, the explanation of how to do these 
two things would have served as good examples on the doc page.

-- Brian

Reply via email to