MATSIM
Public Member Functions | Private Attributes | List of all members
org.matsim.pt.router.PreparedTransitSchedule Class Reference

Public Member Functions

 PreparedTransitSchedule (TransitSchedule schedule)
 
 PreparedTransitSchedule ()
 
double getNextDepartureTime (final TransitRoute route, final TransitRouteStop stop, final double depTime)
 

Private Attributes

final Map< TransitRoute, double[]> sortedDepartureCache = new ConcurrentHashMap<TransitRoute, double[]>()
 

Detailed Description

Allows fast queries of a TransitSchedule for the next departure of a given route at a given stop, from a given point in time. If you need further queries of a TransitSchedule, put them here!

(I renamed this class and put the TransitSchedule in the constructor to make the purpose clear. michaz '13)

Thread-safe.

Author
mrieser

Definition at line 43 of file PreparedTransitSchedule.java.

Constructor & Destructor Documentation

◆ PreparedTransitSchedule() [1/2]

org.matsim.pt.router.PreparedTransitSchedule.PreparedTransitSchedule ( TransitSchedule  schedule)

Definition at line 59 of file PreparedTransitSchedule.java.

59  {
60 
61  }

◆ PreparedTransitSchedule() [2/2]

org.matsim.pt.router.PreparedTransitSchedule.PreparedTransitSchedule ( )

Definition at line 67 of file PreparedTransitSchedule.java.

67  {
68 
69  }

Member Function Documentation

◆ getNextDepartureTime()

double org.matsim.pt.router.PreparedTransitSchedule.getNextDepartureTime ( final TransitRoute  route,
final TransitRouteStop  stop,
final double  depTime 
)

Definition at line 71 of file PreparedTransitSchedule.java.

References org.matsim.pt.transitSchedule.api.TransitRouteStop.getDepartureOffset(), org.matsim.pt.transitSchedule.api.TransitRoute.getDepartures(), and org.matsim.core.utils.misc.OptionalTime.seconds.

71  {
72 
73  double earliestDepartureTimeAtTerminus = depTime - stop.getDepartureOffset().seconds();
74  // This shifts my time back to the terminus.
75 
76  if (earliestDepartureTimeAtTerminus >= MIDNIGHT) {
77  earliestDepartureTimeAtTerminus = earliestDepartureTimeAtTerminus % MIDNIGHT;
78  }
79  if (earliestDepartureTimeAtTerminus < 0) {
80  // this may happen when depTime < departureOffset, e.g. I want to start at 24:03, but the bus departs at 23:55 at terminus
81  earliestDepartureTimeAtTerminus += MIDNIGHT;
82  }
83 
84  // this will search for the terminus departure that corresponds to my departure at the stop:
85  double[] cache = sortedDepartureCache.get(route);
86  if (cache == null) {
87  cache = new double[route.getDepartures().size()];
88  int i = 0;
89  for (Departure dep : route.getDepartures().values()) {
90  cache[i++] = dep.getDepartureTime();
91  }
92  Arrays.sort(cache);
93  sortedDepartureCache.put(route, cache);
94  }
95  int pos = Arrays.binarySearch(cache, earliestDepartureTimeAtTerminus);
96  if (pos < 0) {
97  // (if the departure time is not found _exactly_, binarySearch returns (-(insertion point) - 1). That is
98  // retval = -(insertion point) - 1 or insertion point = -(retval+1) .
99  // This will, in fact, be the normal situation, so it is important to understand this.)
100  pos = -(pos + 1);
101  }
102  if (pos >= cache.length) {
103  pos = 0; // there is no later departure time, take the first in the morning
104  }
105  double bestDepartureTime = cache[pos];
106  // (departure time at terminus)
107 
108  bestDepartureTime += stop.getDepartureOffset().seconds();
109  // (resulting departure time at stop)
110 
111  while (bestDepartureTime < depTime) {
112  bestDepartureTime += MIDNIGHT;
113  // (add enough "MIDNIGHT"s until we are _after_ the desired departure time)
114  }
115  return bestDepartureTime;
116  }
final Map< TransitRoute, double[]> sortedDepartureCache
Here is the call graph for this function:

Member Data Documentation

◆ sortedDepartureCache

final Map<TransitRoute, double[]> org.matsim.pt.router.PreparedTransitSchedule.sortedDepartureCache = new ConcurrentHashMap<TransitRoute, double[]>()
private

Definition at line 54 of file PreparedTransitSchedule.java.


The documentation for this class was generated from the following file: