Otawa  0.10
GenGraph.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * GenGraph class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2006-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef OTAWA_GRAPH_GEN_GRAPH_H
23 #define OTAWA_GRAPH_GEN_GRAPH_H
24 #include <elm/PreIterator.h>
25 #include <otawa/graph/Graph.h>
26 
27 // GCC work-around
28 #if defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ <= 0
29 # define OTAWA_GCAST(t, e) ((t)(e))
30 #else
31 # define OTAWA_GCAST(t, e) static_cast<t>(e)
32 #endif
33 
34 namespace otawa {
35 
36 // GenGraph class
37 template <class N, class E>
38 class GenGraph: private graph::Graph {
39 public:
40  typedef N *Vertex;
41  typedef E *Edge;
42 
43  // GenNode class
44  class GenNode: private graph::Node {
45  friend class GenGraph<N, E>;
46  protected:
47  inline GenNode(/*GenGraph<N, E>*/ graph::Graph *graph = 0): graph::Node(graph) { }
48  virtual ~GenNode(void) { }
49  public:
50  //inline graph::Graph *graph(void) const { return graph::Node::graph(); }
51  inline int index(void) const { return graph::Node::index(); }
52  inline bool hasSucc(void) const { return graph::Node::hasSucc(); }
53  inline bool hasPred(void) const { return graph::Node::hasPred(); }
54  inline int countSucc(void) const { return graph::Node::countSucc(); }
55  inline int countPred(void) const { return graph::Node::countPred(); }
56  inline bool isPredOf(const GenNode *node) { return graph::Node::countPred(); }
57  inline bool isSuccOf(const GenNode *node) { return graph::Node::isSuccOf(); }
58  };
59 
60  // GenEdge class
61  class GenEdge: private graph::Edge {
62  friend class GenGraph<N, E>;
63  protected:
64  virtual ~GenEdge(void) { }
65  public:
66  inline GenEdge(GenNode *source, GenNode *target): graph::Edge(_(source), _(target)) { }
67  inline N *source(void) const { return OTAWA_GCAST(N *, graph::Edge::source()); }
68  inline N *target(void) const { return OTAWA_GCAST(N *, graph::Edge::target()); }
69  };
70 
71  // Collection concept
72  inline int count(void) const { return Graph::count(); }
73  inline bool contains(N *item) const { return Graph::contains(item); }
74  inline bool isEmpty(void) const { return Graph::isEmpty(); }
75  inline operator bool(void) const { return !isEmpty(); }
76 
77  // Iterator class
78  class Iterator: public elm::PreIterator<Iterator, N *> {
80  public:
81  inline Iterator(const GenGraph<N, E> *graph): iter(graph) { }
82  inline Iterator(const GenGraph<N, E>::Iterator& iterator): iter(iterator.iter) { }
83  inline bool ended(void) const { return iter.ended(); }
84  inline N *item(void) const { return OTAWA_GCAST(N *, iter.item()); }
85  inline void next(void) { iter.next(); }
86  };
87 
88  // MutableCollection concept
89  inline void clear(void) { graph::Graph::clear(); }
90  inline void add(GenNode *node) { graph::Graph::add(node); }
91  template <template <class _> class C> void addAll(const C<N *> &items)
92  { graph::Graph::addAll(items); }
93  inline void remove(GenNode *node) { graph::Graph::remove(node); }
94  template <template <class _> class C> void removeAll(const C<N *> &items)
95  { graph::Graph::removeAll(items); }
96  inline void remove(GenEdge *edge) { graph::Graph::remove(edge); }
97 
98  // DiGraph concept
99  inline N *sinkOf(E *edge) const { return Graph::sinkOf(edge); }
100  inline int outDegree(N *vertex) const { return Graph::outDegree(vertex); }
101  inline bool isSuccessorOf(N *succ, N *ref) const { return Graph::isSuccessorOf(succ, ref); }
102 
103  // OutIterator class
104  class OutIterator: public elm::PreIterator<OutIterator, E *> {
105  public:
106  inline OutIterator(const N *node): iter(_(node)) { }
107  inline OutIterator(const GenGraph<N, E>& graph, const N *node): iter(_(node)) { }
108  inline bool ended(void) const { return iter.ended(); }
109  inline void next(void) { iter.next(); }
110  inline E *item(void) const { return OTAWA_GCAST(E *, iter.item()); }
111  private:
112  Graph::OutIterator iter;
113  };
114 
115  // DiGraph concept
116  inline N *sourceOf(E *edge) const { return Graph::sourceOf(edge); }
117  inline int inDegree(N *vertex) const { return Graph::inDegree(vertex); }
118  inline bool isPredecessorOf(N *succ, N *ref) const { return Graph::isPredecessorOf(succ, ref); }
119 
120  // InIterator class
121  class InIterator: public elm::PreIterator<InIterator, E *> {
122  public:
123  inline InIterator(const N *node): iter(_(node)) { }
124  inline InIterator(const GenGraph<N, E>& graph, const N *node): iter(_(node)) { }
125  inline bool ended(void) const { return iter.ended(); }
126  inline void next(void) { iter.next(); }
127  inline E *item(void) const { return OTAWA_GCAST(E *, iter.item()); }
128  private:
129  Graph::InIterator iter;
130  };
131 
132  // DiGraphWithIndexedVertex concept
133  inline int indexOf(N *vertex) const { return Graph::indexOf(vertex); }
134 
135  // private
136  inline static const graph::Node *_(const GenNode *node) { return node; };
137  inline static const graph::Edge *_(const GenEdge *edge) { return edge; };
138  inline const graph::Graph *_(void) const { return this; };
139  inline static graph::Node *_(GenNode *node) { return node; };
140  inline static graph::Edge *_(Edge *edge) { return edge; };
141  inline graph::Graph *_(void) { return this; }
142 };
143 
144 } // otawa
145 
146 #endif // OTAWA_UTIL_GRAPH_GRAPH_H
Graph::InIterator iter
Definition: GenGraph.h:129
A simple iterator on the nodes contained in a graph.
Definition: Graph.h:138
Definition: GenGraph.h:38
N * source(void) const
Definition: GenGraph.h:67
Definition: GenGraph.h:78
static graph::Edge * _(Edge *edge)
Definition: GenGraph.h:140
OutIterator(const GenGraph< N, E > &graph, const N *node)
Definition: GenGraph.h:107
GenEdge(GenNode *source, GenNode *target)
Definition: GenGraph.h:66
dtd::Element edge(dtd::make("edge", _EDGE).attr(source).attr(target).attr(called))
bool hasSucc(void) const
Definition: GenGraph.h:52
bool isSuccOf(const Node *node)
Test if the current node is a successor of the given one.
Definition: Graph.h:214
Definition: GenGraph.h:44
static const graph::Edge * _(const GenEdge *edge)
Definition: GenGraph.h:137
const graph::Graph * _(void) const
Definition: GenGraph.h:138
E * item(void) const
Definition: GenGraph.h:127
#define OTAWA_GCAST(t, e)
Definition: GenGraph.h:31
int countSucc(void) const
Definition: GenGraph.h:54
bool ended(void) const
Definition: GenGraph.h:125
E * Edge
Definition: GenGraph.h:41
int index(void) const
Get the index of the node in the graph.
Definition: Graph.h:76
bool contains(N *item) const
Definition: GenGraph.h:73
void removeAll(const C< Node * > &items)
Definition: Graph.h:152
bool isSuccOf(const GenNode *node)
Definition: GenGraph.h:57
int inDegree(N *vertex) const
Definition: GenGraph.h:117
OutIterator(const N *node)
Definition: GenGraph.h:106
int indexOf(N *vertex) const
Definition: GenGraph.h:133
This class represents a full graph with nodes and edges.
Definition: Graph.h:122
This class represents a directed edge between two nodes.
Definition: Graph.h:97
int count(void) const
Definition: GenGraph.h:72
void next(void)
Definition: GenGraph.h:85
static graph::Node * _(GenNode *node)
Definition: GenGraph.h:139
GenNode(graph::Graph *graph=0)
Definition: GenGraph.h:47
N * item(void) const
Definition: GenGraph.h:84
void addAll(const C< N * > &items)
Definition: GenGraph.h:91
graph::Graph * _(void)
Definition: GenGraph.h:141
Graph * graph(void) const
Get the container graph if any.
Definition: Graph.h:75
graph::Graph::Iterator iter
Definition: GenGraph.h:79
void addAll(const C< Node * > &items)
Definition: Graph.h:149
void remove(graph::Edge *edge)
Destroy an edge.
Definition: util_Graph.cpp:120
int countPred(void) const
Definition: GenGraph.h:55
bool isPredecessorOf(N *succ, N *ref) const
Definition: GenGraph.h:118
InIterator(const GenGraph< N, E > &graph, const N *node)
Definition: GenGraph.h:124
int outDegree(N *vertex) const
Definition: GenGraph.h:100
Node * source(void) const
Get the source node.
Definition: Graph.h:116
E * item(void) const
Definition: GenGraph.h:110
void add(GenNode *node)
Definition: GenGraph.h:90
void clear(void)
Definition: util_Graph.cpp:66
N * sourceOf(E *edge) const
Definition: GenGraph.h:116
bool isSuccessorOf(N *succ, N *ref) const
Definition: GenGraph.h:101
Iterator(const GenGraph< N, E > *graph)
Definition: GenGraph.h:81
void next(void)
Definition: GenGraph.h:109
int countSucc(void) const
Count the successors of the current node.
Definition: Graph.h:79
Node * target(void) const
Get the target node.
Definition: Graph.h:117
Iterator(const GenGraph< N, E >::Iterator &iterator)
Definition: GenGraph.h:82
static const graph::Node * _(const GenNode *node)
Definition: GenGraph.h:136
void clear(void)
Definition: GenGraph.h:89
bool isEmpty(void) const
Definition: GenGraph.h:74
virtual ~GenEdge(void)
Definition: GenGraph.h:64
InIterator(const N *node)
Definition: GenGraph.h:123
bool hasPred(void) const
Test if the node has predecessors.
Definition: Graph.h:78
int index(void) const
Definition: GenGraph.h:51
Definition: GenGraph.h:104
The node from a directed graph.
Definition: Graph.h:40
N * target(void) const
Definition: GenGraph.h:68
Definition: GenGraph.h:121
Graph::OutIterator iter
Definition: GenGraph.h:112
void removeAll(const C< N * > &items)
Definition: GenGraph.h:94
N * sinkOf(E *edge) const
Definition: GenGraph.h:99
bool ended(void) const
Definition: GenGraph.h:83
void add(Node *node)
Add new node.
Definition: util_Graph.cpp:90
bool ended(void) const
Definition: GenGraph.h:108
Node(Graph *graph=0)
Build a new node.
Definition: Graph.h:201
bool hasPred(void) const
Definition: GenGraph.h:53
bool hasSucc(void) const
Test if the node has successors.
Definition: Graph.h:77
Definition: GenGraph.h:61
virtual ~GenNode(void)
Definition: GenGraph.h:48
N * Vertex
Definition: GenGraph.h:40
int countPred(void) const
Count the predecessors of the current node.
Definition: Graph.h:85
bool isPredOf(const GenNode *node)
Definition: GenGraph.h:56
void next(void)
Definition: GenGraph.h:126