|
26 | 26 | # fields except through `getfield` and accessor functions. |
27 | 27 | ArrayPartition(x::NamedArrayPartition) = getfield(x, :array_partition) |
28 | 28 |
|
29 | | -function Base.similar(x::NamedArrayPartition, args...) |
| 29 | +function Base.similar(A::NamedArrayPartition{T, S}) where {T, S} |
| 30 | + NamedArrayPartition(ArrayPartition{T, S}(similar.(getfield(A, :array_partition))), |
| 31 | + getfield(A, :names_to_indices)) |
| 32 | +end |
| 33 | + |
| 34 | +# return ArrayPartition when possible, otherwise next best thing of the correct size |
| 35 | +function Base.similar(A::NamedArrayPartition, dims::NTuple{N, Int}) where {N} |
| 36 | + NamedArrayPartition( |
| 37 | + similar(getfield(A, :array_partition), dims), getfield(A, :names_to_indices)) |
| 38 | +end |
| 39 | + |
| 40 | +# similar array partition of common type |
| 41 | +@inline function Base.similar(A::NamedArrayPartition, ::Type{T}) where {T} |
| 42 | + NamedArrayPartition( |
| 43 | + similar(getfield(A, :array_partition), T), getfield(A, :names_to_indices)) |
| 44 | +end |
| 45 | + |
| 46 | +# return ArrayPartition when possible, otherwise next best thing of the correct size |
| 47 | +function Base.similar(A::NamedArrayPartition, ::Type{T}, dims::NTuple{N, Int}) where {T, N} |
| 48 | + NamedArrayPartition( |
| 49 | + similar(getfield(A, :array_partition), T, dims), getfield(A, :names_to_indices)) |
| 50 | +end |
| 51 | + |
| 52 | +# similar array partition with different types |
| 53 | +function Base.similar( |
| 54 | + A::NamedArrayPartition, ::Type{T}, ::Type{S}, R::DataType...) where {T, S} |
30 | 55 | NamedArrayPartition( |
31 | | - similar(getfield(x, :array_partition), args...), getfield(x, :names_to_indices)) |
| 56 | + similar(getfield(A, :array_partition), T, S, R), getfield(A, :names_to_indices)) |
32 | 57 | end |
33 | 58 |
|
34 | 59 | Base.Array(x::NamedArrayPartition) = Array(ArrayPartition(x)) |
|
0 commit comments