Skip to content

Commit b577022

Browse files
author
Pietro Vertechi
committed
rename
1 parent 9de08ea commit b577022

9 files changed

+121
-121
lines changed

Diff for: .travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ git:
2727

2828
## uncomment the following lines to override the default test script
2929
#script:
30-
# - julia -e 'Pkg.clone(pwd()); Pkg.build("StructureArrays"); Pkg.test("StructureArrays"; coverage=true)'
30+
# - julia -e 'Pkg.clone(pwd()); Pkg.build("StructArrays"); Pkg.test("StructArrays"; coverage=true)'
3131
after_success:
3232
# push coverage results to Coveralls
33-
- julia -e 'cd(Pkg.dir("StructureArrays")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
33+
- julia -e 'cd(Pkg.dir("StructArrays")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
3434
# push coverage results to Codecov
35-
- julia -e 'cd(Pkg.dir("StructureArrays")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
35+
- julia -e 'cd(Pkg.dir("StructArrays")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

Diff for: LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The StructureArrays.jl package is licensed under the MIT "Expat" License:
1+
The StructArrays.jl package is licensed under the MIT "Expat" License:
22

33
> Copyright (c) 2018: Pietro Vertechi.
44
>

Diff for: README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# StructureArrays
1+
# StructArrays
22

