Transit Planner  1.0
An experiment on transfer patterns robustness
src/TransferPatternsDB.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_TRANSFERPATTERNSDB_H_
00003 #define SRC_TRANSFERPATTERNSDB_H_
00004 
00005 #include <boost/serialization/map.hpp>
00006 #include <boost/serialization/set.hpp>
00007 #include <boost/serialization/vector.hpp>
00008 #include <vector>
00009 #include <map>
00010 #include <set>
00011 #include <string>
00012 #include "./HubSet.h"
00013 
00014 using std::vector;
00015 using std::map;
00016 using std::set;
00017 
00018 // Directed acyclic graph holding the transfer patterns in reversed direction
00019 // - from destination stops to the departure stop.
00020 class TransferPatternsGraph {
00021  public:
00022   static const int INVALID_NODE;
00023 
00024   // Pointer to the hubs set.
00025   static const HubSet* _hubs;
00026 
00027   // Copy constructor.
00028   TransferPatternsGraph(const TransferPatternsGraph& rhs);
00029 
00030   // Initialises the graph with the departure stop without hubs.
00031   explicit TransferPatternsGraph(const int depStop);
00032 
00033   // Initialises the graph with the departure stop and hubs.
00034   TransferPatternsGraph(const int depStop, const HubSet& hubs);
00035 
00036   // Returns the number of nodes within the graph including departure,
00037   // destination and prefix nodes.
00038   int numNodes() const;
00039 
00040   // Returns the departure stop index.
00041   int depStop() const;
00042 
00043   // Returns the destination node index for given stop if available and
00044   // INVALID_NODE otherwise.
00045   int destNode(const int stop) const;
00046 
00047   // Returns a const reference to the set of all hubs which occur as
00048   // destination nodes within the graph.
00049   const set<int>& destHubs() const;
00050 
00051   // Returns the stop index for a given graph node.
00052   int stop(const int node) const;
00053 
00054   // Returns a const reference to all successor node indices for a given node.
00055   const vector<int>& successors(const int node) const;
00056 
00057   // Adds nodes and connections according to the given transfer pattern.
00058   void addPattern(const vector<int>& stops);
00059 
00060   // Cleares cache required for efficient graph construction.
00061   // Use this after construction to increase query-time efficiency.
00062   void finalise();
00063 
00064   // Assignment operator.
00065   TransferPatternsGraph& operator=(const TransferPatternsGraph& rhs);
00066 
00067   // Swaps the non-construction-content of two graphs.
00068   void swap(TransferPatternsGraph& rhs);
00069 
00070   // Equality of TransferPatternGraphs. Used for testing.
00071   bool operator==(const TransferPatternsGraph& rhs) const;
00072 
00073   // Returns a debug string of the TPG.
00074   std::string debugString() const;
00075 
00076  private:
00077   static const set<int> _emptySet;
00078 
00079   // Connects a prefix node for given stop to its successor node.
00080   // Adds a new prefix node on demand.
00081   // Returns the index of the prefix node.
00082   int addPrefixNode(const int stop, const int successor);
00083 
00084   // Connects a destination node for given stop to its successor node.
00085   // Adds a new destination node on demand.
00086   // Returns the index of the destination node.
00087   int addDestNode(const int stop, const int successor);
00088 
00089   // Returns the first node of a prefix, which conforms to the given connection
00090   // stop -> successor if available and INVALID_NODE rhswise.
00091   // E.g. in graph {E->D->C->B->A, E->A, E->D->B->A}
00092   // C->B->A and B->A are the only proper prefixes.
00093   int findProperPrefix(const int stop, const int successor) const;
00094 
00095   // Returns the prefix nodes for given stop.
00096   const set<int>& prefixNodes(const int stop) const;
00097 
00098   vector<int> _nodes;
00099   vector<vector<int> > _successors;  // TODO(jonas): could be vector<set<int> >
00100   set<int> _destHubs;
00101   map<int, int> _destMap;
00102 
00103   // Used only during graph construction; cleared on finalising.
00104   map<int, set<int> > _prefixMap;
00105 
00106   // Default Constructor needed for serialization.
00107   TransferPatternsGraph() {}
00108 
00109   // Serialization.
00110   template<class Archive>
00111   void serialize(Archive& ar, const uint version) {  // NOLINT
00112     ar & _nodes;
00113     ar & _destHubs;
00114     ar & _successors;
00115     ar & _destMap;
00116   }
00117   friend class boost::serialization::access;
00118 };
00119 typedef TransferPatternsGraph TPG;
00120 
00121 
00122 // The transfer patterns database holds transfer patterns graphs for all stops.
00123 class TransferPatternsDB {
00124  public:
00125   // Copy constructor.
00126   TransferPatternsDB(const TransferPatternsDB& rhs);
00127 
00128   // Default construction without initialisation.
00129   TransferPatternsDB();
00130 
00131   // Initialises the database given the total number of stops and the hubs.
00132   TransferPatternsDB(const int numStops, const HubSet& hubs);
00133 
00134   // Initialises the database given the total number of stops and the hubs.
00135   void init(const int numStops, const HubSet& hubs);
00136 
00137   // Addition operator used for reduction of parallel results. Asserts both TPDB
00138   // have disjunct set of graphs, meaning they have the same number of graphs
00139   // BUT if one has a non-empty graph for a stop the corresponding graph of the
00140   // other is empty.
00141   TransferPatternsDB& operator+=(TransferPatternsDB& other);  // NOLINT
00142 
00143   // Returns a const reference to the transfer patterns graph for given
00144   // departure stop.
00145   const TPG& graph(const int depStop) const;
00146   // Returns a write reference to the graph used for swapping.
00147   TPG& getGraph(const int depStop);
00148 
00149   // Adds a transfer pattern to the database.
00150   // Remark: must be initialised before calling.
00151   void addPattern(const vector<int>& stops);
00152 
00153   // Returns the number of graphs in the database, which equals the number of
00154   // stops as provided by the initialisation.
00155   size_t numGraphs() const;
00156 
00157   // Cleares cache required for efficient graph construction.
00158   // Use this after the graph construction for the given departure stop
00159   // to increase query-time efficiency.
00160   // Remark: finalising during construction will increase the memory footprint
00161   // of the final graph.
00162   void finalise(const int depStop);
00163 
00164   // Assignment operator.
00165   TransferPatternsDB& operator=(const TransferPatternsDB& rhs);
00166 
00167   // TPDB comparison. Used for testing.
00168   bool operator==(const TransferPatternsDB& rhs) const;
00169 
00170  private:
00171   vector<TPG> _graphs;
00172 
00173   // Serialization.
00174   template<class Archive>
00175   void serialize(Archive& ar, const uint version) {  // NOLINT
00176     ar & _graphs;
00177   }
00178   friend class boost::serialization::access;
00179 };
00180 typedef TransferPatternsDB TPDB;
00181 
00182 
00183 #endif  // SRC_TRANSFERPATTERNSDB_H_
 All Classes