|
31 | 31 | import it.unimi.dsi.fastutil.ints.IntList;
|
32 | 32 | import it.unimi.dsi.fastutil.objects.ObjectIterators;
|
33 | 33 |
|
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 | + } |
45 | 49 | }
|
46 |
| - } |
47 | 50 |
|
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 | + } |
60 | 64 | }
|
61 |
| - } |
62 | 65 |
|
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 | + } |
72 | 76 | }
|
73 |
| - } |
74 | 77 |
|
75 |
| - Iterator<IPath> hamiltonianPathsIter(IndexGraph g); |
| 78 | + abstract Iterator<IPath> hamiltonianPathsIter(IndexGraph g); |
76 | 79 |
|
77 |
| - Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target); |
| 80 | + abstract Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target); |
78 | 81 |
|
79 |
| - Iterator<IPath> hamiltonianCyclesIter(IndexGraph g); |
| 82 | + abstract Iterator<IPath> hamiltonianCyclesIter(IndexGraph g); |
| 83 | + |
| 84 | + } |
80 | 85 |
|
81 |
| - static interface CycleBased extends HamiltonianPathAlgoBase { |
| 86 | + abstract static class CycleBasedAbstractImpl extends HamiltonianPathAlgos.AbstractImpl { |
82 | 87 |
|
83 | 88 | @Override
|
84 |
| - default Iterator<IPath> hamiltonianPathsIter(IndexGraph g) { |
| 89 | + public Iterator<IPath> hamiltonianPathsIter(IndexGraph g) { |
85 | 90 | final int n = g.vertices().size();
|
86 | 91 | final int m = g.edges().size();
|
87 | 92 | if (n == 0)
|
@@ -134,7 +139,7 @@ default Iterator<IPath> hamiltonianPathsIter(IndexGraph g) {
|
134 | 139 | }
|
135 | 140 |
|
136 | 141 | @Override
|
137 |
| - default Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target) { |
| 142 | + public Iterator<IPath> hamiltonianPathsIter(IndexGraph g, int source, int target) { |
138 | 143 | final int n = g.vertices().size();
|
139 | 144 | final int m = g.edges().size();
|
140 | 145 | Assertions.checkVertex(source, n);
|
|
0 commit comments