Skip to content

Commit

Permalink
Deprecate adding integers to CartesianIndex (#26284)
Browse files Browse the repository at this point in the history
fixes #26227
  • Loading branch information
mbauman authored and JeffBezanson committed Mar 2, 2018
1 parent ade40f6 commit 8fff818
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ Library improvements
collection `A`. There are also two other methods with a different API, and
a mutating variant, `replace!` ([#22324]).

* Adding integers to `CartesianIndex` objects is now deprecated. Instead of
`i::Int + x::CartesianIndex`, use `i*one(x) + x` ([#26284]).

* `CartesianRange` changes ([#24715]):
- Inherits from `AbstractArray`, and linear indexing can be used to provide
linear-to-cartesian conversion ([#24715])
Expand Down
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,12 @@ end
@deprecate reprmime(mime, x) repr(mime, x)
@deprecate mimewritable(mime, x) showable(mime, x)

# PR #26284
@deprecate (+)(i::Integer, index::CartesianIndex) (i*one(index) + index)
@deprecate (+)(index::CartesianIndex, i::Integer) (index + i*one(index))
@deprecate (-)(i::Integer, index::CartesianIndex) (i*one(index) - index)
@deprecate (-)(index::CartesianIndex, i::Integer) (index - i*one(index))

# PR #23332
@deprecate ^(x, p::Integer) Base.power_by_squaring(x,p)

Expand Down
6 changes: 1 addition & 5 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ module IteratorsMD
@inline max(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} =
CartesianIndex{N}(map(max, index1.I, index2.I))

@inline (+)(i::Integer, index::CartesianIndex) = index+i
@inline (+)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x+i, index.I))
@inline (-)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x-i, index.I))
@inline (-)(i::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->i-x, index.I))
@inline (*)(a::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->a*x, index.I))
@inline (*)(index::CartesianIndex, a::Integer) = *(a,index)

Expand Down Expand Up @@ -279,7 +275,7 @@ module IteratorsMD
@inline function start(iter::CartesianIndices)
iterfirst, iterlast = first(iter), last(iter)
if any(map(>, iterfirst.I, iterlast.I))
return iterlast+1
return iterlast+one(iterlast)
end
iterfirst
end
Expand Down
4 changes: 2 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,8 @@ end
@test I2 + I1 == CartesianIndex((1,8,2))
@test I1 - I2 == CartesianIndex((3,-2,-2))
@test I2 - I1 == CartesianIndex((-3,2,2))
@test I1 + 1 == CartesianIndex((3,4,1))
@test I1 - 2 == CartesianIndex((0,1,-2))
@test I1 + 1*one(I1) == CartesianIndex((3,4,1))
@test I1 - 2*one(I1) == CartesianIndex((0,1,-2))

@test zero(CartesianIndex{2}) == CartesianIndex((0,0))
@test zero(CartesianIndex((2,3))) == CartesianIndex((0,0))
Expand Down

0 comments on commit 8fff818

Please sign in to comment.