Otawa  0.10
Graph.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Graph class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2005-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_GRAPH_H
23 #define OTAWA_GRAPH_GRAPH_H
24 
25 #include <elm/assert.h>
26 #include <elm/PreIterator.h>
27 #include <elm/genstruct/FragTable.h>
28 #include <elm/util/BitVector.h>
29 #include <elm/genstruct/VectorQueue.h>
30 
31 namespace otawa { namespace graph {
32 
33 // Predefinition
34 class Node;
35 class Edge;
36 class Graph;
37 
38 
39 // Node class
40 class Node {
41  friend class Graph;
42  friend class Edge;
44  int idx;
46  void unlink(void);
47 protected:
48  inline Node(Graph *graph = 0);
49  virtual ~Node(void);
50 public:
51 
52  // Successor class
53  class Successor: public elm::PreIterator<Successor, Node *> {
55  public:
56  inline Successor(const Node *node);
57  inline bool ended(void) const;
58  inline void next(void);
59  inline Node *item(void) const;
60  inline Edge *edge(void) const;
61  };
62 
63  // Predecessor class
64  class Predecessor: public elm::PreIterator<Predecessor, Node *> {
66  public:
67  inline Predecessor(const Node *node);
68  inline bool ended(void) const;
69  inline void next(void);
70  inline Node *item(void) const;
71  inline Edge *edge(void) const;
72  };
73 
74  // Accessors
75  inline Graph *graph(void) const { return _graph; }
76  inline int index(void) const { return idx; }
77  inline bool hasSucc(void) const { return outs; }
78  inline bool hasPred(void) const { return ins; }
79  inline int countSucc(void) const {
80  int cnt = 0;
81  for(Successor edge(this); edge; edge++)
82  cnt++;
83  return cnt;
84  }
85  inline int countPred(void) const {
86  int cnt = 0;
87  for(Predecessor edge(this); edge; edge++)
88  cnt++;
89  return cnt;
90  }
91  inline bool isPredOf(const Node *node);
92  inline bool isSuccOf(const Node *node);
93 };
94 
95 
96 // Edge class
97 class Edge {
98  friend class Node;
99  friend class Graph;
100  friend class Node::Successor;
101  friend class Node::Predecessor;
104 protected:
105  virtual ~Edge(void);
106 public:
107  inline Edge(Node *source, Node *target): src(source), tgt(target) {
108  ASSERT(source->graph() == target->graph());
109  sedges = src->outs;
110  src->outs = this;
111  tedges = tgt->ins;
112  tgt->ins = this;
113  }
114 
115  // Accessors
116  inline Node *source(void) const { return src; }
117  inline Node *target(void) const { return tgt; }
118 };
119 
120 
121 // Graph class
122 class Graph {
123  friend class Node;
124  friend class Edge;
125 public:
128  ~Graph(void);
129  void remove(graph::Edge *edge);
130 
131  // Collection concept
132  inline int count(void) const { return nodes.count(); }
133  inline bool contains(Node *item) const { return nodes.contains(item); }
134  inline bool isEmpty(void) const { return nodes.isEmpty(); }
135  inline operator bool(void) const { return !isEmpty(); }
136 
137  // Iterator class
139  public:
140  inline Iterator(const Graph *graph)
141  : elm::genstruct::FragTable<Node *>::Iterator(graph->nodes) { }
142  inline Iterator(const Iterator& iter)
143  : elm::genstruct::FragTable<Node *>::Iterator(iter) { }
144  };
145 
146  // MutableCollection concept
147  void clear(void);
148  void add(Node *node);
149  template <template <class _> class C> void addAll(const C<Node *> &items)
150  { for(typename C<Node *>::Iterator item(items); item; item++) add(item); }
151  void remove(Node *node);
152  template <template <class _> class C> void removeAll(const C<Node *> &items)
153  { for(typename C<Node *>::Iterator item(items); item; item++) remove(item); }
154  inline void remove(const Iterator& iter) { nodes.remove(iter); }
155 
156  // DiGraph concept
157  Node *sinkOf(graph::Edge *edge) const { return edge->target(); }
158  int outDegree(Node *vertex) const;
159  bool isSuccessorOf(Node *succ, Node *ref) const;
160 
161  // OutIterator class
162  class OutIterator: public elm::PreIterator<OutIterator, graph::Edge *> {
163  public:
164  inline OutIterator(const Node *source): iter(source) { }
165  inline OutIterator(const Graph& graph, const Node *source): iter(source) { }
166  inline OutIterator(const OutIterator& iterator): iter(iterator.iter) { }
167  inline bool ended(void) const { return iter.ended(); }
168  inline graph::Edge *item(void) const { return iter.edge(); }
169  inline void next(void) { iter.next(); }
170  private:
172  };
173 
174  // BiDiGraph concept
175  Node *sourceOf(graph::Edge *edge) const { return edge->source(); }
176  int inDegree(Node *vertex) const;
177  bool isPredecessorOf(Node *prev, Node *ref) const;
178 
179  // InIterator class
180  class InIterator: public elm::PreIterator<InIterator, graph::Edge *> {
181  public:
182  inline InIterator(const Node *source): iter(source) { }
183  inline InIterator(const Graph& graph, const Node *source): iter(source) { }
184  inline InIterator(const InIterator& iterator): iter(iterator.iter) { }
185  inline bool ended(void) const { return iter.ended(); }
186  inline graph::Edge *item(void) const { return iter.edge(); }
187  inline void next(void) { iter.next(); }
188  private:
190  };
191 
192  // DiGraphWithIndexedVertex concept
193  inline int indexOf(Node *vertex) const { return vertex->index(); }
194 
195 private:
197 };
198 
199 
200 // Node Inlines
201 inline Node::Node(Graph *graph)
202 : _graph(0), idx(-1), ins(0), outs(0) {
203  if(graph)
204  graph->add(this);
205 }
206 
207 inline bool Node::isPredOf(const Node *node) {
208  for(Successor succ(this); succ; succ++)
209  if(succ == node)
210  return true;
211  return false;
212 }
213 
214 inline bool Node::isSuccOf(const Node *node) {
215  for(Predecessor pred(this); pred; pred++)
216  if(pred == node)
217  return true;
218  return false;
219 }
220 
221 // Node::Successor inlines
222 inline Node::Successor::Successor(const Node *node): _edge(node->outs) {
223  ASSERT(node);
224 }
225 
226 inline bool Node::Successor::ended(void) const {
227  return !_edge;
228 }
229 
230 inline Node *Node::Successor::item(void) const {
231  return _edge->target();
232 }
233 
234 inline void Node::Successor::next(void) {
235  _edge = _edge->sedges;
236 }
237 
238 inline Edge *Node::Successor::edge(void) const {
239  return _edge;
240 }
241 
242 
243 // Node::Predecessor inlines
244 inline void Node::Predecessor::next(void) {
245  _edge = _edge->tedges;
246 }
247 
248 inline Node::Predecessor::Predecessor(const Node *node): _edge(node->ins) {
249  ASSERT(node);
250 }
251 
252 inline bool Node::Predecessor::ended(void) const {
253  return !_edge;
254 }
255 
256 inline Node *Node::Predecessor::item(void) const {
257  return _edge->source();
258 }
259 
260 inline Edge *Node::Predecessor::edge(void) const {
261  return _edge;
262 }
263 
264 } } // otawa::graph
265 
266 #endif // OTAWA_UTIL_GRAPH_H
267 
A simple iterator on the nodes contained in a graph.
Definition: Graph.h:138
Definition: Graph.h:180
bool ended(void) const
Definition: Graph.h:226
OutIterator(const Node *source)
Definition: Graph.h:164
dtd::RefAttr< BasicBlock * > source("source", dtd::STRICT|dtd::REQUIRED)
Node * sourceOf(graph::Edge *edge) const
Definition: Graph.h:175
InIterator(const InIterator &iterator)
Definition: Graph.h:184
dtd::Element edge(dtd::make("edge", _EDGE).attr(source).attr(target).attr(called))
void next(void)
Definition: Graph.h:244
bool isSuccOf(const Node *node)
Test if the current node is a successor of the given one.
Definition: Graph.h:214
void next(void)
Definition: Graph.h:187
Node::Successor iter
Definition: Graph.h:171
OutIterator(const Graph &graph, const Node *source)
Definition: Graph.h:165
Edge * tedges
Definition: Graph.h:103
Node::Predecessor iter
Definition: Graph.h:189
Graph * _graph
Definition: Graph.h:43
int outDegree(Node *vertex) const
Get the out degree of the given vertex.
Definition: util_Graph.cpp:150
int index(void) const
Get the index of the node in the graph.
Definition: Graph.h:76
void removeAll(const C< Node * > &items)
Definition: Graph.h:152
Predecessor(const Node *node)
Build an iterator on the predecessors of the given node.
Definition: Graph.h:248
void next(void)
Definition: Graph.h:234
This class represents a full graph with nodes and edges.
Definition: Graph.h:122
Successor(const Node *node)
Build an iterator on the successor of the given node.
Definition: Graph.h:222
This class represents a directed edge between two nodes.
Definition: Graph.h:97
Node * item(void) const
Definition: Graph.h:256
int indexOf(Node *vertex) const
Definition: Graph.h:193
Iterator(const Iterator &iter)
Definition: Graph.h:142
Node * tgt
Definition: Graph.h:102
Edge * _edge
Definition: Graph.h:65
int idx
Definition: Graph.h:44
Edge * edge(void) const
Get the edge leading to the current predecessor.
Definition: Graph.h:260
bool isEmpty(void) const
Definition: Graph.h:134
int count(void) const
Definition: Graph.h:132
Edge * ins
Definition: Graph.h:45
Node * src
Definition: Graph.h:102
Graph * graph(void) const
Get the container graph if any.
Definition: Graph.h:75
bool isSuccessorOf(Node *succ, Node *ref) const
Test if the vertex succ is successor of the vertex ref.
Definition: util_Graph.cpp:164
This class allows to iterates on the successors of a node.
Definition: Graph.h:53
Edge(Node *source, Node *target)
Build an edge between two nodes.
Definition: Graph.h:107
void next(void)
Definition: Graph.h:169
graph::Edge * item(void) const
Definition: Graph.h:168
void addAll(const C< Node * > &items)
Definition: Graph.h:149
bool ended(void) const
Definition: Graph.h:167
otawa::graph::Edge * Edge
Definition: Graph.h:127
OutIterator(const OutIterator &iterator)
Definition: Graph.h:166
Node * source(void) const
Get the source node.
Definition: Graph.h:116
void clear(void)
Definition: util_Graph.cpp:66
Edge * sedges
Definition: Graph.h:103
bool ended(void) const
Definition: Graph.h:252
~Graph(void)
Definition: util_Graph.cpp:81
InIterator(const Node *source)
Definition: Graph.h:182
bool isPredecessorOf(Node *prev, Node *ref) const
Test if the vertex pred is predecessor of the vertex ref.
Definition: util_Graph.cpp:191
int countSucc(void) const
Count the successors of the current node.
Definition: Graph.h:79
InIterator(const Graph &graph, const Node *source)
Definition: Graph.h:183
Node * target(void) const
Get the target node.
Definition: Graph.h:117
otawa::graph::Node * Vertex
Definition: Graph.h:126
bool ended(void) const
Definition: Graph.h:185
bool hasPred(void) const
Test if the node has predecessors.
Definition: Graph.h:78
Edge * outs
Definition: Graph.h:45
bool isPredOf(const Node *node)
Test if the current node is a predecessor of the given one.
Definition: Graph.h:207
elm::genstruct::FragTable< Node * > nodes
Definition: Graph.h:196
bool contains(Node *item) const
Definition: Graph.h:133
Node * sinkOf(graph::Edge *edge) const
Definition: Graph.h:157
The node from a directed graph.
Definition: Graph.h:40
virtual ~Edge(void)
Definition: util_Edge.cpp:24
Definition: Graph.h:162
graph::Edge * item(void) const
Definition: Graph.h:186
void add(Node *node)
Add new node.
Definition: util_Graph.cpp:90
Node(Graph *graph=0)
Build a new node.
Definition: Graph.h:201
Iterator(const Graph *graph)
Definition: Graph.h:140
virtual ~Node(void)
Definition: util_Node.cpp:40
void unlink(void)
Unlink the node, that is, remove its entering or leaving edges.
Definition: util_Node.cpp:30
bool hasSucc(void) const
Test if the node has successors.
Definition: Graph.h:77
Node * item(void) const
Definition: Graph.h:230
int countPred(void) const
Count the predecessors of the current node.
Definition: Graph.h:85
int inDegree(Node *vertex) const
Get the in degree of the given vertex.
Definition: util_Graph.cpp:177
This class allows to iterates on the predecessors of a node.
Definition: Graph.h:64
Edge * _edge
Definition: Graph.h:54
Edge * edge(void) const
Get the edge leading to the current successor.
Definition: Graph.h:238