Skip to content

Commit 339eb18

Browse files
author
Pietro Vertechi
authored
tuple cleanup (JuliaArrays#15)
* Tuple cleanup * reallow tuples * tests
1 parent 2707893 commit 339eb18

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/StructArrays.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module StructArrays
22

33
export StructArray
44

5-
const Tup = Union{Tuple, NamedTuple}
6-
75
include("structarray.jl")
86
include("utils.jl")
97

src/structarray.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
22
A type that stores an array of structures as a structure of arrays.
33
# Fields:
4-
- `columns`: a tuple of arrays. Also `columns(x)`
4+
- `columns`: a named tuple of arrays. Also `columns(x)`
55
"""
6-
struct StructArray{T, N, C<:Tup} <: AbstractArray{T, N}
6+
struct StructArray{T, N, C<:NamedTuple} <: AbstractArray{T, N}
77
columns::C
88

9-
function StructArray{T, N, C}(c) where {T, N, C<:Tup}
9+
function StructArray{T, N, C}(c) where {T, N, C<:NamedTuple}
1010
length(c) > 0 || error("must have at least one column")
1111
n = size(c[1])
1212
length(n) == N || error("wrong number of dimensions")
@@ -21,6 +21,7 @@ StructArray{T}(c::C) where {T, C<:Tuple} = StructArray{T}(NamedTuple{fields(T)}(
2121
StructArray{T}(c::C) where {T, C<:NamedTuple} =
2222
StructArray{createtype(T, eltypes(C)), length(size(c[1])), C}(c)
2323
StructArray(c::C) where {C<:NamedTuple} = StructArray{C}(c)
24+
StructArray(c::C) where {C<:Tuple} = StructArray{eltypes(C)}(c)
2425

2526
StructArray{T}(; kwargs...) where {T} = StructArray{T}(values(kwargs))
2627
StructArray(; kwargs...) = StructArray(values(kwargs))
@@ -73,6 +74,10 @@ fields(::Type{<:StructArray{T}}) where {T} = fields(T)
7374
@generated function fields(t::Type{T}) where {T}
7475
return :($(Expr(:tuple, [QuoteNode(f) for f in fieldnames(T)]...)))
7576
end
77+
@generated function fields(t::Type{T}) where {T<:Tuple}
78+
return :($(Expr(:tuple, [QuoteNode(Symbol("x$f")) for f in fieldnames(T)]...)))
79+
end
80+
7681

7782
@generated function Base.push!(s::StructArray{T, 1}, vals) where {T}
7883
args = []

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232
end
3333

3434
createinstance(::Type{T}, args...) where {T} = T(args...)
35-
createinstance(::Type{T}, args...) where {T<:Tup} = T(args)
35+
createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args)
3636

3737
createtype(::Type{T}, ::Type{C}) where {T<:NamedTuple{N}, C} where {N} = NamedTuple{N, C}
3838
createtype(::Type{T}, ::Type{C}) where {T, C} = T

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ end
2222
@test t3 == t
2323
end
2424

25+
@testset "tuple case" begin
26+
s = StructArray(([1], ["test"],))
27+
@test s[1] == (1, "test")
28+
@test Base.getproperty(s, 1) == [1]
29+
@test Base.getproperty(s, 2) == ["test"]
30+
t = StructArray{Tuple{Int, Float64}}([1], [1.2])
31+
@test t[1] == (1, 1.2)
32+
end
33+
2534
@testset "kwargs constructor" begin
2635
a = [1.2]
2736
b = [2.3]

0 commit comments

Comments
 (0)