diff --git a/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java b/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java index eb39642..e73d1d3 100644 --- a/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java +++ b/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java @@ -1,68 +1,63 @@ package org.softwareheritage.graph.algo; import java.util.ArrayList; import java.util.Stack; import it.unimi.dsi.big.webgraph.LazyLongIterator; import it.unimi.dsi.bits.LongArrayBitVector; import org.softwareheritage.graph.Graph; public class Visit { public class Path extends ArrayList {} - Graph graphFwd; - Graph graphBwd; - - private Graph graph; - private LongArrayBitVector visited; - private ArrayList extraEdges; - private Stack currentPath; - private ArrayList paths; - - public Visit(Graph graph, Graph graphSym) { - this.graphFwd = graph; - this.graphBwd = graphSym; - } - - public ArrayList visit(String start, ArrayList extraEdges, boolean backward) { - this.graph = (backward) ? this.graphBwd : this.graphFwd; + Graph graph; + LongArrayBitVector visited; + ArrayList extraEdges; + Stack currentPath; + ArrayList paths; + + public Visit(Graph graph, String start, ArrayList extraEdges) { + this.graph = graph; this.visited = LongArrayBitVector.ofLength(graph.getNbNodes()); this.extraEdges = extraEdges; this.paths = new ArrayList(); this.currentPath = new Stack(); recursiveVisit(graph.getNode(start)); + } + // Allow Jackson JSON to only serialize the 'paths' field + public ArrayList getPaths() { return paths; } private void recursiveVisit(long current) { visited.set(current); currentPath.push(current); long degree = graph.outdegree(current); if (degree == 0) { Path path = new Path(); for (long node : currentPath) { path.add(graph.getHash(node)); } paths.add(path); } LazyLongIterator successors = graph.successors(current); while (degree-- > 0) { long next = successors.nextLong(); if (traversalAllowed(current, next) && !visited.getBoolean(next)) { recursiveVisit(next); } } currentPath.pop(); } private boolean traversalAllowed(long current, long next) { // TODO return true; } }