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

Specialize Iterators functions #390

Merged
merged 5 commits into from
Oct 20, 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
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.13.0"
version = "1.14.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
14 changes: 13 additions & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,22 @@

@inline Base.iterate(F::AbstractFill) = length(F) == 0 ? nothing : (v = getindex_value(F); (v, (v, 1)))
@inline function Base.iterate(F::AbstractFill, (v, n))
n >= length(F) && return nothing
1 <= n < length(F) || return nothing

Check warning on line 232 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L232

Added line #L232 was not covered by tests
v, (v, n+1)
end

# Iterators
Iterators.rest(F::AbstractFill, (_,n)) = fillsimilar(F, n <= 0 ? 0 : max(length(F)-n, 0))
function Iterators.drop(F::AbstractFill, n::Integer)
n >= 0 || throw(ArgumentError("drop length must be nonnegative"))
fillsimilar(F, max(length(F)-n, 0))

Check warning on line 240 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L237-L240

Added lines #L237 - L240 were not covered by tests
end
function Iterators.take(F::AbstractFill, n::Integer)
n >= 0 || throw(ArgumentError("take length must be nonnegative"))
fillsimilar(F, min(n, length(F)))

Check warning on line 244 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L242-L244

Added lines #L242 - L244 were not covered by tests
end
Base.rest(F::AbstractFill, s) = Iterators.rest(F, s)

Check warning on line 246 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L246

Added line #L246 was not covered by tests

#################
# Sorting
#################
Expand Down
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,35 @@ end
end
end

@testset "iterators" begin
@testset "invalid state" begin
@test isnothing(iterate(Ones(4), (1,-3)))
@test isempty(Iterators.rest(Ones(4), (1,-3)))
end
@testset "Iterators.rest" begin
@test Iterators.rest(Fill(4, 10), (4, 3)) === Fill(4, 7)
# Base.rest
a, b... = Fill(3, 4)
@test a === 3
@test b === Fill(3, 3)
a, b... = Ones(3, 4)
@test a === 1.0
@test b === Ones(11)
end
@testset "Iterators.drop/take" begin
@test Iterators.drop(Fill(4, 10), 3) === Fill(4, 7)
@test Iterators.take(Fill(4, 10), 3) === Fill(4, 3)
@test Iterators.drop(Fill(4, 10), 0) === Fill(4, 10)
@test Iterators.take(Fill(4, 10), 0) === Fill(4, 0)
@test Iterators.drop(Fill(4, 10), 11) === Fill(4, 0)
@test Iterators.take(Fill(4, 10), 11) === Fill(4, 10)
@test_throws ArgumentError Iterators.drop(Fill(4, 10), -11)
@test_throws ArgumentError Iterators.take(Fill(4, 10), -11)
@test Iterators.drop(Ones(4, 10), 3) === Ones(37)
@test Iterators.take(Ones(4, 10), 3) === Ones(3)
end
end

@testset "Broadcast" begin
x = Fill(5,5)
@test (.+)(x) ≡ x
Expand Down
Loading