Skip to content

Commit 0c99241

Browse files
author
Pietro Vertechi
authored
add missing methods for indexedtables (#36)
* add convert method for StructVector * rename to default_array
1 parent 92d11c2 commit 0c99241

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/StructArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function __init__()
1818
end
1919
Requires.@require WeakRefStrings="ea10d353-3f73-51f8-a26c-33c1cb351aa5" begin
2020
isstringarray(::WeakRefStrings.StringArray) = true
21-
arrayof(::Type{T}, d) where {T<:AbstractString} = WeakRefStrings.StringArray{T}(d)
21+
default_array(::Type{T}, d) where {T<:Union{AbstractString, Missing}} = WeakRefStrings.StringArray{T}(d)
2222
end
2323
end
2424

src/collect.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
arrayof(S, d) = Array{S}(undef, d)
1+
default_array(::Type{S}, d) where {S} = Array{S}(undef, d)
2+
default_array(::Type{Missing}, d) = Array{Missing}(undef, d)
23

34
struct StructArrayInitializer{F, G}
45
unwrap::F
5-
arrayof::G
6+
default_array::G
67
end
7-
StructArrayInitializer(unwrap = t->false) = StructArrayInitializer(unwrap, arrayof)
8+
StructArrayInitializer(unwrap = t->false) = StructArrayInitializer(unwrap, default_array)
89

910
const default_initializer = StructArrayInitializer()
1011

1112
function (s::StructArrayInitializer)(S, d)
12-
ai = ArrayInitializer(s.unwrap, s.arrayof)
13+
ai = ArrayInitializer(s.unwrap, s.default_array)
1314
buildfromschema(typ -> ai(typ, d), S)
1415
end
1516

1617
struct ArrayInitializer{F, G}
1718
unwrap::F
18-
arrayof::G
19+
default_array::G
1920
end
20-
ArrayInitializer(unwrap = t->false) = ArrayInitializer(unwrap, arrayof)
21+
ArrayInitializer(unwrap = t->false) = ArrayInitializer(unwrap, default_array)
2122

22-
(s::ArrayInitializer)(S, d) = s.unwrap(S) ? buildfromschema(typ -> s(typ, d), S) : s.arrayof(S, d)
23+
(s::ArrayInitializer)(S, d) = s.unwrap(S) ? buildfromschema(typ -> s(typ, d), S) : s.default_array(S, d)
2324

2425
_reshape(v, itr, ::Base.HasShape) = reshape(v, axes(itr))
2526
_reshape(v, itr, ::Union{Base.HasLength, Base.SizeUnknown}) = v

src/structarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ StructArray(s::StructArray) = copy(s)
6161
Base.convert(::Type{StructArray}, v::AbstractArray) = StructArray(v)
6262
Base.convert(::Type{StructArray}, v::StructArray) = v
6363

64+
Base.convert(::Type{StructVector}, v::AbstractVector) = StructVector(v)
65+
Base.convert(::Type{StructVector}, v::StructVector) = v
66+
6467
function Base.similar(::Type{StructArray{T, N, C}}, sz::Dims) where {T, N, C}
6568
cols = map_params(typ -> similar(typ, sz), C)
6669
StructArray{T}(cols)

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ end
111111
t3 = StructArray(t)::StructArray
112112
@test t3 == t
113113
@test convert(StructArray, t) == t
114+
115+
v = rand(ComplexF64, 5)
116+
t = @inferred StructVector(v)
117+
@test t[2] == v[2]
118+
@test size(t) == (5,)
119+
@test t == convert(StructVector, v)
120+
@test t == convert(StructVector, t)
114121
end
115122

116123
@testset "tuple case" begin
@@ -237,6 +244,17 @@ StructArrays.SkipConstructor(::Type{<:S}) = true
237244
@test v[1].y isa Float64
238245
end
239246

247+
@testset "default_array" begin
248+
v = StructArrays.default_array(String, (2, 3))
249+
@test v isa WeakRefStrings.StringArray{String, 2}
250+
v = StructArrays.default_array(Union{String, Missing}, (2, 3))
251+
@test v isa WeakRefStrings.StringArray{Union{String, Missing}, 2}
252+
v = StructArrays.default_array(Missing, (2,))
253+
@test v isa Array{Missing, 1}
254+
v = StructArrays.default_array(Int, (2,))
255+
@test v isa Array{Int, 1}
256+
end
257+
240258
const initializer = StructArrays.ArrayInitializer(t -> t <: Union{Tuple, NamedTuple, Pair})
241259
collect_structarray_rec(t) = collect_structarray(t, initializer = initializer)
242260

0 commit comments

Comments
 (0)