Skip to content

Commit

Permalink
Remove ShortestPathSt.Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
barakugav committed Jun 28, 2024
1 parent 05fd7ed commit 72ca3e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.jgalgo.alg.path.ShortestPathHeuristicSt;
import com.jgalgo.alg.path.ShortestPathSingleSource;
import com.jgalgo.alg.path.ShortestPathSt;
import com.jgalgo.alg.path.ShortestPathStBidirectionalDijkstra;
import com.jgalgo.bench.util.BenchUtils;
import com.jgalgo.bench.util.GraphsTestUtils;
import com.jgalgo.bench.util.TestUtils.SeedGenerator;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void setup() {

@Benchmark
public void BidirectionalDijkstra(Blackhole blackhole) {
benchStShortestPath(ShortestPathSt.builder().build(), blackhole);
benchStShortestPath(new ShortestPathStBidirectionalDijkstra(), blackhole);
}

@Benchmark
Expand Down Expand Up @@ -151,7 +152,7 @@ public void setup() {

@Benchmark
public void BidirectionalDijkstra(Blackhole blackhole) {
benchStShortestPath(ShortestPathSt.builder().build(), blackhole);
benchStShortestPath(new ShortestPathStBidirectionalDijkstra(), blackhole);
}

@Benchmark
Expand Down Expand Up @@ -194,7 +195,7 @@ public void setup() {

@Benchmark
public void BidirectionalDijkstra(Blackhole blackhole) {
benchStShortestPath(ShortestPathSt.builder().build(), blackhole);
benchStShortestPath(new ShortestPathStBidirectionalDijkstra(), blackhole);
}

@Benchmark
Expand Down
59 changes: 13 additions & 46 deletions jgalgo-core/src/main/java/com/jgalgo/alg/path/ShortestPathSt.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.jgalgo.alg.path;

import com.jgalgo.alg.AlgorithmBuilderBase;
import com.jgalgo.graph.Graph;
import com.jgalgo.graph.IWeightFunction;
import com.jgalgo.graph.IntGraph;
Expand All @@ -36,8 +35,7 @@
* A variant with a heuristic distance function is also available, see {@link ShortestPathHeuristicSt}.
*
* <p>
* Use {@link #newInstance()} to get a default implementation of this interface. A builder obtained via
* {@link #builder()} may support different options to obtain different implementations.
* Use {@link #newInstance()} to get a default implementation of this interface.
*
* @see ShortestPathSingleSource
* @see ShortestPathAllPairs
Expand Down Expand Up @@ -89,56 +87,25 @@ <V, E> ObjectDoublePair<Path<V, E>> computeShortestPathAndWeight(Graph<V, E> g,
* Create a new S-T shortest path algorithm object.
*
* <p>
* This is the recommended way to instantiate a new {@link ShortestPathSt} object. The
* {@link ShortestPathSt.Builder} might support different options to obtain different implementations.
* This is the recommended way to instantiate a new {@link ShortestPathSt} object.
*
* @return a default implementation of {@link ShortestPathSt}
*/
static ShortestPathSt newInstance() {
return builder().build();
}

/**
* Create a new S-T shortest path algorithm builder.
*
* <p>
* Use {@link #newInstance()} for a default implementation.
*
* @return a new builder that can build {@link ShortestPathSt} objects
*/
static ShortestPathSt.Builder builder() {
return () -> {
return new ShortestPathSt() {
ShortestPathSt cardinalityStSp = new ShortestPathStBidirectionalBfs();
ShortestPathSt weightedStSp = new ShortestPathStBidirectionalDijkstra();
return new ShortestPathSt() {
ShortestPathSt cardinalityStSp = new ShortestPathStBidirectionalBfs();
ShortestPathSt weightedStSp = new ShortestPathStBidirectionalDijkstra();

@Override
public <V, E> ObjectDoublePair<Path<V, E>> computeShortestPathAndWeight(Graph<V, E> g,
WeightFunction<E> w, V source, V target) {
if (WeightFunction.isCardinality(w)) {
return cardinalityStSp.computeShortestPathAndWeight(g, null, source, target);
} else {
return weightedStSp.computeShortestPathAndWeight(g, w, source, target);
}
@Override
public <V, E> ObjectDoublePair<Path<V, E>> computeShortestPathAndWeight(Graph<V, E> g, WeightFunction<E> w,
V source, V target) {
if (WeightFunction.isCardinality(w)) {
return cardinalityStSp.computeShortestPathAndWeight(g, null, source, target);
} else {
return weightedStSp.computeShortestPathAndWeight(g, w, source, target);
}
};
}
};
}

/**
* A builder for {@link ShortestPathSt} objects.
*
* @see ShortestPathSt#builder()
* @author Barak Ugav
*/
static interface Builder extends AlgorithmBuilderBase {

/**
* Create a new algorithm object for S-T shortest path computation.
*
* @return a new S-T shortest path algorithm
*/
ShortestPathSt build();
}

}

0 comments on commit 72ca3e3

Please sign in to comment.