annotate python/graph.py @ 277:046017431c6a

Started register allocator
author Windel Bouwman
date Thu, 26 Sep 2013 21:14:25 +0200
parents ea93e0a7a31e
children 9fca39eebe50
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 """
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
7 def __init__(self, directed):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
8 self.directed = directed
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
9 self.nodes = set()
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
10 self.edges = set()
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 def newNode(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 n = Node(self)
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
14 self.addNode(n)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
15 return n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
16
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
17 def addNode(self, n):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
18 self.nodes.add(n)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
19
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
20 def delNode(self, n):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
21 self.nodes.remove(n)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
22
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23 def addEdge(self, n, m):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 """ Add an edge from n to m """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 if (n, m) not in self.edges and (m, n) not in self.edges:
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
26 self.edges.add((n, m))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
27
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
28 def hasEdge(self, n, m):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
29 # This can be directional or not :)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
30 if self.directed:
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
31 return (n, m) in self.edges
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
32 else:
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
33 return (n, m) in self.edges or (m, n) in self.edges
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
34
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
35 def adjecent(self, n):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
36 """ Return all nodes with edges to n """
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
37 return set(m for m in self.nodes if self.hasEdge(n, m))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
38
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
39 def preceeding(self, n):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
40 assert self.directed
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
41 return set(m for m in self.nodes if self.hasEdge(m, n))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
42
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
43 def succeeding(self, n):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
44 assert self.directed
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
45 return set(m for m in self.nodes if self.hasEdge(n, m))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 def to_dot(self, f):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 """ Generate graphviz dot representation """
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
49 #print('digraph G {', file=f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 for node in self.nodes:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 print('{} [label="{}" shape=box3d];'.format(id(node), node), file=f)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 for n, m in self.edges:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 print('{} -> {};'.format(id(n), id(m)), file=f)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 270
diff changeset
54 #print('}', file=f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57 class Node:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
58 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
59 Node in a graph.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
60 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
61 def __init__(self, g):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
62 self.g = g
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
63
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
64 @property
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
65 def Succ(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
66 return self.g.succeeding(self)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
67
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
68 @property
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
69 def Pred(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
70 """ Return a set of predecessors """
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
71 return self.g.preceeding(self)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
72
270
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
73 @property
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
74 def Adj(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
75 return self.g.adjecent(self)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
76
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
77 @property
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
78 def Degree(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
79 return len(self.Adj)
270
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
80
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
81