KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > graph > HashMutableDirectedGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrice Pominville, Raja Vallee-Rai, Patrick Lam
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.toolkits.graph;
28
29
30 import java.util.*;
31 import soot.*;
32 import soot.util.*;
33
34
35
36
37 /**
38  * HashMap based implementation of a MutableBlockGraph.
39  */

40
41 public class HashMutableDirectedGraph implements MutableDirectedGraph {
42         
43
44     protected HashMap nodeToPreds = new HashMap();
45     protected HashMap nodeToSuccs = new HashMap();
46
47     protected Chain heads = new HashChain();
48     protected Chain tails = new HashChain();
49
50     public HashMutableDirectedGraph()
51     {
52     }
53
54     /** Removes all nodes and edges. */
55     public void clearAll() {
56         nodeToPreds = new HashMap();
57         nodeToSuccs = new HashMap();
58         heads = new HashChain();
59         tails = new HashChain();
60     }
61
62     public Object JavaDoc clone() {
63         HashMutableDirectedGraph g = new HashMutableDirectedGraph();
64         g.nodeToPreds = (HashMap)nodeToPreds.clone();
65         g.nodeToSuccs = (HashMap)nodeToSuccs.clone();
66         g.heads = HashChain.listToHashChain(HashChain.toList(heads));
67         g.tails = HashChain.listToHashChain(HashChain.toList(tails));
68         return g;
69     }
70
71     /* Returns an unbacked list of heads for this graph. */
72     public List getHeads()
73     {
74         ArrayList l = new ArrayList(); l.addAll(heads);
75         return Collections.unmodifiableList(l);
76     }
77
78     /* Returns an unbacked list of tails for this graph. */
79     public List getTails()
80     {
81         ArrayList l = new ArrayList(); l.addAll(tails);
82         return Collections.unmodifiableList(l);
83     }
84
85     public List getPredsOf(Object JavaDoc s)
86     {
87         List l = (List) nodeToPreds.get(s);
88         if (l != null)
89             return Collections.unmodifiableList(l);
90         else
91             throw new RuntimeException JavaDoc(s+"not in graph!");
92     }
93
94     public List getSuccsOf(Object JavaDoc s)
95     {
96         List l = (List) nodeToSuccs.get(s);
97         if (l != null)
98             return Collections.unmodifiableList(l);
99         else
100             throw new RuntimeException JavaDoc(s+"not in graph!");
101     }
102
103     public int size()
104     {
105         return nodeToPreds.keySet().size();
106     }
107
108     public Iterator iterator()
109     {
110         return nodeToPreds.keySet().iterator();
111     }
112
113     public void addEdge(Object JavaDoc from, Object JavaDoc to)
114     {
115         if (from == null || to == null)
116                         throw new RuntimeException JavaDoc("edge from or to null");
117
118         if (containsEdge(from, to))
119             return;
120
121         List succsList = (List)nodeToSuccs.get(from);
122         if (succsList == null)
123             throw new RuntimeException JavaDoc(from + " not in graph!");
124
125         List predsList = (List)nodeToPreds.get(to);
126         if (predsList == null)
127             throw new RuntimeException JavaDoc(to + " not in graph!");
128
129         if (heads.contains(to))
130             heads.remove(to);
131
132         if (tails.contains(from))
133             tails.remove(from);
134
135         succsList.add(to);
136         predsList.add(from);
137     }
138
139     public void removeEdge(Object JavaDoc from, Object JavaDoc to)
140     {
141         if (!containsEdge(from, to))
142             return;
143
144         List succsList = (List)nodeToSuccs.get(from);
145         if (succsList == null)
146             throw new RuntimeException JavaDoc(from + " not in graph!");
147
148         List predsList = (List)nodeToPreds.get(to);
149         if (predsList == null)
150             throw new RuntimeException JavaDoc(to + " not in graph!");
151
152         succsList.remove(to);
153         predsList.remove(from);
154
155         if (succsList.isEmpty())
156             tails.add(from);
157
158         if (predsList.isEmpty())
159             heads.add(to);
160     }
161
162     public boolean containsEdge(Object JavaDoc from, Object JavaDoc to)
163     {
164                 List succs = (List)nodeToSuccs.get(from);
165                 if (succs == null)
166                         return false;
167         return succs.contains(to);
168     }
169
170     public boolean containsNode(Object JavaDoc node)
171     {
172         return nodeToPreds.keySet().contains(node);
173     }
174
175     public List getNodes()
176     {
177         return Arrays.asList(nodeToPreds.keySet().toArray());
178     }
179
180     public void addNode(Object JavaDoc node)
181     {
182                 if (containsNode(node))
183                         throw new RuntimeException JavaDoc("Node already in graph");
184                 
185                 nodeToSuccs.put(node, new ArrayList());
186         nodeToPreds.put(node, new ArrayList());
187         heads.add(node);
188                 tails.add(node);
189     }
190
191     public void removeNode(Object JavaDoc node)
192     {
193         List succs = (List)((ArrayList)nodeToSuccs.get(node)).clone();
194         for (Iterator succsIt = succs.iterator(); succsIt.hasNext(); )
195             removeEdge(node, succsIt.next());
196         nodeToSuccs.remove(node);
197
198         List preds = (List)((ArrayList)nodeToPreds.get(node)).clone();
199         for (Iterator predsIt = preds.iterator(); predsIt.hasNext(); )
200             removeEdge(predsIt.next(), node);
201         nodeToPreds.remove(node);
202
203         if (heads.contains(node))
204             heads.remove(node);
205
206         if (tails.contains(node))
207             tails.remove(node);
208     }
209
210     public void printGraph() {
211
212     for (Iterator it = iterator(); it.hasNext(); ) {
213         Object JavaDoc node = it.next();
214         G.v().out.println("Node = "+node);
215         G.v().out.println("Preds:");
216         for (Iterator predsIt = getPredsOf(node).iterator(); predsIt.hasNext(); ) {
217         G.v().out.print(" ");
218         G.v().out.println(predsIt.next());
219         }
220         G.v().out.println("Succs:");
221         for (Iterator succsIt = getSuccsOf(node).iterator(); succsIt.hasNext(); ) {
222         G.v().out.print(" ");
223         G.v().out.println(succsIt.next());
224         }
225     }
226     }
227
228 }
229
230  
231
Popular Tags