Skip to content

Commit

Permalink
more IOR broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Jun 5, 2022
1 parent a094f04 commit be7b972
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ for R in [:IIUR, :IdOffsetRange]
end

# offset-preserve broadcasting
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), r::IdOffsetRange{T}, x::Integer) where T =
IdOffsetRange{T}(r.parent .- x, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdOffsetRange{T}, x::Integer) where T =
IdOffsetRange{T}(r.parent .+ x, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Integer, r::IdOffsetRange{T}) where T =
IdOffsetRange{T}(x .+ r.parent, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), r::IdOffsetRange, x::Integer) =
IdOffsetRange(r.parent .- x, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdOffsetRange, x::Integer) =
IdOffsetRange(r.parent .+ x, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Integer, r::IdOffsetRange) =
IdOffsetRange(x .+ r.parent, r.offset)
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(big), r::IdOffsetRange) =
IdOffsetRange(big.(r.parent), r.offset)

Base.show(io::IO, r::IdOffsetRange) = print(io, IdOffsetRange, "(values=",first(r), ':', last(r),", indices=",first(eachindex(r)),':',last(eachindex(r)), ")")

Expand Down
13 changes: 12 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,19 @@ end

# broadcasting behavior with scalars (issue #104)
r3 = (1 .+ OffsetArrays.IdOffsetRange(3:5, -1) .+ 1) .- 1
@test r3 isa OffsetArrays.IdOffsetRange
@test same_value(r3, 3:5)
check_indexed_by(r3, 0:2)
check_indexed_by(r3, axes(r3,1))

r = OffsetArrays.IdOffsetRange(3:5, -1)
rc = copyto!(similar(r), r)
n = big(typemax(Int))
@test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n
@test @inferred(broadcast(-, r, n)) == rc .- n
@test @inferred(broadcast(big, r)) == big.(rc)
for n in Any[2, big(typemax(Int))]
@test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n
end

@testset "Idempotent indexing" begin
@testset "Indexing into an IdOffsetRange" begin
Expand Down

0 comments on commit be7b972

Please sign in to comment.