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