Skip to content

Commit 6b66095

Browse files
authored
Specialize Iterators functions (#390)
* Specialize Iterators.rest to return an AbstractFill Specialize Base.rest to return an AbstractFill * Specialize Iterators.drop and Iterators.take * Fix error message in take * Lowercase in error message * Bump version to v1.14.0
1 parent 6f61dc3 commit 6b66095

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "1.13.0"
3+
version = "1.14.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

+13-1
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,22 @@ Base.@propagate_inbounds getindex(A::AbstractFill, kr::AbstractArray{Bool}) = _f
229229

230230
@inline Base.iterate(F::AbstractFill) = length(F) == 0 ? nothing : (v = getindex_value(F); (v, (v, 1)))
231231
@inline function Base.iterate(F::AbstractFill, (v, n))
232-
n >= length(F) && return nothing
232+
1 <= n < length(F) || return nothing
233233
v, (v, n+1)
234234
end
235235

236+
# Iterators
237+
Iterators.rest(F::AbstractFill, (_,n)) = fillsimilar(F, n <= 0 ? 0 : max(length(F)-n, 0))
238+
function Iterators.drop(F::AbstractFill, n::Integer)
239+
n >= 0 || throw(ArgumentError("drop length must be nonnegative"))
240+
fillsimilar(F, max(length(F)-n, 0))
241+
end
242+
function Iterators.take(F::AbstractFill, n::Integer)
243+
n >= 0 || throw(ArgumentError("take length must be nonnegative"))
244+
fillsimilar(F, min(n, length(F)))
245+
end
246+
Base.rest(F::AbstractFill, s) = Iterators.rest(F, s)
247+
236248
#################
237249
# Sorting
238250
#################

test/runtests.jl

+29
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,35 @@ end
904904
end
905905
end
906906

907+
@testset "iterators" begin
908+
@testset "invalid state" begin
909+
@test isnothing(iterate(Ones(4), (1,-3)))
910+
@test isempty(Iterators.rest(Ones(4), (1,-3)))
911+
end
912+
@testset "Iterators.rest" begin
913+
@test Iterators.rest(Fill(4, 10), (4, 3)) === Fill(4, 7)
914+
# Base.rest
915+
a, b... = Fill(3, 4)
916+
@test a === 3
917+
@test b === Fill(3, 3)
918+
a, b... = Ones(3, 4)
919+
@test a === 1.0
920+
@test b === Ones(11)
921+
end
922+
@testset "Iterators.drop/take" begin
923+
@test Iterators.drop(Fill(4, 10), 3) === Fill(4, 7)
924+
@test Iterators.take(Fill(4, 10), 3) === Fill(4, 3)
925+
@test Iterators.drop(Fill(4, 10), 0) === Fill(4, 10)
926+
@test Iterators.take(Fill(4, 10), 0) === Fill(4, 0)
927+
@test Iterators.drop(Fill(4, 10), 11) === Fill(4, 0)
928+
@test Iterators.take(Fill(4, 10), 11) === Fill(4, 10)
929+
@test_throws ArgumentError Iterators.drop(Fill(4, 10), -11)
930+
@test_throws ArgumentError Iterators.take(Fill(4, 10), -11)
931+
@test Iterators.drop(Ones(4, 10), 3) === Ones(37)
932+
@test Iterators.take(Ones(4, 10), 3) === Ones(3)
933+
end
934+
end
935+
907936
@testset "Broadcast" begin
908937
x = Fill(5,5)
909938
@test (.+)(x) x

0 commit comments

Comments
 (0)