Skip to content

Commit e785ad8

Browse files
author
Pietro Vertechi
authored
Merge pull request #72 from piever/pv/API
API changes pre 1.0
2 parents de3af16 + af25366 commit e785ad8

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

src/collect.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function grow_to_structarray!(dest::AbstractArray, itr, elem = iterate(itr))
110110
end
111111

112112
function to_structarray(::Type{T}, nt::C) where {T, C<:Tup}
113-
S = createtype(T, eltypes(C))
113+
S = add_params(T, eltypes(astuple(C)))
114114
return StructArray{S}(nt)
115115
end
116116

@@ -122,7 +122,7 @@ function widenstructarray(dest::StructArray{T}, i, ::Type{S}) where {T, S}
122122
types = ntuple(i -> fieldtype(sch, i), fieldcount(sch))
123123
cols = fieldarrays(dest)
124124
if names == propertynames(cols)
125-
nt = map((a, b) -> widenstructarray(a, i, b), cols, strip_types(sch)(types))
125+
nt = map((a, b) -> widenstructarray(a, i, b), cols, strip_params(sch)(types))
126126
return to_structarray(T, nt)
127127
else
128128
return widenarray(dest, i, S)

src/interface.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ end
99

1010
staticschema(::Type{T}) where {T<:Tup} = T
1111

12-
tuple_type(::Type{NamedTuple{names, types}}) where {names, types} = types
13-
tuple_type(::Type{T}) where {T<:Tuple} = T
12+
astuple(::Type{NamedTuple{names, types}}) where {names, types} = types
13+
astuple(::Type{T}) where {T<:Tuple} = T
1414

1515
function fields(::Type{T}) where {T}
1616
fieldnames(staticschema(T))
1717
end
1818

19-
strip_types(::Type{<:Tuple}) = Tuple
20-
strip_types(::Type{<:NamedTuple{names}}) where {names} = NamedTuple{names}
19+
strip_params(::Type{<:Tuple}) = Tuple
20+
strip_params(::Type{<:NamedTuple{names}}) where {names} = NamedTuple{names}

src/structarray.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ end
2121
_dims(c::Tup) = length(axes(c[1]))
2222
_dims(c::EmptyTup) = 1
2323

24-
function StructArray{T}(c::C) where {T, C<:Union{Tup, Pair}}
25-
cols = strip_types(staticschema(T))(Tuple(c))
24+
function StructArray{T}(c::C) where {T, C<:Tup}
25+
cols = strip_params(staticschema(T))(c)
2626
StructArray{T, _dims(cols), typeof(cols)}(cols)
2727
end
2828

2929
StructArray(c::C) where {C<:NamedTuple} = StructArray{eltypes(C)}(c)
3030
StructArray(c::Tuple; names = nothing) = _structarray(c, names)
31-
StructArray(c::Pair{P, Q}) where {P, Q} = StructArray{Pair{eltype(P), eltype(Q)}}(c)
3231

3332
StructArray{T}(; kwargs...) where {T} = StructArray{T}(values(kwargs))
3433
StructArray(; kwargs...) = StructArray(values(kwargs))

src/utils.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ end
2525

2626
Base.@pure SkipConstructor(::Type) = false
2727

28-
@inline getfieldindex(v::Tuple, field, index) = getfield(v, index)
29-
@inline getfieldindex(v, field, index) = getproperty(v, field)
28+
@static if VERSION < v"1.2.0"
29+
@inline _getproperty(v::Tuple, field) = getfield(v, field)
30+
@inline _getproperty(v, field) = getproperty(v, field)
31+
else
32+
const _getproperty = getproperty
33+
end
3034

3135
function _foreachfield(names, xs)
3236
exprs = Expr[]
33-
for (ind, field) in enumerate(names)
37+
for field in names
3438
sym = QuoteNode(field)
35-
args = [Expr(:call, :getfieldindex, :(getfield(xs, $j)), sym, ind) for j in 1:length(xs)]
39+
args = [Expr(:call, :_getproperty, :(getfield(xs, $j)), sym) for j in 1:length(xs)]
3640
push!(exprs, Expr(:call, :f, args...))
3741
end
3842
push!(exprs, :(return nothing))
@@ -57,20 +61,18 @@ createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args)
5761
Expr(:block, new_tup, construct)
5862
end
5963

