Skip to content

Commit

Permalink
add method setindex for CartesianIndex (JuliaLang#35280)
Browse files Browse the repository at this point in the history
  • Loading branch information
francescoalemanno authored and ztultrebor committed Apr 14, 2020
1 parent 63526a0 commit 32161ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module IteratorsMD
ndims, IteratorSize, convert, show, iterate, promote_rule, to_indices

import .Base: +, -, *, (:)
import .Base: simd_outer_range, simd_inner_length, simd_index
import .Base: simd_outer_range, simd_inner_length, simd_index, setindex
using .Base: IndexLinear, IndexCartesian, AbstractCartesianIndex, fill_to_length, tail,
ReshapedArray, ReshapedArrayLF, OneTo
using .Base.Iterators: Reverse, PartitionIterator
Expand Down Expand Up @@ -96,6 +96,8 @@ module IteratorsMD
# access to index tuple
Tuple(index::CartesianIndex) = index.I

Base.setindex(x::CartesianIndex,i,j) = CartesianIndex(Base.setindex(Tuple(x),i,j))

# equality
Base.:(==)(a::CartesianIndex{N}, b::CartesianIndex{N}) where N = a.I == b.I

Expand Down
4 changes: 4 additions & 0 deletions test/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ ex = Base.Cartesian.exprresolve(:(if 5 > 4; :x; else :y; end))
@test ex.args[2] == QuoteNode(:x)

@test Base.Cartesian.lreplace!("val_col", Base.Cartesian.LReplace{String}(:col, "col", 1)) == "val_1"
@test Base.setindex(CartesianIndex(1,5,4),3,2) == CartesianIndex(1, 3, 4)

# test conversions for CartesianIndex

@testset "CartesianIndex Conversions" begin
@test convert(Int, CartesianIndex(42)) === 42
@test convert(Float64, CartesianIndex(42)) === 42.0
@test convert(Tuple, CartesianIndex(42, 1)) === (42, 1)

# can't convert higher-dimensional indices to Int
@test_throws MethodError convert(Int, CartesianIndex(42, 1))
end
Expand Down Expand Up @@ -64,3 +66,5 @@ end
@test iterate(I, CartesianIndex(3, typemax(Int)))[1] == CartesianIndex(4,typemax(Int))
@test iterate(I, CartesianIndex(4, typemax(Int))) === nothing
end


0 comments on commit 32161ab

Please sign in to comment.