ValueTimeLine.h
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef ValueTimeLine_h
00020 #define ValueTimeLine_h
00021
00022
00023
00024
00025
00026 #include <map>
00027 #include <cassert>
00028 #include <utility>
00029 #include <utils/common/SUMOTime.h>
00030
00031 #ifdef _MSC_VER
00032 #include <windows_config.h>
00033 #else
00034 #include <config.h>
00035 #endif
00036
00037
00038
00039
00040
00049 template<typename T>
00050 class ValueTimeLine {
00051 public:
00053 typedef std::pair<bool, T> ValidValue;
00054
00056 typedef std::map<SUMOReal, ValidValue> TimedValueMap;
00057
00058 public:
00060 ValueTimeLine() { }
00061
00063 ~ValueTimeLine() { }
00064
00073 void add(SUMOReal begin, SUMOReal end, T value) {
00074 assert(begin>=0);
00075 assert(begin<end);
00076
00077 if (myValues.upper_bound(begin) == myValues.end() ||
00078 myValues.upper_bound(end) == myValues.begin()) {
00079 myValues[begin] = std::make_pair(true, value);
00080 myValues[end] = std::make_pair(false, value);
00081 return;
00082 }
00083
00084 typename TimedValueMap::iterator endIt = myValues.find(end);
00085 if (endIt != myValues.end()) {
00086 myValues.erase(myValues.upper_bound(begin), endIt);
00087 myValues[begin] = std::make_pair(true, value);
00088 return;
00089 }
00090
00091 endIt = myValues.lower_bound(end);
00092 --endIt;
00093 ValidValue oldEndValue = endIt->second;
00094 myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
00095 myValues[begin] = std::make_pair(true, value);
00096 myValues[end] = oldEndValue;
00097 }
00098
00107 T getValue(SUMOReal time) const {
00108 assert(myValues.size()!=0);
00109 typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
00110 assert(it != myValues.begin());
00111 --it;
00112 return it->second.second;
00113 }
00114
00125 bool describesTime(SUMOReal time) const {
00126 typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
00127 if (afterIt == myValues.begin()) {
00128 return false;
00129 }
00130 --afterIt;
00131 return afterIt->second.first;
00132 }
00133
00144 SUMOReal getSplitTime(SUMOReal low, SUMOReal high) const {
00145 typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
00146 typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
00147 --afterHigh;
00148 if (afterLow == afterHigh) {
00149 return afterLow->first;
00150 }
00151 return -1;
00152 }
00153
00159 void fillGaps(T value, bool extendOverBoundaries=false) {
00160 for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
00161 if (!it->second.first) {
00162 it->second.second = value;
00163 }
00164 }
00165 if (extendOverBoundaries && !myValues.empty()) {
00166 typename TimedValueMap::iterator it = --myValues.end();
00167 if (!it->second.first) {
00168 myValues.erase(it, myValues.end());
00169 }
00170 value = myValues.begin()->second.second;
00171 }
00172 myValues[-1] = std::make_pair(false, value);
00173 }
00174
00175 private:
00177 TimedValueMap myValues;
00178
00179 };
00180
00181
00182 #endif
00183
00184