SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // A list of time ranges with assigned values 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 #ifndef ValueTimeLine_h 00023 #define ValueTimeLine_h 00024 00025 00026 // =========================================================================== 00027 // included modules 00028 // =========================================================================== 00029 #include <map> 00030 #include <cassert> 00031 #include <utility> 00032 #include <utils/common/SUMOTime.h> 00033 00034 #ifdef _MSC_VER 00035 #include <windows_config.h> 00036 #else 00037 #include <config.h> 00038 #endif 00039 00040 00041 // =========================================================================== 00042 // class definitions 00043 // =========================================================================== 00052 template<typename T> 00053 class ValueTimeLine { 00054 public: 00056 ValueTimeLine() { } 00057 00059 ~ValueTimeLine() { } 00060 00069 void add(SUMOReal begin, SUMOReal end, T value) { 00070 assert(begin >= 0); 00071 assert(begin < end); 00072 // inserting strictly before the first or after the last interval (includes empty case) 00073 if (myValues.upper_bound(begin) == myValues.end() || 00074 myValues.upper_bound(end) == myValues.begin()) { 00075 myValues[begin] = std::make_pair(true, value); 00076 myValues[end] = std::make_pair(false, value); 00077 return; 00078 } 00079 // our end already has a value 00080 typename TimedValueMap::iterator endIt = myValues.find(end); 00081 if (endIt != myValues.end()) { 00082 myValues.erase(myValues.upper_bound(begin), endIt); 00083 myValues[begin] = std::make_pair(true, value); 00084 return; 00085 } 00086 // we have at least one entry strictly before our end 00087 endIt = myValues.lower_bound(end); 00088 --endIt; 00089 ValidValue oldEndValue = endIt->second; 00090 myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end)); 00091 myValues[begin] = std::make_pair(true, value); 00092 myValues[end] = oldEndValue; 00093 } 00094 00103 T getValue(SUMOReal time) const { 00104 assert(myValues.size() != 0); 00105 typename TimedValueMap::const_iterator it = myValues.upper_bound(time); 00106 assert(it != myValues.begin()); 00107 --it; 00108 return it->second.second; 00109 } 00110 00121 bool describesTime(SUMOReal time) const { 00122 typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time); 00123 if (afterIt == myValues.begin()) { 00124 return false; 00125 } 00126 --afterIt; 00127 return afterIt->second.first; 00128 } 00129 00140 SUMOReal getSplitTime(SUMOReal low, SUMOReal high) const { 00141 typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low); 00142 typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high); 00143 --afterHigh; 00144 if (afterLow == afterHigh) { 00145 return afterLow->first; 00146 } 00147 return -1; 00148 } 00149 00155 void fillGaps(T value, bool extendOverBoundaries = false) { 00156 for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) { 00157 if (!it->second.first) { 00158 it->second.second = value; 00159 } 00160 } 00161 if (extendOverBoundaries && !myValues.empty()) { 00162 typename TimedValueMap::iterator it = --myValues.end(); 00163 if (!it->second.first) { 00164 myValues.erase(it, myValues.end()); 00165 } 00166 value = myValues.begin()->second.second; 00167 } 00168 myValues[-1] = std::make_pair(false, value); 00169 } 00170 00171 private: 00173 typedef std::pair<bool, T> ValidValue; 00174 00176 typedef std::map<SUMOReal, ValidValue> TimedValueMap; 00177 00179 TimedValueMap myValues; 00180 00181 }; 00182 00183 00184 #endif 00185 00186 /****************************************************************************/