Are the 'unset' indices defined to have some value in your application?  If 
not, trying to shoehorn this into an `AbstractArray` probably won't work 
very well.  Also note that, while correct, using the methods in base will 
be slow if you have very large and very sparse matrices.

In defining the type, you still need to store that dictionary somewhere. 
 Here's a quick simplification for a minimal definition:

immutable SpatialData{T} <: AbstractArray{T,3}
    data::Dict{NTuple{3,Int}, T}
    dims::NTuple{3,Int}
end
SpatialData{T}(::Type{T}, d1::Int, d2::Int, d3::Int) = SpatialData(T, 
(d1,d2,d3))
SpatialData{T}(::Type{T}, dims::NTuple{3,Int}) = 
SpatialData(Dict{NTuple{3,Int}, T}(), dims)

Base.size(A::SpatialData) = A.dims
Base.similar{T}(A::SpatialData, ::Type{T}, dims::NTuple{3,Int}) = 
SpatialData(T, dims)
Base.getindex{T}(A::SpatialData{T}, i1::Int, i2::Int, i3::Int) = 
(checkbounds(A,i1,i2,i3); get(A.data,(i1,i2,i3),zero(T)))
Base.setindex!{T}(A::SpatialData{T}, v, i1::Int, i2::Int, i3::Int) = 
(checkbounds(A,i1,i2,i3); A.data[(i1,i2,i3)] = v)

You then construct it and work with it like you do an array:

julia> A = SpatialData(Float64, 4, 3, 2)
4x3x2 SpatialData{Float64}:
[:, :, 1] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

[:, :, 2] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> A[:,1] = 2.0
2.0

julia> A
4x3x2 SpatialData{Float64}:
[:, :, 1] =
 2.0  0.0  0.0
 2.0  0.0  0.0
 2.0  0.0  0.0
 2.0  0.0  0.0

[:, :, 2] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0



On Tuesday, July 7, 2015 at 12:43:36 PM UTC-4, Júlio Hoffimann wrote:
>
> Hi Matt,
>
> That is a very good suggestion! I'm using Julia 0.4, how would you 
> retrieve the locations from an AbstractArray?
>
> immutable SpatialData <: AbstractArray{Real,3} end
>
> is all that is needed to define the type?
>
> -Júlio
>

Reply via email to