Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Functions
Utopia::GraphUtils Namespace Reference

Functions

template<typename Graph , typename Iter , typename Rule >
void apply_async (Iter it_begin, Iter it_end, Graph &&g, Rule &&rule)
 Apply a rule asynchronously.
 
template<typename Iter , typename Graph , typename Rule >
void apply_sync (Iter it_begin, Iter it_end, Graph &&g, Rule &&rule)
 Apply a rule synchronously.
 
template<IterateOver iterate_over, typename Graph >
decltype(autoiterator_pair (const Graph &g)
 Get an iterator pair over selected graph entities.
 
template<IterateOver iterate_over, typename Graph , typename EntityDesc >
decltype(autoiterator_pair (EntityDesc e, const Graph &g)
 Get an iterator pair over selected graph entities.
 

Function Documentation

◆ apply_async()

template<typename Graph , typename Iter , typename Rule >
void Utopia::GraphUtils::apply_async ( Iter  it_begin,
Iter  it_end,
Graph &&  g,
Rule &&  rule 
)

Apply a rule asynchronously.

This helper function applies a rule to a range of entities that is given through an iterator pair one after the other.

Template Parameters
IterThe iterator type
GraphThe graph type
RuleThe rule type
Parameters
it_beginThe begin of the graph entity iterator range.
it_endThe end of the graph entity iterator range.
gThe graph
ruleThe rule function to be applied to each element within the iterator range.
40{
41 // Determine whether the lambda function returns a void
42 // The lambda function can either be called with a vertex descriptor or
43 // and edge descriptor. Therefore, both cases need to be accounted for
44 using Desc = typename std::iterator_traits<Iter>::value_type;
45 using ReturnType = typename std::invoke_result_t<Rule, Desc, Graph>;
46 constexpr bool lambda_returns_void = std::is_same_v<ReturnType, void>;
47
48 // Apply the rule to each element
49 if constexpr (lambda_returns_void){
50 std::for_each(it_begin, it_end,
51 [&rule, &g](auto g_entity){
52 rule(g_entity, g);
53 });
54 }
55 else {
56 std::for_each(it_begin, it_end, [&rule, &g](auto g_entity){
57 g[g_entity].state = rule(g_entity, g);
58 });
59 }
60}
void for_each(const Utopia::ExecPolicy policy, InputIt first, InputIt last, UnaryFunction f)
Apply a function to a range.
Definition parallel.hh:346

◆ apply_sync()

template<typename Iter , typename Graph , typename Rule >
void Utopia::GraphUtils::apply_sync ( Iter  it_begin,
Iter  it_end,
Graph &&  g,
Rule &&  rule 
)

Apply a rule synchronously.

This helper function applies a rule to a range of entities that is given through an iterator pair. A state cache is created that stores the returned states of the rule function. After the rule was applied to each graph entity within the iterator range the cached states are moved to the actual states of the graph entities, thus, updating their states synchronously.

Template Parameters
IterThe iterator type
GraphThe graph type
RuleThe rule type
Parameters
it_beginThe begin of the graph entity iterator range.
it_endThe end of the graph entity iterator range.
gThe graph
ruleThe rule function to be applied to each element within the iterator range.
Warning
Be careful to not operate directly on the state of a graph entity within the rule function. Rather, first create a copy of the state and return the copied and changed state at the end of the function.
86{
87 // initialize the state cache
88 std::vector<decltype(g[*it_begin].state)> state_cache;
89 state_cache.reserve(std::distance(it_begin, it_end));
90
91 // apply the rule
92 for (auto entity : boost::make_iterator_range(it_begin, it_end)){
93 state_cache.push_back(rule(entity, g));
94 }
95
96 // move the cache
97 auto counter = 0u;
98 for (auto entity : boost::make_iterator_range(it_begin, it_end)){
99 g[entity].state = std::move(state_cache[counter]);
100
101 ++counter;
102 }
103}

◆ iterator_pair() [1/2]

template<IterateOver iterate_over, typename Graph >
decltype(auto) Utopia::GraphUtils::iterator_pair ( const Graph &  g)

Get an iterator pair over selected graph entities.

Template Parameters
iterate_overSpecify over which graph entities to iterate Valid options:
GraphThe graph type
Parameters
gThe graph
Returns
decltype(auto) The iterator pair
68 {
69 using namespace boost;
70
71 if constexpr (iterate_over == IterateOver::vertices){
72 return vertices(g);
73 }
74 else if constexpr (iterate_over == IterateOver::edges){
75 return edges(g);
76 }
77 else {
78 static_assert((iterate_over == IterateOver::vertices)
79 or (iterate_over == IterateOver::edges),
80 "Only the iterator pairs of vertices and edges can be"
81 "returned: iterate_over is set wrong!");
82 }
83}
@ vertices
Iterate over vertices.
@ edges
Iterate over edges.

◆ iterator_pair() [2/2]

template<IterateOver iterate_over, typename Graph , typename EntityDesc >
decltype(auto) Utopia::GraphUtils::iterator_pair ( EntityDesc  e,
const Graph &  g 
)

Get an iterator pair over selected graph entities.

This function returns the iterator pair wrt. another graph entity. For example iteration over neighbors (adjacent_vertices) needs a references vertex.

Template Parameters
iterate_overSpecify over which graph entities to iterate over Valid options:
GraphThe graph type
EntityDescThe graph entity descriptor that is the reference point for the iteration.
Parameters
eThe graph entity that serves as reference
gThe graph
Returns
decltype(auto) The iterator pair
107 {
108 using namespace boost;
109
110 if constexpr (iterate_over == IterateOver::neighbors){
111 return adjacent_vertices(e, g);
112 }
113 else if constexpr (iterate_over == IterateOver::inv_neighbors){
114 return inv_adjacent_vertices(e, g);
115 }
116 else if constexpr (iterate_over == IterateOver::in_edges){
117 return in_edges(e, g);
118 }
119 else if constexpr (iterate_over == IterateOver::out_edges){
120 return out_edges(e, g);
121 }
122 else {
123 static_assert((iterate_over == IterateOver::neighbors)
124 or (iterate_over == IterateOver::inv_neighbors)
125 or (iterate_over == IterateOver::in_edges)
126 or (iterate_over == IterateOver::out_edges),
127 "Only the iterator pairs of neighbors, inv_neighbors, "
128 "in_edge, and out_edge can be returned: "
129 "iterate_over is set wrong!");
130 }
131}
@ out_edges
Iterate over the out edges of a vertex.