Skip to content

Commit

Permalink
add get method that accepts CartesianIndex (# 30259) (#30268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 authored and JeffBezanson committed Dec 5, 2018
1 parent f81123f commit 0e155bc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module IteratorsMD

# indexing
getindex(index::CartesianIndex, i::Integer) = index.I[i]
Base.get(A::AbstractArray, I::CartesianIndex, default) = get(A, I.I, default)
eltype(::Type{T}) where {T<:CartesianIndex} = eltype(fieldtype(T, :I))

# access to index tuple
Expand Down
8 changes: 8 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,11 @@ end
X = [1,2,3]
@test isempty(X[Union{}[]])
end

@testset "Issue 30259" begin
A = randn(1,2,3)
@test get(A, CartesianIndex(1,2,3), :some_default) === A[1,2,3]
@test get(A, CartesianIndex(2,2,3), :some_default) === :some_default
@test get(11:15, CartesianIndex(6), nothing) === nothing
@test get(11:15, CartesianIndex(5), nothing) === 15
end

0 comments on commit 0e155bc

Please sign in to comment.