Skip to content

Commit 1971a1d

Browse files
author
Pietro Vertechi
committed
tests
1 parent 91202e3 commit 1971a1d

File tree

5 files changed

+74
-25
lines changed

5 files changed

+74
-25
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
language: julia
33
os:
44
- linux
5-
- osx
5+
#- osx
66
julia:
7-
- 0.6
7+
#- 0.6
88
- nightly
99
notifications:
1010
email: false

src/StructureArrays.jl

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

33
import Base:
4-
getindex, setindex!, size, push!, view, getproperty
4+
getindex, setindex!, size, push!, view, getproperty, append!, cat
55
# linearindexing, push!, size, sort, sort!, permute!, issorted, sortperm,
66
# summary, resize!, vcat, serialize, deserialize, append!, copy!, view
77

src/structurearray.jl

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,59 @@ struct StructureArray{T, N, C<:Tup} <: AbstractArray{T, N}
1717
end
1818
end
1919

20+
StructureArray{T}(c::C) where {T, C<:Tuple} = StructureArray{T}(NamedTuple{fields(T)}(c))
2021
StructureArray{T}(c::C) where {T, C<:NamedTuple} =
2122
StructureArray{createtype(T, eltypes(C)), length(size(c[1])), C}(c)
2223
StructureArray(c::C) where {C<:NamedTuple} = StructureArray{C}(c)
2324

2425
StructureArray{T}(args...) where {T} = StructureArray{T}(NamedTuple{fields(T)}(args))
2526

26-
27-
2827
columns(s::StructureArray) = getfield(s, :columns)
2928
getproperty(s::StructureArray, key::Symbol) = getfield(columns(s), key)
3029
getproperty(s::StructureArray, key::Int) = getfield(columns(s), key)
3130

3231
size(s::StructureArray) = size(columns(s)[1])
3332

34-
getindex(s::StructureArray, I...) = ith_all(s, I...)
33+
getindex(s::StructureArray, I::Int...) = get_ith(s, I...)
34+
function getindex(s::StructureArray{T, N, C}, I::Union{Int, AbstractArray, Colon}...) where {T, N, C}
35+
StructureArray{T}(map(v -> getindex(v, I...), columns(s)))
36+
end
37+
38+
function view(s::StructureArray{T, N, C}, I...) where {T, N, C}
39+
StructureArray{T}(map(v -> view(v, I...), columns(s)))
40+
end
41+
42+
setindex!(s::StructureArray, val, I::Int...) = set_ith!(s, val, I...)
43+
44+
fields(T) = fieldnames(T)
45+
fields(::Type{<:NamedTuple{K}}) where {K} = K
46+
fields(::Type{<:StructureArray{T}}) where {T} = fields(T)
47+
48+
@generated function push!(s::StructureArray{T, 1}, vals) where {T}
49+
args = []
50+
for key in fields(T)
51+
field = Expr(:., :s, Expr(:quote, key))
52+
val = Expr(:., :vals, Expr(:quote, key))
53+
push!(args, :(push!($field, $val)))
54+
end
55+
push!(args, :s)
56+
Expr(:block, args...)
57+
end
58+
59+
@generated function append!(s::StructureArray{T, 1}, vals) where {T}
60+
args = []
61+
for key in fields(T)
62+
field = Expr(:., :s, Expr(:quote, key))
63+
val = Expr(:., :vals, Expr(:quote, key))
64+
push!(args, :(append!($field, $val)))
65+
end
66+
push!(args, :s)
67+
Expr(:block, args...)
68+
end
69+
70+
function cat(dims, args::StructureArray...)
71+
f = key -> cat(dims, (getproperty(t, key) for t in args)...)
72+
T = mapreduce(eltype, promote_type, args)
73+
#map(f, fields(eltype(args[1])))
74+
StructureArray{T}(map(f, fields(eltype(args[1]))))
75+
end

src/utils.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ eltypes(::Type{Tuple{}}) = Tuple{}
44
eltypes(::Type{T}) where {T<:Tuple} =
55
tuple_type_cons(eltype(tuple_type_head(T)), eltypes(tuple_type_tail(T)))
66
eltypes(::Type{NamedTuple{K, V}}) where {K, V} = eltypes(V)
7-
fields(T) = fieldnames(T)
8-
fields(::Type{<:NamedTuple{K}}) where {K} = K
9-
#@inline ith_all(i, ::Tuple{}) = ()
10-
#@inline ith_all(i, as) = (as[1][i], ith_all(i, tail(as))...)
117

12-
@generated function ith_all(s::StructureArray{T}, I...) where {T}
8+
@generated function get_ith(s::StructureArray{T}, I...) where {T}
139
args = []
1410
for key in fields(T)
1511
field = Expr(:., :s, Expr(:quote, key))
@@ -18,6 +14,17 @@ fields(::Type{<:NamedTuple{K}}) where {K} = K
1814
Expr(:call, :createinstance, :T, args...)
1915
end
2016

17+
@generated function set_ith!(s::StructureArray{T}, vals, I...) where {T}
18+
args = []
19+
for key in fields(T)
20+
field = Expr(:., :s, Expr(:quote, key))
21+
val = Expr(:., :vals, Expr(:quote, key))
22+
push!(args, :($field[I...] = $val))
23+
end
24+
push!(args, :s)
25+
Expr(:block, args...)
26+
end
27+
2128
createinstance(::Type{T}, args...) where {T} = T(args...)
2229
createinstance(::Type{T}, args...) where {T<:Tup} = T(args)
2330

test/runtests.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ using StructureArrays
22
using Test
33

44
# write your own tests here
5-
t = StructureArray{typeof((a=1.2, b =2.2))}(rand(2,3), rand(2,3));
6-
t[2,2]
7-
# s = t
8-
# T = typeof(t)
9-
# i = 1
10-
# args = []
11-
# for key in fieldnames(T)
12-
# field = Expr(:., :n, Expr(:quote, key))
13-
# push!(args, :($field[i]))
14-
# end
15-
# Expr(:createinstance, :T, args...)
16-
StructureArrays.createtype(NamedTuple{(:a, :b)}, Tuple{Float64, Float64})
17-
s = StructureArray{NamedTuple{(:a, :b)}}([1,2], rand(2));
18-
s = StructureArray((a = rand(2), b = [1,2]))
5+
@testset "index" begin
6+
a, b = [1 2; 3 4], [4 5; 6 7]
7+
t = StructureArray((a = a, b = b))
8+
@test t[2,2] == (a = 4, b = 7)
9+
@test t[2,1:2] == StructureArray((a = [3, 4], b = [6, 7]))
10+
@test view(t, 2, 1:2) == StructureArray((a = view(a, 2, 1:2), b = view(b, 2, 1:2)))
11+
end
12+
13+
@testset "complex" begin
14+
a, b = [1 2; 3 4], [4 5; 6 7]
15+
t = StructureArray{ComplexF64}(a, b)
16+
@test t[2,2] == ComplexF64(4, 7)
17+
@test t[2,1:2] == StructureArray{ComplexF64}([3, 4], [6, 7])
18+
@test view(t, 2, 1:2) == StructureArray{ComplexF64}(view(a, 2, 1:2), view(b, 2, 1:2))
19+
end

0 commit comments

Comments
 (0)