20 #ifndef ESSENTIA_SCHEDULER_GRAPHUTILS_H
21 #define ESSENTIA_SCHEDULER_GRAPHUTILS_H
28 #include "../streaming/streamingalgorithm.h"
34 template <
typename NodeType>
38 std::stack<NodeType*> toVisit;
39 std::set<NodeType*> visited;
42 while (!toVisit.empty()) {
43 NodeType* currentNode = toVisit.top();
46 if (visited.find(currentNode) != visited.end())
continue;
47 visited.insert(currentNode);
49 nodeFunc(currentNode);
51 const std::vector<NodeType*>& children = currentNode->children();
56 for (
int i=0; i<(int)children.size(); i++) {
57 if (visited.find(children[i]) == visited.end()) {
58 toVisit.push(children[i]);
66 template <
typename NodeType,
typename MappedType>
68 MappedType (*mapFunc)(NodeType* node)) {
70 if (!root)
return std::vector<MappedType>();
72 std::vector<MappedType> result;
73 std::stack<NodeType*> toVisit;
74 std::set<NodeType*> visited;
77 while (!toVisit.empty()) {
78 NodeType* currentNode = toVisit.top();
81 if (visited.find(currentNode) != visited.end())
continue;
82 visited.insert(currentNode);
84 result.push_back(mapFunc(currentNode));
86 const std::vector<NodeType*>& children = currentNode->children();
91 for (
int i=0; i<(int)children.size(); i++) {
92 if (visited.find(children[i]) == visited.end()) {
93 toVisit.push(children[i]);
101 template <
typename NodeType>
106 template <
typename NodeType>
112 std::string::size_type idpos = std::min(name.find(
'<'), name.find(
'['));
113 if (idpos == std::string::npos)
return name;
114 return name.substr(0, idpos);
125 template <
typename T,
typename U>
128 bool operator()(
const std::pair<T, U>& p1,
const std::pair<T, U>& p2)
const {
129 if (p1.second < p2.second)
return true;
130 if (p1.second > p2.second)
return false;
131 return p1.first < p2.first;
135 template <
typename NodeType>
137 std::vector<std::vector<bool> >& adjMatrix) {
138 int nnodes = nodes.size();
139 adjMatrix = std::vector<std::vector<bool> >(nnodes, std::vector<bool>(nnodes,
false));
141 for (
int i=0; i<nnodes; i++) {
142 NodeType* start = nodes[i].first;
143 const std::vector<NodeType*>& children = start->children();
144 for (
int j=0; j<(int)children.size(); j++) {
145 for (
int k=0; k<nnodes; k++) {
146 if (children[j] == nodes[k].first) adjMatrix[i][k] =
true;
153 template <
typename NodeType>
155 const std::vector<std::pair<NodeType*, std::string> >& nodes) {
156 int size = adjMatrix.size();
157 for (
int i=0; i<size; i++) {
158 for (
int j=0; j<size; j++) {
162 for (
int j=0; j<size; j++) {
169 template <
typename NodeType>
185 E_DEBUG(
EGraph,
" n1.size() = " << nodes1.size() <<
" - n2.size() = " << nodes2.size());
187 if (nodes1.size() != nodes2.size())
return false;
188 int nnodes = nodes1.size();
197 for (
int i=0; i<nnodes; i++) {
198 if (nodes1[i].second != nodes2[i].second)
return false;
203 std::vector<std::pair<int, int> > same;
206 while (idx < (nnodes-1)) {
207 if (nodes1[idx].second != nodes1[idx+1].second) { idx++;
continue; }
210 const std::string& name = nodes1[idx].second;
212 while (idx < nnodes && nodes1[idx].second == name) idx++;
213 same.push_back(std::make_pair(start_idx, idx));
217 for (
int i=0; i<(int)same.size(); i++) {
218 std::sort(nodes1.begin() + same[i].first,
219 nodes1.begin() + same[i].second);
222 std::vector<std::vector<bool> > adjMatrix1;
223 std::vector<std::vector<bool> > adjMatrix2;
241 if (adjMatrix1 == adjMatrix2)
return true;
245 bool nextgroup =
true;
247 if (permidx == (
int)same.size())
return false;
249 nextgroup = !std::next_permutation(nodes1.begin() + same[permidx].first,
250 nodes1.begin() + same[permidx].second);
const std::string & name() const
Definition: configurable.h:48
const streaming::Algorithm * algorithm() const
Definition: network.h:64
Definition: graphutils.h:126
bool operator()(const std::pair< T, U > &p1, const std::pair< T, U > &p2) const
Definition: graphutils.h:128
Definition: streamingalgorithm.h:140
#define E_DEBUG(module, msg)
Definition: debugging.h:157
#define E_DEBUG_NONL(module, msg)
Definition: debugging.h:156
void adjacencyMatrix(const std::vector< std::pair< NodeType *, std::string > > &nodes, std::vector< std::vector< bool > > &adjMatrix)
Definition: graphutils.h:136
streaming::Algorithm * returnAlgorithm(NetworkNode *node)
Definition: graphutils.h:121
std::vector< MappedType > depthFirstMap(NodeType *root, MappedType(*mapFunc)(NodeType *node))
Definition: graphutils.h:67
std::vector< NodeType * > depthFirstSearch(NodeType *root)
Definition: graphutils.h:107
std::pair< NetworkNode *, std::string > getIdentityAndName(NetworkNode *node)
Definition: graphutils.h:117
void printAdjacencyMatrix(const std::vector< std::vector< bool > > &adjMatrix, const std::vector< std::pair< NodeType *, std::string > > &nodes)
Definition: graphutils.h:154
bool areNetworkTopologiesEqual(NodeType *n1, NodeType *n2)
Definition: graphutils.h:170
std::string removeNodeIdFromName(const std::string &name)
Definition: graphutils.h:111
void depthFirstApply(NodeType *root, void(*nodeFunc)(NodeType *node))
Definition: graphutils.h:35
NodeType * returnIdentity(NodeType *node)
Definition: graphutils.h:102
Definition: algorithm.h:28
@ EGraph
Definition: debugging.h:46