Skip to content

Commit

Permalink
Give SingletonVector{<:NamedTuple} the Tables.jl interface
Browse files Browse the repository at this point in the history
This improves performance with TypedTables.
  • Loading branch information
tkf committed Nov 5, 2019
1 parent 92a6b8f commit 0eb3142
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/NoBang/NoBang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Base.Iterators: Pairs
using Base: ImmutableDict
using Requires
using ConstructionBase: constructorof, setproperties
using Tables: Tables

using ..BangBang: push!!, ismutable

Expand Down
9 changes: 9 additions & 0 deletions src/NoBang/singletoncontainers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ function Base.getindex(v::SingletonVector, i::Integer)
return v.value
end

# Define table interface as a `SingletonVector{<:NamedTuple}`:
Tables.istable(::Type{<:SingletonVector{<:NamedTuple{names}}}) where {names} = @isdefined(names)
Tables.rowaccess(::Type{<:SingletonVector{names}}) where {names} = @isdefined(names)
Tables.columnaccess(::Type{<:SingletonVector{names}}) where {names} = @isdefined(names)

Tables.rows(x::SingletonVector{<:NamedTuple}) = [x.value]
Tables.columns(x::SingletonVector{<:NamedTuple{names}}) where names =
NamedTuple{names}(map(x -> [x], Tuple(x.value)))


struct SingletonDict{K, V} <: AbstractDict{K, V}
key::K
Expand Down
4 changes: 4 additions & 0 deletions test/test_typedtables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ end
@test_broken tints == Table(a = [1, 3], b = [2, 4])
end

@testset "type inference" begin
@test (@inferred singletonof(Table, (a=1,))) == Table((a=[1]))
end

end # module

0 comments on commit 0eb3142

Please sign in to comment.