1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* Fernflower - The Analytical Java Decompiler
* http://www.reversed-java.com
*
* (C) 2008 - 2010, Stiver
*
* This software is NEITHER public domain NOR free software
* as per GNU License. See license.txt for more details.
*
* This software is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE.
*/
package org.jetbrains.java.decompiler.modules.decompiler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.util.ListStack;
// --------------------------------------------------------------------
// Algorithm
// --------------------------------------------------------------------
// DFS(G)
// {
// make a new vertex x with edges x->v for all v
// initialize a counter N to zero
// initialize list L to empty
// build directed tree T, initially a single vertex {x}
// visit(x)
// }
//
// visit(p)
// {
// add p to L
// dfsnum(p) = N
// increment N
// low(p) = dfsnum(p)
// for each edge p->q
// if q is not already in T
// {
// add p->q to T
// visit(q)
// low(p) = min(low(p), low(q))
// } else low(p) = min(low(p), dfsnum(q))
//
// if low(p)=dfsnum(p)
// {
// output "component:"
// repeat
// remove last element v from L
// output v
// remove v from G
// until v=p
// }
// }
// --------------------------------------------------------------------
public class StrongConnectivityHelper {
private ListStack<Statement> lstack;
private int ncounter;
private HashSet<Statement> tset;
private HashMap<Statement, Integer> dfsnummap;
private HashMap<Statement, Integer> lowmap;
private List<List<Statement>> components;
private HashSet<Statement> setProcessed;
// *****************************************************************************
// constructors
// *****************************************************************************
public StrongConnectivityHelper() {}
public StrongConnectivityHelper(Statement stat) {
findComponents(stat);
}
// *****************************************************************************
// public methods
// *****************************************************************************
public List<List<Statement>> findComponents(Statement stat) {
components = new ArrayList<List<Statement>>();
setProcessed = new HashSet<Statement>();
visitTree(stat.getFirst());
for(Statement st: stat.getStats()) {
if(!setProcessed.contains(st) && st.getPredecessorEdges(Statement.STATEDGE_DIRECT_ALL).isEmpty()) {
visitTree(st);
}
}
// should not find any more nodes! FIXME: ??
for(Statement st: stat.getStats()) {
if(!setProcessed.contains(st)) {
visitTree(st);
}
}
return components;
}
public static boolean isExitComponent(List<Statement> lst) {
HashSet<Statement> set = new HashSet<Statement>();
for(Statement stat : lst) {
set.addAll(stat.getNeighbours(StatEdge.TYPE_REGULAR, Statement.DIRECTION_FORWARD));
}
set.removeAll(lst);
return (set.size() == 0);
}
public static List<Statement> getExitReps(List<List<Statement>> lst) {
List<Statement> res = new ArrayList<Statement>();
for(List<Statement> comp : lst) {
if(isExitComponent(comp)) {
res.add(comp.get(0));
}
}
return res;
}
// *****************************************************************************
// private methods
// *****************************************************************************
private void visitTree(Statement stat) {
lstack = new ListStack<Statement>();
ncounter = 0;
tset = new HashSet<Statement>();
dfsnummap = new HashMap<Statement, Integer>();
lowmap = new HashMap<Statement, Integer>();
visit(stat);
setProcessed.addAll(tset);
setProcessed.add(stat);
}
private void visit(Statement stat) {
lstack.push(stat);
dfsnummap.put(stat, ncounter);
lowmap.put(stat, ncounter);
ncounter++;
List<Statement> lstSuccs = stat.getNeighbours(StatEdge.TYPE_REGULAR, Statement.DIRECTION_FORWARD); // TODO: set?
lstSuccs.removeAll(setProcessed);
for(int i=0;i<lstSuccs.size();i++) {
Statement succ = lstSuccs.get(i);
int secvalue;
if(tset.contains(succ)) {
secvalue = dfsnummap.get(succ);
} else {
tset.add(succ);
visit(succ);
secvalue = lowmap.get(succ);
}
lowmap.put(stat, Math.min(lowmap.get(stat), secvalue));
}
if(lowmap.get(stat).intValue() == dfsnummap.get(stat).intValue()) {
List<Statement> lst = new ArrayList<Statement>();
Statement v;
do {
v = lstack.pop();
lst.add(v);
} while(v!=stat);
components.add(lst);
}
}
// *****************************************************************************
// getter and setter methods
// *****************************************************************************
public List<List<Statement>> getComponents() {
return components;
}
public void setComponents(List<List<Statement>> components) {
this.components = components;
}
}
|