From 0f89f3cb0ec15849d026e159e746df053703abe1 Mon Sep 17 00:00:00 2001 From: Damien Drix Date: Tue, 6 Oct 2015 19:34:50 +0200 Subject: [PATCH 1/3] dict access to arrays --- src/StructsOfArrays.jl | 9 +++++++++ test/runtests.jl | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/src/StructsOfArrays.jl b/src/StructsOfArrays.jl index 12c2751..3f6847e 100644 --- a/src/StructsOfArrays.jl +++ b/src/StructsOfArrays.jl @@ -46,4 +46,13 @@ end x end end + +function Base.getindex{T}(A::StructOfArrays{T}, s::Symbol) + idx = findfirst(fieldnames(T), s) + if (idx == 0) + throw(KeyError(s)) + end + A.arrays[idx] +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index a03e03a..4d29840 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,3 +31,7 @@ small = StructOfArrays(Complex64, 2) @test typeof(similar(small, SubString)) === Vector{SubString} @test typeof(similar(small, OneField)) === Vector{OneField} @test typeof(similar(small, Complex128)) <: StructOfArrays + +fields = StructOfArrays(OneField, 2) +@test typeof(fields[:x]) === Vector{Int} +@test length(fields[:x]) == 2 \ No newline at end of file From a51d7cab5bd1553de21f2e6c0526d61eaf92ef0c Mon Sep 17 00:00:00 2001 From: Damien Drix Date: Tue, 6 Oct 2015 19:40:49 +0200 Subject: [PATCH 2/3] fixed newline --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4d29840..597ed24 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -34,4 +34,4 @@ small = StructOfArrays(Complex64, 2) fields = StructOfArrays(OneField, 2) @test typeof(fields[:x]) === Vector{Int} -@test length(fields[:x]) == 2 \ No newline at end of file +@test length(fields[:x]) == 2 From d4fc7c241669259c0f04542003817fbce8e1de0f Mon Sep 17 00:00:00 2001 From: Damien Drix Date: Tue, 6 Oct 2015 19:54:29 +0200 Subject: [PATCH 3/3] added setter --- src/StructsOfArrays.jl | 8 ++++++++ test/runtests.jl | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/StructsOfArrays.jl b/src/StructsOfArrays.jl index 3f6847e..c42b048 100644 --- a/src/StructsOfArrays.jl +++ b/src/StructsOfArrays.jl @@ -55,4 +55,12 @@ function Base.getindex{T}(A::StructOfArrays{T}, s::Symbol) A.arrays[idx] end +function Base.setindex!{T}(A::StructOfArrays{T}, x, s::Symbol) + idx = findfirst(fieldnames(T), s) + if (idx == 0) + throw(KeyError(s)) + end + A.arrays[idx][:] = x +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 597ed24..4bddb44 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,3 +35,6 @@ small = StructOfArrays(Complex64, 2) fields = StructOfArrays(OneField, 2) @test typeof(fields[:x]) === Vector{Int} @test length(fields[:x]) == 2 + +fields[:x] = [1, 2] +@test fields[:x] == [1, 2]