Skip to content

Commit

Permalink
Use a Multimap instead of a Map of Lists
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146183803
  • Loading branch information
tbreisacher authored and blickly committed Feb 1, 2017
1 parent ecc937a commit f63ad19
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java
Expand Up @@ -21,16 +21,16 @@
import com.google.common.base.Supplier;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Multiset;
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.TokenStream;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -283,7 +283,8 @@ static class ContextualRenameInverter
private Deque<Set<String>> referenceStack = new ArrayDeque<>();

// Name are globally unique initially, so we don't need a per-scope map.
private Map<String, List<Node>> nameMap = new HashMap<>();
private final ListMultimap<String, Node> nameMap =
MultimapBuilder.hashKeys().arrayListValues().build();

private ContextualRenameInverter(AbstractCompiler compiler) {
this.compiler = compiler;
Expand Down Expand Up @@ -360,13 +361,12 @@ void handleScopeVar(Var v) {
// scopes or the current scope renaming another var to this new name.
referencedNames.add(newName);
List<Node> references = nameMap.get(name);
Preconditions.checkState(references != null);
for (Node n : references) {
Preconditions.checkState(n.isName(), n);
n.setString(newName);
}
compiler.reportCodeChange();
nameMap.remove(name);
nameMap.removeAll(name);
}
}

Expand Down Expand Up @@ -415,12 +415,7 @@ public void visit(NodeTraversal t, Node node, Node parent) {
}

private void addCandidateNameReference(String name, Node n) {
List<Node> nodes = nameMap.get(name);
if (null == nodes) {
nodes = new LinkedList<>();
nameMap.put(name, nodes);
}
nodes.add(n);
nameMap.put(name, n);
}
}

Expand Down

0 comments on commit f63ad19

Please sign in to comment.