Skip to content

Commit

Permalink
merge patterns more quickly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwigway committed Apr 2, 2015
1 parent e97fd8b commit c5e56de
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/main/java/org/opentripplanner/profile/ProfileState.java
@@ -1,6 +1,7 @@
package org.opentripplanner.profile; package org.opentripplanner.profile;


import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.Set; import java.util.Set;


import jersey.repackaged.com.google.common.collect.Lists; import jersey.repackaged.com.google.common.collect.Lists;
Expand All @@ -16,7 +17,7 @@ public class ProfileState implements Cloneable {
public Type accessType; public Type accessType;


/** the trip patterns used to access this stop */ /** the trip patterns used to access this stop */
public Collection<TripPattern> patterns = Lists.newArrayList(); public TripPattern[] patterns;


/** the location of this state */ /** the location of this state */
public TransitStop stop; public TransitStop stop;
Expand Down Expand Up @@ -54,10 +55,6 @@ public ProfileState propagate (int delta) {
return propagate(delta, delta); return propagate(delta, delta);
} }


public void clearPatterns () {
this.patterns = Lists.newArrayList();
}

/** two ways to create a profile state: the initial state, reached via an on-street mode, and subsequent states reached via transit */ /** two ways to create a profile state: the initial state, reached via an on-street mode, and subsequent states reached via transit */
public static enum Type { public static enum Type {
STREET, TRANSIT, TRANSFER; STREET, TRANSIT, TRANSFER;
Expand All @@ -68,6 +65,46 @@ public void mergeIn(ProfileState other) {
this.lowerBound = Math.min(this.lowerBound, other.lowerBound); this.lowerBound = Math.min(this.lowerBound, other.lowerBound);
// the upper bound of a common trunk is the _minimum_ upper bound of all its constituents // the upper bound of a common trunk is the _minimum_ upper bound of all its constituents
this.upperBound = Math.min(this.upperBound, other.upperBound); this.upperBound = Math.min(this.upperBound, other.upperBound);
this.patterns.addAll(other.patterns); this.patterns = new TripPattern[this.patterns.length + other.patterns.length];

}

public boolean containsPattern(TripPattern pattern) {
if (patterns == null)
return false;

for (TripPattern tp : patterns) {
if (tp == pattern)
return true;
}

return false;
}


/** merge all the profile states into a new ProfileState. Assumes that each state consists of a single pattern unique among the states. */
public static ProfileState merge (Collection<ProfileState> states, boolean retainPatterns) {
ProfileState ret = new ProfileState();
ret.lowerBound = ret.upperBound = Integer.MAX_VALUE;

if (retainPatterns)
ret.patterns = new TripPattern[states.size()];

{
int i = 0;
for (Iterator<ProfileState> it = states.iterator(); it.hasNext(); i++) {
ProfileState state = it.next();
if (state.lowerBound < ret.lowerBound) ret.lowerBound = state.lowerBound;

// Yes, we want the _minimum_ upper bound: you will never take a journey that is longer than the minimum
// upper bound, in either the perfect information case or the min-upper-bound case.
if (state.upperBound < ret.upperBound) ret.upperBound = state.upperBound;

if (retainPatterns)
ret.patterns[i] = state.patterns[0];
}
}

return ret;
} }
} }

0 comments on commit c5e56de

Please sign in to comment.