Skip to content

Commit

Permalink
Merge pull request #29440 from JuliaLang/teh/colon_cartindex
Browse files Browse the repository at this point in the history
Add colon constructor for CartesianIndices
  • Loading branch information
timholy committed Oct 4, 2018
2 parents be0fceb + 4fa7161 commit 60cff80
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Julia v1.1.0 Release Notes
New language features
---------------------

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

Language changes
----------------
Expand All @@ -22,3 +23,4 @@ Deprecated or removed


<!--- generated by NEWS-update.jl: -->
[#29440]: https://github.com/JuliaLang/julia/issues/29440
5 changes: 4 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module IteratorsMD
setindex!, IndexStyle, min, max, zero, one, isless, eachindex,
ndims, IteratorSize, convert, show, iterate, promote_rule

import .Base: +, -, *
import .Base: +, -, *, (:)
import .Base: simd_outer_range, simd_inner_length, simd_index
using .Base: IndexLinear, IndexCartesian, AbstractCartesianIndex, fill_to_length, tail
using .Base.Iterators: Reverse
Expand Down Expand Up @@ -228,6 +228,9 @@ module IteratorsMD

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

(:)(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))

promote_rule(::Type{CartesianIndices{N,R1}}, ::Type{CartesianIndices{N,R2}}) where {N,R1,R2} =
CartesianIndices{N,Base.indices_promote_type(R1,R2)}

Expand Down
5 changes: 4 additions & 1 deletion doc/NEWS-update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ NEWS = get(ARGS, 1, "NEWS.md")

s = read(NEWS, String)

s = s[1:match(r"\[#[0-9]+\]:", s).offset-1];
m = match(r"\[#[0-9]+\]:", s)
if m !== nothing
s = s[1:m.offset-1]
end

footnote(n) = "[#$n]: https://github.com/JuliaLang/julia/issues/$n"
N = map(m -> parse(Int,m.captures[1]), eachmatch(r"\[#([0-9]+)\]", s))
Expand Down
4 changes: 4 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,10 @@ end

@test @inferred(convert(NTuple{2,UnitRange}, R)) === (2:5, 3:5)
@test @inferred(convert(Tuple{Vararg{UnitRange}}, R)) === (2:5, 3:5)

I = CartesianIndex(2,3)
J = CartesianIndex(5,4)
@test I:J === CartesianIndices((2:5, 3:4))
end

# All we really care about is that we have an optimized
Expand Down

0 comments on commit 60cff80

Please sign in to comment.