Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/StructsOfArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@ 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

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
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ 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

fields[:x] = [1, 2]
@test fields[:x] == [1, 2]