YARP
Yet Another Robot Platform
graph.h
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 #ifndef YARP_MANAGER_GRAPH
7 #define YARP_MANAGER_GRAPH
8 
9 #include <map>
10 #include <string>
11 #include <iterator>
12 
13 #include <yarp/manager/ymm-types.h>
14 #include <yarp/manager/node.h>
15 
16 
17 namespace yarp {
18 namespace manager {
19 
20 typedef std::map<std::string, Node*> NodePContainer;
21 typedef std::map<std::string, Node*>::iterator NodePIterator;
22 
23 class GraphIterator;
24 
28 class Graph {
29 
30 public:
31  Graph();
32  virtual ~Graph();
33 
34  int getSize() { return nodes.size(); }
35  Node* getNodeAt(int index);
36 
37  Node* addNode(Node* node);
38  bool removeNode(Node* node);
39  bool removeNode(const char* szLabel);
40  bool addLink(Node* first, Node* second,
41  float weight, bool _virtual=false);
42  bool addLink(const char* szFirst, const char* szSecond,
43  float weight, bool _virtual=false);
44  bool removeLink(Node* first, Node* second);
45  bool removeLink(const char* szFirst, const char* szSecond);
46  void clear();
47  void setSatisfied(bool sat);
48  void setVisited(bool vis);
49  bool hasNode(Node* node);
50  bool hasNode(const char* szLabel);
51  Node* getNode( const char* szLabel);
54 
55 protected:
56 
57 private:
58  NodePContainer nodes;
59  NodePIterator findNode(Node* node);
60 };
61 
62 
66 class GraphIterator: public std::iterator<std::input_iterator_tag, Node*>
67 {
68 public:
69  GraphIterator() = default;
70  virtual ~GraphIterator() = default;
71  GraphIterator& operator++() {++itr;return *this;}
72  GraphIterator operator++(int) {GraphIterator tmp(*this); operator++(); return tmp;}
73  bool operator==(const GraphIterator& rhs) const {return itr==rhs.itr;}
74  bool operator!=(const GraphIterator& rhs) const {return itr!=rhs.itr;}
75  Node*& operator*() {return (*itr).second;}
76  friend class Graph;
77 
78 private:
79  NodePIterator itr;
80 };
81 
82 
83 #define PRINT_GRAPH(g)\
84  cout<<"Graph "<<#g<<" with "<<g.getSize()<<" nodes:"<<endl;\
85  cout<<"{"<<endl;\
86  for(GraphIterator itr=g.begin(); itr!=g.end(); itr++)\
87  {\
88  Node* n = (*itr);\
89  cout<<" "<<n->getLabel()<<": [";\
90  for(int j=0; j<n->sucCount(); j++)\
91  {\
92  Link l = n->getLinkAt(j);\
93  cout<<l.to()->getLabel()<<"("<<l.weight()<<"), ";\
94  }\
95  cout<<"]"<<endl;\
96  }\
97  cout<<"}"<<endl;
98 
99 
100 /* if( ! *((bool*)list_get_at(&l.marks, i)) )\
101  printf("\033[31m%s\033[0m, ", ((vertex*)list_get_at(&l.nodes, i))->label);\
102  else\
103  printf("%s, ", ((vertex*)list_get_at(&l.nodes, i))->label);\
104 */
105 
106 } // namespace yarp
107 } // namespace manager
108 
109 
110 #endif // __YARP_MANAGER_GRAPH__
Class GraphIterator.
Definition: graph.h:67
GraphIterator operator++(int)
Definition: graph.h:72
GraphIterator & operator++()
Definition: graph.h:71
virtual ~GraphIterator()=default
bool operator!=(const GraphIterator &rhs) const
Definition: graph.h:74
bool operator==(const GraphIterator &rhs) const
Definition: graph.h:73
Class Graph.
Definition: graph.h:28
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
std::map< std::string, Node * > NodePContainer
Definition: graph.h:20
std::map< std::string, Node * >::iterator NodePIterator
Definition: graph.h:21
The main, catch-all namespace for YARP.
Definition: dirs.h:16