Skip to content

Commit

Permalink
Implement faster "iterate" for Eye (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre authored and dlfivefifty committed Dec 1, 2018
1 parent 968b573 commit d87736e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ const Eye{T, Axes} = Diagonal{T, Ones{T,1,Tuple{Axes}}}
Eye{T}(n::Integer) where T = Diagonal(Ones{T}(n))
Eye(n::Integer) = Diagonal(Ones(n))


function Base.iterate(iter::Eye{T}, istate = (1, 1)) where T
(i::Int, j::Int) = istate
m = size(iter, 1)
return i > m ? nothing :
((@inbounds getindex(iter, i, j)),
j == m ? (i + 1, 1) : (i, j + 1))
end

@deprecate Eye(n::Integer, m::Integer) view(Eye(max(n,m)), 1:n, 1:m)
@deprecate Eye{T}(n::Integer, m::Integer) where T view(Eye{T}(max(n,m)), 1:n, 1:m)
Expand Down
9 changes: 8 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import FillArrays: AbstractFill
@test convert(AbstractArray,Z) convert(AbstractFill,Z) Z
@test convert(AbstractArray{T},Z) convert(AbstractFill{T},Z) AbstractArray{T}(Z) Z
@test convert(AbstractMatrix{T},Z) convert(AbstractFill{T,2},Z) AbstractMatrix{T}(Z) Z

@test_throws Exception convert(Fill{Float64}, [1,1,2])
@test_throws Exception convert(Fill, [])
@test convert(Fill{Float64}, [1,1,1]) Fill(1.0, 3)
Expand Down Expand Up @@ -469,3 +469,10 @@ end
@test (1:10) .* Zeros{Int}(10) Zeros{Int}(10)
@test_throws DimensionMismatch (1:11) .* Zeros(10)
end

@testset "iterate" begin
for d in (0, 1, 2, 100)
m = Eye(d)
@test [x for x in m] == m
end
end

0 comments on commit d87736e

Please sign in to comment.