Transit Planner
1.0
An experiment on transfer patterns robustness
|
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko 00002 #ifndef SRC_LINE_H_ 00003 #define SRC_LINE_H_ 00004 00005 #include <boost/serialization/access.hpp> 00006 #include <boost/serialization/set.hpp> 00007 #include <boost/serialization/utility.hpp> 00008 #include <set> 00009 #include <string> 00010 #include <vector> 00011 #include <utility> 00012 00013 using std::string; 00014 using std::set; 00015 using std::vector; 00016 using std::pair; 00017 00018 typedef pair<int64_t, int64_t> Int64Pair; 00019 00020 // Time table for a trip. 00021 class TripTime { 00022 public: 00023 // Comparison operator used for sorting by first departure time. 00024 bool operator<(const TripTime& rhs) const; 00025 bool operator==(const TripTime& rhs) const; 00026 00027 // Returns the arrival time for given stop position. 00028 int64_t arr(const int pos) const; 00029 00030 // Returns the departure time for given stop position. 00031 int64_t dep(const int pos) const; 00032 00033 // Returns the number of stop times. 00034 int size() const; 00035 00036 // Adds a stop time to the time table. 00037 void addStopTime(const int64_t arrTime, const int64_t depTime); 00038 00039 // Returns a write reference to the last element. 00040 Int64Pair& back(); 00041 00042 // Returns a string representation of the time table. 00043 string str() const; 00044 00045 private: 00046 // departure, arrival time tuples 00047 vector<Int64Pair> _times; 00048 00049 // serialization 00050 template<class Archive> 00051 void serialize(Archive& ar, const unsigned int version) { // NOLINT 00052 ar & _times; 00053 } 00054 friend class boost::serialization::access; 00055 }; 00056 00057 // A trip is a sequence of stops without transfers. 00058 // It consists of a time table with corresponding stop indices. 00059 class Trip { 00060 public: 00061 explicit Trip(); 00062 explicit Trip(const string& id); 00063 00064 bool operator==(const Trip& rhs) const; 00065 00066 // Returns the number of stops on the trip. 00067 int size() const; 00068 00069 // Adds a stop to the trip. 00070 void addStop(const int64_t arrTime, const int64_t depTime, const int stop); 00071 00072 // Returns stop index at given stop sequence. 00073 int stop(const int i) const; 00074 00075 // Returns a const reference to the stop indices. 00076 const vector<int>& stops() const; 00077 00078 // Returns a const reference to the trip time table. 00079 const TripTime& time() const; 00080 00081 // Returns a write reference to the trip time table. 00082 TripTime& tripTime(); 00083 00084 // Returns a const reference to the trip id. 00085 const string& id() const; 00086 00087 // Returns a string representation of the trip. 00088 string str() const; 00089 00090 private: 00091 string _id; 00092 TripTime _time; 00093 // stop indices 00094 vector<int> _stops; 00095 }; 00096 00097 // A line is a collection of trips with the same stop sequence. 00098 class Line { 00099 public: 00100 static const int INFINITE; 00101 00102 // Returns the number of stops on the line's trips. 00103 int size() const; 00104 00105 // Returns whether the trip shares the line's stop sequence. 00106 bool candidate(const Trip& trip) const; 00107 00108 // Adds a trip to the line, if the trip is suitable. 00109 // Returns wether the trip was added. 00110 bool addTrip(const Trip& trip); 00111 00112 // Returns a const reference to the stop indices. 00113 const vector<int>& stops() const; 00114 00115 // Returns the stop index at given sequence position. 00116 int stop(const int pos) const; 00117 00118 // Returns the cost to travel from a stop dep to a stop dest starting at time. 00119 // The total cost is waiting time + travel time in seconds. 00120 int cost(const int dep, const int64_t time, const int dest) const; 00121 00122 // Return the next departure time from dep to dest after time. 00123 int nextDeparture(const int dep, const int64_t time, const int dest) const; 00124 00125 // Returns a string representation of the line. 00126 string str() const; 00127 00128 private: 00129 set<TripTime> _tripTimes; 00130 vector<int> _stops; 00131 00132 template<class Archive> 00133 void serialize(Archive& ar, const unsigned int version) { // NOLINT 00134 ar & _tripTimes; 00135 ar & _stops; 00136 } 00137 friend class boost::serialization::access; 00138 }; 00139 00140 // Utilities for trips and line construction. 00141 struct LineFactory { 00142 // Creates trips out of a list of times with corresponding stop indices, adds 00143 // the trips to an existing collection of trips. 00144 static void createTrips(const vector<Int64Pair>& times, 00145 const vector<int>& stops, 00146 vector<Trip>* trips); 00147 00148 // Creates on trip out of a list 00149 static Trip createTrip(const vector<Int64Pair>& times, 00150 const vector<int>& stops); 00151 00152 // Creates lines out of a list of trips. 00153 static vector<Line> createLines(const vector<Trip>& trips); 00154 }; 00155 00156 #endif // SRC_LINE_H_