Skip to content

Commit e4ab8d5

Browse files
committed
Fix checkbounds for trailing/missing dimensions.
1 parent 4b617f6 commit e4ab8d5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/Interpolations.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,12 @@ _checkbounds(::CheckWillPass, itp, x) = true
418418
_checkbounds(::NeedsCheck, itp, x) = checklubounds(lbounds(itp), ubounds(itp), x)
419419

420420
checklubounds(ls, us, xs) = _checklubounds(true, ls, us, xs)
421-
_checklubounds(tf::Bool, ls, us, xs::Tuple{Number, Vararg{Any}}) =
422-
_checklubounds(tf & (ls[1] <= xs[1] <= us[1]), Base.tail(ls), Base.tail(us), Base.tail(xs))
423-
_checklubounds(tf::Bool, ls, us, xs::Tuple{AbstractVector, Vararg{Any}}) =
421+
_checklubounds(tf::Bool, ls::Tuple, us::Tuple, xs::Tuple) =
424422
_checklubounds(tf & allbetween(ls[1], xs[1], us[1]), Base.tail(ls), Base.tail(us), Base.tail(xs))
423+
_checklubounds(tf::Bool, ::Tuple{}, ::Tuple{}, xs::Tuple) =
424+
_checklubounds(tf & all(isone, xs[1]), (), (), Base.tail(xs))
425+
_checklubounds(tf::Bool, ls::Tuple, us::Tuple, ::Tuple{}) =
426+
_checklubounds(tf & (ls[1] == us[1]), Base.tail(ls), Base.tail(us), ())
425427
_checklubounds(tf::Bool, ::Tuple{}, ::Tuple{}, ::Tuple{}) = tf
426428

427429
maybe_clamp(itp, xs) = maybe_clamp(BoundsCheckStyle(itp), itp, xs)

src/utils.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ function getindex!(dest, itp, xs::Vararg{AbstractArray,N}) where N
9898
return dest
9999
end
100100

101-
function allbetween(l::Real, xs, u::Real)
101+
allbetween(l, x::Number, u) = (l <= x) & (x <= u)
102+
function allbetween(l, xs::AbstractVector, u)
102103
ret = true
103104
@inbounds for x in xs
104-
ret = ret & (l <= x) & (x <= u)
105+
ret = ret & allbetween(l, x, u)
105106
end
106107
return ret
107108
end
108-
allbetween(l::Real, xs::AbstractRange, u::Real) = (l <= minimum(xs)) & (maximum(xs) <= u)
109+
allbetween(l::Real, xs::AbstractRange{<:Real}, u::Real) = (l <= minimum(xs)) & (maximum(xs) <= u)
109110

110111
allisreal(x) = _allisreal(true, x...)
111112
@inline _allisreal(ret, x1::Real, xs...) = _allisreal(ret, xs...)

test/core.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ end
7979
@test (@inferred eltype(itp)) == Float64
8080
@test (@inferred ndims(itp)) == 1
8181
end
82+
83+
@testset "checkbounds" begin
84+
itp = interpolate(randn(2,2,1), (BSpline(Linear()), BSpline(Linear()), NoInterp()))
85+
@test !Base.checkbounds(Bool, itp, 1)
86+
@test Base.checkbounds(Bool, itp, 1, 1)
87+
@test Base.checkbounds(Bool, itp, 1, 1, 1)
88+
@test Base.checkbounds(Bool, itp, 1, 1, 1, 1)
89+
@test !Base.checkbounds(Bool, itp, 1, 1, 1, 1, 2)
90+
end

0 commit comments

Comments
 (0)