From d2b56b3812df702f22291a37dc51113f5d77b128 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Sun, 30 Aug 2020 16:40:51 +0200 Subject: [PATCH 1/6] Recursive implementation of getindex(::Vcat, idx) --- src/lazyconcat.jl | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index 30613176..e1b19adf 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -43,28 +43,32 @@ function ==(a::Vcat{T,1,II}, b::Vcat{T,1,II}) where {T,II} all(arguments(a) .== arguments(b)) end -@propagate_inbounds @inline function vcat_getindex(f, k::Integer) +@propagate_inbounds @inline vcat_getindex(f, k::Integer) = + vcat_getindex_recursive(f, (k, ), f.args...) + +@propagate_inbounds @inline vcat_getindex(f, k::Integer, j::Integer) = + vcat_getindex_recursive(f, (k, j), f.args...) + +@propagate_inbounds @inline function vcat_getindex_recursive( + f, idx::NTuple{1}, A, args...) + k, = idx T = eltype(f) - κ = k - for A in f.args - n = length(A) - κ ≤ n && return convert(T,A[κ])::T - κ -= n - end - throw(BoundsError(f, k)) + n = length(A) + k ≤ n && return convert(T, A[k])::T + vcat_getindex_recursive(f, (k - n, ), args...) end -@propagate_inbounds @inline function vcat_getindex(f, k::Integer, j::Integer) +@propagate_inbounds @inline function vcat_getindex_recursive( + f, idx::NTuple{2}, A, args...) + k, j = idx T = eltype(f) - κ = k - for A in f.args - n = size(A,1) - κ ≤ n && return convert(T,A[κ,j])::T - κ -= n - end - throw(BoundsError(f, (k,j))) + n = size(A, 1) + k ≤ n && return convert(T, A[k, j])::T + vcat_getindex_recursive(f, (k - n, j), args...) end +@inline vcat_getindex_recursive(f, idx) = throw(BoundsError(f, idx)) + @propagate_inbounds @inline getindex(f::Vcat{<:Any,1}, k::Integer) = vcat_getindex(f, k) @propagate_inbounds @inline getindex(f::Vcat{<:Any,2}, k::Integer, j::Integer) = vcat_getindex(f, k, j) getindex(f::Applied{DefaultArrayApplyStyle,typeof(vcat)}, k::Integer)= vcat_getindex(f, k) @@ -957,4 +961,4 @@ function sub_paddeddata(::TriangularLayout{'U','N'}, S::SubArray{<:Any,1,<:Abstr P = parent(S) (kr,j) = parentindices(S) view(triangulardata(P), kr ∩ (1:j), j) -end \ No newline at end of file +end From bd138cac6f20da11dbf067efc4012134160233e9 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Sun, 30 Aug 2020 20:28:37 +0200 Subject: [PATCH 2/6] Recursive implementation of setindex!(::Vcat, idx) --- src/lazyconcat.jl | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index e1b19adf..ed10766e 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -43,11 +43,8 @@ function ==(a::Vcat{T,1,II}, b::Vcat{T,1,II}) where {T,II} all(arguments(a) .== arguments(b)) end -@propagate_inbounds @inline vcat_getindex(f, k::Integer) = - vcat_getindex_recursive(f, (k, ), f.args...) - -@propagate_inbounds @inline vcat_getindex(f, k::Integer, j::Integer) = - vcat_getindex_recursive(f, (k, j), f.args...) +@propagate_inbounds @inline vcat_getindex(f, idx::Vararg{Integer}) = + vcat_getindex_recursive(f, idx, f.args...) @propagate_inbounds @inline function vcat_getindex_recursive( f, idx::NTuple{1}, A, args...) @@ -81,24 +78,30 @@ getindex(f::Applied{<:Any,typeof(vcat)}, k::Integer, j::Integer)= vcat_getindex( copy(f::Vcat) = Vcat(map(copy, f.args)...) map(::typeof(copy), f::Vcat) = Vcat(map.(copy, f.args)...) -@propagate_inbounds @inline function setindex!(f::Vcat{T,1}, v, k::Integer) where T - κ = k - for A in f.args - n = length(A) - κ ≤ n && return setindex!(A, v, κ) - κ -= n - end - throw(BoundsError(f, k)) +@propagate_inbounds @inline vcat_setindex!(f, v, idx::Vararg{Integer}) = + vcat_setindex_recursive!(f, v, idx, f.args...) + +@propagate_inbounds @inline function vcat_setindex_recursive!( + f, v, idx::NTuple{1}, A, args...) + k, = idx + n = length(A) + k ≤ n && return setindex!(A, v, idx...) + vcat_setindex_recursive!(f, v, (k - n, ), args...) end -@propagate_inbounds @inline function setindex!(f::Vcat{T,2}, v, k::Integer, j::Integer) where T - κ = k - for A in f.args - n = size(A,1) - κ ≤ n && return setindex!(A, v, κ, j) - κ -= n - end - throw(BoundsError(f, (k,j))) +@propagate_inbounds @inline function vcat_setindex_recursive!( + f, v, idx::NTuple{2}, A, args...) + k, j = idx + n = length(A) + k ≤ n && return setindex!(A, v, idx...) + vcat_setindex_recursive!(f, v, (k - n, j), args...) +end + +@inline vcat_setindex_recursive!(f, v, idx) = throw(BoundsError(f, idx)) + +@propagate_inbounds @inline function setindex!( + f::Vcat{T,N}, v, idx::Vararg{Integer,N}) where {T,N} + vcat_setindex_recursive!(f, v, idx, f.args...) end reverse(f::Vcat{<:Any,1}) = Vcat((reverse(itr) for itr in reverse(f.args))...) From 5b2f827b20eb8c7b4436d6e1e1b4e8fcd75f0f85 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Sun, 30 Aug 2020 20:47:08 +0200 Subject: [PATCH 3/6] Reimplement getindex/setindex! for Hcat --- src/lazyconcat.jl | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index ed10766e..4f51b4a9 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -126,17 +126,20 @@ ndims(::Applied{<:Any,typeof(hcat)}) = 2 size(f::Applied{<:Any,typeof(hcat)}) = (size(f.args[1],1), +(map(a -> size(a,2), f.args)...)) Base.IndexStyle(::Type{<:Hcat}) where T = Base.IndexCartesian() -function hcat_getindex(f, k::Integer, j::Integer) +@inline hcat_getindex(f, k::Integer, j::Integer) = + hcat_getindex_recursive(f, (k, j), f.args...) + +@inline function hcat_getindex_recursive( + f, idx::NTuple{2}, A, args...) + k, j = idx T = eltype(f) - ξ = j - for A in f.args - n = size(A,2) - ξ ≤ n && return T(A[k,ξ])::T - ξ -= n - end - throw(BoundsError(f, (k,j))) + n = size(A, 2) + j ≤ n && return convert(T, A[k, j])::T + hcat_getindex_recursive(f, (k, j - n), args...) end +@inline hcat_getindex_recursive(f, idx) = throw(BoundsError(f, idx)) + getindex(f::Hcat, k::Integer, j::Integer) = hcat_getindex(f, k, j) getindex(f::Applied{DefaultArrayApplyStyle,typeof(hcat)}, k::Integer, j::Integer)= hcat_getindex(f, k, j) getindex(f::Applied{<:Any,typeof(hcat)}, k::Integer, j::Integer)= hcat_getindex(f, k, j) @@ -144,14 +147,19 @@ getindex(f::Applied{<:Any,typeof(hcat)}, k::Integer, j::Integer)= hcat_getindex( # since its mutable we need to make a copy copy(f::Hcat) = Hcat(map(copy, f.args)...) +@inline function hcat_setindex_recursive!( + f, v, idx::NTuple{2}, A, args...) + k, j = idx + T = eltype(f) + n = size(A, 2) + j ≤ n && return setindex!(A, v, k, j) + hcat_setindex_recursive!(f, v, (k, j - n), args...) +end + +@inline hcat_setindex_recursive!(f, v, idx) = throw(BoundsError(f, idx)) + function setindex!(f::Hcat{T}, v, k::Integer, j::Integer) where T - ξ = j - for A in f.args - n = size(A,2) - ξ ≤ n && return setindex!(A, v, k, ξ) - ξ -= n - end - throw(BoundsError(f, (k,j))) + hcat_setindex_recursive!(f, v, (k, j), f.args...) end From 6353882a07f6c833b89d111ba4813d0018192667 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Sun, 30 Aug 2020 21:16:39 +0200 Subject: [PATCH 4/6] Fix setindex!(::Vcat{2}, ...) --- src/lazyconcat.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index 4f51b4a9..224872a1 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -92,7 +92,7 @@ end @propagate_inbounds @inline function vcat_setindex_recursive!( f, v, idx::NTuple{2}, A, args...) k, j = idx - n = length(A) + n = size(A, 1) k ≤ n && return setindex!(A, v, idx...) vcat_setindex_recursive!(f, v, (k - n, j), args...) end From 0887f54e67eabca3efecc48dc9eed0f61e246bd7 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Sun, 30 Aug 2020 21:17:05 +0200 Subject: [PATCH 5/6] Add some type annotations --- src/lazyconcat.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index 224872a1..84eff562 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -82,7 +82,7 @@ map(::typeof(copy), f::Vcat) = Vcat(map.(copy, f.args)...) vcat_setindex_recursive!(f, v, idx, f.args...) @propagate_inbounds @inline function vcat_setindex_recursive!( - f, v, idx::NTuple{1}, A, args...) + f::Vcat{T,1} where T, v, idx::NTuple{1}, A, args...) k, = idx n = length(A) k ≤ n && return setindex!(A, v, idx...) @@ -90,7 +90,7 @@ map(::typeof(copy), f::Vcat) = Vcat(map.(copy, f.args)...) end @propagate_inbounds @inline function vcat_setindex_recursive!( - f, v, idx::NTuple{2}, A, args...) + f::Vcat{T,2} where T, v, idx::NTuple{2}, A, args...) k, j = idx n = size(A, 1) k ≤ n && return setindex!(A, v, idx...) From e2cda407102f98b3e60cbe7c50cdbe56cd4b0519 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Polanco Date: Tue, 1 Sep 2020 20:16:08 +0200 Subject: [PATCH 6/6] Remove unused function --- src/lazyconcat.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lazyconcat.jl b/src/lazyconcat.jl index 84eff562..044ea15e 100644 --- a/src/lazyconcat.jl +++ b/src/lazyconcat.jl @@ -78,9 +78,6 @@ getindex(f::Applied{<:Any,typeof(vcat)}, k::Integer, j::Integer)= vcat_getindex( copy(f::Vcat) = Vcat(map(copy, f.args)...) map(::typeof(copy), f::Vcat) = Vcat(map.(copy, f.args)...) -@propagate_inbounds @inline vcat_setindex!(f, v, idx::Vararg{Integer}) = - vcat_setindex_recursive!(f, v, idx, f.args...) - @propagate_inbounds @inline function vcat_setindex_recursive!( f::Vcat{T,1} where T, v, idx::NTuple{1}, A, args...) k, = idx