Skip to content

Commit 0d65ee3

Browse files
authored
Merge pull request #32 from JuliaArrays/teh/showarg
Inference improvements on 0.7 and improvements in `show`
2 parents 78055a6 + 6f78dcf commit 0d65ee3

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/OffsetArrays.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,13 @@ Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
133133
### Low-level utilities ###
134134

135135
# Computing a shifted index (subtracting the offset)
136-
@inline offset(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) where {N} = _offset((), offsets, inds)
137-
_offset(out, ::Tuple{}, ::Tuple{}) = out
138-
@inline _offset(out, offsets, inds) =
139-
_offset((out..., inds[1]-offsets[1]), Base.tail(offsets), Base.tail(inds))
136+
@inline offset(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) where {N} =
137+
(inds[1]-offsets[1], offset(Base.tail(offsets), Base.tail(inds))...)
138+
offset(::Tuple{}, ::Tuple{}) = ()
140139

141140
# Support trailing 1s
142141
@inline offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{Vararg{Int}}) =
143142
(offset(offsets, Base.front(inds))..., inds[end])
144-
offset(offsets::Tuple{}, inds::Tuple{}) = ()
145143
offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be shorter than offsets")
146144

147145
indexoffset(r::AbstractRange) = first(r) - 1
@@ -221,4 +219,18 @@ end
221219
@inline unsafe_getindex(a::OffsetSubArray, I::Union{Integer,CartesianIndex}...) = unsafe_getindex(a, Base.IteratorsMD.flatten(I)...)
222220
@inline unsafe_setindex!(a::OffsetSubArray, val, I::Union{Integer,CartesianIndex}...) = unsafe_setindex!(a, val, Base.IteratorsMD.flatten(I)...)
223221

222+
if VERSION >= v"0.7.0-DEV.1790"
223+
function Base.showarg(io::IO, a::OffsetArray, toplevel)
224+
print(io, "OffsetArray(")
225+
Base.showarg(io, parent(a), false)
226+
print(io, ", ")
227+
printindices(io, indices(a)...)
228+
print(io, ')')
229+
toplevel && print(io, " with eltype ", eltype(a))
230+
end
231+
printindices(io::IO, ind1, inds...) =
232+
(print(io, ind1, ", "); printindices(io, inds...))
233+
printindices(io::IO, ind1) = print(io, ind1)
234+
end
235+
224236
end # module

test/runtests.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # IndexCartesian
4545
@test_throws DimensionMismatch OffsetArray(A0, 0:1, 2:4)
4646

4747
# Scalar indexing
48-
@test A[0,3] == A[0,3,1] == A[1] == S[0,3] == S[0,3,1] == S[1] == 1
48+
@test @inferred(A[0,3]) == @inferred(A[0,3,1]) == @inferred(A[1]) == @inferred(S[0,3]) == @inferred(S[0,3,1]) == @inferred(S[1]) == 1
4949
@test A[1,3] == A[1,3,1] == A[2] == S[1,3] == S[1,3,1] == S[2] == 2
5050
@test A[0,4] == A[0,4,1] == A[3] == S[0,4] == S[0,4,1] == S[3] == 3
5151
@test A[1,4] == A[1,4,1] == A[4] == S[1,4] == S[1,4,1] == S[4] == 4
@@ -353,3 +353,9 @@ for i = -3:3
353353
@test a[i] == i
354354
end
355355
@test unsafe_sum(a) == 0
356+
357+
if VERSION >= v"0.7.0-DEV.1790"
358+
a = OffsetArray([1 2; 3 4], -1:0, 5:6)
359+
@test summary(a) == "OffsetArray(::Array{$(Int),2}, -1:0, 5:6) with eltype $(Int) with indices -1:0×5:6"
360+
@test summary(view(a, :, 5)) == "view(OffsetArray(::Array{Int64,2}, -1:0, 5:6), :, 5) with eltype Int64 with indices -1:0"
361+
end

0 commit comments

Comments
 (0)