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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // A list of time ranges with assigned values
18 /****************************************************************************/
19 #ifndef ValueTimeLine_h
20 #define ValueTimeLine_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <map>
27 #include <cassert>
28 #include <utility>
29 #include <utils/common/SUMOTime.h>
30 
31 #include <config.h>
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
45 template<typename T>
47 public:
50 
53 
62  void add(double begin, double end, T value) {
63  assert(begin >= 0);
64  assert(begin < end);
65  // inserting strictly before the first or after the last interval (includes empty case)
66  if (myValues.upper_bound(begin) == myValues.end() ||
67  myValues.upper_bound(end) == myValues.begin()) {
68  myValues[begin] = std::make_pair(true, value);
69  myValues[end] = std::make_pair(false, value);
70  return;
71  }
72  // our end already has a value
73  typename TimedValueMap::iterator endIt = myValues.find(end);
74  if (endIt != myValues.end()) {
75  myValues.erase(myValues.upper_bound(begin), endIt);
76  myValues[begin] = std::make_pair(true, value);
77  return;
78  }
79  // we have at least one entry strictly before our end
80  endIt = myValues.lower_bound(end);
81  --endIt;
82  ValidValue oldEndValue = endIt->second;
83  myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
84  myValues[begin] = std::make_pair(true, value);
85  myValues[end] = oldEndValue;
86  }
87 
96  T getValue(double time) const {
97  assert(myValues.size() != 0);
98  typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
99  assert(it != myValues.begin());
100  --it;
101  return it->second.second;
102  }
103 
114  bool describesTime(double time) const {
115  typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
116  if (afterIt == myValues.begin()) {
117  return false;
118  }
119  --afterIt;
120  return afterIt->second.first;
121  }
122 
133  double getSplitTime(double low, double high) const {
134  typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
135  typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
136  --afterHigh;
137  if (afterLow == afterHigh) {
138  return afterLow->first;
139  }
140  return -1;
141  }
142 
148  void fillGaps(T value, bool extendOverBoundaries = false) {
149  for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
150  if (!it->second.first) {
151  it->second.second = value;
152  }
153  }
154  if (extendOverBoundaries && !myValues.empty()) {
155  typename TimedValueMap::iterator it = --myValues.end();
156  if (!it->second.first) {
157  myValues.erase(it, myValues.end());
158  }
159  value = myValues.begin()->second.second;
160  }
161  myValues[-1] = std::make_pair(false, value);
162  }
163 
164 private:
166  typedef std::pair<bool, T> ValidValue;
167 
169  typedef std::map<double, ValidValue> TimedValueMap;
170 
172  TimedValueMap myValues;
173 
174 };
175 
176 
177 #endif
178 
179 /****************************************************************************/
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:52
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:62
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.
Definition: ValueTimeLine.h:96
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:49