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