60-
createtype(::Type{T}, ::Type{C}) where {T<:Tup, C<:Tup} = C
61-
function createtype(::Type{<:Pair}, ::Type{C}) where {C<:Tup}
62-
tp = tuple_type(C).parameters
63-
Pair{tp[1], tp[2]}
64-
end
65-
createtype(::Type{T}, ::Type{C}) where {T, C<:Tup} = T
64+
add_params(::Type{T}, ::Type{C}) where {T, C<:Tuple} = T
65+
add_params(::Type{T}, ::Type{C}) where {T<:Tuple, C<:Tuple} = C
66+
add_params(::Type{<:NamedTuple{names}}, ::Type{C}) where {names, C<:Tuple} = NamedTuple{names, C}
67+
add_params(::Type{<:Pair}, ::Type{Tuple{S, T}}) where {S, T} = Pair{S, T}
6668

6769
"""
6870
`iscompatible(::Type{S}, ::Type{V}) where {S, V<:AbstractArray}`
6971
7072
Check whether element type `S` can be pushed to a container of type `V`.
7173
"""
7274
iscompatible(::Type{S}, ::Type{<:AbstractArray{T}}) where {S, T} = S<:T
73-
iscompatible(::Type{S}, ::Type{StructArray{T, N, C}}) where {S, T, N, C} = iscompatible(tuple_type(staticschema(S)), tuple_type(C))
75+
iscompatible(::Type{S}, ::Type{StructArray{T, N, C}}) where {S, T, N, C} = iscompatible(astuple(staticschema(S)), astuple(C))
7476

7577
iscompatible(::Type{Tuple{}}, ::Type{T}) where {T<:Tuple} = false
7678
iscompatible(::Type{T}, ::Type{Tuple{}}) where {T<:Tuple} = false

test/runtests.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,34 +420,36 @@ end
420420
@test isequal(StructArrays.fieldarrays(t)[1], [1, missing, 3])
421421
end
422422

423+
pair_structarray((first, last)) = StructArray{Pair{eltype(first), eltype(last)}}((first, last))
424+
423425
@testset "collectpairs" begin
424426
v = (i=>i+1 for i in 1:3)
425-
@test collect_structarray_rec(v) == StructArray{Pair{Int, Int}}(([1,2,3], [2,3,4]))
427+
@test collect_structarray_rec(v) == pair_structarray([1,2,3] => [2,3,4])
426428
@test eltype(collect_structarray_rec(v)) == Pair{Int, Int}
427429

428430
v = (i == 1 ? (1.2 => i+1) : (i => i+1) for i in 1:3)
429-
@test collect_structarray_rec(v) == StructArray{Pair{Real, Int}}(([1.2,2,3], [2,3,4]))
431+
@test collect_structarray_rec(v) == pair_structarray([1.2,2,3] => [2,3,4])
430432
@test eltype(collect_structarray_rec(v)) == Pair{Real, Int}
431433

432434
v = ((a=i,) => (b="a$i",) for i in 1:3)
433-
@test collect_structarray_rec(v) == StructArray(StructArray((a = [1,2,3],)) => StructArray((b = ["a1","a2","a3"],)))
435+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = [1,2,3],)) => StructArray((b = ["a1","a2","a3"],)))
434436
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int64}}, NamedTuple{(:b,), Tuple{String}}}
435437

436438
v = (i == 1 ? (a="1",) => (b="a$i",) : (a=i,) => (b="a$i",) for i in 1:3)
437-
@test collect_structarray_rec(v) == StructArray(StructArray((a = ["1",2,3],)) => StructArray((b = ["a1","a2","a3"],)))
439+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = ["1",2,3],)) => StructArray((b = ["a1","a2","a3"],)))
438440
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Any}}, NamedTuple{(:b,), Tuple{String}}}
439441

440442
# empty
441443
v = ((a=i,) => (b="a$i",) for i in 0:-1)
442-
@test collect_structarray_rec(v) == StructArray(StructArray((a = Int[],)) => StructArray((b = String[],)))
444+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = Int[],)) => StructArray((b = String[],)))
443445
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int}}, NamedTuple{(:b,), Tuple{String}}}
444446

445447
v = Iterators.filter(t -> t.first.a == 4, ((a=i,) => (b="a$i",) for i in 1:3))
446-
@test collect_structarray_rec(v) == StructArray(StructArray((a = Int[],)) => StructArray((b = String[],)))
448+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = Int[],)) => StructArray((b = String[],)))
447449
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int}}, NamedTuple{(:b,), Tuple{String}}}
448450

449451
t = collect_structarray_rec((b = 1,) => (a = i,) for i in (2, missing, 3))
450-
s = StructArray(StructArray(b = [1,1,1]) => StructArray(a = [2, missing, 3]))
452+
s = pair_structarray(StructArray(b = [1,1,1]) => StructArray(a = [2, missing, 3]))
451453
@test s[1] == t[1]
452454
@test ismissing(t[2].second.a)
453455
@test s[3] == t[3]

0 commit comments

Comments
 (0)