| 1 |
import java.util.ArrayList; |
| 2 |
import java.util.Collections; |
| 3 |
import java.util.HashMap; |
| 4 |
import java.util.List; |
| 5 |
import java.util.Map; |
| 6 |
import java.util.Set; |
| 7 |
import org.checkerframework.checker.nullness.qual.*; |
| 8 |
|
| 9 |
class MethodTypeVars3 { |
| 10 |
public static <@KeyFor("#1") T extends @KeyFor("#1") Object> Map<T, List<T>> dominators( |
| 11 |
Map<T, List<T>> preds) { |
| 12 |
List<T> nodes = new ArrayList<>(preds.keySet()); |
| 13 |
|
| 14 |
// Compute roots & non-roots, for convenience |
| 15 |
List<@KeyFor("preds") T> roots = new ArrayList<>(); |
| 16 |
List<@KeyFor("preds") T> non_roots = new ArrayList<>(); |
| 17 |
|
| 18 |
Map<@KeyFor("preds") T, List<T>> dom = new HashMap<>(); |
| 19 |
|
| 20 |
// Initialize result: for roots just the root, otherwise everything |
| 21 |
for (@KeyFor("preds") T node : preds.keySet()) { |
| 22 |
if (preds.get(node).isEmpty()) { |
| 23 |
// This is a root |
| 24 |
roots.add(node); |
| 25 |
// Its only dominator is itself. |
| 26 |
Set<@KeyFor("preds") T> set = Collections.singleton(node); |
| 27 |
|
| 28 |
dom.put(node, new ArrayList<T>(set)); |
| 29 |
|
| 30 |
dom.put(node, new ArrayList<T>(Collections.singleton(node))); |
| 31 |
} else { |
| 32 |
non_roots.add(node); |
| 33 |
dom.put(node, new ArrayList<T>(nodes)); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return dom; |
| 38 |
} |
| 39 |
|
| 40 |
<XXX extends Object> void test(Map<XXX, List<XXX>> dom, XXX node) { |
| 41 |
dom.put(node, new ArrayList<XXX>(Collections.singleton(node))); |
| 42 |
} |
| 43 |
} |