aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2018-05-24 21:02:21 +0200
committerJon Marius Venstad <venstad@gmail.com>2018-05-24 21:02:21 +0200
commit3d474dac8e68ee68af425aec0abe419b5997c898 (patch)
tree97a04d91fb3efeb406b2c2f817b123c82dcac96a
parent45b3928599e554f66310b02905ba64cc14cee6d1 (diff)
Doc
-rw-r--r--vespajlib/src/main/java/com/yahoo/graph/Traversals.java19
1 files changed, 12 insertions, 7 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/graph/Traversals.java b/vespajlib/src/main/java/com/yahoo/graph/Traversals.java
index 8e80d44c44b..a3567b7a974 100644
--- a/vespajlib/src/main/java/com/yahoo/graph/Traversals.java
+++ b/vespajlib/src/main/java/com/yahoo/graph/Traversals.java
@@ -3,7 +3,6 @@ package com.yahoo.graph;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
@@ -11,8 +10,6 @@ import java.util.List;
import java.util.Map;
import java.util.function.Function;
-import static java.util.Collections.emptyList;
-
/**
* Provides graph traversal algorithms in an ad-hoc manner.
*
@@ -100,15 +97,23 @@ public class Traversals {
};
}
- public static <T> Function<T, Iterable<T>> reversed(Collection<T> nodes, Function<T, Iterable<T>> parents) {
+ /**
+ * Returns an edge set which is the reverse of the given one, restricted to the given node set.
+ *
+ * @param nodes The node set of the input and output graphs.
+ * @param edges The directed edge set for which to find a reverse.
+ * @param <T> The node type.
+ * @return The given edge set, but with all edges reversed.
+ */
+ public static <T> Function<T, Iterable<T>> reversed(Collection<T> nodes, Function<T, Iterable<T>> edges) {
Map<T, List<T>> reverse = new HashMap<>();
for (T node : nodes)
reverse.put(node, new ArrayList<>());
for (T node : nodes)
- for (T parent : parents.apply(node))
- if (reverse.containsKey(parent))
- reverse.get(parent).add(node);
+ for (T neighbour : edges.apply(node))
+ if (reverse.containsKey(neighbour))
+ reverse.get(neighbour).add(node);
return reverse::get;
}