Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.11.5"
version = "0.11.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
45 changes: 26 additions & 19 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ abstract type AbstractFill{T, N, Axes} <: AbstractArray{T, N} end

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

@inline function getindex(F::AbstractFill, k::Integer)
@boundscheck checkbounds(F, k)
getindex_value(F)
end

@inline function getindex(F::AbstractFill{T, N}, kj::Vararg{<:Integer, N}) where {T, N}
Base.@propagate_inbounds @inline function _fill_getindex(F::AbstractFill, kj::Integer...)
@boundscheck checkbounds(F, kj...)
getindex_value(F)
end

getindex(F::AbstractFill, k::Integer) = _fill_getindex(F, k)
getindex(F::AbstractFill{T, N}, kj::Vararg{<:Integer, N}) where {T, N} = _fill_getindex(F, kj...)

@inline function setindex!(F::AbstractFill, v, k::Integer)
@boundscheck checkbounds(F, k)
v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for an AbstractFill with value $(getindex_value(F))."))
Expand Down Expand Up @@ -166,20 +164,25 @@ convert(::Type{T}, F::T) where T<:Fill = F

getindex(F::Fill{<:Any,0}) = getindex_value(F)

function Base._unsafe_getindex(::IndexStyle, F::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N
Base.@propagate_inbounds @inline function _fill_getindex(A::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N
@boundscheck checkbounds(A, I...)
shape = Base.index_shape(I...)
fillsimilar(F, shape)
fillsimilar(A, shape)
end

function getindex(A::AbstractFill, kr::AbstractVector{Bool})
length(A) == length(kr) || throw(DimensionMismatch())
fillsimilar(A, count(kr))
end
function getindex(A::AbstractFill, kr::AbstractArray{Bool})
size(A) == size(kr) || throw(DimensionMismatch())
Base.@propagate_inbounds @inline function _fill_getindex(A::AbstractFill, kr::AbstractArray{Bool})
@boundscheck checkbounds(A, kr)
fillsimilar(A, count(kr))
end

Base.@propagate_inbounds @inline Base._unsafe_getindex(::IndexStyle, F::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N =
@inbounds(return _fill_getindex(F, I...))



getindex(A::AbstractFill, kr::AbstractVector{Bool}) = _fill_getindex(A, kr)
getindex(A::AbstractFill, kr::AbstractArray{Bool}) = _fill_getindex(A, kr)

sort(a::AbstractFill; kwds...) = a
sort!(a::AbstractFill; kwds...) = a
svdvals!(a::AbstractFill{<:Any,2}) = [getindex_value(a)*sqrt(prod(size(a))); Zeros(min(size(a)...)-1)]
Expand Down Expand Up @@ -644,11 +647,15 @@ getindex_value(a::SubArray) = getindex_value(parent(a))
# view
##

Base.@propagate_inbounds view(A::AbstractFill{<:Any,N}, kr::AbstractArray{Bool,N}) where N = getindex(A, kr)
Base.@propagate_inbounds view(A::AbstractFill{<:Any,1}, kr::AbstractVector{Bool}) = getindex(A, kr)
Base.@propagate_inbounds view(A::AbstractFill{<:Any,N}, kr::AbstractArray{Bool,N}) where N = _fill_getindex(A, kr)
Base.@propagate_inbounds view(A::AbstractFill{<:Any,1}, kr::AbstractVector{Bool}) = _fill_getindex(A, kr)
Base.@propagate_inbounds view(A::AbstractFill{<:Any,N}, I::Vararg{Union{Real, AbstractArray}, N}) where N =
getindex(A, I...)
Base.@propagate_inbounds view(A::AbstractFill{<:Any,N}, I::Vararg{Real, N}) where N =
Base.invoke(view, Tuple{AbstractArray,Vararg{Any,N}}, A, I...)
_fill_getindex(A, Base.to_indices(A,I)...)

# not getindex since we need array-like indexing
Base.@propagate_inbounds function view(A::AbstractFill{<:Any,N}, I::Vararg{Real, N}) where N
@boundscheck checkbounds(A, I...)
fillsimilar(A)
end

end # module
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ end
@test A[[true, false, true, false, false]] ≡ Fill(3.0, 2)
A = Fill(3.0, 2, 2)
@test A[[true true; true false]] ≡ Fill(3.0, 3)
@test_throws DimensionMismatch A[[true, false]]
@test_throws BoundsError A[[true, false]]

A = Ones{Int}(5,5)
@test A[1:3] ≡ Ones{Int}(3)
Expand All @@ -208,7 +208,7 @@ end
A = Ones{Int}(2,2)
@test A[[true false; true false]] ≡ Ones{Int}(2)
@test A[[true, false, true, false]] ≡ Ones{Int}(2)
@test_throws DimensionMismatch A[[true false false; true false false]]
@test_throws BoundsError A[[true false false; true false false]]

A = Zeros{Int}(5,5)
@test A[1:3] ≡ Zeros{Int}(3)
Expand All @@ -218,7 +218,7 @@ end
A = Zeros{Int}(2,2)
@test A[[true false; true false]] ≡ Zeros{Int}(2)
@test A[[true, false, true, false]] ≡ Zeros{Int}(2)
@test_throws DimensionMismatch A[[true false false; true false false]]
@test_throws BoundsError A[[true false false; true false false]]

@testset "colon" begin
@test Ones(2)[:] ≡ Ones(2)[Base.Slice(Base.OneTo(2))] ≡ Ones(2)
Expand Down Expand Up @@ -1214,7 +1214,7 @@ end
@test v isa Fill
@test FillArrays.getindex_value(v) == FillArrays.unique_value(v) == 2.0
@test convert(Fill, v) ≡ Fill(2.0,2)
@test view(a,1) isa SubArray
@test view(a,1) ≡ Fill(2.0)
end

@testset "view with bool" begin
Expand Down