Eclipse 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-2019 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 /****************************************************************************/
16 // A list of time ranges with assigned values
17 /****************************************************************************/
18 #ifndef ValueTimeLine_h
19 #define ValueTimeLine_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <map>
26 #include <cassert>
27 #include <utility>
28 #include <utils/common/SUMOTime.h>
29 
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
43 template<typename T>
45 public:
48 
51 
60  void add(double begin, double end, T value) {
61  assert(begin >= 0);
62  assert(begin < end);
63  // inserting strictly before the first or after the last interval (includes empty case)
64  if (myValues.upper_bound(begin) == myValues.end() ||
65  myValues.upper_bound(end) == myValues.begin()) {
66  myValues[begin] = std::make_pair(true, value);
67  myValues[end] = std::make_pair(false, value);
68  return;
69  }
70  // our end already has a value
71  typename TimedValueMap::iterator endIt = myValues.find(end);
72  if (endIt != myValues.end()) {
73  myValues.erase(myValues.upper_bound(begin), endIt);
74  myValues[begin] = std::make_pair(true, value);
75  return;
76  }
77  // we have at least one entry strictly before our end
78  endIt = myValues.lower_bound(end);
79  --endIt;
80  ValidValue oldEndValue = endIt->second;
81  myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
82  myValues[begin] = std::make_pair(true, value);
83  myValues[end] = oldEndValue;
84  }
85 
94  T getValue(double time) const {
95  assert(myValues.size() != 0);
96  typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
97  assert(it != myValues.begin());
98  --it;
99  return it->second.second;
100  }
101 
112  bool describesTime(double time) const {
113  typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
114  if (afterIt == myValues.begin()) {
115  return false;
116  }
117  --afterIt;
118  return afterIt->second.first;
119  }
120 
131  double getSplitTime(double low, double high) const {
132  typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
133  typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
134  --afterHigh;
135  if (afterLow == afterHigh) {
136  return afterLow->first;
137  }
138  return -1;
139  }
140 
146  void fillGaps(T value, bool extendOverBoundaries = false) {
147  for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
148  if (!it->second.first) {
149  it->second.second = value;
150  }
151  }
152  if (extendOverBoundaries && !myValues.empty()) {
153  typename TimedValueMap::iterator it = --myValues.end();
154  if (!it->second.first) {
155  myValues.erase(it, myValues.end());
156  }
157  value = myValues.begin()->second.second;
158  }
159  myValues[-1] = std::make_pair(false, value);
160  }
161 
162 private:
164  typedef std::pair<bool, T> ValidValue;
165 
167  typedef std::map<double, ValidValue> TimedValueMap;
168 
171 
172 };
173 
174 
175 #endif
176 
177 /****************************************************************************/
ValueTimeLine::TimedValueMap
std::map< double, ValidValue > TimedValueMap
Sorted map from start of intervals to values.
Definition: ValueTimeLine.h:167
SUMOTime.h
ValueTimeLine::describesTime
bool describesTime(double time) const
Returns whether a value for the given time is known.
Definition: ValueTimeLine.h:112
ValueTimeLine::~ValueTimeLine
~ValueTimeLine()
Destructor.
Definition: ValueTimeLine.h:50
ValueTimeLine::getSplitTime
double getSplitTime(double low, double high) const
Returns the time point at which the value changes.
Definition: ValueTimeLine.h:131
ValueTimeLine::ValueTimeLine
ValueTimeLine()
Constructor.
Definition: ValueTimeLine.h:47
ValueTimeLine::add
void add(double begin, double end, T value)
Adds a value for a time interval into the container.
Definition: ValueTimeLine.h:60
ValueTimeLine::fillGaps
void fillGaps(T value, bool extendOverBoundaries=false)
Sets a default value for all unset intervals.
Definition: ValueTimeLine.h:146
ValueTimeLine::getValue
T getValue(double time) const
Returns the value for the given time.
Definition: ValueTimeLine.h:94
ValueTimeLine::myValues
TimedValueMap myValues
The list of time periods (with values)
Definition: ValueTimeLine.h:170
ValueTimeLine< double >::ValidValue
std::pair< bool, double > ValidValue
Value of time line, indicating validity.
Definition: ValueTimeLine.h:164
ValueTimeLine
Definition: ValueTimeLine.h:44