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