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