DiaDes  0.1
DIAgnosis of Discrete-Event System
GraphInt.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__GRAPHDATA__HH
2 #define __DIADES__GRAPH__GRAPHDATA__HH
3 
4 #include <list>
8 #include <diades/graph/Edge.hh>
9 #include <diades/graph/Node.hh>
10 
11 
12 
13 
14 
15 namespace Diades {
16  namespace Graph {
17 
23  template<typename Any>
24  struct GraphIterator {
25 
26  static string typeName() {
27  return "Graph::GraphIterator";
28  }
30 
31 
32 
33  typedef GraphIterator<Any> self;
34  typedef ptrdiff_t difference_type;
35  typedef std::forward_iterator_tag iterator_category;
36  typedef Any value_type;
37  typedef Any* pointer;
38  typedef Any& reference;
39 
40  typedef typename vector<value_type>::size_type SizeType;
41 
42  vector<value_type> * _pVect;
43  SizeType _index;
44 
48  GraphIterator() : _pVect(), _index() {
49  }
50 
56  explicit GraphIterator(vector<value_type> * pVect, SizeType index) : _pVect(pVect), _index(index) {
57  if (_index < _pVect->size()) {
58  if (!(*_pVect)[_index].valid()) {
59  operator++();
60  }
61  } else {
62  _index = _pVect->size();
63  }
64  ensure(Exception, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
65  "constructor()");
66  }
67 
72  reference operator*() const {
73  assertion(Exception, (_index < _pVect->size() && (*_pVect)[_index].valid()), "operator*()");
74  return (*_pVect)[_index];
75  }
76 
81  pointer operator->() const {
82  assertion(Exception, (_index < _pVect->size() && (*_pVect)[_index].valid()), "operator*()");
83  return &(*_pVect)[_index];
84  }
85 
90  self & operator++() {
91  ++_index;
92  while ((_index < _pVect->size())
93  && !((*_pVect)[_index].valid())) {
94  ++_index;
95  }
96  ensure(Exception, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
97  "operator++()");
98  return *this;
99  }
100 
105  self operator++(int) {
106  self tmp = *this;
107  ++_index;
108  while ((_index < _pVect->size()) && !((*_pVect)[_index].valid())) {
109  ++_index;
110  }
111  ensure(Exception, (_index == _pVect->size()) || (_index < _pVect->size() && (*_pVect)[_index].valid()),
112  "operator++()");
113  return tmp;
114  }
115 
121  bool operator==(const self & it) const {
122  return (_pVect == it._pVect) && (_index == it._index);
123  }
124 
130  bool operator!=(const self & it) const {
131  return !(*this == it);
132  }
133 
134  };
135 
144 
176  class Graph {
177  public:
178  static string typeName() {
179  return "Graph::Graph";
180  }
182  typedef vector<Edge>::size_type EdgeSizeType;
183  typedef vector<Node>::size_type NodeSizeType;
186 
187 
188 
189 
190  private:
191  mutable vector<Edge> _vEdges; // list of edges
192  mutable vector<Node> _vNodes; // list of nodes
193  list<EdgeSizeType> _listEdgeId; // list of unused identifers for edges
194  list<NodeSizeType> _listNodeId; // list of unused identifiers for nodes
195  NodeSizeType _nbNodes;
196  EdgeSizeType _nbEdges;
197  unsigned _id;
198 
206  void copyGraph(const Graph & graph);
207 
208  public: // for serialization
209 
210  vector<Edge> & vEdges() {
211  return _vEdges;
212  }
213 
214  vector<Node> & vNodes() {
215  return _vNodes;
216  }
217 
218  list<EdgeSizeType> & listEdgeId() {
219  return _listEdgeId;
220  }
221 
222  list<NodeSizeType> & listNodeId() {
223  return _listNodeId;
224  }
225 
226  const vector<Edge> & vEdges() const {
227  return _vEdges;
228  }
229 
230  const vector<Node> & vNodes() const {
231  return _vNodes;
232  }
233 
234  const list<EdgeSizeType> & listEdgeId() const {
235  return _listEdgeId;
236  }
237 
238  const list<NodeSizeType> & listNodeId() const {
239  return _listNodeId;
240  }
241 
242  unsigned id() const {
243  return _id;
244  }
245 
246  public:
247 
248 
253  Graph();
254 
262  Graph(const Graph & graph);
263 
264 
271  virtual ~Graph();
272 
273 
282  Graph & operator=(const Graph & graph)
283  {
284  if(this != &graph)
285  {
286  copyGraph(graph);
287  }
288  return (*this);
289  }
290 
291 
294 
295 
296  //****************** Accessors *********************************
297 
298 
307  bool succeeds(const Node & n1, const Node & n2) const;
308 
314  NodeSizeType numberOfNodes() const {
315  return _nbNodes;
316  }
317 
323  EdgeSizeType numberOfEdges() const {
324  return _nbEdges;
325  }
326 
332  bool empty() const {
333  return numberOfNodes() == 0;
334  }
335 
336 
342  Node target(const Edge & edge) const;
343 
344 
350  Node source(const Edge & edge) const;
351 
352 
353 
360  const Node & targetRef(const Edge & edge) const;
361 
368  const Node & sourceRef(const Edge & edge) const;
369 
370 
371 
372 
373 
381  bool cycleDetection(const Node & n) const;
382 
383 
385 
388 
394  Node newNode();
395 
396 
402  Node newNode(NodeSizeType index);
403 
410  Node getNode(NodeSizeType index) const;
411 
418  Edge getEdge(EdgeSizeType index) const;
419 
420 
429  Edge newEdge(Node source, Node target);
430 
431 
432 
442  Edge newEdge(EdgeSizeType index, Node source, Node target);
443 
444 
446 
447 
458  virtual void deleteNode(Node & node);
459 
460 
461 
462 
470  virtual void deleteNode(NodeIterator start, NodeIterator end);
471 
472 
473 
481  virtual void deleteEdge(Edge & edge);
482 
483 
484 
491  virtual void deleteEdge(EdgeIterator start, EdgeIterator end);
492  virtual void deleteEdge(LocalEdgeIterator start, LocalEdgeIterator end);
493 
494 
495 
498  virtual void deleteAllEdges();
499 
500 
501  // /** Elimination of edge paths
502  // * @param s a Node
503  // * @pre s.valid() && s.owner()==this
504  // * @post s.outDeg()==0
505  // *
506  // * Elimination of transition paths from the state s in the current graph.
507  // */
508  // virtual void eliminatePathsFrom(Node s);
509 
516  void clear();
517 
519 
523 
529  NodeIterator nodeBegin() {
530  return NodeIterator(&_vNodes, 0);
531  }
532 
538  NodeIterator nodeEnd() {
539  return NodeIterator(&_vNodes, _vNodes.size());
540  }
541 
547  NodeIterator nodeBegin() const {
548  return NodeIterator(&_vNodes, 0);
549  }
550 
556  NodeIterator nodeEnd() const {
557  return NodeIterator(&_vNodes, _vNodes.size());
558  }
559 
565  EdgeIterator edgeBegin() {
566  return EdgeIterator(&_vEdges, 0);
567  }
568 
574  EdgeIterator edgeEnd() {
575  return EdgeIterator(&_vEdges, _vEdges.size());
576  }
577 
583  EdgeIterator edgeBegin() const {
584  return EdgeIterator(&_vEdges, 0);
585  }
586 
592  EdgeIterator edgeEnd() const {
593  return EdgeIterator(&_vEdges, _vEdges.size());
594  }
595 
596 
602  OutEdgeIterator outEdgeBegin(const Node & node) const;
603 
609  OutEdgeIterator outEdgeEnd(const Node & node) const;
610 
611 
617  InEdgeIterator inEdgeBegin(const Node & node) const;
618 
624  InEdgeIterator inEdgeEnd(const Node & node) const;
625 
626 
627 
629  //@name Others
631 
632 
633  // /** Size of a graph
634  // * @return the memory usage of a graph
635  // **/
636  // SizeType memoryUsage() const;
637 
638  // /** Size of a sub-graph
639  // * @param transitions to measure
640  // * @return the memory usage of a sub-graph
641  // **/
642  // SizeType memoryUsage(const unordered_set<Edge> & transitions) const;
643 
644 
645  // /** Size of a graph (nodes)
646  // * @return the memory usage of the graph nodes
647  // **/
648  // SizeType nodeMemoryUsage() const;
649 
650  // /** Size of a graph (edges)
651  // * @return the memory usage of the graph edges
652  // **/
653  // SizeType edgeMemoryUsage() const;
654 
660  EdgeSizeType edgeCapacity() const {
661  return _vEdges.size();
662  }
663 
669  NodeSizeType nodeCapacity() const {
670  return _vNodes.size();
671  }
673 
674 
676  bool sanityCheck(string & log) const;
677 
679  ostream & toDot(ostream & os);
680 
681 
690  void transitiveClosure(Edge edge);
691 
692  };
693 
700  template<typename InputIterator>
701  inline void deleteEdge(Graph & g, InputIterator first, InputIterator last) {
702  for (; first != last; ++first) {
703  g.deleteEdge(*first);
704  }
705  }
706 
713  template<typename InputIterator>
714  inline void deleteNode(Graph & g, InputIterator first, InputIterator last) {
715  for (; first != last; ++first) {
716  g.deleteNode(*first);
717  }
718  }
719 
728  template<typename InputIterator, typename Predicate>
729  inline void deleteNode(Graph & g, InputIterator first, InputIterator last, Predicate pred) {
730  for (; first != last; ++first) {
731  if (pred()(*first)) {
732  g.deleteNode(*first);
733  }
734  }
735  }
736 
737 
738 
739 
740 
741  };
742 };
743 
744 #endif
745 
746 
OutEdgeIterator outEdgeBegin(const Node &node) const
Definition: GraphIntOld.hh:810
list< NodeSizeType > & listNodeId()
Definition: GraphInt.hh:222
NodeSizeType nodeCapacity() const
Definition: GraphInt.hh:669
list< EdgeSizeType > & listEdgeId()
Definition: GraphInt.hh:218
GraphIterator< Edge > EdgeIterator
Definition: GraphInt.hh:143
Edge newEdge(Node source, Node target)
Diades::Utils::Exception< Graph > Exception
Definition: GraphInt.hh:181
EdgeSizeType numberOfEdges() const
Definition: GraphInt.hh:323
void copyGraph(const Graph &graph)
Edge::EdgeId EdgeId
Definition: GraphInt.hh:184
Definition: Run.cc:88
NodeIterator nodeEnd()
Definition: GraphInt.hh:538
vector< Edge >::size_type EdgeSizeType
Definition: GraphInt.hh:182
Diades::Utils::Exception< GraphIterator > Exception
Definition: GraphInt.hh:29
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
list< NodeSizeType > _listNodeId
Definition: GraphInt.hh:194
EdgeIterator edgeEnd()
Definition: GraphInt.hh:574
bool valid() const
Definition: GraphIntOld.hh:683
const Node & targetRef(const Edge &edge) const
Definition: GraphIntOld.hh:767
vector< value_type >::size_type SizeType
Definition: GraphInt.hh:40
bool operator!=(const self &it) const
Definition: GraphInt.hh:130
EdgeIterator edgeBegin()
Definition: GraphInt.hh:565
vector< value_type > * _pVect
Definition: GraphInt.hh:42
EdgeIterator edgeBegin() const
Definition: GraphInt.hh:583
static string typeName()
Definition: GraphInt.hh:26
GraphIterator< Node > NodeIterator
Definition: GraphInt.hh:139
Node source(const Edge &edge) const
Definition: GraphIntOld.hh:744
NodeIterator nodeEnd() const
Definition: GraphInt.hh:556
ostream & toDot(ostream &os)
EdgeSizeType _nbEdges
Definition: GraphInt.hh:196
Namespace of the Diades project.
Edge getEdge(EdgeSizeType index) const
static string typeName()
Definition: GraphInt.hh:178
list< Edge >::iterator LocalEdgeIterator
Definition: Edge.hh:315
reference operator*() const
Definition: GraphInt.hh:72
Graph & operator=(const Graph &graph)
Definition: GraphInt.hh:282
InEdgeIterator inEdgeBegin(const Node &node) const
Definition: GraphIntOld.hh:822
bool sanityCheck(string &log) const
vector< Edge > _vEdges
Definition: GraphInt.hh:191
bool succeeds(const Node &n1, const Node &n2) const
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:314
virtual void deleteNode(Node &node)
Node getNode(NodeSizeType index) const
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
const vector< Edge > & vEdges() const
Definition: GraphInt.hh:226
virtual void deleteEdge(Edge &edge)
OutEdgeIterator outEdgeEnd(const Node &node) const
Definition: GraphIntOld.hh:816
void transitiveClosure(Edge edge)
pointer operator->() const
Definition: GraphInt.hh:81
vector< Node >::size_type NodeSizeType
Definition: GraphInt.hh:183
std::forward_iterator_tag iterator_category
Definition: GraphInt.hh:35
GraphIterator(vector< value_type > *pVect, SizeType index)
Definition: GraphInt.hh:56
vector< Node > & vNodes()
Definition: GraphInt.hh:214
const list< EdgeSizeType > & listEdgeId() const
Definition: GraphInt.hh:234
EdgeData::EdgeId EdgeId
Definition: Edge.hh:45
bool empty() const
Definition: GraphInt.hh:332
Log log(Logger logger, const string &msg)
Definition: Log.hh:124
bool cycleDetection(const Node &n) const
unsigned id() const
Definition: GraphInt.hh:242
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:313
list< EdgeSizeType > _listEdgeId
Definition: GraphInt.hh:193
SizeType NodeId
Definition: Node.hh:42
NodeIterator nodeBegin()
Definition: GraphInt.hh:529
vector< Node > _vNodes
Definition: GraphInt.hh:192
const list< NodeSizeType > & listNodeId() const
Definition: GraphInt.hh:238
Node target(const Edge &edge) const
Definition: GraphIntOld.hh:736
EdgeIterator edgeEnd() const
Definition: GraphInt.hh:592
virtual void deleteAllEdges()
NodeIterator nodeBegin() const
Definition: GraphInt.hh:547
NodeSizeType numberOfNodes() const
Definition: GraphInt.hh:314
NodeSizeType _nbNodes
Definition: GraphInt.hh:195
vector< Edge > & vEdges()
Definition: GraphInt.hh:210
Node::NodeId NodeId
Definition: GraphInt.hh:185
const vector< Node > & vNodes() const
Definition: GraphInt.hh:230
bool operator==(const self &it) const
Definition: GraphInt.hh:121
#define assertion(Exception, expr, message)
Definition: Assertion.hh:106
InEdgeIterator inEdgeEnd(const Node &node) const
Definition: GraphIntOld.hh:828
EdgeSizeType edgeCapacity() const
Definition: GraphInt.hh:660
const Node & sourceRef(const Edge &edge) const
Definition: GraphIntOld.hh:775