-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Refaster matching algorithm #104
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aba1e92
Improve Refaster matching algorithm
Stephan202 9cc6c84
Suggestions
Stephan202 763d81e
Remove unused import
rickie 711169c
Post rebase fixes
rickie dac45fb
Second round of fixes to let the build pass
rickie 125ee81
Add XXX and format
rickie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Suggestions
- Loading branch information
commit 9cc6c8448509bbcab8a77babe76adedc9aba9ea2
There are no files selected for viewing
52 changes: 0 additions & 52 deletions
52
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/BuildNode.java
This file was deleted.
Oops, something went wrong.
96 changes: 67 additions & 29 deletions
96
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,99 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.ImmutableSortedMap; | ||
import com.google.common.collect.ImmutableSortedSet; | ||
import com.google.common.collect.Maps; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A node in an immutable tree. | ||
* | ||
* <p>The tree's edges are string-labeled, while its leaves store values of type {@code T}. | ||
*/ | ||
@AutoValue | ||
abstract class Node<T> { | ||
static <T> Node<T> create(Map<String, Node<T>> children, ImmutableSet<T> candidateRules) { | ||
return new AutoValue_Node<>(ImmutableMap.copyOf(children), candidateRules); | ||
static <T> Node<T> create(Map<String, Node<T>> children, ImmutableList<T> values) { | ||
return new AutoValue_Node<>(ImmutableSortedMap.copyOf(children), values); | ||
} | ||
|
||
public abstract ImmutableMap<String, Node<T>> children(); | ||
|
||
public abstract ImmutableSet<T> candidateRules(); | ||
|
||
static <C> Node<C> createRefasterTemplateTree( | ||
List<C> refasterRules, Function<C, ImmutableSet<ImmutableSortedSet<String>>> edgeExtractor) { | ||
// XXX: Improve this method... | ||
List<ImmutableSet<ImmutableSortedSet<String>>> beforeTemplateIdentifiers = | ||
refasterRules.stream().map(edgeExtractor).collect(toImmutableList()); | ||
|
||
BuildNode<C> tree = BuildNode.create(); | ||
for (int i = 0; i < refasterRules.size(); i++) { | ||
tree.register(beforeTemplateIdentifiers.get(i), refasterRules.get(i)); | ||
} | ||
static <T> Node<T> create( | ||
List<T> values, Function<T, ImmutableSet<ImmutableSortedSet<String>>> pathExtractor) { | ||
BuildNode<T> tree = BuildNode.create(); | ||
tree.register(values, pathExtractor); | ||
return tree.immutable(); | ||
} | ||
|
||
void collectCandidateTemplates(ImmutableList<String> sourceIdentifiers, Consumer<T> sink) { | ||
candidateRules().forEach(sink); | ||
abstract ImmutableMap<String, Node<T>> children(); | ||
|
||
if (sourceIdentifiers.isEmpty() || children().isEmpty()) { | ||
abstract ImmutableList<T> values(); | ||
|
||
void collectCandidateTemplates(ImmutableList<String> candidateEdges, Consumer<T> sink) { | ||
values().forEach(sink); | ||
|
||
if (candidateEdges.isEmpty() || children().isEmpty()) { | ||
return; | ||
} | ||
|
||
if (children().size() < sourceIdentifiers.size()) { | ||
if (children().size() < candidateEdges.size()) { | ||
for (Map.Entry<String, Node<T>> e : children().entrySet()) { | ||
if (sourceIdentifiers.contains(e.getKey())) { | ||
e.getValue().collectCandidateTemplates(sourceIdentifiers, sink); | ||
if (candidateEdges.contains(e.getKey())) { | ||
e.getValue().collectCandidateTemplates(candidateEdges, sink); | ||
} | ||
} | ||
} else { | ||
ImmutableList<String> remainingSourceCandidateEdges = | ||
sourceIdentifiers.subList(1, sourceIdentifiers.size()); | ||
Node<T> child = children().get(sourceIdentifiers.get(0)); | ||
ImmutableList<String> remainingCandidateEdges = | ||
candidateEdges.subList(1, candidateEdges.size()); | ||
Node<T> child = children().get(candidateEdges.get(0)); | ||
if (child != null) { | ||
child.collectCandidateTemplates(remainingSourceCandidateEdges, sink); | ||
child.collectCandidateTemplates(remainingCandidateEdges, sink); | ||
} | ||
collectCandidateTemplates(remainingCandidateEdges, sink); | ||
} | ||
} | ||
|
||
@AutoValue | ||
@SuppressWarnings("AutoValueImmutableFields" /* Type is used only during `Node` construction. */) | ||
abstract static class BuildNode<T> { | ||
static <T> BuildNode<T> create() { | ||
return new AutoValue_Node_BuildNode<>(new HashMap<>(), new ArrayList<>()); | ||
} | ||
|
||
abstract Map<String, BuildNode<T>> children(); | ||
|
||
abstract List<T> values(); | ||
|
||
private void register( | ||
List<T> values, Function<T, ImmutableSet<ImmutableSortedSet<String>>> pathsExtractor) { | ||
for (T value : values) { | ||
for (ImmutableSet<String> path : pathsExtractor.apply(value)) { | ||
registerPath(value, path.asList()); | ||
} | ||
} | ||
collectCandidateTemplates(remainingSourceCandidateEdges, sink); | ||
} | ||
|
||
private void registerPath(T value, ImmutableList<String> path) { | ||
path.stream() | ||
.findFirst() | ||
.ifPresentOrElse( | ||
edge -> | ||
children() | ||
.computeIfAbsent(edge, k -> BuildNode.create()) | ||
.registerPath(value, path.subList(1, path.size())), | ||
() -> values().add(value)); | ||
} | ||
|
||
private Node<T> immutable() { | ||
return Node.create( | ||
Maps.transformValues(children(), BuildNode::immutable), ImmutableList.copyOf(values())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add some documentation to this class. (And arguably we should add unit tests.)