Skip to content

Commit c907bde

Browse files
authored
Merge pull request #9 from JuliaArrays/teh/IndexStyle
Don't rely on one (fix #7), and fix IndexStyle depwarns
2 parents 00256ff + 0a86bbb commit c907bde

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.5
2-
Compat 0.17.0
2+
Compat 0.19.0

src/MappedArrays.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
creates 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
"""
3737
function 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)
4040
end
4141

4242
"""
@@ -54,10 +54,17 @@ Base.size(A::AbstractMappedArray) = size(A.data)
5454
Base.indices(A::AbstractMappedArray) = indices(A.data)
5555
parenttype{T,N,A,F}(::Type{ReadonlyMappedArray{T,N,A,F}}) = A
5656
parenttype{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+
6370
end # module

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ aabs = mappedarray(abs, a)
6565
for i = -2:2
6666
@test aabs[i] == abs(a[i])
6767
end
68+
69+
# issue #7
70+
astr = mappedarray(length, ["abc", "onetwothree"])
71+
@test eltype(astr) == Int
72+
@test astr == [3, 11]
73+
a = mappedarray(x->x+0.5, Int[])
74+
@test eltype(a) == Float64

0 commit comments

Comments
 (0)