Skip to content

Commit ba0add8

Browse files
committed
Add colon constructor for CartesianIndices
See https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666/6 and posts below it.
1 parent b8ca1a4 commit ba0add8

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Julia v1.1.0 Release Notes
44
New language features
55
---------------------
66

7+
* `CartesianIndices` can now be constructed from two `CartesianIndex`es `I` and `J` with `I:J` ([#29440]).
78

89
Language changes
910
----------------
@@ -22,3 +23,4 @@ Deprecated or removed
2223

2324

2425
<!--- generated by NEWS-update.jl: -->
26+
[#29440]: https://github.com/JuliaLang/julia/issues/29440

base/multidimensional.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module IteratorsMD
66
setindex!, IndexStyle, min, max, zero, one, isless, eachindex,
77
ndims, IteratorSize, convert, show, iterate, promote_rule
88

9-
import .Base: +, -, *
9+
import .Base: +, -, *, (:)
1010
import .Base: simd_outer_range, simd_inner_length, simd_index
1111
using .Base: IndexLinear, IndexCartesian, AbstractCartesianIndex, fill_to_length, tail
1212
using .Base.Iterators: Reverse
@@ -228,6 +228,9 @@ module IteratorsMD
228228

229229
CartesianIndices(A::AbstractArray) = CartesianIndices(axes(A))
230230

231+
(:)(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
232+
CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))
233+
231234
promote_rule(::Type{CartesianIndices{N,R1}}, ::Type{CartesianIndices{N,R2}}) where {N,R1,R2} =
232235
CartesianIndices{N,Base.indices_promote_type(R1,R2)}
233236

test/arrayops.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,10 @@ end
17531753

17541754
@test @inferred(convert(NTuple{2,UnitRange}, R)) === (2:5, 3:5)
17551755
@test @inferred(convert(Tuple{Vararg{UnitRange}}, R)) === (2:5, 3:5)
1756+
1757+
I = CartesianIndex(2,3)
1758+
J = CartesianIndex(5,4)
1759+
@test I:J === CartesianIndices((2:5, 3:4))
17561760
end
17571761

17581762
# All we really care about is that we have an optimized

0 commit comments

Comments
 (0)