DiaDes  0.1
DIAgnosisofDiscrete-EventSystem
Node.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__NODE__HH
2 #define __DIADES__GRAPH__NODE__HH
3 
4 #include <list>
5 #include <boost/functional/hash.hpp>
9 #include <diades/graph/Edge.hh>
10 
11 
12 
13 namespace Diades
14 {
15  namespace Graph
16  {
17 
30  class Node
31  {
32  public:
33 
34  static string typeName()
35  {
36  return "Graph::Node";
37  }
39 
40  public:
41  typedef vector<Node>::size_type SizeType;
42  typedef SizeType NodeId;
43 
44  public:
45 
48 
54  Node() : _nData(nullptr)
55  {
56  }
57 
62  Node(const Node & node) : _nData(node._nData)
63  {
64  }
65 
70  Node(NodeData * data) : _nData(data)
71  {
72  require(Exception, data != nullptr,
73  "Node: null data pointer");
74  }
75 
80  {
81  }
82 
88  {
89  return _nData;
90  }
91 
95  Node & operator=(const Node & node)
96  {
97  if (this != &node)
98  {
99  _nData = node._nData;
100  }
101  return *this;
102  }
103 
104 
105 
107 
109 
113  SizeType memoryUsage() const
114  {
115  return sizeof (Node);
116  }
117 
122  SizeType degree() const
123  {
124  require(Exception, valid(), "Node::degree: invalid node");
125  return outDeg() + inDeg();
126  }
127 
132  SizeType outDeg() const
133  {
134  require(Exception, valid(), "Node::outDeg: invalid node");
135  return _nData->outEdgesNb();
136  }
137 
142  SizeType inDeg() const
143  {
144  require(Exception, valid(), "Node::inDeg: invalid node");
145  return _nData->inEdgesNb();
146  }
147 
153  {
154  require(Exception, valid(), "Node::outEdgeBegin: invalid node");
155  return _nData->outEdges().begin();
156  }
157 
163  {
164  require(Exception, valid(), "Node::outEdgeEnd: invalid node");
165  return _nData->outEdges().end();
166  }
167 
173  {
174  require(Exception, valid(), "Node::inEdgeBegin: invalid node");
175  return _nData->inEdges().begin();
176  }
177 
183  {
184  require(Exception, valid(), "Node::inEdgeEnd: invalid node");
185  return _nData->inEdges().end();
186  }
187 
191  bool valid() const;
192 
197  bool operator==(const Node & node) const
198  {
199  return node._nData == _nData;
200  }
201 
206  bool operator!=(const Node & node) const
207  {
208  return !(*this == node);
209  }
210 
214  bool operator<(const Node & node) const;
215 
219  bool operator>(const Node & node) const
220  {
221  return !((*this == node) || ((*this) < node));
222  }
223 
224 
225 
226 
230  const Graph & owner() const;
231 
237  NodeId id() const
238  {
239  require(Exception, valid(), "id: node is not valid");
240  return _nData->identifier();
241 
242  }
243 
244 
246  void printInformation() const;
247 
249 
250  private:
251 
259  void create(NodeId idt, Graph * own);
260 
264  void destroy()
265  {
266  require(Exception, _nData == nullptr || (_nData->inEdges().empty() && _nData->outEdges().empty()),
267  "Node::destroy: _nData is not null and output or input edges exist");
268  if (_nData != nullptr)
269  {
270  delete _nData;
271  _nData = nullptr;
272  }
273  ensure(Exception, !valid(), "Node::destroy: valid node");
274  }
275 
280  void changeId(NodeId id)
281  {
282  require(Exception, valid(), "Node::changeId: invalid node");
283  _nData->setIdentifier(id);
284  }
285 
286 
288 
290  friend class Graph;
291 
292  //@name stream operation
294 
298  friend ostream & operator<<(ostream & os, const Node & node)
299  {
300  if (node.valid())
301  {
302  os << node.id();
303  } else
304  {
305  os << "null_node";
306  }
307  return os;
308  }
310 
311  public:
312 
313  // template<class Archive>
314  // void save(Archive & ar, const unsigned int version) const;
315  //
316  //
317  // template<class Archive>
318  // void load(Archive & ar, const unsigned int version);
319  //
320  // BOOST_SERIALIZATION_SPLIT_MEMBER()
321 
322  };
323 
330  inline std::size_t hash_value(Diades::Graph::Node const & node)
331  {
332  if (!node.valid())
333  {
334  return 0;
335  }
336  return boost::hash_value(node.id());
337  }
338  };
339 };
340 
341 
342 
343 namespace std
344 {
345 
350  template <> struct hash<Diades::Graph::Node>
351  {
352 
353  size_t operator()(Diades::Graph::Node const& node) const
354  {
355  return Diades::Graph::hash_value(node);
356  }
357  };
358 
359 };
360 
361 
362 
363 
364 
365 
366 
367 #endif
SizeType outEdgesNb() const
Definition: NodeData.hh:123
friend ostream & operator<<(ostream &os, const Node &node)
Definition: Node.hh:298
void destroy()
Definition: Node.hh:264
bool valid() const
Definition: NodeImpl.hh:23
SizeType inDeg() const
Definition: Node.hh:142
STL namespace.
InEdgeIterator inEdgeBegin() const
Definition: Node.hh:172
NodeData * data()
Definition: Node.hh:87
SizeType memoryUsage() const
Definition: Node.hh:113
#define ensure(Exception, expr, message)
Definition: Assertion.hh:97
const Graph & owner() const
Definition: NodeImpl.hh:46
NodeId identifier() const
Definition: NodeData.hh:155
NodeData * _nData
Definition: Node.hh:287
InEdgeIterator inEdgeEnd() const
Definition: Node.hh:182
list< Edge > & outEdges() const
Definition: NodeData.hh:147
OutEdgeIterator outEdgeBegin() const
Definition: Node.hh:152
list< Edge > & inEdges() const
Definition: NodeData.hh:139
SizeType outDeg() const
Definition: Node.hh:132
void setIdentifier(NodeId newId)
Definition: NodeData.hh:163
std::size_t hash_value(Diades::Graph::Edge const &e)
Definition: Edge.hh:326
#define require(Exception, expr, message)
Definition: Assertion.hh:89
Namespace of the Diades project.
SizeType degree() const
Definition: Node.hh:122
size_t operator()(Diades::Graph::Node const &node) const
Definition: Node.hh:353
bool operator<(const Node &node) const
Definition: NodeImpl.hh:83
Diades::Utils::Exception< Node > Exception
Definition: Node.hh:38
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:316
NodeId id() const
Definition: Node.hh:237
static string typeName()
Definition: Node.hh:34
bool operator==(const Node &node) const
Definition: Node.hh:197
OutEdgeIterator outEdgeEnd() const
Definition: Node.hh:162
Node(NodeData *data)
Definition: Node.hh:70
SizeType inEdgesNb() const
Definition: NodeData.hh:131
std::size_t hash_value(Diades::Graph::Node const &node)
Definition: Node.hh:330
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:315
vector< Node >::size_type SizeType
Definition: Node.hh:41
void changeId(NodeId id)
Definition: Node.hh:280
SizeType NodeId
Definition: Node.hh:42
void create(NodeId idt, Graph *own)
Definition: NodeImpl.hh:70
Node(const Node &node)
Definition: Node.hh:62
bool operator>(const Node &node) const
Definition: Node.hh:219
Node & operator=(const Node &node)
Definition: Node.hh:95
bool operator!=(const Node &node) const
Definition: Node.hh:206
void printInformation() const
Definition: NodeImpl.hh:52