Skip to content

Commit 5a27101

Browse files
committed
AlgorithmBase: use abstract base instead of interface, HamiltonianPath
1 parent bf42a2f commit 5a27101

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

jgalgo-core/src/main/java/com/jgalgo/alg/HamiltonianPathAlgoBase.java jgalgo-core/src/main/java/com/jgalgo/alg/HamiltonianPathAlgos.java

+46-41
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,62 @@
3131
import it.unimi.dsi.fastutil.ints.IntList;
3232
import it.unimi.dsi.fastutil.objects.ObjectIterators;
3333

34-
interface HamiltonianPathAlgoBase extends HamiltonianPathAlgo {
35-
36-
@SuppressWarnings({ "rawtypes", "unchecked" })
37-
@Override
38-
default <V, E> Iterator<Path<V, E>> hamiltonianPathsIter(Graph<V, E> g) {
39-
if (g instanceof IndexGraph) {
40-
return (Iterator) hamiltonianPathsIter((IndexGraph) g);
41-
} else {
42-
IndexGraph ig = g.indexGraph();
43-
Iterator<IPath> indexIter = hamiltonianPathsIter(ig);
44-
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
34+
class HamiltonianPathAlgos {
35+
private HamiltonianPathAlgos() {}
36+
37+
abstract static class AbstractImpl implements HamiltonianPathAlgo {
38+
39+
@SuppressWarnings({ "rawtypes", "unchecked" })
40+
@Override
41+
public <V, E> Iterator<Path<V, E>> hamiltonianPathsIter(Graph<V, E> g) {
42+
if (g instanceof IndexGraph) {
43+
return (Iterator) hamiltonianPathsIter((IndexGraph) g);
44+
} else {
45+
IndexGraph ig = g.indexGraph();
46+
Iterator<IPath> indexIter = hamiltonianPathsIter(ig);
47+
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
48+
}
4549
}
46-
}
4750

48-
@SuppressWarnings({ "rawtypes", "unchecked" })
49-
@Override
50-
default <V, E> Iterator<Path<V, E>> hamiltonianPathsIter(Graph<V, E> g, V source, V target) {
51-
if (g instanceof IndexGraph) {
52-
int src = ((Integer) source).intValue(), trg = ((Integer) target).intValue();
53-
return (Iterator) hamiltonianPathsIter((IndexGraph) g, src, trg);
54-
} else {
55-
IndexGraph ig = g.indexGraph();
56-
IndexIdMap<V> viMap = g.indexGraphVerticesMap();
57-
int src = viMap.idToIndex(source), trg = viMap.idToIndex(target);
58-
Iterator<IPath> indexIter = hamiltonianPathsIter(ig, src, trg);
59-
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
51+
@SuppressWarnings({ "rawtypes", "unchecked" })
52+
@Override
53+
public <V, E> Iterator<Path<V, E>> hamiltonianPathsIter(Graph<V, E> g, V source, V target) {
54+
if (g instanceof IndexGraph) {
55+
int src = ((Integer) source).intValue(), trg = ((Integer) target).intValue();
56+
return (Iterator) hamiltonianPathsIter((IndexGraph) g, src, trg);
57+
} else {
58+
IndexGraph ig = g.indexGraph();
59+
IndexIdMap<V> viMap = g.indexGraphVerticesMap();
60+
int src = viMap.idToIndex(source), trg = viMap.idToIndex(target);
61+
Iterator<IPath> indexIter = hamiltonianPathsIter(ig, src, trg);
62+
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
63+
}
6064
}
61-
}
6265

63-
@SuppressWarnings({ "rawtypes", "unchecked" })
64-
@Override
65-
default <V, E> Iterator<Path<V, E>> hamiltonianCyclesIter(Graph<V, E> g) {
66-
if (g instanceof IndexGraph) {
67-
return (Iterator) hamiltonianCyclesIter((IndexGraph) g);
68-
} else {
69-
IndexGraph ig = g.indexGraph();
70-
Iterator<IPath> indexIter = hamiltonianCyclesIter(ig);
71-
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
66+
@SuppressWarnings({ "rawtypes", "unchecked" })
67+
@Override
68+
public <V, E> Iterator<Path<V, E>> hamiltonianCyclesIter(Graph<V, E> g) {
69+
if (g instanceof IndexGraph) {
70+
return (Iterator) hamiltonianCyclesIter((IndexGraph) g);
71+
} else {
72+
IndexGraph ig = g.indexGraph();
73+
Iterator<IPath> indexIter = hamiltonianCyclesIter(ig);
74+
return IterTools.map(indexIter, iPath -> Paths.pathFromIndexPath(g, iPath));
75+
}
7276
}
73-
}
7477

75-
Iterator<IPath> hamiltonianPathsIter(IndexGraph g);
78+
abstract Iterator<IPath> hamiltonianPathsIter(IndexGraph g);
7679

77-
Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target);
80+
abstract Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target);
7881

79-
Iterator<IPath> hamiltonianCyclesIter(IndexGraph g);
82+
abstract Iterator<IPath> hamiltonianCyclesIter(IndexGraph g);
83+
84+
}
8085

81-
static interface CycleBased extends HamiltonianPathAlgoBase {
86+
abstract static class CycleBasedAbstractImpl extends HamiltonianPathAlgos.AbstractImpl {
8287

8388
@Override
84-
default Iterator<IPath> hamiltonianPathsIter(IndexGraph g) {
89+
public Iterator<IPath> hamiltonianPathsIter(IndexGraph g) {
8590
final int n = g.vertices().size();
8691
final int m = g.edges().size();
8792
if (n == 0)
@@ -134,7 +139,7 @@ default Iterator<IPath> hamiltonianPathsIter(IndexGraph g) {
134139
}
135140

136141
@Override
137-
default Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target) {
142+
public Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target) {
138143
final int n = g.vertices().size();
139144
final int m = g.edges().size();
140145
Assertions.checkVertex(source, n);

jgalgo-core/src/main/java/com/jgalgo/alg/HamiltonianPathRubin.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
*
5050
* @author Barak Ugav
5151
*/
52-
class HamiltonianPathRubin implements HamiltonianPathAlgoBase.CycleBased {
52+
class HamiltonianPathRubin extends HamiltonianPathAlgos.CycleBasedAbstractImpl {
5353

5454
@Override
55-
public Iterator<IPath> hamiltonianCyclesIter(IndexGraph g) {
55+
Iterator<IPath> hamiltonianCyclesIter(IndexGraph g) {
5656
final int n = g.vertices().size();
5757
if (n == 0)
5858
return Collections.emptyIterator();

0 commit comments

Comments
 (0)