SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ValueTimeLine.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // A list of time ranges with assigned values
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef ValueTimeLine_h
23 #define ValueTimeLine_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #include <map>
30 #include <cassert>
31 #include <utility>
32 #include <utils/common/SUMOTime.h>
33 
34 #ifdef _MSC_VER
35 #include <windows_config.h>
36 #else
37 #include <config.h>
38 #endif
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
52 template<typename T>
54 public:
57 
60 
69  void add(SUMOReal begin, SUMOReal end, T value) {
70  assert(begin >= 0);
71  assert(begin < end);
72  // inserting strictly before the first or after the last interval (includes empty case)
73  if (myValues.upper_bound(begin) == myValues.end() ||
74  myValues.upper_bound(end) == myValues.begin()) {
75  myValues[begin] = std::make_pair(true, value);
76  myValues[end] = std::make_pair(false, value);
77  return;
78  }
79  // our end already has a value
80  typename TimedValueMap::iterator endIt = myValues.find(end);
81  if (endIt != myValues.end()) {
82  myValues.erase(myValues.upper_bound(begin), endIt);
83  myValues[begin] = std::make_pair(true, value);
84  return;
85  }
86  // we have at least one entry strictly before our end
87  endIt = myValues.lower_bound(end);
88  --endIt;
89  ValidValue oldEndValue = endIt->second;
90  myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
91  myValues[begin] = std::make_pair(true, value);
92  myValues[end] = oldEndValue;
93  }
94 
103  T getValue(SUMOReal time) const {
104  assert(myValues.size() != 0);
105  typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
106  assert(it != myValues.begin());
107  --it;
108  return it->second.second;
109  }
110 
121  bool describesTime(SUMOReal time) const {
122  typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
123  if (afterIt == myValues.begin()) {
124  return false;
125  }
126  --afterIt;
127  return afterIt->second.first;
128  }
129 
141  typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
142  typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
143  --afterHigh;
144  if (afterLow == afterHigh) {
145  return afterLow->first;
146  }
147  return -1;
148  }
149 
155  void fillGaps(T value, bool extendOverBoundaries = false) {
156  for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
157  if (!it->second.first) {
158  it->second.second = value;
159  }
160  }
161  if (extendOverBoundaries && !myValues.empty()) {
162  typename TimedValueMap::iterator it = --myValues.end();
163  if (!it->second.first) {
164  myValues.erase(it, myValues.end());
165  }
166  value = myValues.begin()->second.second;
167  }
168  myValues[-1] = std::make_pair(false, value);
169  }
170 
171 private:
173  typedef std::pair<bool, T> ValidValue;
174 
176  typedef std::map<SUMOReal, ValidValue> TimedValueMap;
177 
180 
181 };
182 
183 
184 #endif
185 
186 /****************************************************************************/
void fillGaps(T value, bool extendOverBoundaries=false)
Sets a default value for all unset intervals.
SUMOReal getSplitTime(SUMOReal low, SUMOReal high) const
Returns the time point at which the value changes.
~ValueTimeLine()
Destructor.
Definition: ValueTimeLine.h:59
void add(SUMOReal begin, SUMOReal end, T value)
Adds a value for a time interval into the container.
Definition: ValueTimeLine.h:69
bool describesTime(SUMOReal time) const
Returns whether a value for the given time is known.
std::pair< bool, SUMOReal > ValidValue
Value of time line, indicating validity.
T getValue(SUMOReal time) const
Returns the value for the given time.
std::map< SUMOReal, ValidValue > TimedValueMap
Sorted map from start of intervals to values.
TimedValueMap myValues
The list of time periods (with values)
ValueTimeLine()
Constructor.
Definition: ValueTimeLine.h:56
#define SUMOReal
Definition: config.h:215