Skip to content

Commit

Permalink
Fix checkbounds for trailing/missing dimensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
N5N3 committed Oct 30, 2023
1 parent 4b617f6 commit 1d00d5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,12 @@ _checkbounds(::CheckWillPass, itp, x) = true
_checkbounds(::NeedsCheck, itp, x) = checklubounds(lbounds(itp), ubounds(itp), x)

checklubounds(ls, us, xs) = _checklubounds(true, ls, us, xs)
_checklubounds(tf::Bool, ls, us, xs::Tuple{Number, Vararg{Any}}) =
_checklubounds(tf & (ls[1] <= xs[1] <= us[1]), Base.tail(ls), Base.tail(us), Base.tail(xs))
_checklubounds(tf::Bool, ls, us, xs::Tuple{AbstractVector, Vararg{Any}}) =
_checklubounds(tf::Bool, ls::Tuple, us::Tuple, xs::Tuple) =
_checklubounds(tf & allbetween(ls[1], xs[1], us[1]), Base.tail(ls), Base.tail(us), Base.tail(xs))
_checklubounds(tf::Bool, ::Tuple{}, ::Tuple{}, xs::Tuple) =
_checklubounds(tf & all(isone, xs[1]), (), (), Base.tail(xs))
_checklubounds(tf::Bool, ls::Tuple, us::Tuple, ::Tuple{}) =
_checklubounds(tf & (ls[1] == us[1]), Base.tail(ls), Base.tail(us), ())
_checklubounds(tf::Bool, ::Tuple{}, ::Tuple{}, ::Tuple{}) = tf

maybe_clamp(itp, xs) = maybe_clamp(BoundsCheckStyle(itp), itp, xs)
Expand Down
7 changes: 4 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ function getindex!(dest, itp, xs::Vararg{AbstractArray,N}) where N
return dest
end

function allbetween(l::Real, xs, u::Real)
allbetween(l, x::Number, u) = (l <= x) & (x <= u)
function allbetween(l, xs, u)
ret = true
@inbounds for x in xs
ret = ret & (l <= x) & (x <= u)
ret = ret & allbetween(l, x, u)
end
return ret
end
allbetween(l::Real, xs::AbstractRange, u::Real) = (l <= minimum(xs)) & (maximum(xs) <= u)
allbetween(l::Real, xs::AbstractRange{<:Real}, u::Real) = (l <= minimum(xs)) & (maximum(xs) <= u)

allisreal(x) = _allisreal(true, x...)
@inline _allisreal(ret, x1::Real, xs...) = _allisreal(ret, xs...)
Expand Down
9 changes: 9 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ end
@test (@inferred eltype(itp)) == Float64
@test (@inferred ndims(itp)) == 1
end

@testset "checkbounds" begin
itp = interpolate(randn(2,2,1), (BSpline(Linear()), BSpline(Linear()), NoInterp()))
@test !Base.checkbounds(Bool, itp, 1)
@test Base.checkbounds(Bool, itp, 1, 1)
@test Base.checkbounds(Bool, itp, 1, 1, 1)
@test Base.checkbounds(Bool, itp, 1, 1, 1, 1)
@test !Base.checkbounds(Bool, itp, 1, 1, 1, 1, 2)
end

0 comments on commit 1d00d5e

Please sign in to comment.