Skip to content

Commit

Permalink
Extract PrepareCHEdgeIterator interface and clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Dec 29, 2019
1 parent b521afb commit 4c06402
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 105 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/com/graphhopper/GraphHopper.java
Expand Up @@ -615,6 +615,7 @@ private void printInfo() {
* disc which is usually a lot faster.
*/
public GraphHopper importOrLoad() {
clean();
if (!load(ghLocation)) {
printInfo();
process(ghLocation, false);
Expand Down
Expand Up @@ -18,101 +18,28 @@

package com.graphhopper.routing.ch;

import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.util.CHEdgeExplorer;
import com.graphhopper.util.CHEdgeIterator;
import com.graphhopper.util.EdgeIterator;
public interface PrepareCHEdgeIterator {
boolean next();

public class PrepareCHEdgeIterator implements PrepareCHEdgeExplorer {
private final CHEdgeExplorer edgeExplorer;
private final Weighting weighting;
private CHEdgeIterator chIterator;
int getEdge();

public PrepareCHEdgeIterator(CHEdgeExplorer edgeExplorer, Weighting weighting) {
this.edgeExplorer = edgeExplorer;
this.weighting = weighting;
}
int getBaseNode();

@Override
public PrepareCHEdgeIterator setBaseNode(int node) {
chIterator = edgeExplorer.setBaseNode(node);
return this;
}
int getAdjNode();

public boolean next() {
assertBaseNodeSet();
return chIterator.next();
}
int getOrigEdgeFirst();

public int getEdge() {
assertBaseNodeSet();
return chIterator.getEdge();
}
int getOrigEdgeLast();

public int getBaseNode() {
assertBaseNodeSet();
return chIterator.getBaseNode();
}
boolean isShortcut();

public int getAdjNode() {
assertBaseNodeSet();
return chIterator.getAdjNode();
}
double getWeight(boolean reverse);

public int getOrigEdgeFirst() {
assertBaseNodeSet();
return chIterator.getOrigEdgeFirst();
}
void setWeight(double weight);

public int getOrigEdgeLast() {
assertBaseNodeSet();
return chIterator.getOrigEdgeLast();
}
int getMergeStatus(int flags);

public boolean isShortcut() {
assertBaseNodeSet();
return chIterator.isShortcut();
}
void setFlagsAndWeight(int flags, double weight);

public double getWeight(boolean reverse) {
if (isShortcut()) {
return chIterator.getWeight();
} else {
assertBaseNodeSet();
return weighting.calcWeight(chIterator, reverse, EdgeIterator.NO_EDGE);
}
}

public void setWeight(double weight) {
assertBaseNodeSet();
chIterator.setWeight(weight);
}

@Override
public String toString() {
if (chIterator == null) {
return "not initialized";
} else {
return getBaseNode() + "->" + getAdjNode() + " (" + getEdge() + ")";
}
}

int getMergeStatus(int flags) {
assertBaseNodeSet();
return chIterator.getMergeStatus(flags);
}

void setFlagsAndWeight(int flags, double weight) {
assertBaseNodeSet();
chIterator.setFlagsAndWeight(flags, weight);
}

void setSkippedEdges(int skippedEdge1, int skippedEdge2) {
assertBaseNodeSet();
chIterator.setSkippedEdges(skippedEdge1, skippedEdge2);
}

private void assertBaseNodeSet() {
assert chIterator != null : "You need to call setBaseNode() before using the iterator";
}
void setSkippedEdges(int skippedEdge1, int skippedEdge2);
}
@@ -0,0 +1,130 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper.routing.ch;

import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.util.CHEdgeExplorer;
import com.graphhopper.util.CHEdgeIterator;
import com.graphhopper.util.EdgeIterator;

public class PrepareCHEdgeIteratorImpl implements PrepareCHEdgeExplorer, PrepareCHEdgeIterator {
private final CHEdgeExplorer edgeExplorer;
private final Weighting weighting;
private CHEdgeIterator chIterator;

public PrepareCHEdgeIteratorImpl(CHEdgeExplorer edgeExplorer, Weighting weighting) {
this.edgeExplorer = edgeExplorer;
this.weighting = weighting;
}

@Override
public PrepareCHEdgeIteratorImpl setBaseNode(int node) {
chIterator = edgeExplorer.setBaseNode(node);
return this;
}

@Override
public boolean next() {
assertBaseNodeSet();
return chIterator.next();
}

@Override
public int getEdge() {
assertBaseNodeSet();
return chIterator.getEdge();
}

@Override
public int getBaseNode() {
assertBaseNodeSet();
return chIterator.getBaseNode();
}

@Override
public int getAdjNode() {
assertBaseNodeSet();
return chIterator.getAdjNode();
}

@Override
public int getOrigEdgeFirst() {
assertBaseNodeSet();
return chIterator.getOrigEdgeFirst();
}

@Override
public int getOrigEdgeLast() {
assertBaseNodeSet();
return chIterator.getOrigEdgeLast();
}

@Override
public boolean isShortcut() {
assertBaseNodeSet();
return chIterator.isShortcut();
}

@Override
public double getWeight(boolean reverse) {
if (isShortcut()) {
return chIterator.getWeight();
} else {
assertBaseNodeSet();
return weighting.calcWeight(chIterator, reverse, EdgeIterator.NO_EDGE);
}
}

@Override
public void setWeight(double weight) {
assertBaseNodeSet();
chIterator.setWeight(weight);
}

@Override
public String toString() {
if (chIterator == null) {
return "not initialized";
} else {
return getBaseNode() + "->" + getAdjNode() + " (" + getEdge() + ")";
}
}

@Override
public int getMergeStatus(int flags) {
assertBaseNodeSet();
return chIterator.getMergeStatus(flags);
}

@Override
public void setFlagsAndWeight(int flags, double weight) {
assertBaseNodeSet();
chIterator.setFlagsAndWeight(flags, weight);
}

@Override
public void setSkippedEdges(int skippedEdge1, int skippedEdge2) {
assertBaseNodeSet();
chIterator.setSkippedEdges(skippedEdge1, skippedEdge2);
}

private void assertBaseNodeSet() {
assert chIterator != null : "You need to call setBaseNode() before using the iterator";
}
}
Expand Up @@ -59,15 +59,15 @@ private PrepareCHGraph(CHGraph chGraph, Weighting weighting, TurnWeighting turnW
}

public PrepareCHEdgeExplorer createInEdgeExplorer() {
return new PrepareCHEdgeIterator(chGraph.createEdgeExplorer(DefaultEdgeFilter.inEdges(encoder)), weighting);
return new PrepareCHEdgeIteratorImpl(chGraph.createEdgeExplorer(DefaultEdgeFilter.inEdges(encoder)), weighting);
}

public PrepareCHEdgeExplorer createOutEdgeExplorer() {
return new PrepareCHEdgeIterator(chGraph.createEdgeExplorer(DefaultEdgeFilter.outEdges(encoder)), weighting);
return new PrepareCHEdgeIteratorImpl(chGraph.createEdgeExplorer(DefaultEdgeFilter.outEdges(encoder)), weighting);
}

public PrepareCHEdgeExplorer createAllEdgeExplorer() {
return new PrepareCHEdgeIterator(chGraph.createEdgeExplorer(DefaultEdgeFilter.allEdges(encoder)), weighting);
return new PrepareCHEdgeIteratorImpl(chGraph.createEdgeExplorer(DefaultEdgeFilter.allEdges(encoder)), weighting);
}

public EdgeExplorer createOriginalInEdgeExplorer() {
Expand Down
Expand Up @@ -20,9 +20,8 @@
import com.carrotsearch.hppc.IntHashSet;
import com.carrotsearch.hppc.IntSet;
import com.graphhopper.coll.GHTreeMapComposed;
import com.graphhopper.routing.*;
import com.graphhopper.routing.RoutingAlgorithmFactory;
import com.graphhopper.routing.util.AbstractAlgoPreparation;
import com.graphhopper.routing.util.LevelEdgeFilter;
import com.graphhopper.routing.util.TraversalMode;
import com.graphhopper.routing.weighting.TurnWeighting;
import com.graphhopper.routing.weighting.Weighting;
Expand Down
Expand Up @@ -59,7 +59,6 @@ public void setup() {
ghStorage = new GraphBuilder(encodingManager)
.setCHProfiles(profileNode1, profileNode2, profileNode3, profileEdge1, profileEdge2, profileEdge3)
.setDir(dir)
.set3D(false)
.withTurnCosts(true)
.create();
}
Expand Down
Expand Up @@ -175,7 +175,7 @@ public void testDirectedGraph() {
prepare.doWork();
assertEquals(2, prepare.getShortcuts());
assertEquals(oldCount, lg.getOriginalEdges());
assertEquals(oldCount + 2,lg.getEdges());
assertEquals(oldCount + 2, lg.getEdges());
RoutingAlgorithm algo = prepare.getRoutingAlgorithmFactory().createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
Path p = algo.calcPath(4, 2);
assertEquals(3, p.getDistance(), 1e-6);
Expand Down

0 comments on commit 4c06402

Please sign in to comment.