-
Notifications
You must be signed in to change notification settings - Fork 34
/
array_interface.jl
150 lines (127 loc) · 7.19 KB
/
array_interface.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Base.parent(x::ComponentArray) = getfield(x, :data)
Base.size(x::ComponentArray) = size(getdata(x))
StaticArrayInterface.static_size(A::ComponentArray) = StaticArrayInterface.static_size(parent(A))
Base.elsize(x::Type{<:ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = Base.elsize(A)
# Base.axes(x::ComponentArray) = axes(getdata(x))
Base.axes(x::ComponentArray) = CombinedAxis.(getaxes(x), axes(getdata(x)))
Base.reinterpret(::Type{T}, x::ComponentArray, args...) where T = ComponentArray(reinterpret(T, getdata(x), args...), getaxes(x))
ArrayInterface.indices_do_not_alias(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = ArrayInterface.indices_do_not_alias(A)
ArrayInterface.instances_do_not_alias(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = ArrayInterface.instances_do_not_alias(A)
# Cats
# TODO: Make this a little less copy-pastey
function Base.hcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat)
ax_x, ax_y = second_axis.((x,y))
if reduce((accum, key) -> accum || (key in keys(ax_x)), keys(ax_y); init=false) || getaxes(x)[1] != getaxes(y)[1]
return hcat(getdata(x), getdata(y))
else
data_x, data_y = getdata.((x, y))
ax_y = reindex(ax_y, size(x,2))
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y))
axs = getaxes(x)
return ComponentArray(hcat(data_x, data_y), axs[1], Axis((;idxmap_x..., idxmap_y...)), axs[3:end]...)
end
end
second_axis(ca::AbstractComponentVecOrMat) = getaxes(ca)[2]
second_axis(::ComponentVector) = FlatAxis()
# Are all these methods necessary?
# TODO: See what we can reduce down to without getting ambiguity errors
Base.vcat(x::ComponentVector, y::AbstractVector) = vcat(getdata(x), y)
Base.vcat(x::AbstractVector, y::ComponentVector) = vcat(x, getdata(y))
function Base.vcat(x::ComponentVector, y::ComponentVector)
if reduce((accum, key) -> accum || (key in keys(x)), keys(y); init=false)
return vcat(getdata(x), getdata(y))
else
data_x, data_y = getdata.((x, y))
ax_x, ax_y = getindex.(getaxes.((x, y)), 1)
ax_y = reindex(ax_y, length(x))
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y))
return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...)))
end
end
function Base.vcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat)
ax_x, ax_y = getindex.(getaxes.((x, y)), 1)
if reduce((accum, key) -> accum || (key in keys(ax_x)), keys(ax_y); init=false) || getaxes(x)[2:end] != getaxes(y)[2:end]
return vcat(getdata(x), getdata(y))
else
data_x, data_y = getdata.((x, y))
ax_y = reindex(ax_y, size(x,1))
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y))
return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...)), getaxes(x)[2:end]...)
end
end
Base.vcat(x::CV...) where {CV<:AdjOrTransComponentArray} = ComponentArray(reduce(vcat, map(y->getdata(y.parent)', x)), getaxes(x[1]))
Base.vcat(x::ComponentVector, args...) = vcat(getdata(x), getdata.(args)...)
Base.vcat(x::ComponentVector, args::Vararg{Union{Number, UniformScaling, AbstractVecOrMat}}) = vcat(getdata(x), getdata.(args)...)
Base.vcat(x::ComponentVector, args::Vararg{AbstractVector{T}, N}) where {T,N} = vcat(getdata(x), getdata.(args)...)
function Base.hvcat(row_lengths::NTuple{N,Int}, xs::Vararg{AbstractComponentVecOrMat}) where {N}
i = 1
idxs = UnitRange{Int}[]
for row_length in row_lengths
i_last = i + row_length - 1
push!(idxs, i:i_last)
i = i_last + 1
end
rows = [reduce(hcat, xs[idx]) for idx in idxs]
return vcat(rows...)
end
function Base.permutedims(x::ComponentArray, dims)
axs = getaxes(x)
return ComponentArray(permutedims(getdata(x), dims), map(i->axs[i], dims)...)
end
## Indexing
Base.IndexStyle(::Type{<:ComponentArray{T,N,<:A,<:Axes}}) where {T,N,A,Axes} = IndexStyle(A)
# Since we aren't really using the standard approach to indexing, this will forward things to
# the correct methods
Base.to_indices(x::ComponentArray, i::Tuple) = i
Base.to_indices(x::ComponentArray, i::NTuple{N,Union{Integer, CartesianIndex}}) where N = i
Base.to_indices(x::ComponentArray, i::NTuple{N,Int64}) where N = i
Base.to_index(x::ComponentArray, i) = i
# Get ComponentArray index
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::CartesianIndex) = getdata(x)[idx]
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::Vararg{FlatIdx}) = getdata(x)[idx...]
Base.@propagate_inbounds function Base.getindex(x::ComponentArray, idx::Vararg{FlatOrColonIdx})
axs = map((ax, i) -> getindex(ax, i).ax, getaxes(x), idx)
axs = remove_nulls(axs...)
return ComponentArray(getdata(x)[idx...], axs...)
end
Base.@propagate_inbounds Base.getindex(x::ComponentArray, ::Colon) = getdata(x)[:]
Base.@propagate_inbounds Base.getindex(x::ComponentArray, ::Colon, ::Vararg{Colon}) = x
@inline Base.getindex(x::ComponentArray, idx...) = getindex(x, toval.(idx)...)
@inline Base.getindex(x::ComponentArray, idx::Vararg{Val}) = _getindex(getindex, x, idx...)
# Set ComponentArray index
Base.@propagate_inbounds Base.setindex!(x::ComponentArray, v, idx::FlatOrColonIdx...) = setindex!(getdata(x), v, idx...)
Base.@propagate_inbounds Base.setindex!(x::ComponentArray, v, ::Colon) = setindex!(getdata(x), v, :)
@inline Base.setindex!(x::ComponentArray, v, idx...) = setindex!(x, v, toval.(idx)...)
@inline Base.setindex!(x::ComponentArray, v, idx::Vararg{Val}) = _setindex!(x, v, idx...)
# Explicitly view
Base.@propagate_inbounds Base.view(x::ComponentArray, idx::Vararg{ComponentArrays.FlatIdx}) = view(getdata(x), idx...)
Base.@propagate_inbounds Base.view(x::ComponentArray, idx...) = _getindex(view, x, toval.(idx)...)
Base.@propagate_inbounds Base.maybeview(x::ComponentArray, idx::Vararg{ComponentArrays.FlatIdx}) = Base.maybeview(getdata(x), idx...)
Base.@propagate_inbounds Base.maybeview(x::ComponentArray, idx...) = _getindex(Base.maybeview, x, toval.(idx)...)
# Generated get and set index methods to do all of the heavy lifting in the type domain
@generated function _getindex(index_fun, x::ComponentArray, idx...)
ci = getindex.(getaxes(x), getval.(idx))
inds = map(i -> i.idx, ci)
axs = map(i -> i.ax, ci)
axs = remove_nulls(axs...)
if isa(axs, Tuple{})
return :(index_fun(getdata(x), $inds...))
else
return :(ComponentArray(index_fun(getdata(x), $inds...), $axs...))
end
end
@generated function _setindex!(x::ComponentArray, v, idx...)
ci = getindex.(getaxes(x), getval.(idx))
inds = map(i -> i.idx, ci)
return :(setindex!(getdata(x), v, $inds...))
end
## Linear Algebra
Base.pointer(x::ComponentArray{T,N,A,Axes}) where {T,N,A<:DenseArray,Axes} = pointer(getdata(x))
Base.unsafe_convert(::Type{Ptr{T}}, x::ComponentArray{T,N,A,Axes}) where {T,N,A,Axes} = Base.unsafe_convert(Ptr{T}, getdata(x))
Base.strides(x::ComponentArray) = strides(getdata(x))
for f in [:device, :stride_rank, :contiguous_axis, :contiguous_batch_size, :dense_dims]
@eval StaticArrayInterface.$f(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = StaticArrayInterface.$f(A)
end
Base.stride(x::ComponentArray, k) = stride(getdata(x), k)
Base.stride(x::ComponentArray, k::Int64) = stride(getdata(x), k)
ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A