Skip to content

Commit

Permalink
Merge pull request #37 from fredrikekre/fe/fixes-for-julia-0.7
Browse files Browse the repository at this point in the history
Fixes for julia 0.7
  • Loading branch information
timholy authored Jan 13, 2018
2 parents 78f1b6f + ce854a7 commit e0ec9ed
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 71 deletions.
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))

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

0 comments on commit e0ec9ed

Please sign in to comment.