DiaDes  0.1
DIAgnosis of Discrete-Event System
GraphData.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__GRAPHDATA__HH
2 #define __DIADES__GRAPH__GRAPHDATA__HH
3 
4 #include <sstream>
5 #include <list>
6 #include <boost/serialization/split_free.hpp>
7 #include <boost/archive/text_oarchive.hpp>
8 #include <boost/archive/text_iarchive.hpp>
9 #include <boost/serialization/list.hpp>
10 #include <boost/serialization/vector.hpp>
11 #include "GraphGen.hh"
12 #include "EdgeData.hh"
13 #include "NodeData.hh"
14 #include "Edge.hh"
15 #include "Node.hh"
16 
17 
18 namespace std
19 {
20 
21  template<> struct hash<Diades::Graph::Node>
22  {
23  size_t operator()(const Diades::Graph::Node & node) const { if(node.valid()) { return node.id(); } return 0; }
24  };
25 
26  template<> struct hash<Diades::Graph::Edge>
27  {
28  size_t operator()(const Diades::Graph::Edge & edge) const { if(edge.valid()) { return edge.id(); } return 0; }
29  };
30 
31 };
32 
33 
34 
35 
36 namespace Diades
37 {
38  namespace Graph
39  {
40 
41 
42  template<typename Any>
44  {
45  typedef GraphIterator<Any> self;
46  typedef ptrdiff_t difference_type;
47  typedef std::forward_iterator_tag iterator_category;
48  typedef Any value_type;
49  typedef Any* pointer;
50  typedef Any& reference;
51 
52 
53  vector<value_type> * _pVect;
55 
56  GraphIterator() : _pVect(),_index() { }
57 
58  explicit GraphIterator(vector<value_type> * pVect, SizeType index) : _pVect(pVect), _index(index) {
59  if(_index < _pVect->size())
60  {
61  if( !(*_pVect)[_index].valid() )
62  {
63  operator++();
64  }
65  }
66  else
67  {
68  _index = _pVect->size();
69  }
70  ensure(GraphInvalid, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
71  "GraphIterator::constructor()");
72  }
73 
74  reference operator*() const
75  {
76  assertion(GraphInvalid, (_index < _pVect->size() && (*_pVect)[_index].valid()), "operator*()");
77  return (*_pVect)[_index];
78  }
79 
80  pointer operator->() const
81  {
82  assertion(GraphInvalid, (_index < _pVect->size() && (*_pVect)[_index].valid()), "operator*()");
83  return &(*_pVect)[_index];
84  }
85 
86  self & operator++()
87  {
88  ++_index;
89  while( (_index < _pVect->size())
90  && !((*_pVect)[_index].valid()))
91  {
92  ++_index;
93  }
94  ensure(GraphInvalid, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
95  "GraphIterator::operator++()");
96  return *this;
97  }
98 
99  self operator++(int)
100  {
101  self tmp = *this;
102  ++_index;
103  while( (_index < _pVect->size()) && !((*_pVect)[_index].valid()))
104  {
105  ++_index;
106  }
107  ensure(GraphInvalid, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
108  "GraphIterator::operator++()");
109  return tmp;
110  }
111 
112 
113 
114  bool operator==(const self & it) const
115  {
116  return (_pVect == it._pVect) && (_index == it._index);
117  }
118 
119  bool operator !=(const self & it) const
120  {
121  return !(*this == it);
122  }
123 
124  };
125 
126 
127 
128  // typedef GraphConstIterator<Node> NodeConstIterator;
129  // typedef GraphConstIterator<Edge> EdgeConstIterator;
132  class Graph;
133 
134  class GraphData
135  {
136 
137  private:
138  mutable vector<Edge> _vEdges; // list of edges
139  mutable vector<Node> _vNodes; // list of nodes
140  list<SizeType> _listEdgeId; // list of unused identifers for edges
141  list<SizeType> _listNodeId; // list of unused identifiers for nodes
144  unsigned _id;
145 
146  public: // for serialization
147  vector<Edge> & vEdges() { return _vEdges; }
148  vector<Node> & vNodes() { return _vNodes; }
149  list<SizeType> & listEdgeId() { return _listEdgeId; }
150  list<SizeType> & listNodeId() { return _listNodeId; }
151  const vector<Edge> & vEdges() const { return _vEdges; }
152  const vector<Node> & vNodes() const { return _vNodes; }
153  const list<SizeType> & listEdgeId() const { return _listEdgeId; }
154  const list<SizeType> & listNodeId() const { return _listNodeId; }
155  unsigned id() const { return _id; }
156 
157  public:
158 
159 
164  GraphData();
165 
176  virtual ~GraphData();
177 
178 
181 
182 
183  //****************** Accessors *********************************
184 
185 
192  {
193  return _nbNodes;
194  }
195 
202  {
203  return _nbEdges;
204  }
205 
211  bool empty() const
212  {
213  return numberOfNodes()==0;
214  }
215 
216 
222  const Node & target(const Edge & edge) const;
223 
229  const Node & source(const Edge & edge) const;
230 
231 
232 
233 
234 
242  bool cycleDetection(const Node & n) const;
243 
244 
246 
249 
255  const Node & newNode();
256 
257 
263  const Node & newNode(SizeType index);
264 
271  Node getNode(SizeType index) const;
272 
279  Edge getEdge(SizeType index) const;
280 
281 
290  const Edge & newEdge(Node source, Node target);
291 
292 
293 
303  const Edge & newEdge(SizeType index, Node source, Node target);
304 
305 
307 
308 
319  virtual void deleteNode(Node & node);
320 
321 
322 
323 
331  virtual void deleteNode(NodeIterator start,NodeIterator end);
332 
333 
334 
342  virtual void deleteEdge(Edge & edge);
343 
344 
345 
352  virtual void deleteEdge(EdgeIterator start, EdgeIterator end);
353  virtual void deleteEdge(LocalEdgeIterator start, LocalEdgeIterator end);
354 
355 
356 
359  virtual void deleteAllEdges();
360 
361 
362  // /** Elimination of edge paths
363  // * @param s a Node
364  // * @pre s.valid() && s.owner()==this
365  // * @post s.outDeg()==0
366  // *
367  // * Elimination of transition paths from the state s in the current graph.
368  // */
369  // virtual void eliminatePathsFrom(Node s);
370 
377  void clear();
378 
380 
384 
390  NodeIterator nodeBegin() {
391  return NodeIterator(&_vNodes,0);
392  }
393 
399  NodeIterator nodeEnd() {
400  return NodeIterator(&_vNodes,_vNodes.size());
401  }
402 
403 
404 
410  NodeIterator nodeBegin() const {
411  return NodeIterator(&_vNodes,0);
412  }
413 
419  NodeIterator nodeEnd() const {
420  return NodeIterator(&_vNodes,_vNodes.size());
421  }
422 
423 
424 
425 
426 
432  EdgeIterator edgeBegin()
433  {
434  return EdgeIterator(&_vEdges,0);
435  }
436 
442  EdgeIterator edgeEnd()
443  {
444  return EdgeIterator(&_vEdges,_vEdges.size());
445  }
446 
447 
453  EdgeIterator edgeBegin() const
454  {
455  return EdgeIterator(&_vEdges,0);
456  }
457 
463  EdgeIterator edgeEnd() const
464  {
465  return EdgeIterator(&_vEdges,_vEdges.size());
466  }
467 
468 
474  OutEdgeIterator outEdgeBegin(const Node & node) const;
475 
481  OutEdgeIterator outEdgeEnd(const Node & node) const;
482 
483 
489  InEdgeIterator inEdgeBegin(const Node & node) const;
490 
496  InEdgeIterator inEdgeEnd(const Node & node) const;
497 
498 
499 
501  //@name Others
503 
507  GraphData & operator=(const GraphData &);
508 
509 
510  // /** Size of a graph
511  // * @return the memory usage of a graph
512  // **/
513  // SizeType memoryUsage() const;
514 
515  // /** Size of a sub-graph
516  // * @param transitions to measure
517  // * @return the memory usage of a sub-graph
518  // **/
519  // SizeType memoryUsage(const unordered_set<Edge> & transitions) const;
520 
521 
522  // /** Size of a graph (nodes)
523  // * @return the memory usage of the graph nodes
524  // **/
525  // SizeType nodeMemoryUsage() const;
526 
527  // /** Size of a graph (edges)
528  // * @return the memory usage of the graph edges
529  // **/
530  // SizeType edgeMemoryUsage() const;
531 
532 
533 
539  SizeType edgeCapacity() const { return _vEdges.size(); }
545  SizeType nodeCapacity() const { return _vNodes.size(); }
547 
548 
550  bool sanityCheck(string & log) const;
551 
552 
553 
554  friend class Graph;
555  };
556  };
557 };
558 
559 #endif
560 
561 
OutEdgeIterator outEdgeEnd(const Node &node) const
InEdgeIterator inEdgeEnd(const Node &node) const
bool valid() const
Definition: EdgeImpl.hh:51
const list< SizeType > & listEdgeId() const
Definition: GraphData.hh:153
bool operator==(const self &it) const
Definition: GraphData.hh:114
InEdgeIterator inEdgeBegin(const Node &node) const
std::vector< Node >::size_type SizeType
Definition: GraphGen.hh:57
Edge getEdge(SizeType index) const
GraphIterator< Edge > EdgeIterator
Definition: GraphData.hh:131
size_t operator()(const Diades::Graph::Edge &edge) const
Definition: GraphData.hh:28
NodeIterator nodeEnd() const
Definition: GraphData.hh:419
list< SizeType > _listNodeId
Definition: GraphData.hh:141
OutEdgeIterator outEdgeBegin(const Node &node) const
const Node & newNode()
unsigned id() const
Definition: GraphData.hh:155
STL namespace.
vector< Node > & vNodes()
Definition: GraphData.hh:148
#define ensure(Exception, expr, message)
Definition: Assertion.hh:64
Log log(Logger logger, const char *msg)
Definition: Log.hh:124
list< SizeType > & listEdgeId()
Definition: GraphData.hh:149
const vector< Node > & vNodes() const
Definition: GraphData.hh:152
NodeIterator nodeBegin() const
Definition: GraphData.hh:410
pointer operator->() const
Definition: GraphData.hh:80
EdgeId id() const
Definition: Edge.hh:118
SizeType numberOfEdges() const
Definition: GraphData.hh:201
vector< value_type > * _pVect
Definition: GraphData.hh:53
reference operator*() const
Definition: GraphData.hh:74
bool sanityCheck(string &log) const
size_t operator()(const Diades::Graph::Node &node) const
Definition: GraphData.hh:23
virtual void deleteEdge(Edge &edge)
SizeType edgeCapacity() const
Definition: GraphData.hh:539
list< SizeType > _listEdgeId
Definition: GraphData.hh:140
GraphIterator< Node > NodeIterator
Definition: GraphData.hh:130
virtual void deleteAllEdges()
GraphData & operator=(const GraphData &)
list< SizeType > & listNodeId()
Definition: GraphData.hh:150
bool valid() const
Definition: NodeImpl.hh:27
Namespace of the Diades project.
const Node & target(const Edge &edge) const
Definition: GraphInt.hh:668
EdgeIterator edgeBegin() const
Definition: GraphData.hh:453
list< Edge >::iterator LocalEdgeIterator
Definition: Edge.hh:228
const Edge & newEdge(Node source, Node target)
SizeType nodeCapacity() const
Definition: GraphData.hh:545
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:227
EdgeIterator edgeEnd() const
Definition: GraphData.hh:463
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
EdgeIterator edgeEnd()
Definition: GraphData.hh:442
const Node & source(const Edge &edge) const
Definition: GraphInt.hh:675
std::forward_iterator_tag iterator_category
Definition: GraphData.hh:47
Node getNode(SizeType index) const
GraphIterator(vector< value_type > *pVect, SizeType index)
Definition: GraphData.hh:58
const list< SizeType > & listNodeId() const
Definition: GraphData.hh:154
bool operator!=(const self &it) const
Definition: GraphData.hh:119
EdgeIterator edgeBegin()
Definition: GraphData.hh:432
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:226
vector< Node > _vNodes
Definition: GraphData.hh:139
NodeIterator nodeEnd()
Definition: GraphData.hh:399
const vector< Edge > & vEdges() const
Definition: GraphData.hh:151
NodeIterator nodeBegin()
Definition: GraphData.hh:390
bool cycleDetection(const Node &n) const
vector< Edge > _vEdges
Definition: GraphData.hh:138
NodeId id() const
Definition: Node.hh:210
SizeType numberOfNodes() const
Definition: GraphData.hh:191
virtual void deleteNode(Node &node)
#define assertion(Exception, expr, message)
Definition: Assertion.hh:72
vector< Edge > & vEdges()
Definition: GraphData.hh:147