Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colon constructor for CartesianIndices #29440

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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