|
| 1 | +""" |
| 2 | + module Concatenate |
| 3 | +
|
| 4 | +Alternative implementation for `Base.cat` through [`cat(!)`](@ref cat). |
| 5 | +
|
| 6 | +This is mostly a copy of the Base implementation, with the main difference being |
| 7 | +that the destination is chosen based on all inputs instead of just the first. |
| 8 | +
|
| 9 | +Additionally, we have an intermediate representation in terms of a Concatenated object, |
| 10 | +reminiscent of how Broadcast works. |
| 11 | +
|
| 12 | +The various entry points for specializing behavior are: |
| 13 | +
|
| 14 | +* Destination selection can be achieved through |
| 15 | +
|
| 16 | + Base.similar(concat::Concatenated{Interface}, ::Type{T}, axes) where {Interface} |
| 17 | +
|
| 18 | +* Custom implementations: |
| 19 | +
|
| 20 | + Base.copy(concat::Concatenated{Interface}) # custom implementation of cat |
| 21 | + Base.copyto!(dest, concat::Concatenated{Interface}) # custom implementation of cat! based on interface |
| 22 | + Base.copyto!(dest, concat::Concatenated{Nothing}) # custom implementation of cat! based on typeof(dest) |
| 23 | +""" |
| 24 | +module Concatenate |
| 25 | + |
| 26 | +using Compat: @compat |
| 27 | +export concatenate |
| 28 | +@compat public Concatenated, cat, cat!, concatenated |
| 29 | + |
| 30 | +using Base: promote_eltypeof |
| 31 | +using ..DerivableInterfaces: |
| 32 | + DerivableInterfaces, AbstractInterface, interface, zero!, arraytype |
| 33 | + |
| 34 | +""" |
| 35 | + Concatenated{Interface,Dims,Args<:Tuple} |
| 36 | +
|
| 37 | +Lazy representation of the concatenation of various `Args` along `Dims`, in order to provide |
| 38 | +hooks to customize the implementation. |
| 39 | +""" |
| 40 | +struct Concatenated{Interface,Dims,Args<:Tuple} |
| 41 | + interface::Interface |
| 42 | + dims::Val{Dims} |
| 43 | + args::Args |
| 44 | + |
| 45 | + function Concatenated( |
| 46 | + interface::Union{Nothing,AbstractInterface}, dims::Val{Dims}, args::Tuple |
| 47 | + ) where {Dims} |
| 48 | + return new{typeof(interface),Dims,typeof(args)}(interface, dims, args) |
| 49 | + end |
| 50 | + function Concatenated(dims, args::Tuple) |
| 51 | + return Concatenated(interface(args...), dims, args) |
| 52 | + end |
| 53 | + function Concatenated{Interface}(dims, args) where {Interface} |
| 54 | + return Concatenated(Interface(), dims, args) |
| 55 | + end |
| 56 | + function Concatenated{Interface,Dims}(args) where {Interface,Dims} |
| 57 | + return new{Interface,Dims,typeof(args)}(Interface(), Val(Dims), args) |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +dims(::Concatenated{A,D}) where {A,D} = D |
| 62 | +DerivableInterfaces.interface(concat::Concatenated) = concat.interface |
| 63 | + |
| 64 | +concatenated(dims, args...) = concatenated(Val(dims), args...) |
| 65 | +concatenated(dims::Val, args...) = Concatenated(dims, args) |
| 66 | + |
| 67 | +function Base.convert( |
| 68 | + ::Type{Concatenated{NewInterface}}, concat::Concatenated{<:Any,Dims,Args} |
| 69 | +) where {NewInterface,Dims,Args} |
| 70 | + return Concatenated{NewInterface}( |
| 71 | + concat.dims, concat.args |
| 72 | + )::Concatenated{NewInterface,Dims,Args} |
| 73 | +end |
| 74 | + |
| 75 | +# allocating the destination container |
| 76 | +# ------------------------------------ |
| 77 | +Base.similar(concat::Concatenated) = similar(concat, eltype(concat)) |
| 78 | +Base.similar(concat::Concatenated, ::Type{T}) where {T} = similar(concat, T, axes(concat)) |
| 79 | +function Base.similar(concat::Concatenated, ::Type{T}, ax) where {T} |
| 80 | + return similar(arraytype(interface(concat), T), ax) |
| 81 | +end |
| 82 | + |
| 83 | +Base.eltype(concat::Concatenated) = promote_eltypeof(concat.args...) |
| 84 | + |
| 85 | +# For now, simply couple back to base implementation |
| 86 | +function Base.axes(concat::Concatenated) |
| 87 | + catdims = Base.dims2cat(dims(concat)) |
| 88 | + return Base.cat_size_shape(catdims, concat.args...) |
| 89 | +end |
| 90 | + |
| 91 | +# Main logic |
| 92 | +# ---------- |
| 93 | +""" |
| 94 | + concatenate(dims, args...) |
| 95 | +
|
| 96 | +Concatenate the supplied `args` along dimensions `dims`. |
| 97 | +
|
| 98 | +See also [`cat`] and [`cat!`](@ref). |
| 99 | +""" |
| 100 | +concatenate(dims, args...) = Base.materialize(concatenated(dims, args...)) |
| 101 | + |
| 102 | +""" |
| 103 | + Concatenate.cat(args...; dims) |
| 104 | +
|
| 105 | +Concatenate the supplied `args` along dimensions `dims`. |
| 106 | +
|
| 107 | +See also [`concatenate`] and [`cat!`](@ref). |
| 108 | +""" |
| 109 | +cat(args...; dims) = concatenate(dims, args...) |
| 110 | +Base.materialize(concat::Concatenated) = copy(concat) |
| 111 | + |
| 112 | +""" |
| 113 | + Concatenate.cat!(dest, args...; dims) |
| 114 | +
|
| 115 | +Concatenate the supplied `args` along dimensions `dims`, placing the result into `dest`. |
| 116 | +""" |
| 117 | +function cat!(dest, args...; dims) |
| 118 | + Base.materialize!(dest, concatenated(dims, args...)) |
| 119 | + return dest |
| 120 | +end |
| 121 | +Base.materialize!(dest, concat::Concatenated) = copyto!(dest, concat) |
| 122 | + |
| 123 | +Base.copy(concat::Concatenated) = copyto!(similar(concat), concat) |
| 124 | + |
| 125 | +# default falls back to replacing interface with Nothing |
| 126 | +# this permits specializing on typeof(dest) without ambiguities |
| 127 | +# Note: this needs to be defined for AbstractArray specifically to avoid ambiguities with Base. |
| 128 | +@inline Base.copyto!(dest::AbstractArray, concat::Concatenated) = |
| 129 | + copyto!(dest, convert(Concatenated{Nothing}, concat)) |
| 130 | + |
| 131 | +# couple back to Base implementation if no specialization exists: |
| 132 | +# https://github.com/JuliaLang/julia/blob/29da86bb983066dd076439c2c7bc5e28dbd611bb/base/abstractarray.jl#L1852 |
| 133 | +function Base.copyto!(dest::AbstractArray, concat::Concatenated{Nothing}) |
| 134 | + catdims = Base.dims2cat(dims(concat)) |
| 135 | + shape = Base.cat_size_shape(catdims, concat.args...) |
| 136 | + count(!iszero, catdims)::Int > 1 && zero!(dest) |
| 137 | + return Base.__cat(dest, shape, catdims, concat.args...) |
| 138 | +end |
| 139 | + |
| 140 | +end |
0 commit comments