Skip to content

Commit 78055a6

Browse files
authored
Merge pull request #31 from JuliaArrays/teh/arithdep
Fix ::array + ::Number, for loop, and Val depwarns on 0.7
2 parents f671550 + 5d4bf2a commit 78055a6

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
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.19.0
2+
Compat 0.32

src/OffsetArrays.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ using Compat
77

88
export OffsetArray, @unsafe
99

10+
# TODO: just use .+
11+
# See https://github.com/JuliaLang/julia/pull/22932#issuecomment-330711997
12+
if VERSION < v"0.7.0-DEV.1759"
13+
plus(r::AbstractUnitRange, i::Integer) = r + i
14+
else
15+
plus(r::AbstractUnitRange, i::Integer) = broadcast(+, r, i)
16+
end
17+
1018
struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
1119
parent::AA
1220
offsets::NTuple{N,Int}
@@ -39,7 +47,7 @@ end
3947
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N} =
4048
OffsetArray(A, inds)
4149

42-
Compat.IndexStyle(::Type{OA}) where {OA<:OffsetArray} = IndexStyle(parenttype(OA))
50+
Base.IndexStyle(::Type{OA}) where {OA<:OffsetArray} = IndexStyle(parenttype(OA))
4351
parenttype(::Type{OffsetArray{T,N,AA}}) where {T,N,AA} = AA
4452
parenttype(A::OffsetArray) = parenttype(typeof(A))
4553

@@ -55,11 +63,11 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = indices(A, 1)
5563
# performance-critical and relies on indices, these are usually worth
5664
# optimizing thoroughly.
5765
@inline Base.indices(A::OffsetArray, d) =
58-
1 <= d <= length(A.offsets) ? indices(parent(A))[d] + A.offsets[d] : (1:1)
66+
1 <= d <= length(A.offsets) ? plus(indices(parent(A))[d], A.offsets[d]) : (1:1)
5967
@inline Base.indices(A::OffsetArray) =
6068
_indices(indices(parent(A)), A.offsets) # would rather use ntuple, but see #15276
6169
@inline _indices(inds, offsets) =
62-
(inds[1]+offsets[1], _indices(tail(inds), tail(offsets))...)
70+
(plus(inds[1], offsets[1]), _indices(tail(inds), tail(offsets))...)
6371
_indices(::Tuple{}, ::Tuple{}) = ()
6472
Base.indices1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize this one
6573

@@ -136,7 +144,7 @@ _offset(out, ::Tuple{}, ::Tuple{}) = out
136144
offset(offsets::Tuple{}, inds::Tuple{}) = ()
137145
offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be shorter than offsets")
138146

139-
indexoffset(r::Range) = first(r) - 1
147+
indexoffset(r::AbstractRange) = first(r) - 1
140148
indexoffset(i::Integer) = 0
141149

142150
macro unsafe(ex)

test/runtests.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using Base.Test
22
using OffsetArrays
3-
using Compat
43

54
@test isempty(detect_ambiguities(OffsetArrays, Base, Core))
65

76
# Basics
87
for n = 0:5
98
for a in (OffsetArray(ones(Int,ntuple(d->1,n)), ntuple(x->x-1,n)),
10-
fill!(OffsetArray(Float64, ntuple(x->(0:0)+x, n)...), 1),
11-
fill!(OffsetArray{Float64}(ntuple(x->(0:0)+x, n)), 1),
12-
fill!(OffsetArray{Float64}(ntuple(x->(0:0)+x, n)...), 1),
13-
fill!(OffsetArray{Float64,n}(ntuple(x->(0:0)+x, n)), 1),
14-
fill!(OffsetArray{Float64,n}(ntuple(x->(0:0)+x, n)...), 1))
9+
fill!(OffsetArray(Float64, ntuple(x->x:x, n)...), 1),
10+
fill!(OffsetArray{Float64}(ntuple(x->x:x, n)), 1),
11+
fill!(OffsetArray{Float64}(ntuple(x->x:x, n)...), 1),
12+
fill!(OffsetArray{Float64,n}(ntuple(x->x:x, n)), 1),
13+
fill!(OffsetArray{Float64,n}(ntuple(x->x:x, n)...), 1))
1514
@test length(linearindices(a)) == 1
16-
@test indices(a) == ntuple(x->x:x,n)
15+
@test indices(a) == ntuple(x->x:x, n)
1716
@test a[1] == 1
1817
end
1918
end
@@ -129,8 +128,10 @@ S = view(A, :, :)
129128
@test indices(S) === (0:1, 3:4)
130129

131130
# iteration
132-
for (a,d) in zip(A, A0)
133-
@test a == d
131+
let a
132+
for (a,d) in zip(A, A0)
133+
@test a == d
134+
end
134135
end
135136

136137
# show
@@ -184,8 +185,8 @@ b = reshape(A, -7:-4)
184185
@test isa(parent(b), Vector{Int})
185186
@test parent(b) == A0[:]
186187
a = OffsetArray(rand(3,3,3), -1:1, 0:2, 3:5)
187-
@test_throws ArgumentError reshape(a, Val{2})
188-
@test_throws ArgumentError reshape(a, Val{4})
188+
@test_throws ArgumentError reshape(a, Val(2))
189+
@test_throws ArgumentError reshape(a, Val(4))
189190

190191
# Indexing with OffsetArray indices
191192
i1 = OffsetArray([2,1], (-5,))
@@ -328,7 +329,7 @@ v = OffsetArray(rand(8), (-2,))
328329
@test flipdim(A, 1) == OffsetArray(flipdim(parent(A), 1), A.offsets)
329330
@test flipdim(A, 2) == OffsetArray(flipdim(parent(A), 2), A.offsets)
330331

331-
@test A+1 == OffsetArray(parent(A)+1, A.offsets)
332+
@test A.+1 == OffsetArray(parent(A).+1, A.offsets)
332333
@test 2*A == OffsetArray(2*parent(A), A.offsets)
333334
@test A+A == OffsetArray(parent(A)+parent(A), A.offsets)
334335
@test A.*A == OffsetArray(parent(A).*parent(A), A.offsets)

0 commit comments

Comments
 (0)