annotate python/graph.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents cdc76d183bcc
children ea93e0a7a31e
rev   line source
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2 class Graph:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
4 Generic graph base class.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5 Can dump to graphiz dot format for example!
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 def __init__(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 self.nodes = []
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
9 self.edges = []
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11 def newNode(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 n = Node(self)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 self.nodes.append(n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14 return n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
15
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
16 def addEdge(self, n, m):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
17 """ Add an edge from n to m """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
18 if (n, m) not in self.edges and (m, n) not in self.edges:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
19 self.edges.append((n, m))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
20 n.succ.append(m)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
21 m.pred.append(n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23 def delEdge(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 # TODO
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 pass
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
26
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 def to_dot(self, f):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28 """ Generate graphviz dot representation """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 print('digraph G {', file=f)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
30 for node in self.nodes:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
31 print('{} [label="{}" shape=box3d];'.format(id(node), node), file=f)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
32 for n, m in self.edges:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
33 print('{} -> {};'.format(id(n), id(m)), file=f)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
34 print('}', file=f)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 class Node:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 Node in a graph.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 def __init__(self, g):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42 self.g = g
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 self.succ = []
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 self.pred = []
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45
270
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
46 @property
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
47 def Adj(self):
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
48 return set(self.succ) | set(self.pred)
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
49
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
50