Skip to content

Commit 8700038

Browse files
Pramodh Gopalan Vjohanmon
authored andcommitted
Add get method, tests for Tuple and AbstractArrays (JuliaLang#41007)
Replaces JuliaLang#40856 Fixes JuliaLang#40809
1 parent c840568 commit 8700038

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

base/abstractarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,9 @@ RangeVecIntList{A<:AbstractVector{Int}} = Union{Tuple{Vararg{Union{AbstractRange
14691469
get(A::AbstractArray, i::Integer, default) = checkbounds(Bool, A, i) ? A[i] : default
14701470
get(A::AbstractArray, I::Tuple{}, default) = checkbounds(Bool, A) ? A[] : default
14711471
get(A::AbstractArray, I::Dims, default) = checkbounds(Bool, A, I...) ? A[I...] : default
1472+
get(f::Callable, A::AbstractArray, i::Integer) = checkbounds(Bool, A, i) ? A[i] : f()
1473+
get(f::Callable, A::AbstractArray, I::Tuple{}) = checkbounds(Bool, A) ? A[] : f()
1474+
get(f::Callable, A::AbstractArray, I::Dims) = checkbounds(Bool, A, I...) ? A[I...] : f()
14721475

14731476
function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T
14741477
# 1d is not linear indexing

base/tuple.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ getindex(t::Tuple, r::AbstractArray{<:Any,1}) = (eltype(t)[t[ri] for ri in r]...
3232
getindex(t::Tuple, b::AbstractArray{Bool,1}) = length(b) == length(t) ? getindex(t, findall(b)) : throw(BoundsError(t, b))
3333
getindex(t::Tuple, c::Colon) = t
3434

35+
get(t::Tuple, i::Integer, default) = i in 1:length(t) ? getindex(t, i) : default
36+
get(f::Callable, t::Tuple, i::Integer) = i in 1:length(t) ? getindex(t, i) : f()
37+
3538
# returns new tuple; N.B.: becomes no-op if i is out-of-bounds
3639

3740
"""

test/abstractarray.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,31 @@ function test_get(::Type{TestAbstractArray})
598598
@test get(TSlow([]), (), 0) == 0
599599
@test get(TSlow([1]), (), 0) == 1
600600
@test get(TSlow(fill(1)), (), 0) == 1
601+
602+
global c = 0
603+
f() = (global c = c+1; 0)
604+
@test get(f, A, ()) == 0
605+
@test c == 1
606+
@test get(f, B, ()) == 0
607+
@test c == 2
608+
@test get(f, A, (1,)) == get(f, A, 1) == A[1] == 1
609+
@test c == 2
610+
@test get(f, B, (1,)) == get(f, B, 1) == B[1] == 1
611+
@test c == 2
612+
@test get(f, A, (25,)) == get(f, A, 25) == 0
613+
@test c == 4
614+
@test get(f, B, (25,)) == get(f, B, 25) == 0
615+
@test c == 6
616+
@test get(f, A, (1,1,1)) == A[1,1,1] == 1
617+
@test get(f, B, (1,1,1)) == B[1,1,1] == 1
618+
@test get(f, A, (1,1,3)) == 0
619+
@test c == 7
620+
@test get(f, B, (1,1,3)) == 0
621+
@test c == 8
622+
@test get(f, TSlow([]), ()) == 0
623+
@test c == 9
624+
@test get(f, TSlow([1]), ()) == 1
625+
@test get(f, TSlow(fill(1)), ()) == 1
601626
end
602627

603628
function test_cat(::Type{TestAbstractArray})

test/tuple.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ end
179179
@test_throws MethodError (1,)[]
180180
@test_throws MethodError (1,1,1)[1,1]
181181
end
182+
183+
@testset "get() method for Tuple (Issue #40809)" begin
184+
@test get((5, 6, 7), 1, 0) == 5
185+
@test get((), 5, 0) == 0
186+
@test get((1,), 3, 0) == 0
187+
@test get(()->0, (5, 6, 7), 1) == 5
188+
@test get(()->0, (), 4) == 0
189+
@test get(()->0, (1,), 3) == 0
190+
end
182191
end
183192

184193
@testset "fill to length" begin

0 commit comments

Comments
 (0)