Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fixes for julia 0.7 #37

Merged
merged 1 commit into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
Compat 0.32
Compat 0.45
49 changes: 28 additions & 21 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module OffsetArrays

using Base: Indices, tail
using Compat
using Compat: axes, CartesianIndices

export OffsetArray, OffsetVector, @unsafe

Expand All @@ -27,7 +28,7 @@ OffsetArray(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) where {T,N} =
OffsetArray(A, offsets)

OffsetArray{T,N}(inds::Indices{N}) where {T,N} =
OffsetArray{T,N,Array{T,N}}(Array{T,N}(map(length, inds)), map(indexoffset, inds))
OffsetArray{T,N,Array{T,N}}(Array{T,N}(uninitialized, map(length, inds)), map(indexoffset, inds))
OffsetArray{T}(inds::Indices{N}) where {T,N} = OffsetArray{T,N}(inds)
OffsetArray{T,N}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
OffsetArray{T}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
Expand All @@ -44,9 +45,9 @@ OffsetVector{T}(inds::AbstractUnitRange) where {T} = OffsetArray{T}(inds)
OffsetArray(A::AbstractArray{T,0}, inds::Tuple{}) where {T} = OffsetArray{T,0,typeof(A)}(A, ())
OffsetArray(A::AbstractArray{T,N}, inds::Tuple{}) where {T,N} = error("this should never be called")
function OffsetArray(A::AbstractArray{T,N}, inds::NTuple{N,AbstractUnitRange}) where {T,N}
lA = map(length, indices(A))
lA = map(length, axes(A))
lI = map(length, inds)
lA == lI || throw(DimensionMismatch("supplied indices do not agree with the size of the array (got size $lA for the array and $lI for the indices"))
lA == lI || throw(DimensionMismatch("supplied axes do not agree with the size of the array (got size $lA for the array and $lI for the indices"))
OffsetArray(A, map(indexoffset, inds))
end
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N} =
Expand All @@ -58,22 +59,22 @@ parenttype(A::OffsetArray) = parenttype(typeof(A))

Base.parent(A::OffsetArray) = A.parent

errmsg(A) = error("size not supported for arrays with indices $(indices(A)); see http://docs.julialang.org/en/latest/devdocs/offset-arrays/")
errmsg(A) = error("size not supported for arrays with axes $(axes(A)); see http://docs.julialang.org/en/latest/devdocs/offset-arrays/")
Base.size(A::OffsetArray) = errmsg(A)
Base.size(A::OffsetArray, d) = errmsg(A)
Base.eachindex(::IndexCartesian, A::OffsetArray) = CartesianRange(indices(A))
Base.eachindex(::IndexLinear, A::OffsetVector) = indices(A, 1)
Base.eachindex(::IndexCartesian, A::OffsetArray) = CartesianIndices(axes(A))
Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)

# Implementations of indices and indices1. Since bounds-checking is
# performance-critical and relies on indices, these are usually worth
# Implementations of axes and indices1. Since bounds-checking is
# performance-critical and relies on axes, these are usually worth
# optimizing thoroughly.
@inline Base.indices(A::OffsetArray, d) =
1 <= d <= length(A.offsets) ? plus(indices(parent(A))[d], A.offsets[d]) : (1:1)
@inline Base.indices(A::OffsetArray) =
_indices(indices(parent(A)), A.offsets) # would rather use ntuple, but see #15276
@inline _indices(inds, offsets) =
(plus(inds[1], offsets[1]), _indices(tail(inds), tail(offsets))...)
_indices(::Tuple{}, ::Tuple{}) = ()
@inline Compat.axes(A::OffsetArray, d) =
1 <= d <= length(A.offsets) ? plus(axes(parent(A))[d], A.offsets[d]) : (1:1)
@inline Compat.axes(A::OffsetArray) =
_axes(axes(parent(A)), A.offsets) # would rather use ntuple, but see #15276
@inline _axes(inds, offsets) =
(plus(inds[1], offsets[1]), _axes(tail(inds), tail(offsets))...)
_axes(::Tuple{}, ::Tuple{}) = ()
Base.indices1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize this one

function Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T
Expand All @@ -84,8 +85,14 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{UnitRange,Vararg{
OffsetArray(B, map(indexoffset, inds))
end

Base.similar(f::Union{Function,Type}, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:OffsetArray} =
OffsetArray(T(map(length, shape)), map(indexoffset, shape))
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:Array} =
OffsetArray(T(uninitialized, map(length, shape)), map(indexoffset, shape))
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:BitArray} =
OffsetArray(T(uninitialized, map(length, shape)), map(indexoffset, shape))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all three of these specializations or would

Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:AbstractArray} =
    OffsetArray(T(map(length, shape)), map(indexoffset, shape))

be good enough? (OffsetArray being the defacto "owner" of UnitRange indices.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the are needed for ambiguity resolution with https://github.com/JuliaLang/julia/blob/7d7d81af41448aeaafb2dadedf54e831616c25cf/base/array.jl#L254 and https://github.com/JuliaLang/julia/blob/7d7d81af41448aeaafb2dadedf54e831616c25cf/base/bitarray.jl#L355. And we also need a separate for OffsetArray, which does not use the new uninitialized singleton for construction. I plan to implement that in a separate PR.


Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
OffsetArray(reshape(A, map(length, inds)), map(indexoffset, inds))
Expand All @@ -94,7 +101,7 @@ Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
OffsetArray(reshape(parent(A), map(length, inds)), map(indexoffset, inds))

function Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{Union{UnitRange,Int,Base.OneTo}}})
throw(ArgumentError("reshape must supply UnitRange indices, got $(typeof(inds)).\n Note that reshape(A, Val{N}) is not supported for OffsetArrays."))
throw(ArgumentError("reshape must supply UnitRange axes, got $(typeof(inds)).\n Note that reshape(A, Val{N}) is not supported for OffsetArrays."))
end

# Don't allow bounds-checks to be removed during Julia 0.5
Expand Down Expand Up @@ -198,20 +205,20 @@ end
ret
end
@inline _unsafe_getindex(::IndexCartesian, a::OffsetArray, i::Int) =
unsafe_getindex(a, ind2sub(indices(a), i)...)
unsafe_getindex(a, CartesianIndices(axes(a))[i])
@inline function _unsafe_setindex!(::IndexLinear, a::OffsetArray, val, i::Int)
@inbounds parent(a)[i] = val
val
end
@inline _unsafe_setindex!(::IndexCartesian, a::OffsetArray, val, i::Int) =
unsafe_setindex!(a, val, ind2sub(indices(a), i)...)
unsafe_setindex!(a, val, CartesianIndices(axes(a))[i]...)

@inline unsafe_getindex(a::OffsetArray, I::Int...) = unsafe_getindex(parent(a), offset(a.offsets, I)...)
@inline unsafe_setindex!(a::OffsetArray, val, I::Int...) = unsafe_setindex!(parent(a), val, offset(a.offsets, I)...)
@inline unsafe_getindex(a::OffsetArray, I...) = unsafe_getindex(a, Base.IteratorsMD.flatten(I)...)
@inline unsafe_setindex!(a::OffsetArray, val, I...) = unsafe_setindex!(a, val, Base.IteratorsMD.flatten(I)...)

# Indexing a SubArray which has OffsetArray indices
# Indexing a SubArray which has OffsetArray axes
OffsetSubArray{T,N,P,I<:Tuple{OffsetArray,Vararg{OffsetArray}}} = SubArray{T,N,P,I,false}
@inline function unsafe_getindex(a::OffsetSubArray{T,N}, I::Vararg{Int,N}) where {T,N}
J = map(unsafe_getindex, a.indexes, I)
Expand All @@ -230,7 +237,7 @@ if VERSION >= v"0.7.0-DEV.1790"
Base.showarg(io, parent(a), false)
if ndims(a) > 0
print(io, ", ")
printindices(io, indices(a)...)
printindices(io, axes(a)...)
end
print(io, ')')
toplevel && print(io, " with eltype ", eltype(a))
Expand Down
Loading