Skip to content

Commit

Permalink
hcat and vcat too
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty committed Dec 7, 2024
1 parent 92585c9 commit 95b503c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
74 changes: 38 additions & 36 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,37 @@ Vcat() = Vcat{Any}()
@inline function applied_instantiate(::typeof(vcat), args...)
iargs = map(instantiate, args)
if !isempty(iargs)
m = _vcat_size(iargs[1],2)
m = _cat_size(iargs[1],2)

Check warning on line 24 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L24

Added line #L24 was not covered by tests
for k=2:length(iargs)
_vcat_size(iargs[k],2) == m || throw(ArgumentError("number of columns of each array must match (got $(map(x->_vcat_size(x,2), args)))"))
_cat_size(iargs[k],2) == m || throw(ArgumentError("number of columns of each array must match (got $(map(x->_cat_size(x,2), args)))"))

Check warning on line 26 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L26

Added line #L26 was not covered by tests
end
end
vcat, iargs
end

_vcat_axes(a, k) = Base.OneTo(1)
_vcat_axes(a::AbstractArray, k) = axes(a, k)
_vcat_size(a, k) = 1
_vcat_size(a::AbstractArray, k) = size(a, k)
_vcat_ndims(a) = 1
_vcat_ndims(a::AbstractArray) = ndims(a)
_vcat_eltype(a) = typeof(a)
_vcat_eltype(a::AbstractArray) = eltype(a)
_vcat_length(a) = 1
_vcat_length(a::AbstractArray) = length(a)
_vcat_getindex(a, k...) = a
_vcat_getindex(a::AbstractArray, k...) = a[k...]
_cat_axes(a, k) = Base.OneTo(1)
_cat_axes(a::AbstractArray, k) = axes(a, k)
_cat_size(a, k) = 1
_cat_size(a::AbstractArray, k) = size(a, k)
_cat_ndims(a) = 1
_cat_ndims(a::AbstractArray) = ndims(a)
_cat_eltype(a) = typeof(a)
_cat_eltype(a::AbstractArray) = eltype(a)
_cat_length(a) = 1
_cat_length(a::AbstractArray) = length(a)
_cat_getindex(a, k...) = a
_cat_getindex(a::AbstractArray, k...) = a[k...]
_cat_colsupport(a, k...) = 1
_cat_colsupport(a::AbstractArray, k...) = colsupport(a, k)

Check warning on line 45 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L32-L45

Added lines #L32 - L45 were not covered by tests


@inline applied_eltype(::typeof(vcat)) = Any
@inline applied_eltype(::typeof(vcat), args...) = promote_type(map(_vcat_eltype, args)...)
@inline applied_ndims(::typeof(vcat), args...) = max(1,maximum(map(_vcat_ndims,args)))
@inline applied_eltype(::typeof(vcat), args...) = promote_type(map(_cat_eltype, args)...)
@inline applied_ndims(::typeof(vcat), args...) = max(1,maximum(map(_cat_ndims,args)))

Check warning on line 50 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L49-L50

Added lines #L49 - L50 were not covered by tests
@inline applied_ndims(::typeof(vcat)) = 1
@inline axes(f::Vcat{<:Any,1,Tuple{}}) = (OneTo(0),)
@inline axes(f::Vcat{<:Any,1}) = tuple(oneto(+(map(_vcat_length,f.args)...)))
@inline axes(f::Vcat{<:Any,2}) = (oneto(+(map(a -> _vcat_size(a,1), f.args)...)), _vcat_axes(f.args[1],2))
@inline axes(f::Vcat{<:Any,1}) = tuple(oneto(+(map(_cat_length,f.args)...)))
@inline axes(f::Vcat{<:Any,2}) = (oneto(+(map(a -> _cat_size(a,1), f.args)...)), _cat_axes(f.args[1],2))

Check warning on line 54 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L53-L54

Added lines #L53 - L54 were not covered by tests
@inline size(f::Vcat) = map(length, axes(f))


Expand All @@ -71,17 +73,17 @@ end
f, idx::Tuple{Integer}, A, args...)
k, = idx
T = eltype(f)
n = _vcat_length(A)
k n && return convert(T, _vcat_getindex(A,k))::T
n = _cat_length(A)
k n && return convert(T, _cat_getindex(A,k))::T

Check warning on line 77 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L76-L77

Added lines #L76 - L77 were not covered by tests
vcat_getindex_recursive(f, (k - n, ), args...)
end

@propagate_inbounds @inline function vcat_getindex_recursive(
f, idx::Tuple{Integer,Integer}, A, args...)
k, j = idx
T = eltype(f)
n = _vcat_size(A, 1)
k n && return convert(T, _vcat_getindex(A, k, j))::T
n = _cat_size(A, 1)
k n && return convert(T, _cat_getindex(A, k, j))::T

Check warning on line 86 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L85-L86

Added lines #L85 - L86 were not covered by tests
vcat_getindex_recursive(f, (k - n, j), args...)
end

Expand Down Expand Up @@ -146,26 +148,26 @@ Hcat(A...) = ApplyArray(hcat, A...)
Hcat() = Hcat{Any}()
Hcat{T}(A...) where T = ApplyArray{T}(hcat, A...)

@inline applied_eltype(::typeof(hcat), args...) = promote_type(map(eltype,args)...)
@inline applied_eltype(::typeof(hcat), args...) = promote_type(map(_cat_eltype,args)...)

