Skip to content

Commit 175d0b5

Browse files
author
Pietro Vertechi
authored
add more utility functions (JuliaArrays#26)
* add pair constructor * add StructVector * added empty and indexstyle * export StructVector * add columns colnames and ncols * test columns * test empty
1 parent 17bf7da commit 175d0b5

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/StructArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module StructArrays
22

33
import Requires
4-
export StructArray
4+
export StructArray, StructVector
55

66
include("interface.jl")
77
include("structarray.jl")

src/structarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ end
1919

2020
StructArray{T}(c::C) where {T, C<:Tuple} = StructArray{T}(NamedTuple{fields(T)}(c))
2121
StructArray{T}(c::C) where {T, C<:NamedTuple} = StructArray{T, length(size(c[1])), C}(c)
22+
StructArray{T}(c::C) where {T, C<:Pair} = StructArray{T}(Tuple(c))
2223
StructArray(c::C) where {C<:NamedTuple} = StructArray{eltypes(C)}(c)
2324
StructArray(c::C) where {C<:Tuple} = StructArray{eltypes(C)}(c)
25+
StructArray(c::Pair{P, Q}) where {P, Q} = StructArray{Pair{eltype(P), eltype(Q)}}(c)
2426

2527
StructArray{T}(; kwargs...) where {T} = StructArray{T}(values(kwargs))
2628
StructArray(; kwargs...) = StructArray(values(kwargs))
2729

2830
StructArray{T}(args...) where {T} = StructArray{T}(NamedTuple{fields(T)}(args))
2931

32+
const StructVector{T, C<:NamedTuple} = StructArray{T, 1, C}
33+
StructVector{T}(args...; kwargs...) where {T} = StructArray{T}(args...; kwargs...)
34+
StructVector(args...; kwargs...) = StructArray(args...; kwargs...)
35+
36+
Base.IndexStyle(::Type{StructArray{T, N, C}}) where {T, N, C} = Base.IndexStyle(gettypes(C).parameters[1])
37+
3038
_undef_array(::Type{T}, sz; unwrap = t -> false) where {T} = unwrap(T) ? StructArray{T}(undef, sz; unwrap = unwrap) : Array{T}(undef, sz)
3139

3240
_similar(v::AbstractArray, ::Type{Z}; unwrap = t -> false) where {Z} =
@@ -53,6 +61,12 @@ StructArray(s::StructArray) = copy(s)
5361
Base.convert(::Type{StructArray}, v::AbstractArray) = StructArray(v)
5462

5563
columns(s::StructArray) = getfield(s, :columns)
64+
columns(v::AbstractVector) = v
65+
ncols(v::AbstractVector) = 1
66+
ncols(v::StructArray{T, N, C}) where {T, N, C} = length(getnames(C))
67+
colnames(v::AbstractVector) = (1,)
68+
colnames(v::StructArray{T, N, C}) where {T, N, C} = getnames(C)
69+
5670
Base.getproperty(s::StructArray, key::Symbol) = getfield(columns(s), key)
5771
Base.getproperty(s::StructArray, key::Int) = getfield(columns(s), key)
5872
Base.propertynames(s::StructArray) = fieldnames(typeof(columns(s)))
@@ -106,6 +120,10 @@ function Base.resize!(s::StructArray, i::Integer)
106120
return s
107121
end
108122

123+
function Base.empty!(s::StructArray)
124+
foreachcolumn(empty!, s)
125+
end
126+
109127
for op in [:hcat, :vcat]
110128
@eval begin
111129
function Base.$op(args::StructArray...)

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ using Test
1212
@test (@inferred view(t, 2, 1:2)) == StructArray((a = view(a, 2, 1:2), b = view(b, 2, 1:2)))
1313
end
1414

15+
@testset "columns" begin
16+
t = StructArray(a = 1:10, b = rand(Bool, 10))
17+
@test StructArrays.ncols(t) == 2
18+
@test StructArrays.colnames(t) == (:a, :b)
19+
@test StructArrays.ncols(t.a) == 1
20+
@test StructArrays.colnames(t.a) == (1,)
21+
end
22+
23+
@testset "empty" begin
24+
s = StructVector(a = [1, 2, 3], b = ["a", "b", "c"])
25+
empty!(s)
26+
@test isempty(s.a)
27+
@test isempty(s.b)
28+
end
29+
1530
@testset "constructor from existing array" begin
1631
v = rand(ComplexF64, 5, 3)
1732
t = @inferred StructArray(v)

0 commit comments

Comments
 (0)