Skip to content

Commit 9de08ea

Browse files
author
Pietro Vertechi
authored
Merge pull request JuliaArrays#1 from piever/pv/tests
append vcat tests
2 parents f877345 + c2b2739 commit 9de08ea

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This package introduces the type `StructureArray` which is an `AbstractArray` wh
77

88
`Base.getproperty` or the dot syntax can be used to access columns, whereas rows can be accessed with `getindex`.
99

10+
The package is largely inspired from the `Columns` type in [IndexedTables](https://github.com/JuliaComputing/IndexedTables.jl)
11+
1012
## Example usage to store complex numbers
1113

1214
```julia

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, append!, cat
4+
getindex, setindex!, size, push!, view, getproperty, append!, cat, vcat, hcat
55
# linearindexing, push!, size, sort, sort!, permute!, issorted, sortperm,
66
# summary, resize!, vcat, serialize, deserialize, append!, copy!, view
77

src/structurearray.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ end
7070
function cat(dims, args::StructureArray...)
7171
f = key -> cat(dims, (getproperty(t, key) for t in args)...)
7272
T = mapreduce(eltype, promote_type, args)
73-
#map(f, fields(eltype(args[1])))
7473
StructureArray{T}(map(f, fields(eltype(args[1]))))
7574
end
75+
76+
for op in [:hcat, :vcat]
77+
@eval begin
78+
function $op(args::StructureArray...)
79+
f = key -> $op((getproperty(t, key) for t in args)...)
80+
T = mapreduce(eltype, promote_type, args)
81+
StructureArray{T}(map(f, fields(eltype(args[1]))))
82+
end
83+
end
84+
end

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ end
1717
@test t[2,1:2] == StructureArray{ComplexF64}([3, 4], [6, 7])
1818
@test view(t, 2, 1:2) == StructureArray{ComplexF64}(view(a, 2, 1:2), view(b, 2, 1:2))
1919
end
20+
21+
@testset "concat" begin
22+
t = StructureArray{Pair}([3, 5], ["a", "b"])
23+
push!(t, (2 => "c"))
24+
@test t == StructureArray{Pair}([3, 5, 2], ["a", "b", "c"])
25+
append!(t, t)
26+
@test t == StructureArray{Pair}([3, 5, 2, 3, 5, 2], ["a", "b", "c", "a", "b", "c"])
27+
t = StructureArray{Pair}([3, 5], ["a", "b"])
28+
t2 = StructureArray{Pair}([1, 6], ["a", "b"])
29+
@test cat(1, t, t2) == StructureArray{Pair}([3, 5, 1, 6], ["a", "b", "a", "b"]) == vcat(t, t2)
30+
@test vcat(t, t2) isa StructureArray
31+
@test cat(2, t, t2) == StructureArray{Pair}([3 1; 5 6], ["a" "a"; "b" "b"]) == hcat(t, t2)
32+
@test hcat(t, t2) isa StructureArray
33+
end

0 commit comments

Comments
 (0)