Skip to content

Commit 2651452

Browse files
authored
support ± for CartesianIndex (#85)
In Base we have `CartesianIndex(a, b):CartesianIndex(c,d)` construct the region `CartesianIndices((a:c, b:d))`. Similiarly, we would want to have `CartesianIndex(a,b)..CartesianIndex(c,d)` mean the same thing. This commit fixes the ± constructor for CartesianIndex
1 parent 4a6e746 commit 2651452

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/interval.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Construct a ClosedInterval `iv` spanning the region from
9393
"""
9494
±(x, y) = ClosedInterval(x - y, x + y)
9595
±(x::CartesianIndex, y) = (xy = y * one(x); map(ClosedInterval, (x - xy).I, (x + xy).I))
96+
±(x::CartesianIndex, y::CartesianIndex) = ClosedInterval(x-y, x+y)
9697

9798
show(io::IO, I::ClosedInterval) = print(io, leftendpoint(I), "..", rightendpoint(I))
9899
show(io::IO, I::OpenInterval) = print(io, leftendpoint(I), "..", rightendpoint(I), " (open)")

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ struct IncompleteInterval <: AbstractInterval{Int} end
4949
O = @inferred(CartesianIndex(1, 2, 3, 4) ± 2)
5050
@test O == (-1..3, 0..4, 1..5, 2..6)
5151

52+
x, y = CartesianIndex(1, 2, 3, 4), CartesianIndex(1, 2, 3, 4)
53+
O = @inferred x±y
54+
@test O == ClosedInterval(x-y, x+y)
55+
5256
@test eltype(I) == Int
5357
@test eltype(M) == Float64
5458

0 commit comments

Comments
 (0)