Skip to content

Commit

Permalink
I::CartesianIndices .+ j::CartesianIndex -> ::CartesianIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Nov 18, 2018
1 parent 6b2d211 commit 34e2b7f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Standard library changes
------------------------

* `CartesianIndices` can now be constructed from two `CartesianIndex`es `I` and `J` with `I:J` ([#29440]).
* `CartesianIndices` support broadcasting arithmetic (+ and -) with a `CartesianIndex` ([#29890]).
* `copy!` support for arrays, dicts, and sets has been moved to Base from the Future package ([#29173]).
* Channels now convert inserted values (like containers) instead of requiring types to match ([#29092]).
* `range` can accept the stop value as a positional argument, e.g. `range(1,10,step=2)` ([#28708]).
Expand Down
12 changes: 12 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,18 @@ broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::StepRange) = big(r.start):
broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::StepRangeLen) = StepRangeLen(big(r.ref), big(r.step), length(r), r.offset)
broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::LinRange) = LinRange(big(r.start), big(r.stop), length(r))

## CartesianIndices
broadcasted(::typeof(+), I::CartesianIndices{N}, j::CartesianIndex{N}) where N =
CartesianIndices(map((rng, offset)->rng .+ offset, I.indices, Tuple(j)))
broadcasted(::typeof(+), j::CartesianIndex{N}, I::CartesianIndices{N}) where N =
I .+ j
broadcasted(::typeof(-), I::CartesianIndices{N}, j::CartesianIndex{N}) where N =
CartesianIndices(map((rng, offset)->rng .- offset, I.indices, Tuple(j)))
function broadcasted(::typeof(-), j::CartesianIndex{N}, I::CartesianIndices{N}) where N
diffrange(offset, rng) = range(offset-last(rng), length=length(rng))
Iterators.reverse(CartesianIndices(map(diffrange, Tuple(j), I.indices)))
end

## In specific instances, we can broadcast masked BitArrays whole chunks at a time
# Very intentionally do not support much functionality here: scalar indexing would be O(n)
struct BitMaskedBitArray{N,M}
Expand Down
5 changes: 5 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ end
J1, J2 = @inferred(promote(I1, I2))
@test J1 === J2
end

i = CartesianIndex(17,-2)
@test CR .+ i === i .+ CR === CartesianIndices((19:21, -1:3))
@test CR .- i === CartesianIndices((-15:-13, 3:7))
@test collect(i .- CR) == Ref(i) .- collect(CR)
end

@testset "issue #25770" begin
Expand Down

0 comments on commit 34e2b7f

Please sign in to comment.