Transit Planner
1.0
An experiment on transfer patterns robustness
|
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko 00002 #ifndef SRC_DIRECTCONNECTION_H_ 00003 #define SRC_DIRECTCONNECTION_H_ 00004 00005 #include <gtest/gtest_prod.h> 00006 #include <boost/serialization/access.hpp> 00007 #include <set> 00008 #include <vector> 00009 #include <string> 00010 #include "./Line.h" 00011 00012 using std::set; 00013 using std::vector; 00014 using std::string; 00015 00016 // A line incidence for some stop. 00017 struct Incidence { 00018 Incidence() 00019 : line(-1), pos(-1) {} 00020 00021 Incidence(const int line, const int pos) 00022 : line(line), pos(pos) {} 00023 00024 // Comparison operator used for sorting by line index. 00025 bool operator<(const Incidence& rhs) const { 00026 return line < rhs.line; 00027 } 00028 00029 // Equality operator used for set intersection by line index. 00030 bool operator==(const Incidence& rhs) const { 00031 return line == rhs.line; 00032 } 00033 00034 // The line index. 00035 int line; 00036 00037 // The position of the stop within the line's sequence. 00038 int pos; 00039 }; 00040 00041 // This data structure computes direct-connection queries efficiently. 00042 class DirectConnection { 00043 public: 00044 // Used for unreachable connection costs. 00045 static const int INFINITE; 00046 00047 DirectConnection(); 00048 00049 // Prepares the data structure for lines with given total number of stops. 00050 explicit DirectConnection(const int numStops); 00051 00052 // Initialises the data structure with the given lines. 00053 explicit DirectConnection(const int numStops, const vector<Line>& lines); 00054 00055 // Initialises the data structure with the given lines. 00056 void init(const int numStops, const vector<Line>& lines); 00057 00058 // Adds a line for efficient direct-connection queries. 00059 void addLine(const Line& line); 00060 00061 // Computes the optimal costs between the given stops at the given time. 00062 // Returns the total time difference between the arrival time and the query 00063 // time if a direct connection exists and INFINITE otherwise. 00064 int query(const int dep, const int64_t time, const int dest) const; 00065 00066 // Returns the next possible start time on the connection from dep to dest 00067 // after time. 00068 int nextDepartureTime(const int dep, const int64_t time, const int dest) 00069 const; 00070 00071 // Returns a string representation of the direct connection structure. 00072 string str() const; 00073 00074 template<class Archive> 00075 void serialize(Archive& ar, const unsigned int version) { // NOLINT 00076 ar & _incidents; 00077 ar & _lines; 00078 } 00079 friend class boost::serialization::access; 00080 00081 private: 00082 vector<set<Incidence> > _incidents; 00083 vector<Line> _lines; 00084 FRIEND_TEST(DirectConnectionTest, LineFactory_doubleStop); 00085 }; 00086 00087 #endif // SRC_DIRECTCONNECTION_H_