| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.deps; |
| 18 |
|
| 19 |
import com.google.common.base.Joiner; |
| 20 |
import com.google.common.base.Preconditions; |
| 21 |
import com.google.common.collect.ArrayListMultimap; |
| 22 |
import com.google.common.collect.HashMultimap; |
| 23 |
import com.google.common.collect.HashMultiset; |
| 24 |
import com.google.common.collect.ImmutableList; |
| 25 |
import com.google.common.collect.Lists; |
| 26 |
import com.google.common.collect.Maps; |
| 27 |
import com.google.common.collect.Multimap; |
| 28 |
import com.google.common.collect.Multimaps; |
| 29 |
import com.google.common.collect.Multiset; |
| 30 |
import com.google.common.collect.Sets; |
| 31 |
|
| 32 |
import java.util.ArrayDeque; |
| 33 |
import java.util.Collection; |
| 34 |
import java.util.Collections; |
| 35 |
import java.util.Comparator; |
| 36 |
import java.util.Deque; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
import java.util.PriorityQueue; |
| 40 |
import java.util.Set; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class SortedDependencies<INPUT extends DependencyInfo> { |
| 53 |
|
| 54 |
private final List<INPUT> inputs; |
| 55 |
|
| 56 |
|
| 57 |
private final List<INPUT> sortedList; |
| 58 |
|
| 59 |
|
| 60 |
private final List<INPUT> noProvides; |
| 61 |
|
| 62 |
private final Map<String, INPUT> provideMap = Maps.newHashMap(); |
| 63 |
|
| 64 |
public SortedDependencies(List<INPUT> inputs) |
| 65 |
throws CircularDependencyException { |
| 66 |
this.inputs = Lists.newArrayList(inputs); |
| 67 |
noProvides = Lists.newArrayList(); |
| 68 |
|
| 69 |
|
| 70 |
for (INPUT input : inputs) { |
| 71 |
Collection<String> currentProvides = input.getProvides(); |
| 72 |
if (currentProvides.isEmpty()) { |
| 73 |
noProvides.add(input); |
| 74 |
} |
| 75 |
|
| 76 |
for (String provide : currentProvides) { |
| 77 |
provideMap.put(provide, input); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
final Multimap<INPUT, INPUT> deps = HashMultimap.create(); |
| 83 |
for (INPUT input : inputs) { |
| 84 |
for (String req : input.getRequires()) { |
| 85 |
INPUT dep = provideMap.get(req); |
| 86 |
if (dep != null && dep != input) { |
| 87 |
deps.put(input, dep); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
sortedList = topologicalStableSort(inputs, deps); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if (sortedList.size() < inputs.size()) { |
| 102 |
List<INPUT> subGraph = Lists.newArrayList(inputs); |
| 103 |
subGraph.removeAll(sortedList); |
| 104 |
|
| 105 |
throw new CircularDependencyException( |
| 106 |
cycleToString(findCycle(subGraph, deps))); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public INPUT getInputProviding(String symbol) |
| 116 |
throws MissingProvideException { |
| 117 |
if (provideMap.containsKey(symbol)) { |
| 118 |
return provideMap.get(symbol); |
| 119 |
} |
| 120 |
throw new MissingProvideException(symbol); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public INPUT maybeGetInputProviding(String symbol) { |
| 127 |
return provideMap.get(symbol); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private List<INPUT> findCycle( |
| 136 |
List<INPUT> subGraph, Multimap<INPUT, INPUT> deps) { |
| 137 |
return findCycle(subGraph.get(0), Sets.<INPUT>newHashSet(subGraph), |
| 138 |
deps, Sets.<INPUT>newHashSet()); |
| 139 |
} |
| 140 |
|
| 141 |
private List<INPUT> findCycle( |
| 142 |
INPUT current, Set<INPUT> subGraph, Multimap<INPUT, INPUT> deps, |
| 143 |
Set<INPUT> covered) { |
| 144 |
if (covered.add(current)) { |
| 145 |
List<INPUT> cycle = findCycle( |
| 146 |
findRequireInSubGraphOrFail(current, subGraph), |
| 147 |
subGraph, deps, covered); |
| 148 |
|
| 149 |
|
| 150 |
if (cycle.get(0) != cycle.get(cycle.size() - 1)) { |
| 151 |
cycle.add(current); |
| 152 |
} |
| 153 |
|
| 154 |
return cycle; |
| 155 |
} else { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
List<INPUT> cycle = Lists.<INPUT>newArrayList(); |
| 160 |
cycle.add(current); |
| 161 |
return cycle; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private INPUT findRequireInSubGraphOrFail(INPUT input, Set<INPUT> subGraph) { |
| 166 |
for (String symbol : input.getRequires()) { |
| 167 |
INPUT candidate = provideMap.get(symbol); |
| 168 |
if (subGraph.contains(candidate)) { |
| 169 |
return candidate; |
| 170 |
} |
| 171 |
} |
| 172 |
throw new IllegalStateException("no require found in subgraph"); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private String cycleToString(List<INPUT> cycle) { |
| 179 |
List<String> symbols = Lists.newArrayList(); |
| 180 |
for (int i = cycle.size() - 1; i >= 0; i--) { |
| 181 |
symbols.add(cycle.get(i).getProvides().iterator().next()); |
| 182 |
} |
| 183 |
symbols.add(symbols.get(0)); |
| 184 |
return Joiner.on(" -> ").join(symbols); |
| 185 |
} |
| 186 |
|
| 187 |
public List<INPUT> getSortedList() { |
| 188 |
return Collections.<INPUT>unmodifiableList(sortedList); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public List<INPUT> getSortedDependenciesOf(List<INPUT> roots) { |
| 198 |
return getDependenciesOf(roots, true); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public List<INPUT> getDependenciesOf(List<INPUT> roots, boolean sorted) { |
| 211 |
Preconditions.checkArgument(inputs.containsAll(roots)); |
| 212 |
Set<INPUT> included = Sets.newHashSet(); |
| 213 |
Deque<INPUT> worklist = new ArrayDeque<INPUT>(roots); |
| 214 |
while (!worklist.isEmpty()) { |
| 215 |
INPUT current = worklist.pop(); |
| 216 |
if (included.add(current)) { |
| 217 |
for (String req : current.getRequires()) { |
| 218 |
INPUT dep = provideMap.get(req); |
| 219 |
if (dep != null) { |
| 220 |
worklist.add(dep); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
ImmutableList.Builder<INPUT> builder = ImmutableList.builder(); |
| 227 |
for (INPUT current : (sorted ? sortedList : inputs)) { |
| 228 |
if (included.contains(current)) { |
| 229 |
builder.add(current); |
| 230 |
} |
| 231 |
} |
| 232 |
return builder.build(); |
| 233 |
} |
| 234 |
|
| 235 |
public List<INPUT> getInputsWithoutProvides() { |
| 236 |
return Collections.<INPUT>unmodifiableList(noProvides); |
| 237 |
} |
| 238 |
|
| 239 |
private static <T> List<T> topologicalStableSort( |
| 240 |
List<T> items, Multimap<T, T> deps) { |
| 241 |
if (items.size() == 0) { |
| 242 |
|
| 243 |
|
| 244 |
return Lists.newArrayList(); |
| 245 |
} |
| 246 |
|
| 247 |
final Map<T, Integer> originalIndex = Maps.newHashMap(); |
| 248 |
for (int i = 0; i < items.size(); i++) { |
| 249 |
originalIndex.put(items.get(i), i); |
| 250 |
} |
| 251 |
|
| 252 |
PriorityQueue<T> inDegreeZero = new PriorityQueue<T>(items.size(), |
| 253 |
new Comparator<T>() { |
| 254 |
@Override |
| 255 |
public int compare(T a, T b) { |
| 256 |
return originalIndex.get(a).intValue() - |
| 257 |
originalIndex.get(b).intValue(); |
| 258 |
} |
| 259 |
}); |
| 260 |
List<T> result = Lists.newArrayList(); |
| 261 |
|
| 262 |
Multiset<T> inDegree = HashMultiset.create(); |
| 263 |
Multimap<T, T> reverseDeps = ArrayListMultimap.create(); |
| 264 |
Multimaps.invertFrom(deps, reverseDeps); |
| 265 |
|
| 266 |
|
| 267 |
for (T item : items) { |
| 268 |
Collection<T> itemDeps = deps.get(item); |
| 269 |
inDegree.add(item, itemDeps.size()); |
| 270 |
if (itemDeps.isEmpty()) { |
| 271 |
inDegreeZero.add(item); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
while (!inDegreeZero.isEmpty()) { |
| 277 |
T item = inDegreeZero.remove(); |
| 278 |
result.add(item); |
| 279 |
for (T inWaiting : reverseDeps.get(item)) { |
| 280 |
inDegree.remove(inWaiting, 1); |
| 281 |
if (inDegree.count(inWaiting) == 0) { |
| 282 |
inDegreeZero.add(inWaiting); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return result; |
| 288 |
} |
| 289 |
|
| 290 |
public static class CircularDependencyException extends Exception { |
| 291 |
CircularDependencyException(String message) { |
| 292 |
super(message); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
public static class MissingProvideException extends Exception { |
| 297 |
MissingProvideException(String provide) { |
| 298 |
super(provide); |
| 299 |
} |
| 300 |
} |
| 301 |
} |