SUMO - Simulation of Urban MObility

ValueTimeLine.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A list of time ranges with assigned values
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef ValueTimeLine_h
00020 #define ValueTimeLine_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
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 // class definitions
00040 // ===========================================================================
00049 template<typename T>
00050 class ValueTimeLine {
00051 public:
00053     ValueTimeLine() { }
00054 
00056     ~ValueTimeLine() { }
00057 
00066     void add(SUMOReal begin, SUMOReal end, T value) {
00067         assert(begin>=0);
00068         assert(begin<end);
00069         // inserting strictly before the first or after the last interval (includes empty case)
00070         if (myValues.upper_bound(begin) == myValues.end() ||
00071                 myValues.upper_bound(end) == myValues.begin()) {
00072             myValues[begin] = std::make_pair(true, value);
00073             myValues[end] = std::make_pair(false, value);
00074             return;
00075         }
00076         // our end already has a value
00077         typename TimedValueMap::iterator endIt = myValues.find(end);
00078         if (endIt != myValues.end()) {
00079             myValues.erase(myValues.upper_bound(begin), endIt);
00080             myValues[begin] = std::make_pair(true, value);
00081             return;
00082         }
00083         // we have at least one entry strictly before our end
00084         endIt = myValues.lower_bound(end);
00085         --endIt;
00086         ValidValue oldEndValue = endIt->second;
00087         myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
00088         myValues[begin] = std::make_pair(true, value);
00089         myValues[end] = oldEndValue;
00090     }
00091 
00100     T getValue(SUMOReal time) const {
00101         assert(myValues.size()!=0);
00102         typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
00103         assert(it != myValues.begin());
00104         --it;
00105         return it->second.second;
00106     }
00107 
00118     bool describesTime(SUMOReal time) const {
00119         typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
00120         if (afterIt == myValues.begin()) {
00121             return false;
00122         }
00123         --afterIt;
00124         return afterIt->second.first;
00125     }
00126 
00137     SUMOReal getSplitTime(SUMOReal low, SUMOReal high) const {
00138         typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
00139         typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
00140         --afterHigh;
00141         if (afterLow == afterHigh) {
00142             return afterLow->first;
00143         }
00144         return -1;
00145     }
00146 
00152     void fillGaps(T value, bool extendOverBoundaries=false) {
00153         for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
00154             if (!it->second.first) {
00155                 it->second.second = value;
00156             }
00157         }
00158         if (extendOverBoundaries && !myValues.empty()) {
00159             typename TimedValueMap::iterator it = --myValues.end();
00160             if (!it->second.first) {
00161                 myValues.erase(it, myValues.end());
00162             }
00163             value = myValues.begin()->second.second;
00164         }
00165         myValues[-1] = std::make_pair(false, value);
00166     }
00167 
00168 private:
00170     typedef std::pair<bool, T> ValidValue;
00171 
00173     typedef std::map<SUMOReal, ValidValue> TimedValueMap;
00174 
00176     TimedValueMap myValues;
00177 
00178 };
00179 
00180 
00181 #endif
00182 
00183 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines