Transit Planner  1.0
An experiment on transfer patterns robustness
src/Utilities.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_UTILITIES_H_
00003 #define SRC_UTILITIES_H_
00004 
00005 #include <string>
00006 #include <sstream>
00007 #include <map>
00008 #include <vector>
00009 #include <set>
00010 
00011 // For formatting ASCII output.
00012 #define BOLD     "\033[1m"
00013 #define BLACK    "\033[30m"
00014 #define RED      "\033[31m"
00015 #define GREEN    "\033[32m"
00016 #define BROWN    "\033[33m"
00017 #define BLUE     "\033[34m"
00018 #define RESET    "\033[0m"
00019 
00020 
00021 // Use global Logger LOG instead.
00022 /*#define PARSER     GREEN << "[Parser    ] " << RESET
00023 #define PRECOMP    GREEN << "[Prepare   ] " << RESET
00024 #define EXPERIMENT GREEN << "[Experiment] " << RESET
00025 #define RESULTS    GREEN << "[Results   ] " << RESET*/
00026 
00027 using std::set;
00028 using std::map;
00029 using std::vector;
00030 using std::pair;
00031 using std::string;
00032 using std::stringstream;
00033 
00034 typedef map<string, string> StrStrMap;
00035 typedef pair<int, int> IntPair;
00036 
00037 const int kSecondsPerDay = 86400;
00038 
00039 // Converts variable a from its type A to type B.
00040 template<typename B, typename A>
00041     B convert(const A& a) {
00042   B b;
00043   stringstream ss;
00044   ss << a;
00045   ss >> b;
00046   return b;
00047 }
00048 
00049 // Map search with inline type conversion.
00050 // Returns whether the key was found in the map.
00051 template<typename A, typename B, typename C>
00052     bool found(const map<A, B>& m, const A& key, C& value) {
00053   const typename map<A, B>::const_iterator iter = m.find(key);
00054   const bool foundit = iter != m.end();
00055   if (foundit) {
00056     value = convert<C>(iter->second);
00057   }
00058   return foundit;
00059 }
00060 
00061 // Initialises a map with given value for given key iff key is not contained
00062 // in the map.
00063 // Returns whether true if the map was initialised with the value for the key
00064 // and false if the key was already contained within the map.
00065 template<typename K, typename V>
00066 bool safeInit(map<K, V>& c, const K& k, const V& v) {
00067   if (c.find(k) == c.end()) {
00068     c[k] = v;
00069     return true;
00070   }
00071   return false;
00072 }
00073 
00074 // Inserts a value into the container found in the map at given key.
00075 // If there is no container in the map at that key, it creates new container
00076 // at given key and inserts the value into it.
00077 template<typename K, typename V, class Container>
00078 void safeInsert(map<K, Container>& c, const K& k, const V& v) {
00079   typename map<K, Container>::iterator it = c.find(k);
00080   if (it == c.end()) {
00081     Container& value = (c[k] = Container());
00082     value.insert(v);
00083   } else {
00084     it->second.insert(v);
00085   }
00086 }
00087 
00088 template<class T, class Container>
00089 bool contains(const Container& c, const T& key) {
00090   return c.find(key) != c.end();
00091 }
00092 
00093 template<class T>
00094 bool contains(const vector<T>& c, const T& item) {
00095   for (auto it = c.begin(); it != c.end(); ++it) {
00096     if (*it == item) {
00097       return true;
00098     }
00099   }
00100   return false;
00101 }
00102 
00103 // Returns a formatted time string.
00104 string formatPerfTime(const double s);
00105 
00106 // Retuns all directories listed under path.
00107 vector<string> listDirs(const string& path);
00108 
00109 // Returns whether the file exists.
00110 bool fileExists(const string& path);
00111 
00112 // Returns the file size.
00113 int fileSize(const string& path);
00114 
00115 // Reads the whole file into a string.
00116 // Remark: do not use it for big files.
00117 string readFile(const string& path);
00118 
00119 // checks whether a string is a valid time string to be converted with str2time
00120 bool isValidTimeString(const string& str);
00121 
00122 // Converts a timestring of format yyyymmddThhmmss into seconds since 1970.
00123 int64_t str2time(const string& str);
00124 
00125 // Converts a number of seconds into the iso format string yyyymmddThhmmss.
00126 string time2str(const int64_t time);
00127 
00128 // Gets the weekday for a date given by seconds since 1.1.1970
00129 string getWeekday(const int64_t time);
00130 
00131 // Considers TIME as a date specified by seconds since 1970. Gets the offset in
00132 // seconds between 0:00:00 at this date and 1.1.1970.
00133 int64_t getDateOffsetSeconds(const int64_t time);
00134 
00135 // Returns the local time in seconds since 1970.
00136 int64_t localTime();
00137 
00138 // Returns a seed (usually localtime) for random number generation.
00139 int64_t getSeed();
00140 
00141 // Returns the first of may localTime at 0:00.
00142 int64_t firstOfMay();
00143 
00144 // Computes and returns the great circle distance between two positions on the
00145 // globe in meters.
00146 float greatCircleDistance(const float latitude1, const float longitude1,
00147                           const float latitude2, const float longitude2);
00148 
00149 // Writes a set of transfer patterns to stdout.
00150 void printTransferPatterns(const set<vector<int> >& patterns);
00151 
00152 // Splits a string at whitespaces.
00153 vector<string> splitString(const string& content);
00154 
00155 #endif  // SRC_UTILITIES_H_
 All Classes