Skip to content

Commit

Permalink
Toward 💯 code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty committed Aug 13, 2018
1 parent 84707fd commit de07181
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ and multiplication. This helps with the implementation of matrix-free methods
for iterative solvers.

The package has been designed with high-performance in mind, so should outperform
the non-lazy analogues from Base. Please file an issue for any example that
violates this.
the non-lazy analogues from Base for many operations like `copyto!` and broadcasting.
Some operations will be inherently slower due to extra computation, like `getindex`.
Please file an issue for any examples that are significantly slower than their
the analogue in Base.

## Concatenation

Expand Down
21 changes: 12 additions & 9 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ Base.IndexStyle(::Type{<:Vcat{T,1}}) where T = Base.IndexLinear()
Base.IndexStyle(::Type{<:Vcat{T,2}}) where T = Base.IndexCartesian()

function getindex(f::Vcat{T,1}, k::Integer) where T
κ = k
for A in f.arrays
n = length(A)
k ≤ n && return T(A[k])::T
k -= n
κ ≤ n && return T(A[κ])::T
κ -= n
end
throw(BoundsError("attempt to access $length(f) Vcat array."))
throw(BoundsError(f, k))
end

function getindex(f::Vcat{T,2}, k::Integer, j::Integer) where T
κ = k
for A in f.arrays
n = size(A,1)
k ≤ n && return T(A[k,j])::T
k -= n
κ ≤ n && return T(A[κ,j])::T
κ -= n
end
throw(BoundsError("attempt to access $length(f) Vcat array."))
throw(BoundsError(f, (k,j)))
end

reverse(f::Vcat{<:Any,1}) = Vcat((reverse(itr) for itr in reverse(f.arrays))...)
Expand All @@ -69,12 +71,13 @@ size(f::Hcat) = (size(f.arrays[1],1), +(map(a -> size(a,2), f.arrays)...))
Base.IndexStyle(::Type{<:Hcat}) where T = Base.IndexCartesian()

function getindex(f::Hcat{T}, k::Integer, j::Integer) where T
ξ = j
for A in f.arrays
n = size(A,2)
j ≤ n && return T(A[k,j])::T
j -= n
ξ ≤ n && return T(A[k,ξ])::T
ξ -= n
end
throw(BoundsError("attempt to access $(size(f)) Hcat array."))
throw(BoundsError(f, (k,j)))
end


Expand Down
9 changes: 8 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ end
b = Array{Float64}(undef, 6,10)
@test @allocated(copyto!(b, A)) == 0
@test b == vcat(A.arrays...)

A = Hcat(1:10, 2:11)
@test_throws BoundsError A[1,3]
@test @inferred(size(A)) == (10,2)
@test @inferred(A[5]) == @inferred(A[5,1]) == 5
@test @inferred(A[11]) == @inferred(A[1,2]) == 2
Expand All @@ -166,6 +167,12 @@ end
@test @allocated(copyto!(b, A)) == 0
@test b == hcat(A.arrays...)

A = Hcat(Vector(1:10), Vector(2:11))
b = Array{Int}(undef, 10, 2)
copyto!(b, A)
@test b == hcat(A.arrays...)
@test @allocated(copyto!(b, A)) == 0

A = Hcat(1, zeros(1,5))
@test A == hcat(1, zeros(1,5))
end
Expand Down

0 comments on commit de07181

Please sign in to comment.