Hi,
I’m trying to create an array view (for data from C++) that uses 0-based indices. I got most of it working, except for the use of end
as an index into a multidimensional error. The following code mimics what I want to do in a standalone example, the final test fails:
using Compat
using Base.Test
using CustomUnitRanges: filename_for_zerorange
include(filename_for_zerorange)
immutable PtrView{ScalarT,N,LayoutT} <: AbstractArray{ScalarT,N}
ptr::Ptr{ScalarT}
size::NTuple{N,Int}
end
immutable LayoutLeft
end
PtrView{ScalarT,N,LayoutT}(A::Array{ScalarT,N},::Type{LayoutT}) = PtrView{ScalarT,N,LayoutT}(pointer(A), size(A))
@compat Base.IndexStyle{ScalarT,N,LayoutT}(::Type{PtrView{ScalarT,N,LayoutT}}) = IndexLinear()
Base.indices{ScalarT,N}(v::PtrView{ScalarT,N,LayoutLeft}) = map(ZeroRange, v.size)
Base.endof{ScalarT,N}(v::PtrView{ScalarT,N,LayoutLeft}) = prod(v.size)-1
Base.getindex{ScalarT,N}(v::PtrView{ScalarT,N,LayoutLeft}, i::Integer) = unsafe_load(v.ptr,i+1)
Base.setindex!{ScalarT,N}(v::PtrView{ScalarT,N,LayoutLeft}, value, i::Integer) = unsafe_store!(v.ptr,value,i+1)
v_arr = [1,2,3]
A_arr = [1 2 3;4 5 6]
v = PtrView(v_arr, LayoutLeft)
A = PtrView(A_arr, LayoutLeft)
@show @test v[0] == 1
@show @test v[end] == 3
@show @test A[0] == 1
@show @test A[end] == 6
@show @test A[0,end] == 3
The error:
Error During Test
Test threw an exception of type MethodError
Expression: A[0,end] == 3
MethodError: no method matching size(::PtrView{Int64,2,LayoutLeft})
Closest candidates are:
size{T,N}(::AbstractArray{T,N}, !Matched::Any) at abstractarray.jl:47
size{N}(::Any, !Matched::Integer, !Matched::Integer, !Matched::Integer...) at abstractarray.jl:48
size(!Matched::BitArray{1}) at bitarray.jl:39
...
in size at ./abstractarray.jl:47 [inlined]
in trailingsize(::PtrView{Int64,2,LayoutLeft}, ::Int64) at ./abstractarray.jl:201
ERROR: LoadError: There was an error during testing
in record(::Base.Test.FallbackTestSet, ::Base.Test.Error) at ./test.jl:397
in do_test(::Base.Test.Threw, ::Expr) at ./test.jl:281
Do I get around this by overriding Base.trailingsize
? This seems related but unconcluded:
https://github.com/JuliaLang/julia/issues/16104