Skip to content

Specialize indexing with a AbstractFill{Bool} index #410

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 = "1.14.0"
version = "1.15.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
9 changes: 9 additions & 0 deletions src/trues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ function Base.to_indices(A::AbstractArray{T,N}, inds, I::Tuple{Trues{N}}) where
@boundscheck axes(A) == axes(I[1]) || Base.throw_boundserror(A, I[1])
(vec(LinearIndices(A)),)
end

Base.@propagate_inbounds function getindex(v::AbstractArray, f::AbstractFill{Bool})
@boundscheck checkbounds(v, f)
v[range(begin, length = getindex_value(f) ? length(v) : 0)]
end
Base.@propagate_inbounds function getindex(v::AbstractFill, f::AbstractFill{Bool})
@boundscheck checkbounds(v, f)
fillsimilar(v, getindex_value(f) ? length(v) : 0)
end
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,27 @@ end
@test StepRangeLen(z) ≡ convert(StepRangeLen, z) ≡ convert(StepRangeLen{Int}, z) ≡ convert(typeof(s), z) ≡ convert(AbstractRange, z) ≡ s
end
end
@testset "indexing with a Bool AbstractFill" begin
@testset for v in ([1, 2, 3], 1:3, rand(Int,3,1), Base.IdentityUnitRange(1:3))
n = length(v)
@test v[Trues(n)] == v[Fill(true, n)] == v[trues(n)] == vec(v)
@test v[Falses(n)] == v[Fill(false, n)] == v[falses(n)] == Int[]
@test v[Trues(n,1)] == v[Fill(true, n,1)] == v[trues(n,1)] == vec(v)
@test v[Falses(n,1)] == v[Fill(false, n,1)] == v[falses(n,1)] == Int[]
end

for f in (Fill(3, 4), Fill(3, 4, 1))
n = length(f)
@test f[Trues(n)] === f[Fill(true, n)] === vec(f)
@test f[Falses(n)] === f[Fill(false, n)] === Fill(3,0)
@test f[Trues(n)] == f[trues(n)]
@test f[Falses(n)] == f[falses(n)]
@test f[Trues(n,1)] === f[Fill(true, n,1)] === vec(f)
@test f[Falses(n,1)] === f[Fill(false, n,1)] === Fill(3,0)
@test f[Trues(n,1)] == f[trues(n,1)]
@test f[Falses(n,1)] == f[falses(n,1)]
end
end
end

@testset "RectDiagonal" begin
Expand Down
Loading