From ba0add86fd8cc8bd3ba6fbc98d8b243c4c740b6d Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 30 Sep 2018 07:34:33 -0500 Subject: [PATCH 1/2] 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. --- NEWS.md | 2 ++ base/multidimensional.jl | 5 ++++- test/arrayops.jl | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 6a3f6ca4ac769..1c69bdfd8722c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ---------------- @@ -22,3 +23,4 @@ Deprecated or removed +[#29440]: https://github.com/JuliaLang/julia/issues/29440 diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 1a78b84c1e6a3..272c26521c075 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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 @@ -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)} diff --git a/test/arrayops.jl b/test/arrayops.jl index 20a0fb26e3963..c4a22076de805 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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 From 4fa71610e251ad19943b367f9765be2476f5a1ed Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 4 Oct 2018 05:38:39 -0500 Subject: [PATCH 2/2] Fix NEWS-update.jl for cases of empty issues list --- doc/NEWS-update.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/NEWS-update.jl b/doc/NEWS-update.jl index 1841dc06df7ca..4014c67645eb2 100644 --- a/doc/NEWS-update.jl +++ b/doc/NEWS-update.jl @@ -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))