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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A list of time ranges with assigned values
21 /****************************************************************************/
22 #pragma once
23 #include <map>
24 #include <cassert>
25 #include <utility>
26 #include <utils/common/SUMOTime.h>
27 
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
41 template<typename T>
43 public:
46 
49 
58  void add(double begin, double end, T value) {
59  assert(begin >= 0);
60  assert(begin < end);
61  // inserting strictly before the first or after the last interval (includes empty case)
62  if (myValues.upper_bound(begin) == myValues.end() ||
63  myValues.upper_bound(end) == myValues.begin()) {
64  myValues[begin] = std::make_pair(true, value);
65  myValues[end] = std::make_pair(false, value);
66  return;
67  }
68  // our end already has a value
69  typename TimedValueMap::iterator endIt = myValues.find(end);
70  if (endIt != myValues.end()) {
71  myValues.erase(myValues.upper_bound(begin), endIt);
72  myValues[begin] = std::make_pair(true, value);
73  return;
74  }
75  // we have at least one entry strictly before our end
76  endIt = myValues.lower_bound(end);
77  --endIt;
78  ValidValue oldEndValue = endIt->second;
79  myValues.erase(myValues.upper_bound(begin), myValues.lower_bound(end));
80  myValues[begin] = std::make_pair(true, value);
81  myValues[end] = oldEndValue;
82  }
83 
92  T getValue(double time) const {
93  assert(myValues.size() != 0);
94  typename TimedValueMap::const_iterator it = myValues.upper_bound(time);
95  assert(it != myValues.begin());
96  --it;
97  return it->second.second;
98  }
99 
110  bool describesTime(double time) const {
111  typename TimedValueMap::const_iterator afterIt = myValues.upper_bound(time);
112  if (afterIt == myValues.begin()) {
113  return false;
114  }
115  --afterIt;
116  return afterIt->second.first;
117  }
118 
129  double getSplitTime(double low, double high) const {
130  typename TimedValueMap::const_iterator afterLow = myValues.upper_bound(low);
131  typename TimedValueMap::const_iterator afterHigh = myValues.upper_bound(high);
132  --afterHigh;
133  if (afterLow == afterHigh) {
134  return afterLow->first;
135  }
136  return -1;
137  }
138 
144  void fillGaps(T value, bool extendOverBoundaries = false) {
145  for (typename TimedValueMap::iterator it = myValues.begin(); it != myValues.end(); ++it) {
146  if (!it->second.first) {
147  it->second.second = value;
148  }
149  }
150  if (extendOverBoundaries && !myValues.empty()) {
151  typename TimedValueMap::iterator it = --myValues.end();
152  if (!it->second.first) {
153  myValues.erase(it, myValues.end());
154  }
155  value = myValues.begin()->second.second;
156  }
157  myValues[-1] = std::make_pair(false, value);
158  }
159 
160 private:
162  typedef std::pair<bool, T> ValidValue;
163 
165  typedef std::map<double, ValidValue> TimedValueMap;
166 
169 
170 };
double getSplitTime(double low, double high) const
Returns the time point at which the value changes.
bool describesTime(double time) const
Returns whether a value for the given time is known.
T getValue(double time) const
Returns the value for the given time.
Definition: ValueTimeLine.h:92
~ValueTimeLine()
Destructor.
Definition: ValueTimeLine.h:48
void fillGaps(T value, bool extendOverBoundaries=false)
Sets a default value for all unset intervals.
TimedValueMap myValues
The list of time periods (with values)
void add(double begin, double end, T value)
Adds a value for a time interval into the container.
Definition: ValueTimeLine.h:58
ValueTimeLine()
Constructor.
Definition: ValueTimeLine.h:45
std::pair< bool, T > ValidValue
Value of time line, indicating validity.
std::map< double, ValidValue > TimedValueMap
Sorted map from start of intervals to values.