Check warning on line 151 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L151

Added line #L151 was not covered by tests
@inline applied_ndims(::typeof(hcat), args...) = 2
@inline applied_size(::typeof(hcat), args...) = (size(args[1],1), +(map(a -> size(a,2), args)...))
@inline applied_size(::typeof(hcat), args...) = (_cat_size(args[1],1), +(map(a -> _cat_size(a,2), args)...))

Check warning on line 153 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L153

Added line #L153 was not covered by tests
@inline applied_size(::typeof(hcat)) = (0,0)

@inline hcat_getindex(f, k, j::Integer) = hcat_getindex_recursive(f, (k, j), f.args...)

@inline function hcat_getindex_recursive(f, idx::Tuple{Integer,Integer}, A, args...)
k, j = idx
T = eltype(f)
n = size(A, 2)
j n && return convert(T, A[k, j])::T
n = _cat_size(A, 2)
j n && return convert(T, _cat_getindex(A,k, j))::T

Check warning on line 162 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L161-L162

Added lines #L161 - L162 were not covered by tests
hcat_getindex_recursive(f, (k, j - n), args...)
end

@inline function hcat_getindex_recursive(f, idx::Tuple{Union{Colon,AbstractVector},Integer}, A, args...)
kr, j = idx
T = eltype(f)
n = size(A, 2)
j n && return convert(AbstractVector{T}, A[kr, j])
n = _cat_size(A, 2)
j n && return convert(AbstractVector{T}, _cat_getindex(A, kr, j))

Check warning on line 170 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L169-L170

Added lines #L169 - L170 were not covered by tests
hcat_getindex_recursive(f, (kr, j - n), args...)
end

Expand Down Expand Up @@ -197,27 +199,27 @@ end
# Hvcat
####

@inline applied_eltype(::typeof(hvcat), a, b...) = promote_type(map(eltype, b)...)
@inline applied_eltype(::typeof(hvcat), a, b...) = promote_type(map(_cat_eltype, b)...)

Check warning on line 202 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L202

Added line #L202 was not covered by tests
@inline applied_ndims(::typeof(hvcat), args...) = 2
@inline applied_size(::typeof(hvcat), n::Int, b...) = sum(size.(b[1:n:end],1)),sum(size.(b[1:n],2))
@inline applied_size(::typeof(hvcat), n::Int, b...) = sum(_cat_size.(b[1:n:end],1)),sum(_cat_size.(b[1:n],2))

Check warning on line 204 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L204

Added line #L204 was not covered by tests

@inline function applied_size(::typeof(hvcat), n::NTuple{N,Int}, b...) where N
as = tuple(2, (2 .+ cumsum(Base.front(n)))...)
sum(size.(getindex.(Ref((n, b...)), as),1)),sum(size.(b[1:n[1]],2))
sum(_cat_size.(getindex.(Ref((n, b...)), as),1)),sum(_cat_size.(b[1:n[1]],2))

Check warning on line 208 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L208

Added line #L208 was not covered by tests
end


@inline hvcat_getindex(f, k, j::Integer) = hvcat_getindex_recursive(f, (k, j), f.args...)

@inline _hvcat_size(A) = size(A)
@inline _hvcat_size(A::Number) = (1,1)
@inline _hvcat_size(A::AbstractArray) = size(A)
@inline _hvcat_size(A) = (1,1)

Check warning on line 215 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L214-L215

Added lines #L214 - L215 were not covered by tests
@inline _hvcat_size(A::AbstractVector) = (size(A,1),1)

@inline function hvcat_getindex_recursive(f, (k,j)::Tuple{Integer,Integer}, N::Int, A, args...)
T = eltype(f)
m,n = _hvcat_size(A)
N 0 && throw(BoundsError(f, (k,j))) # ran out of arrays
k m && j n && return convert(T, A[k, j])::T
k m && j n && return convert(T, _cat_getindex(A, k, j))::T

Check warning on line 222 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L222

Added line #L222 was not covered by tests
k m && return hvcat_getindex_recursive(f, (k, j - n), N-1, args...)
hvcat_getindex_recursive(f, (k - m, j), N, args[N:end]...)
end
Expand Down Expand Up @@ -769,8 +771,8 @@ end
function colsupport(lay::ApplyLayout{typeof(hcat)}, H::AbstractArray, j::Integer)
ξ = j
for A in arguments(lay,H)
n = size(A,2)
ξ n && return colsupport(A, ξ)
n = _cat_size(A,2)
ξ n && return _cat_colsupport(A, ξ)

Check warning on line 775 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L774-L775

Added lines #L774 - L775 were not covered by tests
ξ -= n
end
return 1:0
Expand Down
2 changes: 2 additions & 0 deletions test/concattests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ import LazyArrays: MemoryLayout, DenseColumnMajor, materialize!, call, paddeddat
@test Vcat("hi", "bye") == ["hi", "bye"]
@test Vcat(["hi" "bye"], [2 3]) == ["hi" "bye"; 2 3]
@test Vcat("hi", [2;;]) == ["hi"; 2 ;;]
@test Hcat("hi", "bye") == ["hi" "bye"]
@test ApplyArray(hvcat, 2, "hi", 2, 3, "bye") == ApplyArray(hvcat, (2,1), "hi", 2, [3 "bye"]) == ["hi" 2; 3 "bye"]
end
end

Expand Down

0 comments on commit 95b503c

Please sign in to comment.