KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > typing > integer > StronglyConnectedComponents


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-2000 Etienne Gagnon. All rights reserved.
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.jimple.toolkits.typing.integer;
28
29 import soot.*;
30 import soot.jimple.*;
31 import soot.util.*;
32 import java.util.*;
33
34 class StronglyConnectedComponents
35 {
36   List variables;
37   Set black;
38   LinkedList finished;
39   
40   LinkedList forest = new LinkedList();
41   LinkedList current_tree;
42   
43   private static final boolean DEBUG = false;
44   
45   public static void merge(List typeVariableList) throws TypeException
46   {
47     new StronglyConnectedComponents(typeVariableList);
48   }
49
50   private StronglyConnectedComponents(List typeVariableList) throws TypeException
51   {
52     variables = typeVariableList;
53     
54     black = new TreeSet();
55     finished = new LinkedList();
56     
57     for(Iterator i = variables.iterator(); i.hasNext(); )
58       {
59     TypeVariable var = (TypeVariable) i.next();
60
61     if(!black.contains(var))
62       {
63         black.add(var);
64         dfsg_visit(var);
65       }
66       }
67     
68     black = new TreeSet();
69     
70     for(Iterator i = finished.iterator(); i.hasNext(); )
71       {
72     TypeVariable var = (TypeVariable) i.next();
73     
74     if(!black.contains(var))
75       {
76         current_tree = new LinkedList();
77         forest.add(current_tree);
78         black.add(var);
79         dfsgt_visit(var);
80       }
81       }
82     
83     for(Iterator i = forest.iterator(); i.hasNext();)
84       {
85     LinkedList list = (LinkedList) i.next();
86     TypeVariable previous = null;
87     StringBuffer JavaDoc s = null;
88     if(DEBUG)
89       {
90         s = new StringBuffer JavaDoc("scc:\n");
91       }
92     
93     for(Iterator j = list.iterator(); j.hasNext();)
94       {
95         TypeVariable current = (TypeVariable) j.next();
96        
97         if(DEBUG)
98           {
99         s.append(" " + current + "\n");
100           }
101
102         if(previous == null)
103           {
104         previous = current;
105           }
106         else
107           {
108         try
109           {
110             previous = previous.union(current);
111           }
112         catch(TypeException e)
113           {
114             if(DEBUG)
115               {
116             G.v().out.println(s);
117               }
118             throw e;
119           }
120           }
121       }
122       }
123   }
124   
125   private void dfsg_visit(TypeVariable var)
126   {
127     List parents = var.parents();
128     
129     for(Iterator i = parents.iterator(); i.hasNext(); )
130       {
131     TypeVariable parent = (TypeVariable) i.next();
132
133     if(!black.contains(parent))
134       {
135         black.add(parent);
136         dfsg_visit(parent);
137       }
138       }
139     
140     finished.add(0, var);
141   }
142   
143   private void dfsgt_visit(TypeVariable var)
144   {
145     current_tree.add(var);
146     
147     List children = var.children();
148     
149     for(Iterator i = children.iterator(); i.hasNext(); )
150       {
151     TypeVariable child = (TypeVariable) i.next();
152
153     if(!black.contains(child))
154       {
155         black.add(child);
156         dfsgt_visit(child);
157       }
158       }
159   }
160 }
161
Popular Tags