DiaDes  0.1
DIAgnosis of Discrete-Event System
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() =default;
55 
60  Node(const Node & node) = default;
61 
62 
63 
68  Node(Node && node) = default;
69 
70 
71 
77  Node& operator=(Node && other) = default;
78 
79 
80 
85  Node(NodeData * data) : _nData(data)
86  {
87  require(Exception, data != nullptr,
88  "Node: null data pointer");
89  }
90 
94  ~Node() =default;
95 
96 
102  {
103  return _nData;
104  }
105 
109  Node & operator=(const Node & node) = default;
110 
111 
113 
115 
119  SizeType memoryUsage() const
120  {
121  return sizeof (Node);
122  }
123 
128  SizeType degree() const
129  {
130  require(Exception, valid(), "Node::degree: invalid node");
131  return outDeg() + inDeg();
132  }
133 
138  SizeType outDeg() const
139  {
140  require(Exception, valid(), "Node::outDeg: invalid node");
141  return _nData->outEdgesNb();
142  }
143 
148  SizeType inDeg() const
149  {
150  require(Exception, valid(), "Node::inDeg: invalid node");
151  return _nData->inEdgesNb();
152  }
153 
159  {
160  require(Exception, valid(), "Node::outEdgeBegin: invalid node");
161  return _nData->outEdges().begin();
162  }
163 
169  {
170  require(Exception, valid(), "Node::outEdgeEnd: invalid node");
171  return _nData->outEdges().end();
172  }
173 
179  {
180  require(Exception, valid(), "Node::inEdgeBegin: invalid node");
181  return _nData->inEdges().begin();
182  }
183 
189  {
190  require(Exception, valid(), "Node::inEdgeEnd: invalid node");
191  return _nData->inEdges().end();
192  }
193 
197  bool valid() const;
198 
202  bool isValid() const { return valid(); }
203 
208  bool operator==(const Node & node) const
209  {
210  return node._nData == _nData;
211  }
212 
217  bool operator!=(const Node & node) const
218  {
219  return !(*this == node);
220  }
221 
225  bool operator<(const Node & node) const;
226 
230  bool operator>(const Node & node) const
231  {
232  return !((*this == node) || ((*this) < node));
233  }
234 
235 
236 
237 
241  const Graph & owner() const;
242 
248  NodeId id() const
249  {
250  require(Exception, valid(), "id: node is not valid");
251  return _nData->identifier();
252 
253  }
254 
255 
257  void printInformation() const;
258 
260 
261  private:
262 
270  void create(NodeId idt, Graph * own);
271 
275  void destroy()
276  {
277  require(Exception, _nData == nullptr || (_nData->inEdges().empty() && _nData->outEdges().empty()),
278  "Node::destroy: _nData is not null and output or input edges exist");
279  if (_nData != nullptr)
280  {
281  delete _nData;
282  _nData = nullptr;
283  }
284  ensure(Exception, !valid(), "Node::destroy: valid node");
285  }
286 
291  void changeId(NodeId id)
292  {
293  require(Exception, valid(), "Node::changeId: invalid node");
294  _nData->setIdentifier(id);
295  }
296 
297 
299 
301  friend class Graph;
302 
303  //@name stream operation
305 
309  friend ostream & operator<<(ostream & os, const Node & node)
310  {
311  if (node.valid())
312  {
313  os << node.id();
314  } else
315  {
316  os << "null_node";
317  }
318  return os;
319  }
321 
322  public:
323 
324  // template<class Archive>
325  // void save(Archive & ar, const unsigned int version) const;
326  //
327  //
328  // template<class Archive>
329  // void load(Archive & ar, const unsigned int version);
330  //
331  // BOOST_SERIALIZATION_SPLIT_MEMBER()
332 
333  };
334 
341  inline std::size_t hash_value(Diades::Graph::Node const & node)
342  {
343  if (!node.valid())
344  {
345  return 0;
346  }
347  return boost::hash_value(node.id());
348  }
349  };
350 };
351 
352 
353 
354 namespace std
355 {
356 
361  template <> struct hash<Diades::Graph::Node>
362  {
363 
364  size_t operator()(Diades::Graph::Node const& node) const
365  {
366  return Diades::Graph::hash_value(node);
367  }
368  };
369 
370 };
371 
372 
373 
374 
375 
376 
377 
378 #endif
SizeType outEdgesNb() const
Definition: NodeData.hh:123
friend ostream & operator<<(ostream &os, const Node &node)
Definition: Node.hh:309
void destroy()
Definition: Node.hh:275
bool valid() const
Definition: NodeImpl.hh:23
SizeType inDeg() const
Definition: Node.hh:148
STL namespace.
InEdgeIterator inEdgeBegin() const
Definition: Node.hh:178
NodeData * data()
Definition: Node.hh:101
SizeType memoryUsage() const
Definition: Node.hh:119
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
const Graph & owner() const
Definition: NodeImpl.hh:46
NodeId identifier() const
Definition: NodeData.hh:155
NodeData * _nData
Definition: Node.hh:298
InEdgeIterator inEdgeEnd() const
Definition: Node.hh:188
list< Edge > & outEdges() const
Definition: NodeData.hh:147
OutEdgeIterator outEdgeBegin() const
Definition: Node.hh:158
list< Edge > & inEdges() const
Definition: NodeData.hh:139
Node & operator=(Node &&other)=default
bool isValid() const
Definition: Node.hh:202
SizeType outDeg() const
Definition: Node.hh:138
void setIdentifier(NodeId newId)
Definition: NodeData.hh:163
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.
SizeType degree() const
Definition: Node.hh:128
size_t operator()(Diades::Graph::Node const &node) const
Definition: Node.hh:364
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:314
NodeId id() const
Definition: Node.hh:248
static string typeName()
Definition: Node.hh:34
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
bool operator==(const Node &node) const
Definition: Node.hh:208
OutEdgeIterator outEdgeEnd() const
Definition: Node.hh:168
Node(NodeData *data)
Definition: Node.hh:85
SizeType inEdgesNb() const
Definition: NodeData.hh:131
std::size_t hash_value(Diades::Graph::Node const &node)
Definition: Node.hh:341
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:313
vector< Node >::size_type SizeType
Definition: Node.hh:41
void changeId(NodeId id)
Definition: Node.hh:291
SizeType NodeId
Definition: Node.hh:42
void create(NodeId idt, Graph *own)
Definition: NodeImpl.hh:70
bool operator>(const Node &node) const
Definition: Node.hh:230
bool operator!=(const Node &node) const
Definition: Node.hh:217
void printInformation() const
Definition: NodeImpl.hh:52