Skip to content

Commit e0ec9ed

Browse files
authored
Merge pull request #37 from fredrikekre/fe/fixes-for-julia-0.7
Fixes for julia 0.7
2 parents 78f1b6f + ce854a7 commit e0ec9ed

File tree

3 files changed

+80
-71
lines changed

3 files changed

+80
-71
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.6
2-
Compat 0.32
2+
Compat 0.45

src/OffsetArrays.jl

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module OffsetArrays
44

55
using Base: Indices, tail
66
using Compat
7+
using Compat: axes, CartesianIndices
78

89
export OffsetArray, OffsetVector, @unsafe
910

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

2930
OffsetArray{T,N}(inds::Indices{N}) where {T,N} =
30-
OffsetArray{T,N,Array{T,N}}(Array{T,N}(map(length, inds)), map(indexoffset, inds))
31+
OffsetArray{T,N,Array{T,N}}(Array{T,N}(uninitialized, map(length, inds)), map(indexoffset, inds))
3132
OffsetArray{T}(inds::Indices{N}) where {T,N} = OffsetArray{T,N}(inds)
3233
OffsetArray{T,N}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
3334
OffsetArray{T}(inds::Vararg{AbstractUnitRange,N}) where {T,N} = OffsetArray{T,N}(inds)
@@ -44,9 +45,9 @@ OffsetVector{T}(inds::AbstractUnitRange) where {T} = OffsetArray{T}(inds)
4445
OffsetArray(A::AbstractArray{T,0}, inds::Tuple{}) where {T} = OffsetArray{T,0,typeof(A)}(A, ())
4546
OffsetArray(A::AbstractArray{T,N}, inds::Tuple{}) where {T,N} = error("this should never be called")
4647
function OffsetArray(A::AbstractArray{T,N}, inds::NTuple{N,AbstractUnitRange}) where {T,N}
47-
lA = map(length, indices(A))
48+
lA = map(length, axes(A))
4849
lI = map(length, inds)
49-
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"))
50+
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"))
5051
OffsetArray(A, map(indexoffset, inds))
5152
end
5253
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N} =
@@ -58,22 +59,22 @@ parenttype(A::OffsetArray) = parenttype(typeof(A))
5859

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

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

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

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

87-
Base.similar(f::Union{Function,Type}, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
88+
Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
8889
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
90+
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:OffsetArray} =
91+
OffsetArray(T(map(length, shape)), map(indexoffset, shape))
92+
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:Array} =
93+
OffsetArray(T(uninitialized, map(length, shape)), map(indexoffset, shape))
94+
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:BitArray} =
95+
OffsetArray(T(uninitialized, map(length, shape)), map(indexoffset, shape))
8996

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

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

100107
# Don't allow bounds-checks to be removed during Julia 0.5
@@ -198,20 +205,20 @@ end
198205
ret
199206
end
200207
@inline _unsafe_getindex(::IndexCartesian, a::OffsetArray, i::Int) =
201-
unsafe_getindex(a, ind2sub(indices(a), i)...)
208+
unsafe_getindex(a, CartesianIndices(axes(a))[i])
202209
@inline function _unsafe_setindex!(::IndexLinear, a::OffsetArray, val, i::Int)
203210
@inbounds parent(a)[i] = val
204211
val
205212
end
206213
@inline _unsafe_setindex!(::IndexCartesian, a::OffsetArray, val, i::Int) =
207-
unsafe_setindex!(a, val, ind2sub(indices(a), i)...)
214+
unsafe_setindex!(a, val, CartesianIndices(axes(a))[i]...)
208215

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

214-
# Indexing a SubArray which has OffsetArray indices
221+
# Indexing a SubArray which has OffsetArray axes
215222
OffsetSubArray{T,N,P,I<:Tuple{OffsetArray,Vararg{OffsetArray}}} = SubArray{T,N,P,I,false}
216223
@inline function unsafe_getindex(a::OffsetSubArray{T,N}, I::Vararg{Int,N}) where {T,N}
217224
J = map(unsafe_getindex, a.indexes, I)
@@ -230,7 +237,7 @@ if VERSION >= v"0.7.0-DEV.1790"
230237
Base.showarg(io, parent(a), false)
231238
if ndims(a) > 0
232239
print(io, ", ")
233-
printindices(io, indices(a)...)
240+
printindices(io, axes(a)...)
234241
end
235242
print(io, ')')
236243
toplevel && print(io, " with eltype ", eltype(a))

0 commit comments

Comments
 (0)