YARP
Yet Another Robot Platform
graph.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/manager/graph.h>
7 
8 using namespace yarp::manager;
9 
10 Graph::Graph() = default;
11 
13 {
14  clear();
15 }
16 
18 {
19  //__CHECK_NULLPTR(_node);
20  if (!_node) {
21  return nullptr;
22  }
23  if (hasNode(_node)) {
24  return nullptr;
25  }
26 
27  Node* node = _node->clone();
28  nodes[node->getLabel()] = node;
29  return node;
30 }
31 
33 {
34  __CHECK_NULLPTR(node);
35 
36  auto itr = nodes.find(node->getLabel());
37  if (itr == nodes.end()) {
38  return true;
39  }
40  delete (*itr).second;
41  nodes.erase(itr);
42  return true;
43 }
44 
45 
46 bool Graph::removeNode(const char* szLabel)
47 {
48  auto itr = nodes.find(szLabel);
49  if (itr == nodes.end()) {
50  return true;
51  }
52  delete (*itr).second;
53  nodes.erase(itr);
54  return true;
55 }
56 
58 {
59  NodePIterator itr;
60  for (itr = nodes.begin(); itr != nodes.end(); itr++) {
61  delete ((*itr).second);
62  }
63  nodes.clear();
64 }
65 
66 void Graph::setSatisfied(bool sat)
67 {
68  NodePIterator itr;
69  for (itr = nodes.begin(); itr != nodes.end(); itr++) {
70  ((*itr).second)->setSatisfied(sat);
71  }
72 }
73 
74 void Graph::setVisited(bool vis)
75 {
76  NodePIterator itr;
77  for (itr = nodes.begin(); itr != nodes.end(); itr++) {
78  ((*itr).second)->setVisited(vis);
79  }
80 }
81 
82 Node* Graph::getNode( const char* szLabel)
83 {
84  auto itr = nodes.find(szLabel);
85  if (itr != nodes.end()) {
86  return (*itr).second;
87  }
88  return nullptr;
89 }
90 
91 bool Graph::addLink(Node* first, Node* second,
92  float weight, bool _virtual)
93 {
94  __CHECK_NULLPTR(first);
95  __CHECK_NULLPTR(second);
96 
97  first->addSuc(second, weight, _virtual);
98  return true;
99 }
100 
101 bool Graph::addLink(const char* szFirst, const char* szSecond,
102  float weight, bool _virtual)
103 {
104  Node* first = getNode(szFirst);
105  Node* second = getNode(szSecond);
106  __CHECK_NULLPTR(first);
107  __CHECK_NULLPTR(second);
108 
109  first->addSuc(second, weight, _virtual);
110  return true;
111 }
112 
113 
114 bool Graph::removeLink(Node* first, Node* second)
115 {
116  __CHECK_NULLPTR(first);
117  __CHECK_NULLPTR(second);
118 
119  first->removeSuc(second);
120  return true;
121 }
122 
123 bool Graph::removeLink(const char* szFirst, const char* szSecond)
124 {
125  Node* first = getNode(szFirst);
126  Node* second = getNode(szSecond);
127  __CHECK_NULLPTR(first);
128  __CHECK_NULLPTR(second);
129 
130  first->removeSuc(second);
131  return true;
132 }
133 
134 
135 bool Graph::hasNode(Node* node)
136 {
137  __CHECK_NULLPTR(node);
138 
139  auto itr = nodes.find(node->getLabel());
140  if (itr == nodes.end()) {
141  return false;
142  }
143  return true;
144 }
145 
146 bool Graph::hasNode(const char* szLabel)
147 {
148  if (getNode(szLabel)) {
149  return true;
150  }
151  return false;
152 }
153 
154 
156 {
157  auto itr = nodes.begin();
158  for (int i = 0; i < index; i++) {
159  itr++;
160  }
161  return (*itr).second;
162 }
163 
164 
166 {
167  GraphIterator itr;
168  itr.itr = nodes.begin();
169  return itr;
170 }
171 
173 {
174  GraphIterator itr;
175  itr.itr = nodes.end();
176  return itr;
177 }
178 
179 NodePIterator Graph::findNode(Node* node)
180 {
181  NodePIterator itr;
182  for (itr = nodes.begin(); itr != nodes.end(); itr++) {
183  if ((*itr).second == node) {
184  return itr;
185  }
186  }
187  return nodes.end();
188 }
Class GraphIterator.
Definition: graph.h:67
GraphIterator begin()
Definition: graph.cpp:165
Node * addNode(Node *node)
Definition: graph.cpp:17
bool removeNode(Node *node)
Definition: graph.cpp:32
Node * getNode(const char *szLabel)
Definition: graph.cpp:82
bool hasNode(Node *node)
Definition: graph.cpp:135
bool removeLink(Node *first, Node *second)
Definition: graph.cpp:114
virtual ~Graph()
Definition: graph.cpp:12
bool addLink(Node *first, Node *second, float weight, bool _virtual=false)
Definition: graph.cpp:91
GraphIterator end()
Definition: graph.cpp:172
Node * getNodeAt(int index)
Definition: graph.cpp:155
void setVisited(bool vis)
Definition: graph.cpp:74
void setSatisfied(bool sat)
Definition: graph.cpp:66
a Node of a Graph
Definition: node.h:65
bool addSuc(Node *node, float weight, bool _virtual=false)
class Node
Definition: node.cpp:15
bool removeSuc(Node *node)
Definition: node.cpp:28
const char * getLabel()
Definition: node.h:94
virtual Node * clone()=0
std::map< std::string, Node * >::iterator NodePIterator
Definition: graph.h:21
#define __CHECK_NULLPTR(_ptr)
Definition: ymm-types.h:80