DiaDes  0.1
DIAgnosis of Discrete-Event System
Edge.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__EDGE__HH
2 #define __DIADES__GRAPH__EDGE__HH
3 
4 #include <sstream>
5 #include <list>
6 #include <boost/functional/hash.hpp>
10 #include <diades/graph/EdgeData.hh>
11 #include <diades/graph/NodeData.hh>
12 
13 
14 namespace Diades
15 {
16  namespace Graph
17  {
18  using std::list;
19  using std::ostream;
20  using std::stringstream;
21 
22  class Node;
23 
35  class Edge
36  {
37  public:
38 
39  static string typeName()
40  {
41  return "Graph::Edge";
42  }
44 
46 
47  public:
50 
56  Edge() = default;
57 
62  Edge(const Edge & edge) =default;
63 
68  Edge(EdgeData * data) : _eData(data)
69  {
70  require(Exception, data != nullptr,
71  "Edge: there is no data (nullptr)");
72  require(Exception, data != nullptr,
73  "Edge: there is no data (nullptr)");
74  }
75 
79  ~Edge() = default;
80 
81 
82 
88  {
89  return _eData;
90  }
91 
96  Edge & operator=(const Edge & edge) = default;
97 
98 
104  Edge& operator=(Edge && other) = default;
105 
106 
108 
110 
115  Node source() const;
116 
121  Node target() const;
122 
123 
129  const Node & sourceRef() const;
130 
136  const Node & targetRef() const;
137 
138 
139 
140 
144  bool valid() const;
145 
150  bool operator==(const Edge & edge) const
151  {
152  return _eData == edge._eData;
153  }
154 
159  bool operator!=(const Edge & edge) const
160  {
161  return !(*this == edge);
162  }
163 
167  bool operator<(const Edge & edge) const;
168 
169  bool operator>(const Edge & edge) const
170  {
171  return ( (*this != edge) && !(*this < edge));
172  }
173 
177  EdgeId id() const
178  {
179  require(Exception, valid(),
180  "id(): invalid edge");
181  return _eData->_identifier;
182  }
183 
184 
189  const Graph & owner() const;
190 
194  int memoryUsage() const
195  {
196  return sizeof (Edge);
197  }
198 
199 
201 
202  private:
203 
205 
210  list<Edge>::iterator sourcePos() const
211  {
212  require(Exception, _eData != nullptr, "Edge::sourcePos: _eData is null");
213  return _eData->_sourcePos;
214  }
215 
220  list<Edge>::iterator targetPos() const
221  {
222  require(Exception, _eData != nullptr, "Edge::targetPos: _eData is null");
223  return _eData->_targetPos;
224  }
225 
226 
236  void create(EdgeId idt, NodeData * s, NodeData * t);
237 
241  void destroy()
242  {
243  require(Exception, valid(), "Edge::destroy: invalid edge");
244  _eData->_target->delInEdge(_eData);
245  _eData->_source->delOutEdge(_eData);
246  delete _eData;
247  _eData = 0;
248  ensure(Exception, !valid(), "Edge::destroy: valid edge");
249  }
250 
255  void changeSource(NodeData * s);
256 
257 
262  void changeTarget(NodeData * t);
263 
265  friend class Graph;
267  friend class Node;
268 
271 
275  friend ostream & operator<<(ostream & os, const Edge & edge)
276  {
277  if (edge.valid())
278  {
279  os << edge.id();
280  } else
281  {
282  os << "null";
283  }
284  return os;
285  }
287  };
288 
294  inline list<Edge>::iterator NodeData::newInEdge(EdgeData * eData)
295  {
296  _inEdges.push_back(Edge(eData));
297  _inEdgesNb++;
298  return --_inEdges.end();
299  }
300 
306  inline list<Edge>::iterator NodeData::newOutEdge(EdgeData * eData)
307  {
308  _outEdges.push_back(Edge(eData));
309  _outEdgesNb++;
310  return --_outEdges.end();
311  }
312 
313  typedef list<Edge>::iterator InEdgeIterator;
314  typedef list<Edge>::iterator OutEdgeIterator;
315  typedef list<Edge>::iterator LocalEdgeIterator;
316 
324  inline std::size_t hash_value(Diades::Graph::Edge const & e)
325  {
326  if (!e.valid())
327  {
328  return 0;
329  }
330  return boost::hash_value(e.id());
331  }
332  };
333 };
334 
335 
336 
337 namespace std
338 {
339 
344  template <> struct hash<Diades::Graph::Edge>
345  {
346 
347  size_t operator()(Diades::Graph::Edge const& edge) const
348  {
349  return Diades::Graph::hash_value(edge);
350  }
351  };
352 
353 };
354 
355 
356 #endif
friend ostream & operator<<(ostream &os, const Edge &edge)
Definition: Edge.hh:275
int memoryUsage() const
Definition: Edge.hh:194
void changeTarget(NodeData *t)
Definition: EdgeImpl.hh:143
NodeData * _source
identifier of the Edge
Definition: EdgeData.hh:106
Edge & operator=(const Edge &edge)=default
void create(EdgeId idt, NodeData *s, NodeData *t)
Definition: EdgeImpl.hh:115
NodeData * _target
pointer to the NodeData source of the Edge
Definition: EdgeData.hh:108
const Graph & owner() const
Definition: EdgeImpl.hh:85
list< Edge >::iterator delInEdge(EdgeData *eData)
Definition: NodeData.hh:67
Edge(EdgeData *data)
Definition: Edge.hh:68
STL namespace.
list< Edge >::iterator sourcePos() const
data associated with the current Edge
Definition: Edge.hh:210
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
bool operator==(const Edge &edge) const
Definition: Edge.hh:150
list< Edge >::iterator delOutEdge(EdgeData *eData)
Definition: NodeData.hh:82
Diades::Utils::Exception< Edge > Exception
Definition: Edge.hh:43
list< Edge >::iterator _sourcePos
pointer to the NodeData source of the Edge
Definition: EdgeData.hh:110
size_t operator()(Diades::Graph::Edge const &edge) const
Definition: Edge.hh:347
EdgeData * _eData
Definition: Edge.hh:204
EdgeData * data()
Definition: Edge.hh:87
std::size_t hash_value(Diades::Graph::Edge const &e)
Definition: Edge.hh:324
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
list< Edge >::iterator newOutEdge(EdgeData *eData)
Definition: Edge.hh:306
bool operator>(const Edge &edge) const
Definition: Edge.hh:169
list< Edge >::iterator LocalEdgeIterator
Definition: Edge.hh:315
bool operator<(const Edge &edge) const
Definition: EdgeImpl.hh:93
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:314
EdgeId id() const
Definition: Edge.hh:177
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
static string typeName()
Definition: Edge.hh:39
list< Edge >::iterator newInEdge(EdgeData *eData)
Definition: Edge.hh:294
EdgeData::EdgeId EdgeId
Definition: Edge.hh:45
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:313
list< Edge >::iterator targetPos() const
Definition: Edge.hh:220
list< Edge >::iterator _targetPos
position of the Edge in the list of Edge associated with the source Node
Definition: EdgeData.hh:112
bool valid() const
Definition: EdgeImpl.hh:72
vector< Edge >::size_type EdgeId
Definition: EdgeData.hh:40
const Node & sourceRef() const
Definition: EdgeImpl.hh:56
void destroy()
Definition: Edge.hh:241
void changeSource(NodeData *s)
Definition: EdgeImpl.hh:129
Node target() const
Definition: EdgeImpl.hh:47
bool operator!=(const Edge &edge) const
Definition: Edge.hh:159
Node source() const
Definition: EdgeImpl.hh:39
const Node & targetRef() const
Definition: EdgeImpl.hh:65