|
1 | 1 | # StructureArrays
|
| 2 | + |
| 3 | +[](https://travis-ci.org/piever/StructureArrays.jl) |
| 4 | +[](http://codecov.io/github/piever/StructureArrays.jl?branch=master) |
| 5 | + |
| 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`). |
| 7 | + |
| 8 | +`Base.getproperty` or the dot syntax can be used to access columns, whereas rows can be accessed with `getindex`. |
| 9 | + |
| 10 | +## Example usage to store complex numbers |
| 11 | + |
| 12 | +```julia |
| 13 | +julia> using StructureArrays, Random |
| 14 | + |
| 15 | +julia> srand(4); |
| 16 | + |
| 17 | +julia> s = StructureArray{ComplexF64}(rand(2,2), rand(2,2)) |
| 18 | +2×2 StructureArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}: |
| 19 | + 0.680079+0.625239im 0.92407+0.267358im |
| 20 | + 0.874437+0.737254im 0.929336+0.804478im |
| 21 | + |
| 22 | +julia> s[1, 1] |
| 23 | +0.680079235935741 + 0.6252391193298537im |
| 24 | + |
| 25 | +julia> s.re |
| 26 | +2×2 Array{Float64,2}: |
| 27 | + 0.680079 0.92407 |
| 28 | + 0.874437 0.929336 |
| 29 | +``` |
| 30 | + |
| 31 | +## Example usage to store a data table |
| 32 | + |
| 33 | +```julia |
| 34 | +julia> t = StructureArray((a = [1, 2], b = ["x", "y"])) |
| 35 | +2-element StructureArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}: |
| 36 | + (a = 1, b = "x") |
| 37 | + (a = 2, b = "y") |
| 38 | + |
| 39 | +julia> t[1] |
| 40 | +(a = 1, b = "x") |
| 41 | + |
| 42 | +julia> t.a |
| 43 | +2-element Array{Int64,1}: |
| 44 | + 1 |
| 45 | + 2 |
| 46 | + |
| 47 | +julia> push!(t, (a = 3, b = "z")) |
| 48 | +3-element StructureArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}: |
| 49 | + (a = 1, b = "x") |
| 50 | + (a = 2, b = "y") |
| 51 | + (a = 3, b = "z") |
| 52 | +``` |
| 53 | + |
| 54 | +## Lightweight package |
| 55 | + |
| 56 | +This package aims to be extremely lightweight: so far it has 0 dependencies. One of the reasons to keep it so is to promote its use as a building block for table manipulation packages. |
| 57 | + |
| 58 | +## Warning |
| 59 | + |
| 60 | +The package is still pretty much under development and available only on Julia 0.7 (as it uses `NamedTuples` extensively). |
0 commit comments