Skip to content

Commit c12c912

Browse files
author
Shambles
authored
Support empty components (#162)
* no reduce * primitivetype * some changes * treat dict like nt * abstractdict * delete comment * simpler recursive_eltype * Update componentarray.jl * empty components * more tests
1 parent f095e15 commit c12c912

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/componentarray.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,11 @@ end
143143
make_carray_args(::Type{T}, nt) where {T} = make_carray_args(Vector{T}, nt)
144144
function make_carray_args(A::Type{<:AbstractArray}, nt)
145145
T = recursive_eltype(nt)
146-
init = _isbitstype(T) ? T[] : []
146+
init = isbitstype(T) ? T[] : []
147147
data, idx = make_idx(init, nt, 0)
148148
return (A(data), Axis(idx))
149149
end
150150

151-
_isbitstype(::Type{<:Union{T, Nothing, Missing}}) where {T} = isbitstype(T)
152-
_isbitstype(T) = isbitstype(T)
153-
154151
# Builds up data vector and returns appropriate AbstractAxis type for each input type
155152
function make_idx(data, nt::Union{NamedTuple, AbstractDict}, last_val)
156153
len = recursive_length(nt)
@@ -238,6 +235,11 @@ end
238235
last_index(x) = last(x)
239236
last_index(x::ViewAxis) = last_index(viewindex(x))
240237
last_index(x::AbstractAxis) = last_index(last(indexmap(x)))
238+
function last_index(f::FlatAxis)
239+
nt = indexmap(f)
240+
length(nt) == 0 && return 0
241+
return last_index(last(nt))
242+
end
241243

242244
# Reduce singleton dimensions
243245
remove_nulls() = ()

src/utils.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ 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+
recursive_length(nt::NamedTuple{(), Tuple{}}) = 0
4546

4647
# 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
48+
recursive_eltype(nt::NamedTuple) = isempty(nt) ? Base.Bottom : mapreduce(recursive_eltype, promote_type, nt)
49+
recursive_eltype(x::Vector{Any}) = isempty(x) ? Base.Bottom : mapreduce(recursive_eltype, promote_type, x)
50+
recursive_eltype(x::Dict) = isempty(x) ? Base.Bottom : mapreduce(recursive_eltype, promote_type, values(x))
51+
recursive_eltype(::AbstractArray{T,N}) where {T<:Number, N} = T
5152
recursive_eltype(x) = typeof(x)

test/runtests.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ end
112112
# Issue #116
113113
# Part 2: Arrays of arrays
114114
@test_throws Exception ComponentVector(a = [[3], [4, 5]], b = 1)
115+
116+
# empty components
117+
for T in [Int64, Int32, Float64, Float32, ComplexF64, ComplexF32]
118+
@test ComponentArray(a = T[]) == ComponentVector{T}(a = T[])
119+
@test ComponentArray(a = T[], b = T[]) == ComponentVector{T}(a = T[], b = T[])
120+
@test ComponentArray(a = T[], b = (;)) == ComponentVector{T}(a = T[], b = T[])
121+
@test ComponentArray(a = Any[one(Int32)], b=T[]) == ComponentVector{T}(a = [one(T)], b = T[])
122+
end
123+
@test ComponentArray(NamedTuple()) == ComponentVector{Any}()
124+
@test_broken ComponentArray(a=[])
115125
end
116126

117127
@testset "Attributes" begin
@@ -612,4 +622,4 @@ end
612622

613623
@testset "GPU" begin
614624
include("gpu_tests.jl")
615-
end
625+
end

0 commit comments

Comments
 (0)