3-
[![Build Status](https://travis-ci.org/piever/StructureArrays.jl.svg?branch=master)](https://travis-ci.org/piever/StructureArrays.jl)
4-
[![codecov.io](http://codecov.io/github/piever/StructureArrays.jl/coverage.svg?branch=master)](http://codecov.io/github/piever/StructureArrays.jl?branch=master)
3+
[![Build Status](https://travis-ci.org/piever/StructArrays.jl.svg?branch=master)](https://travis-ci.org/piever/StructArrays.jl)
4+
[![codecov.io](http://codecov.io/github/piever/StructArrays.jl/coverage.svg?branch=master)](http://codecov.io/github/piever/StructArrays.jl?branch=master)
55

6-
This package introduces the type `StructureArray` which is an `AbstractArray` whose elements are `struct` (for example `NamedTuples`, or `ComplexF64`, or a custom user defined `struct`). While a `StructureArray` iterates `structs`, the layout is column based (meaning each field of the `struct` is stored in a seprate `Array`).
6+
This package introduces the type `StructArray` which is an `AbstractArray` whose elements are `struct` (for example `NamedTuples`, or `ComplexF64`, or a custom user defined `struct`). While a `StructArray` iterates `structs`, the layout is column based (meaning each field of the `struct` is stored in a seprate `Array`).
77

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

@@ -12,12 +12,12 @@ The package is largely inspired from the `Columns` type in [IndexedTables](https
1212
## Example usage to store complex numbers
1313

1414
```julia
15-
julia> using StructureArrays, Random
15+
julia> using StructArrays, Random
1616

1717
julia> srand(4);
1818

19-
julia> s = StructureArray{ComplexF64}(rand(2,2), rand(2,2))
20-
2×2 StructureArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
19+
julia> s = StructArray{ComplexF64}(rand(2,2), rand(2,2))
20+
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
2121
0.680079+0.625239im 0.92407+0.267358im
2222
0.874437+0.737254im 0.929336+0.804478im
2323

@@ -33,8 +33,8 @@ julia> s.re
3333
## Example usage to store a data table
3434

3535
```julia
36-
julia> t = StructureArray((a = [1, 2], b = ["x", "y"]))
37-
2-element StructureArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
36+
julia> t = StructArray((a = [1, 2], b = ["x", "y"]))
37+
2-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
3838
(a = 1, b = "x")
3939
(a = 2, b = "y")
4040

@@ -47,7 +47,7 @@ julia> t.a
4747
2
4848

4949
julia> push!(t, (a = 3, b = "z"))
50-
3-element StructureArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
50+
3-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
5151
(a = 1, b = "x")
5252
(a = 2, b = "y")
5353
(a = 3, b = "z")

Diff for: appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ build_script:
4141
# Need to convert from shallow to complete for Pkg.clone to work
4242
- IF EXIST .git\shallow (git fetch --unshallow)
4343
- C:\projects\julia\bin\julia -e "versioninfo();
44-
Pkg.clone(pwd(), \"StructureArrays\"); Pkg.build(\"StructureArrays\")"
44+
Pkg.clone(pwd(), \"StructArrays\"); Pkg.build(\"StructArrays\")"
4545

4646
test_script:
47-
- C:\projects\julia\bin\julia -e "Pkg.test(\"StructureArrays\")"
47+
- C:\projects\julia\bin\julia -e "Pkg.test(\"StructArrays\")"
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
module StructureArrays
1+
module StructArrays
22

33
import Base:
44
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

8-
export StructureArray
8+
export StructArray
99

1010
const Tup = Union{Tuple, NamedTuple}
1111

12-
include("structurearray.jl")
12+
include("structarray.jl")
1313
include("utils.jl")
1414

1515
end # module

Diff for: src/structarray.jl

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
A type that stores an array of structures as a structure of arrays.
3+
# Fields:
4+
- `columns`: a tuple of arrays. Also `columns(x)`
5+
"""
6+
struct StructArray{T, N, C<:Tup} <: AbstractArray{T, N}
7+
columns::C
8+
9+
function StructArray{T, N, C}(c) where {T, N, C<:Tup}
10+
length(c) > 0 || error("must have at least one column")
11+
n = size(c[1])
12+
length(n) == N || error("wrong number of dimensions")
13+
for i = 2:length(c)
14+
size(c[i]) == n || error("all columns must have same size")
15+
end
16+
new{T, N, C}(c)
17+
end
18+
end
19+
20+
StructArray{T}(c::C) where {T, C<:Tuple} = StructArray{T}(NamedTuple{fields(T)}(c))
21+
StructArray{T}(c::C) where {T, C<:NamedTuple} =
22+
StructArray{createtype(T, eltypes(C)), length(size(c[1])), C}(c)
23+
StructArray(c::C) where {C<:NamedTuple} = StructArray{C}(c)
24+
25+
StructArray{T}(args...) where {T} = StructArray{T}(NamedTuple{fields(T)}(args))
26+
27+
columns(s::StructArray) = getfield(s, :columns)
28+
getproperty(s::StructArray, key::Symbol) = getfield(columns(s), key)
29+
getproperty(s::StructArray, key::Int) = getfield(columns(s), key)
30+
31+
size(s::StructArray) = size(columns(s)[1])
32+
33+
getindex(s::StructArray, I::Int...) = get_ith(s, I...)
34+
function getindex(s::StructArray{T, N, C}, I::Union{Int, AbstractArray, Colon}...) where {T, N, C}
35+
StructArray{T}(map(v -> getindex(v, I...), columns(s)))
36+
end
37+
38+
function view(s::StructArray{T, N, C}, I...) where {T, N, C}
39+
StructArray{T}(map(v -> view(v, I...), columns(s)))
40+
end
41+
42+
setindex!(s::StructArray, 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{<:StructArray{T}}) where {T} = fields(T)
47+
48+
@generated function push!(s::StructArray{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::StructArray{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::StructArray...)
71+
f = key -> cat(dims, (getproperty(t, key) for t in args)...)
72+
T = mapreduce(eltype, promote_type, args)
73+
StructArray{T}(map(f, fields(eltype(args[1]))))
74+
end
75+
76+
for op in [:hcat, :vcat]
77+
@eval begin
78+
function $op(args::StructArray...)
79+
f = key -> $op((getproperty(t, key) for t in args)...)
80+
T = mapreduce(eltype, promote_type, args)
81+
StructArray{T}(map(f, fields(eltype(args[1]))))
82+
end
83+
end
84+
end

Diff for: src/structurearray.jl

-84
This file was deleted.

Diff for: src/utils.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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)
77

8-
@generated function get_ith(s::StructureArray{T}, I...) where {T}
8+
@generated function get_ith(s::StructArray{T}, I...) where {T}
99
args = []
1010
for key in fields(T)
1111
field = Expr(:., :s, Expr(:quote, key))
@@ -14,7 +14,7 @@ eltypes(::Type{NamedTuple{K, V}}) where {K, V} = eltypes(V)
1414
Expr(:call, :createinstance, :T, args...)
1515
end
1616

17-
@generated function set_ith!(s::StructureArray{T}, vals, I...) where {T}
17+
@generated function set_ith!(s::StructArray{T}, vals, I...) where {T}
1818
args = []
1919
for key in fields(T)
2020
field = Expr(:., :s, Expr(:quote, key))

Diff for: test/runtests.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
using StructureArrays
1+
using StructArrays
22
using Test
33

44
# write your own tests here
55
@testset "index" begin
66
a, b = [1 2; 3 4], [4 5; 6 7]
7-
t = StructureArray((a = a, b = b))
7+
t = StructArray((a = a, b = b))
88
@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)))
9+
@test t[2,1:2] == StructArray((a = [3, 4], b = [6, 7]))
10+
@test view(t, 2, 1:2) == StructArray((a = view(a, 2, 1:2), b = view(b, 2, 1:2)))
1111
end
1212

1313
@testset "complex" begin
1414
a, b = [1 2; 3 4], [4 5; 6 7]
15-
t = StructureArray{ComplexF64}(a, b)
15+
t = StructArray{ComplexF64}(a, b)
1616
@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))
17+
@test t[2,1:2] == StructArray{ComplexF64}([3, 4], [6, 7])
18+
@test view(t, 2, 1:2) == StructArray{ComplexF64}(view(a, 2, 1:2), view(b, 2, 1:2))
1919
end
2020

2121
@testset "concat" begin
22-
t = StructureArray{Pair}([3, 5], ["a", "b"])
22+
t = StructArray{Pair}([3, 5], ["a", "b"])
2323
push!(t, (2 => "c"))
24-
@test t == StructureArray{Pair}([3, 5, 2], ["a", "b", "c"])
24+
@test t == StructArray{Pair}([3, 5, 2], ["a", "b", "c"])
2525
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
26+
@test t == StructArray{Pair}([3, 5, 2, 3, 5, 2], ["a", "b", "c", "a", "b", "c"])
27+
t = StructArray{Pair}([3, 5], ["a", "b"])
28+
t2 = StructArray{Pair}([1, 6], ["a", "b"])
29+
@test cat(1, t, t2) == StructArray{Pair}([3, 5, 1, 6], ["a", "b", "a", "b"]) == vcat(t, t2)
30+
@test vcat(t, t2) isa StructArray
31+
@test cat(2, t, t2) == StructArray{Pair}([3 1; 5 6], ["a" "a"; "b" "b"]) == hcat(t, t2)
32+
@test hcat(t, t2) isa StructArray
3333
end

0 commit comments

Comments
 (0)