Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdentityMultiple: throw BoundsError for nonpositive index and use @boundscheck #317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,13 @@ Base.IndexStyle(::Type{<:IdentityMultiple}) = IndexLinear()
Base.size(𝐼::IdentityMultiple) = (𝐼.n, 𝐼.n)

function Base.getindex(𝐼::IdentityMultiple, inds...)
any(idx -> idx > 𝐼.n, inds) && throw(BoundsError(𝐼, inds))
@boundscheck all(idx -> 1 ≤ idx 𝐼.n, inds) || throw(BoundsError(𝐼, inds))
return getindex(𝐼.M, inds...)
end

function Base.getindex(𝐼::IdentityMultiple{T}, ind) where {T}
if 1 ≤ ind ≤ 𝐼.n^2
return rem(ind - 1, 𝐼.n + 1) == 0 ? 𝐼.M.λ : zero(T)
else
throw(BoundsError(𝐼, ind))
end
@boundscheck 1 ≤ ind ≤ 𝐼.n^2 || throw(BoundsError(𝐼, ind))
return rem(ind - 1, 𝐼.n + 1) == 0 ? 𝐼.M.λ : zero(T)
end

function Base.setindex!(::IdentityMultiple, ::Any, inds...)
Expand Down
2 changes: 2 additions & 0 deletions test/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
@test size(I1) == (1, 1)
@test I1[1, 1] == 1.0
@test_throws BoundsError I1[1, 2]
@test_throws BoundsError I1[1, 0]
@test_throws BoundsError I1[3]
@test_throws BoundsError I1[0]
@test_throws ErrorException I1[1] = 2

for n in [2, 1000]
Expand Down
Loading