Skip to content

Commit

Permalink
New function group_by_element_type
Browse files Browse the repository at this point in the history
Function groups a list of elements to several lists, where each
list contains elements of particular type.
  • Loading branch information
ahojukka5 committed Aug 18, 2017
1 parent 115b621 commit c8c53e3
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/elements.jl
Expand Up @@ -56,6 +56,24 @@ function filter_by_element_type(element_type, elements)
return filter(element -> is_element_type(element, element_type), elements)
end

"""
group_by_element_type(elements::Vector{Element})
Given a vector of elements, group elements by element type to several vectors.
Returns a dictionary, where key is the element type and value is a vector
containing all elements of type `element_type`.
"""
function group_by_element_type(elements::Vector{Element})
results = Dict{DataType, Any}()
basis_types = map(element -> typeof(element.properties), elements)
for basis in unique(basis_types)
element_type = Element{basis}
subset = filter(element -> isa(element, element_type), elements)
results[element_type] = convert(Vector{element_type}, subset)
end
return results
end

function setindex!(element::Element, data::Function, field_name)
if method_exists(data, Tuple{Element, Vector, Float64})
# create enclosure to pass element as argument
Expand Down

0 comments on commit c8c53e3

Please sign in to comment.