1
module DataTables
2

3
import TableShowUtils, TableTraitsUtils, ReadOnlyArrays
4

5
using DataValues
6

7
export DataTable, NA, isna
8

9
struct DataTable{T, TCOLS} <: AbstractVector{T}
10 18
    columns::TCOLS
11
end
12

13
function fromNT(nt)
14 18
    nt = map(i->i isa ReadOnlyArrays.ReadOnlyArray ? i : ReadOnlyArrays.ReadOnlyArray(i), nt)
15
    tx = typeof(nt)
16 18
    et = NamedTuple{propertynames(nt), Tuple{(eltype(fieldtype(tx, i)) for i in 1:fieldcount(typeof(nt)))...}}
17 18
    return DataTable{et, typeof(nt)}(nt)
18
end
19

20 0
swap_dva_in(A) = A
21 0
swap_dva_in(A::Array{<:DataValue}) = DataValueArray(A)
22

23
function DataTable(;cols...)
24 18
    return fromNT(map(col -> swap_dva_in(col), values(cols)))
25
end
26

27
function DataTable(table)
28 18
    cols, colnames = TableTraitsUtils.create_columns_from_iterabletable(table)
29

30 18
    return fromNT(NamedTuple{tuple(colnames...)}(tuple(cols...)))
31
end
32

33 18
columns(dt::DataTable) = getfield(dt, :columns)
34

35 18
@inline Base.getproperty(dt::DataTable, name::Symbol) = getproperty(columns(dt), name)
36

37 15
Base.size(dt::DataTable) = size(columns(dt)[1])
38

39 18
Base.IndexStyle(::Type{T}) where {T<:DataTable}= Base.IndexLinear()
40

41
function Base.checkbounds(::Type{Bool}, dt::DataTable, i)
42 15
    cols = columns(dt)
43 0
    if length(cols)==0
44 0
        return true
45
    else
46 15
        return checkbounds(Bool, cols[1], i)
47
    end
48
end
49

50
@inline function Base.getindex(dt::DataTable{T}, i::Int) where {T}
51 18
    @boundscheck checkbounds(dt, i)
52 18
    return map(col -> @inbounds(getindex(col, i)), columns(dt))
53
end
54

55
function Base.show(io::IO, dt::DataTable)
56 18
    TableShowUtils.printtable(io, dt, "DataTable")
57
end
58

59
function Base.show(io::IO, ::MIME"text/plain", dt::DataTable)
60 18
    TableShowUtils.printtable(io, dt, "DataTable")
61
end
62

63
function Base.show(io::IO, ::MIME"text/html", dt::DataTable)
64 18
    TableShowUtils.printHTMLtable(io, dt)
65
end
66

67 18
Base.showable(::MIME"text/html", dt::DataTable) = true
68

69
function Base.show(io::IO, ::MIME"application/vnd.dataresource+json", dt::DataTable)
70 18
    TableShowUtils.printdataresource(io, dt)
71
end
72

73 18
Base.showable(::MIME"application/vnd.dataresource+json", dt::DataTable) = true
74

75
end

Read our documentation on viewing source code .

Loading