SUMO - Simulation of Urban MObility
ValueTimeLine.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A list of time ranges with assigned values
20 /****************************************************************************/
21 #ifndef ValueTimeLine_h
22 #define ValueTimeLine_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #include <map>
29 #include <cassert>
30 #include <utility>
31 #include <utils/common/SUMOTime.h>
32 
33 #ifdef _MSC_VER
34 #include <windows_config.h>
35 #else
36 #include <config.h>
37 #endif
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
51 template<typename T>
53 public:
56 
59 
68  void add(double begin, double end, T value) {
69  assert(begin >= 0);
70  assert(begin < end);
71  // inserting strictly before the first or after the last interval (includes empty case)
72  if (myValues.upper_bound(begin) == myValues.end() ||
73  myValues.upper_bound(end) == myValues.begin()) {
74  myValues[begin] = std::make_pair(true, value);
75  myValues[end] = std::make_pair(false, value);
76  return;
77  }
78  // our end already has a value
79  typename TimedValueMap::iterator endIt = myValues.find(end);
80  if (endIt != myValues.end()) {
81  myValues.erase(myValues.upper_bound(begin), endIt);
82  myValues[begin] = std::make_pair(true, value);
83  return;
84  }
85  // we have at least one entry strictly before our end
86  endIt = myValues.lower_bound(end);
87  --endIt;
88  ValidValue oldEndValue = endIt->second;
89  myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
90  myValues[begin] = std::make_pair(true, value);
91  myValues[end] = oldEndValue;
92  }
93 
102  T getValue(double time) const {
103  assert(myValues.size() != 0);
104  typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
105  assert(it != myValues.begin());
106  --it;
107  return it->second.second;
108  }
109 
120  bool describesTime(double time) const {
121  typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
122  if (afterIt == myValues.begin()) {
123  return false;
124  }
125  --afterIt;
126  return afterIt->second.first;
127  }
128 
139  double getSplitTime(double low, double high) const {
140  typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
141  typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
142  --afterHigh;
143  if (afterLow == afterHigh) {
144  return afterLow->first;
145  }
146  return -1;
147  }
148 
154  void fillGaps(T value, bool extendOverBoundaries = false) {
155  for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
156  if (!it->second.first) {
157  it->second.second = value;
158  }
159  }
160  if (extendOverBoundaries && !myValues.empty()) {
161  typename TimedValueMap::iterator it = --myValues.end();
162  if (!it->second.first) {
163  myValues.erase(it, myValues.end());
164  }
165  value = myValues.begin()->second.second;
166  }
167  myValues[-1] = std::make_pair(false, value);
168  }
169 
170 private:
172  typedef std::pair<bool, T> ValidValue;
173 
175  typedef std::map<double, ValidValue> TimedValueMap;
176 
178  TimedValueMap myValues;
179 
180 };
181 
182 
183 #endif
184 
185 /****************************************************************************/
void fillGaps(T value, bool extendOverBoundaries=false)
Sets a default value for all unset intervals.
double getSplitTime(double low, double high) const
Returns the time point at which the value changes.
~ValueTimeLine()
Destructor.
Definition: ValueTimeLine.h:58
bool describesTime(double time) const
Returns whether a value for the given time is known.
void add(double begin, double end, T value)
Adds a value for a time interval into the container.
Definition: ValueTimeLine.h:68
std::map< double, ValidValue > TimedValueMap
Sorted map from start of intervals to values.
T getValue(double time) const
Returns the value for the given time.
std::pair< bool, double > ValidValue
Value of time line, indicating validity.
TimedValueMap myValues
The list of time periods (with values)
ValueTimeLine()
Constructor.
Definition: ValueTimeLine.h:55