Skip to content

Commit

Permalink
Specialize isassigned for AbstractFill (#351)
Browse files Browse the repository at this point in the history
* Specialize isassigned for AbstractFill

* Define method only for Integer indices

* isassigned for OneElement
  • Loading branch information
jishnub authored May 4, 2024
1 parent 45113c3 commit dfe0e62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const AbstractFillVecOrMat{T} = Union{AbstractFillVector{T},AbstractFillMatrix{T

==(a::AbstractFill, b::AbstractFill) = axes(a) == axes(b) && getindex_value(a) == getindex_value(b)

@inline function Base.isassigned(F::AbstractFill, i::Integer...)
@boundscheck checkbounds(Bool, F, to_indices(F, i)...) || return false
return true
end

@inline function _fill_getindex(F::AbstractFill, kj::Integer...)
@boundscheck checkbounds(F, kj...)
Expand Down
5 changes: 5 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ julia> FillArrays.getindex_value(A)
"""
getindex_value(A::OneElement) = all(in.(A.ind, axes(A))) ? A.val : zero(eltype(A))

@inline function Base.isassigned(F::OneElement, i::Integer...)
@boundscheck checkbounds(Bool, F, to_indices(F, i)...) || return false
return true
end

Base.AbstractArray{T,N}(A::OneElement{<:Any,N}) where {T,N} = OneElement(T(A.val), A.ind, A.axes)

Base.replace_in_print_matrix(o::OneElementVector, k::Integer, j::Integer, s::AbstractString) =
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ end
end
end

@testset "isassigned" begin
for f in (Fill("", 3, 4), Zeros(3,4), Ones(3,4))
@test !isassigned(f, 0, 0)
@test isassigned(f, 2, 2)
@test !isassigned(f, 10, 10)
@test_throws ArgumentError isassigned(f, true)
end
end

@testset "indexing" begin
A = Fill(3.0,5)
@test A[1:3] Fill(3.0,3)
Expand Down Expand Up @@ -2184,6 +2193,14 @@ end
@test adjoint(A) == OneElement(3, (1,2), (1,4))
end

@testset "isassigned" begin
f = OneElement(2, (3,3), (4,4))
@test !isassigned(f, 0, 0)
@test isassigned(f, 2, 2)
@test !isassigned(f, 10, 10)
@test_throws ArgumentError isassigned(f, true)
end

@testset "matmul" begin
A = reshape(Float64[1:9;], 3, 3)
v = reshape(Float64[1:3;], 3)
Expand Down

0 comments on commit dfe0e62

Please sign in to comment.