Skip to content

Commit f175c28

Browse files
author
Shambles
authored
way faster constructor (#157)
* no reduce * primitivetype * some changes * treat dict like nt * abstractdict * delete comment * simpler recursive_eltype * Update componentarray.jl
1 parent f3a1b6d commit f175c28

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/componentarray.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ end
6060
# Entry from NamedTuple, Dict, or kwargs
6161
ComponentArray{T}(nt::NamedTuple) where T = ComponentArray(make_carray_args(T, nt)...)
6262
ComponentArray{T}(::NamedTuple{(), Tuple{}}) where T = ComponentArray(T[], (FlatAxis(),))
63-
ComponentArray(nt::NamedTuple) = ComponentArray(make_carray_args(nt)...)
63+
ComponentArray(nt::Union{NamedTuple, AbstractDict}) = ComponentArray(make_carray_args(nt)...)
6464
ComponentArray(::NamedTuple{(), Tuple{}}) = ComponentArray(Any[], (FlatAxis(),))
65-
ComponentArray(d::AbstractDict) = ComponentArray(NamedTuple{Tuple(keys(d))}(values(d)))
6665
ComponentArray{T}(;kwargs...) where T = ComponentArray{T}((;kwargs...))
6766
ComponentArray(;kwargs...) = ComponentArray((;kwargs...))
6867

@@ -138,17 +137,22 @@ make_carray_args(::NamedTuple{(), Tuple{}}) = (Any[], FlatAxis())
138137
make_carray_args(::Type{T}, ::NamedTuple{(), Tuple{}}) where {T} = (T[], FlatAxis())
139138
function make_carray_args(nt)
140139
data, ax = make_carray_args(Vector, nt)
141-
data = length(data)==1 ? [data[1]] : reduce(vcat, data)
140+
data = length(data)==1 ? [data[1]] : map(identity, data)
142141
return (data, ax)
143142
end
144143
make_carray_args(::Type{T}, nt) where {T} = make_carray_args(Vector{T}, nt)
145144
function make_carray_args(A::Type{<:AbstractArray}, nt)
146-
data, idx = make_idx([], nt, 0)
145+
T = recursive_eltype(nt)
146+
init = _isbitstype(T) ? T[] : []
147+
data, idx = make_idx(init, nt, 0)
147148
return (A(data), Axis(idx))
148149
end
149150

151+
_isbitstype(::Type{<:Union{T, Nothing, Missing}}) where {T} = isbitstype(T)
152+
_isbitstype(T) = isbitstype(T)
153+
150154
# Builds up data vector and returns appropriate AbstractAxis type for each input type
151-
function make_idx(data, nt::NamedTuple, last_val)
155+
function make_idx(data, nt::Union{NamedTuple, AbstractDict}, last_val)
152156
len = recursive_length(nt)
153157
kvs = []
154158
lv = 0
@@ -325,4 +329,4 @@ julia> sum(prod(ca[k]) for k in valkeys(ca))
325329
k = Val.(keys(idxmap))
326330
return :($k)
327331
end
328-
valkeys(ca::ComponentVector) = valkeys(getaxes(ca)[1])
332+
valkeys(ca::ComponentVector) = valkeys(getaxes(ca)[1])

src/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ recursive_length(a::AbstractArray{T,N}) where {T<:Number,N} = length(a)
4242
recursive_length(a::AbstractArray) = recursive_length.(a) |> sum
4343
recursive_length(nt::NamedTuple) = values(nt) .|> recursive_length |> sum
4444
recursive_length(::Union{Nothing, Missing}) = 1
45+
46+
# Find the highest element type
47+
recursive_eltype(nt::NamedTuple) = mapreduce(recursive_eltype, promote_type, nt)
48+
recursive_eltype(x::Vector) = mapreduce(recursive_eltype, promote_type, x)
49+
recursive_eltype(x::Dict) = mapreduce(recursive_eltype, promote_type, values(x))
50+
recursive_eltype(::AbstractArray{T,N}) where {T<:Number, N}= T
51+
recursive_eltype(x) = typeof(x)

0 commit comments

Comments
 (0)