Skip to content

Commit

Permalink
refactor: improve encapsulation in StopIndexForRaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Dec 17, 2021
1 parent da7f90b commit d6e3ece
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FlexAccessEgressAdapter(
FlexAccessEgress flexAccessEgress, boolean isEgress, StopIndexForRaptor stopIndex
) {
super(
stopIndex.indexByStop.get(flexAccessEgress.stop),
stopIndex.indexOf(flexAccessEgress.stop),
isEgress ? flexAccessEgress.lastState.reverse() : flexAccessEgress.lastState
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@
* The scope of instances of this class is limited to the mapping process, the final state is
* stored in the {@link TransitLayer}.
*/
public class StopIndexForRaptor {
public final List<Stop> stopsByIndex;
public final Map<Stop, Integer> indexByStop = new HashMap<>();
public final class StopIndexForRaptor {
private final List<Stop> stopsByIndex;
private final Map<Stop, Integer> indexByStop = new HashMap<>();
public final int[] stopBoardAlightCosts;

public StopIndexForRaptor(Collection<Stop> stops, TransitTuningParameters tuningParameters) {
this.stopsByIndex = new ArrayList<>(stops);
this.stopsByIndex = List.copyOf(stops);
initializeIndexByStop();
this.stopBoardAlightCosts = createStopBoardAlightCosts(stopsByIndex, tuningParameters);
}

/**
* Create map between stop and index used by Raptor to stop objects in original graph
*/
void initializeIndexByStop() {
for(int i = 0; i< stopsByIndex.size(); ++i) {
indexByStop.put(stopsByIndex.get(i), i);
}
public Stop stopByIndex(int index) {
return stopsByIndex.get(index);
}

public int indexOf(Stop stop) {
return indexByStop.get(stop);
}

public int size() {
return stopsByIndex.size();
}

/**
Expand All @@ -58,6 +61,15 @@ public int[] listStopIndexesForStops(Stop[] stops) {
return stopIndex;
}

/**
* Create map between stop and index used by Raptor to stop objects in original graph
*/
private void initializeIndexByStop() {
for(int i = 0; i< stopsByIndex.size(); ++i) {
indexByStop.put(stopsByIndex.get(i), i);
}
}

/**
* Create static board/alight cost for Raptor to include for each stop.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public TransitLayer(
}

public int getIndexByStop(Stop stop) {
return stopIndex.indexByStop.get(stop);
return stopIndex.indexOf(stop);
}

@Nullable
public Stop getStopByIndex(int stop) {
return stop != -1 ? this.stopIndex.stopsByIndex.get(stop) : null;
return stop == -1 ? null : this.stopIndex.stopByIndex(stop);
}

public StopIndexForRaptor getStopIndex() {
Expand All @@ -103,7 +103,7 @@ public ZoneId getTransitDataZoneId() {
}

public int getStopCount() {
return stopIndex.stopsByIndex.size();
return stopIndex.size();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.opentripplanner.routing.algorithm.raptor.transit.mappers;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.opentripplanner.ext.flex.FlexAccessEgress;
import org.opentripplanner.model.Stop;
import org.opentripplanner.routing.algorithm.raptor.transit.AccessEgress;
import org.opentripplanner.routing.algorithm.raptor.transit.FlexAccessEgressAdapter;
import org.opentripplanner.routing.algorithm.raptor.transit.StopIndexForRaptor;
import org.opentripplanner.routing.graphfinder.NearbyStop;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class AccessEgressMapper {

private final StopIndexForRaptor stopIndex;
Expand All @@ -23,7 +22,9 @@ public AccessEgressMapper(StopIndexForRaptor stopIndex) {
public AccessEgress mapNearbyStop(NearbyStop nearbyStop, boolean isEgress) {
if (!(nearbyStop.stop instanceof Stop)) { return null; }
return new AccessEgress(
stopIndex.indexByStop.get(nearbyStop.stop),
// TODO: This cast is potentially causing problems look into if it is possible
// to improve the code and get rid of the cast
stopIndex.indexOf((Stop)nearbyStop.stop),
isEgress ? nearbyStop.state.reverse() : nearbyStop.state
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ static List<List<Transfer>> mapTransfers(

List<List<Transfer>> transferByStopIndex = new ArrayList<>();

for (int i = 0; i < stopIndex.stopsByIndex.size(); ++i) {
Stop stop = stopIndex.stopsByIndex.get(i);
for (int i = 0; i < stopIndex.size(); ++i) {
Stop stop = stopIndex.stopByIndex(i);
ArrayList<Transfer> list = new ArrayList<>();
transferByStopIndex.add(list);

for (PathTransfer pathTransfer : transfersByStop.get(stop)) {
if (pathTransfer.to instanceof Stop) {
int toStopIndex = stopIndex.indexByStop.get(pathTransfer.to);
int toStopIndex = stopIndex.indexOf((Stop)pathTransfer.to);
Transfer newTransfer;
if (pathTransfer.getEdges() != null) {
newTransfer = new Transfer(
Expand Down

0 comments on commit d6e3ece

Please sign in to comment.