diff options
author | Ulf Adams <ulfjack@google.com> | 2015-03-05 14:47:37 +0000 |
---|---|---|
committer | Han-Wen Nienhuys <hanwen@google.com> | 2015-03-05 18:31:47 +0000 |
commit | 07dba941e21619830adcbcae10c5942cf3343f26 (patch) | |
tree | c0e7b1250bf547b20398a0c3a437864b7b915383 /src/main/java/com/google/devtools/build/lib/graph | |
parent | a34d5071784ff51f68714b61f4100c35f1e4db3a (diff) |
Some cleanup changes.
--
MOS_MIGRATED_REVID=87821306
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/graph')
5 files changed, 26 insertions, 30 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/graph/CollectingVisitor.java b/src/main/java/com/google/devtools/build/lib/graph/CollectingVisitor.java index caeb07ba7a..88311ecc0f 100644 --- a/src/main/java/com/google/devtools/build/lib/graph/CollectingVisitor.java +++ b/src/main/java/com/google/devtools/build/lib/graph/CollectingVisitor.java @@ -22,8 +22,7 @@ import java.util.List; * they were visited, and allows them to be accessed as a list. */ public class CollectingVisitor<T> extends AbstractGraphVisitor<T> { - - private final List<Node<T>> order = new ArrayList<Node<T>>(); + private final List<Node<T>> order = new ArrayList<>(); @Override public void visitNode(Node<T> node) { diff --git a/src/main/java/com/google/devtools/build/lib/graph/DFS.java b/src/main/java/com/google/devtools/build/lib/graph/DFS.java index 37cd30eab6..66d10739a0 100644 --- a/src/main/java/com/google/devtools/build/lib/graph/DFS.java +++ b/src/main/java/com/google/devtools/build/lib/graph/DFS.java @@ -51,7 +51,7 @@ public class DFS<T> { private final boolean transpose; - private final Set<Node<T>> marked = new HashSet<Node<T>>(); + private final Set<Node<T>> marked = new HashSet<>(); /** * Constructs a DFS instance for searching over the enclosing Digraph diff --git a/src/main/java/com/google/devtools/build/lib/graph/Digraph.java b/src/main/java/com/google/devtools/build/lib/graph/Digraph.java index 92e973a2e7..b1ff09a469 100644 --- a/src/main/java/com/google/devtools/build/lib/graph/Digraph.java +++ b/src/main/java/com/google/devtools/build/lib/graph/Digraph.java @@ -289,7 +289,7 @@ public final class Digraph<T> implements Cloneable { * any "root". */ public Set<Node<T>> getRoots() { - Set<Node<T>> roots = new HashSet<Node<T>>(); + Set<Node<T>> roots = new HashSet<>(); for (Node<T> node: nodes.values()) { if (!node.hasPredecessors()) { roots.add(node); @@ -302,7 +302,7 @@ public final class Digraph<T> implements Cloneable { * @return the set of leaf nodes: those with no successors. */ public Set<Node<T>> getLeaves() { - Set<Node<T>> leaves = new HashSet<Node<T>>(); + Set<Node<T>> leaves = new HashSet<>(); for (Node<T> node: nodes.values()) { if (!node.hasSuccessors()) { leaves.add(node); @@ -407,8 +407,7 @@ public final class Digraph<T> implements Cloneable { final Object WHITE = null; // i.e. not present in nodeToColor, the default. final Object GREY = new Object(); final Object BLACK = new Object(); - final Map<Node<T>, Object> nodeToColor = - new HashMap<Node<T>, Object>(); // empty => all white + final Map<Node<T>, Object> nodeToColor = new HashMap<>(); // empty => all white class CycleDetector { /* defining a class gives us lexical scope */ boolean visit(Node<T> node) { @@ -459,7 +458,7 @@ public final class Digraph<T> implements Cloneable { * one strongly-connected component of the graph. */ public Collection<Set<Node<T>>> getStronglyConnectedComponents() { - final List<Set<Node<T>>> sccs = new ArrayList<Set<Node<T>>>(); + final List<Set<Node<T>>> sccs = new ArrayList<>(); NodeSetReceiver<T> r = new NodeSetReceiver<T>() { @Override public void accept(Set<Node<T>> scc) { @@ -491,8 +490,7 @@ public final class Digraph<T> implements Cloneable { createImageUnderPartition(Collection<Set<Node<T>>> partition) { // Build mapping function: each node label is mapped to its equiv class: - Map<T, Set<Node<T>>> labelToImage = - new HashMap<T, Set<Node<T>>>(); + Map<T, Set<Node<T>>> labelToImage = new HashMap<>(); for (Set<Node<T>> set: partition) { // It's important to use immutable sets of node labels when sets are keys // in a map; see ImmutableSet class for explanation. @@ -516,8 +514,7 @@ public final class Digraph<T> implements Cloneable { */ public <IMAGE> Digraph<IMAGE> createImageUnderMapping(Map<T, IMAGE> map) { - - Digraph<IMAGE> imageGraph = new Digraph<IMAGE>(); + Digraph<IMAGE> imageGraph = new Digraph<>(); for (Node<T> fromNode: nodes.values()) { T fromLabel = fromNode.getLabel(); @@ -568,16 +565,15 @@ public final class Digraph<T> implements Cloneable { return Collections.singletonList(fromNode); } - Map<Node<T>, Node<T>> pathPredecessor = - new HashMap<Node<T>, Node<T>>(); + Map<Node<T>, Node<T>> pathPredecessor = new HashMap<>(); - Set<Node<T>> marked = new HashSet<Node<T>>(); + Set<Node<T>> marked = new HashSet<>(); - LinkedList<Node<T>> queue = new LinkedList<Node<T>>(); + LinkedList<Node<T>> queue = new LinkedList<>(); queue.addLast(fromNode); marked.add(fromNode); - while (queue.size() > 0) { + while (!queue.isEmpty()) { Node<T> u = queue.removeFirst(); for (Node<T> v: u.getSuccessors()) { if (marked.add(v)) { @@ -903,13 +899,16 @@ public final class Digraph<T> implements Cloneable { */ private class SccVisitor<T> { // Nodes already assigned to a strongly connected component. - private final Set<Node<T>> assigned = new HashSet<Node<T>>(); + private final Set<Node<T>> assigned = new HashSet<>(); + // The order each node was visited in. - private final Map<Node<T>, Integer> preorder = new HashMap<Node<T>, Integer>(); + private final Map<Node<T>, Integer> preorder = new HashMap<>(); + // Stack of all nodes visited whose SCC has not yet been determined. When an SCC is found, // that SCC is an initial segment of this stack, and is popped off. Every time a new node is // visited, it is put on this stack. - private final List<Node<T>> stack = new ArrayList<Node<T>>(); + private final List<Node<T>> stack = new ArrayList<>(); + // Stack of visited indices for the first-visited nodes in each of their known-so-far // strongly connected components. A node pushes its index on when it is visited. If any of // its successors have already been visited and are not in an already-found strongly connected @@ -922,7 +921,8 @@ public final class Digraph<T> implements Cloneable { // still the current node's index, then it was the first element visited of the current strongly // connected component. So all nodes on {@code stack} down to the current node are in its // strongly connected component. And the node's index is popped from preorderStack. - private final List<Integer> preorderStack = new ArrayList<Integer>(); + private final List<Integer> preorderStack = new ArrayList<>(); + // Index of node being visited. private int counter = 0; @@ -956,7 +956,7 @@ public final class Digraph<T> implements Cloneable { // nodes that were part of a cycle with this node. So this node is the first-visited // element in its strongly connected component, and we collect the component. preorderStack.remove(preorderStack.size() - 1); - Set<Node<T>> scc = new HashSet<Node<T>>(); + Set<Node<T>> scc = new HashSet<>(); Node<T> compNode; do { compNode = stack.remove(stack.size() - 1); diff --git a/src/main/java/com/google/devtools/build/lib/graph/Matrix.java b/src/main/java/com/google/devtools/build/lib/graph/Matrix.java index 225d3d2604..338fcac911 100644 --- a/src/main/java/com/google/devtools/build/lib/graph/Matrix.java +++ b/src/main/java/com/google/devtools/build/lib/graph/Matrix.java @@ -41,7 +41,7 @@ final class Matrix<T> { public Matrix(Set<T> labels) { this.N = labels.size(); this.values = new ArrayList<T>(N); - this.indices = new HashMap<T, Integer>(); + this.indices = new HashMap<>(); this.m = new boolean[N][N]; for (T label: labels) { diff --git a/src/main/java/com/google/devtools/build/lib/graph/Node.java b/src/main/java/com/google/devtools/build/lib/graph/Node.java index 9db2a4c3e7..d271ca0f78 100644 --- a/src/main/java/com/google/devtools/build/lib/graph/Node.java +++ b/src/main/java/com/google/devtools/build/lib/graph/Node.java @@ -163,8 +163,7 @@ public final class Node<T> { int previousSize = set.size(); if (previousSize == 1) { // SingletonList -> ArrayList - Collection<Node<T>> newSet = - new ArrayList<Node<T>>(ARRAYLIST_THRESHOLD); + Collection<Node<T>> newSet = new ArrayList<>(ARRAYLIST_THRESHOLD); newSet.addAll(set); newSet.add(value); return updateField(predecessorSet, newSet); @@ -174,9 +173,8 @@ public final class Node<T> { return true; } else if (previousSize == ARRAYLIST_THRESHOLD) { // ArrayList -> HashSet - Collection<Node<T>> newSet = - new HashSet<Node<T>>(INITIAL_HASHSET_CAPACITY); - newSet.addAll(set); + Collection<Node<T>> newSet = new HashSet<>(INITIAL_HASHSET_CAPACITY); + newSet.addAll(set); newSet.add(value); return updateField(predecessorSet, newSet); } else { @@ -219,8 +217,7 @@ public final class Node<T> { } else if (previousSize == 1 + ARRAYLIST_THRESHOLD) { // -> ArrayList - Collection<Node<T>> newSet = - new ArrayList<Node<T>>(ARRAYLIST_THRESHOLD); + Collection<Node<T>> newSet = new ArrayList<>(ARRAYLIST_THRESHOLD); newSet.addAll(set); return updateField(predecessorSet, newSet); } |