Skip to content

Automatically skip boundschecks in setindex! when safe to do so #68

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

Merged
merged 2 commits into from
Oct 13, 2024
Merged
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
6 changes: 5 additions & 1 deletion src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ end

Base.IndexStyle(::Type{<:FixedSizeArray}) = IndexLinear()
Base.@propagate_inbounds Base.getindex(A::FixedSizeArray, i::Int) = A.mem[i]
Base.@propagate_inbounds Base.setindex!(A::FixedSizeArray, v, i::Int) = A.mem[i] = v
Base.@propagate_inbounds Base.@assume_effects :noub_if_noinbounds function Base.setindex!(A::FixedSizeArray{T}, x, i::Int) where {T}
@boundscheck checkbounds(A, i)
@inbounds A.mem[i] = x
return A
end

Base.size(a::FixedSizeArray) = a.size

Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ end
end
end

@testset "setindex!" begin
v = FSV{Float64}(undef, 2)
v[1] = 1
v[2] = 2
@test v[1] == 1
@test v[2] == 2
@test_throws BoundsError v[3] = 3
end

@testset "FixedSizeVector" begin
v = FSV{Float64}(undef, 3)
@test length(v) == 3
Expand Down