|
| 1 | +# Tables traits for AbstractDiffEqArray |
| 2 | +Tables.istable(::Type{<:AbstractDiffEqArray}) = true |
| 3 | +Tables.rowaccess(::Type{<:AbstractDiffEqArray}) = true |
| 4 | +function Tables.rows(A::AbstractDiffEqArray) |
| 5 | + VT = eltype(A.u) |
| 6 | + if VT <: AbstractArray |
| 7 | + N = length(A.u[1]) |
| 8 | + names = [ |
| 9 | + :timestamp, |
| 10 | + (A.syms !== nothing ? (A.syms[i] for i in 1:N) : |
| 11 | + (Symbol("value", i) for i in 1:N))..., |
| 12 | + ] |
| 13 | + types = Type[eltype(A.t), (eltype(A.u[1]) for _ in 1:N)...] |
| 14 | + else |
| 15 | + names = [:timestamp, A.syms !== nothing ? A.syms[1] : :value] |
| 16 | + types = Type[eltype(A.t), VT] |
| 17 | + end |
| 18 | + return AbstractDiffEqArrayRows(names, types, A.t, A.u) |
| 19 | +end |
| 20 | + |
| 21 | +# Override fallback definitions for AbstractMatrix |
| 22 | +Tables.istable(::AbstractDiffEqArray) = true # Ref: https://github.com/JuliaData/Tables.jl/pull/198 |
| 23 | +Tables.columns(x::AbstractDiffEqArray) = Tables.columntable(Tables.rows(x)) |
| 24 | + |
| 25 | +# Iterator of Tables.AbstractRow rows |
| 26 | +struct AbstractDiffEqArrayRows{T, U} |
| 27 | + names::Vector{Symbol} |
| 28 | + types::Vector{Type} |
| 29 | + lookup::Dict{Symbol, Int} |
| 30 | + t::T |
| 31 | + u::U |
| 32 | +end |
| 33 | +function AbstractDiffEqArrayRows(names, types, t, u) |
| 34 | + AbstractDiffEqArrayRows(names, types, |
| 35 | + Dict(nm => i for (i, nm) in enumerate(names)), t, u) |
| 36 | +end |
| 37 | + |
| 38 | +Base.length(x::AbstractDiffEqArrayRows) = length(x.u) |
| 39 | +function Base.eltype(::Type{AbstractDiffEqArrayRows{T, U}}) where {T, U} |
| 40 | + AbstractDiffEqArrayRow{eltype(T), eltype(U)} |
| 41 | +end |
| 42 | +function Base.iterate(x::AbstractDiffEqArrayRows, (t_state, u_state)=(iterate(x.t), iterate(x.u))) |
| 43 | + t_state === nothing && return nothing |
| 44 | + u_state === nothing && return nothing |
| 45 | + t, _t_state = t_state |
| 46 | + u, _u_state = u_state |
| 47 | + st = (iterate(x.t, _t_state), iterate(x.u, _u_state)) |
| 48 | + return (AbstractDiffEqArrayRow(x.names, x.lookup, t, u), st) |
| 49 | +end |
| 50 | + |
| 51 | +Tables.istable(::Type{<:AbstractDiffEqArrayRows}) = true |
| 52 | +Tables.rowaccess(::Type{<:AbstractDiffEqArrayRows}) = true |
| 53 | +Tables.rows(x::AbstractDiffEqArrayRows) = x |
| 54 | +Tables.schema(x::AbstractDiffEqArrayRows) = Tables.Schema(x.names, x.types) |
| 55 | + |
| 56 | +# AbstractRow subtype |
| 57 | +struct AbstractDiffEqArrayRow{T, U} <: Tables.AbstractRow |
| 58 | + names::Vector{Symbol} |
| 59 | + lookup::Dict{Symbol, Int} |
| 60 | + t::T |
| 61 | + u::U |
| 62 | +end |
| 63 | + |
| 64 | +Tables.columnnames(x::AbstractDiffEqArrayRow) = getfield(x, :names) |
| 65 | +function Tables.getcolumn(x::AbstractDiffEqArrayRow, i::Int) |
| 66 | + i == 1 ? getfield(x, :t) : getfield(x, :u)[i - 1] |
| 67 | +end |
| 68 | +function Tables.getcolumn(x::AbstractDiffEqArrayRow, nm::Symbol) |
| 69 | + nm === :timestamp ? getfield(x, :t) : getfield(x, :u)[getfield(x, :lookup)[nm] - 1] |
| 70 | +end |
| 71 | + |
| 72 | +# Iterator interface for QueryVerse |
| 73 | +# (see also https://tables.juliadata.org/stable/#Tables.datavaluerows) |
| 74 | +IteratorInterfaceExtensions.isiterable(::AbstractDiffEqArray) = true |
| 75 | +function IteratorInterfaceExtensions.getiterator(A::AbstractDiffEqArray) |
| 76 | + Tables.datavaluerows(Tables.rows(A)) |
| 77 | +end |
0 commit comments