From 8fff818005fe956f969da7247f435c897adf200d Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Fri, 2 Mar 2018 13:24:36 -0600 Subject: [PATCH] Deprecate adding integers to CartesianIndex (#26284) fixes #26227 --- NEWS.md | 3 +++ base/deprecated.jl | 6 ++++++ base/multidimensional.jl | 6 +----- test/arrayops.jl | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 17332744894b1..190a456e62bb3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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]) diff --git a/base/deprecated.jl b/base/deprecated.jl index 1ca11b14bf637..9f90cf11e1858 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index d88794cd46798..66c3e255adf48 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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) @@ -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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 570c1510592b3..fde0b3f7d4fac 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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))