2525creates a view of the array `A` that applies `f` to every element of
2626`A`. The view is read-only (you can get values but not set them).
2727"""
28- mappedarray {T,N} (f, data:: AbstractArray{T,N} ) = ReadonlyMappedArray {typeof(f(one(T ))),N,typeof(data),typeof(f)} (f, data)
28+ mappedarray {T,N} (f, data:: AbstractArray{T,N} ) = ReadonlyMappedArray {typeof(f(testvalue(data ))),N,typeof(data),typeof(f)} (f, data)
2929
3030"""
3131 mappedarray((f, finv), A)
@@ -36,7 +36,7 @@ the view and, correspondingly, the values in `A`.
3636"""
3737function mappedarray {T,N} (f_finv:: Tuple{Any,Any} , data:: AbstractArray{T,N} )
3838 f, finv = f_finv
39- MappedArray {typeof(f(one(T ))),N,typeof(data),typeof(f),typeof(finv)} (f, finv, data)
39+ MappedArray {typeof(f(testvalue(data ))),N,typeof(data),typeof(f),typeof(finv)} (f, finv, data)
4040end
4141
4242"""
@@ -54,10 +54,17 @@ Base.size(A::AbstractMappedArray) = size(A.data)
5454Base. indices (A:: AbstractMappedArray ) = indices (A. data)
5555parenttype {T,N,A,F} (:: Type{ReadonlyMappedArray{T,N,A,F}} ) = A
5656parenttype {T,N,A,F,Finv} (:: Type{MappedArray{T,N,A,F,Finv}} ) = A
57- Base. linearindexing {MA<:AbstractMappedArray} (:: Type{MA} ) = Base . linearindexing (parenttype (MA))
57+ @compat Base. IndexStyle {MA<:AbstractMappedArray} (:: Type{MA} ) = IndexStyle (parenttype (MA))
5858
5959@propagate_inbounds Base. getindex (A:: AbstractMappedArray , i:: Int... ) = A. f (A. data[i... ])
6060@propagate_inbounds Base. setindex! {T} (A:: MappedArray{T} , val:: T , i:: Int... ) = A. data[i... ] = A. finv (val)
6161@inline Base. setindex! {T} (A:: MappedArray{T} , val, i:: Int... ) = setindex! (A, convert (T, val), i... )
6262
63+ function testvalue (data)
64+ if ! isempty (data)
65+ return first (data)
66+ end
67+ zero (eltype (data))
68+ end
69+
6370end # module
0 commit comments