17 #ifndef MSF_SORTEDCONTAINER_H_
18 #define MSF_SORTEDCONTAINER_H_
25 #define CHECK_IN_BOUNDS(iterator, container) \
27 decltype(iterator) __it = it; \
29 if (__it == container.begin()) { \
30 MSF_ERROR_STREAM("Iterator out of bounds (begin) " << \
31 __FILE__ << ":" << __LINE__); \
33 if (iterator == container.end()) { \
34 MSF_ERROR_STREAM("Iterator out of bounds (end) " << \
35 __FILE__ << ":" << __LINE__); \
46 template<
typename T,
typename PrototypeInval
idT = T>
51 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 typedef std::map<double, Ptr_T>
ListT;
61 invalid.reset(
new PrototypeInvalidT());
83 inline typename ListT::size_type
size() {
91 inline typename ListT::iterator
insert(
const shared_ptr<T>& value) {
92 std::pair<typename ListT::iterator, bool> itpr =
stateList.insert(
93 std::pair<
double, shared_ptr<T> >(value->time, value));
96 "Wanted to insert a value to the sorted container at time " <<
97 std::fixed << std::setprecision(9) << value->time <<
98 " but the map already contained a value at this time. discarding.");
114 typename ListT::iterator it =
stateList.begin();
132 bool warnIfNotExistant =
134 typename ListT::iterator it =
stateList.find(value->time);
136 if (warnIfNotExistant)
138 "getIteratorAtValue(state): Could not find value for time " <<
139 std::fixed << std::setprecision(9) << value->time);
152 bool warnIfNotExistant =
154 typename ListT::iterator it =
stateList.find(time);
156 if (warnIfNotExistant)
158 "getIteratorAtValue(double): Could not find value for time " <<
159 std::fixed << std::setprecision(9) << time);
171 const double& statetime) {
172 typename ListT::iterator it =
stateList.lower_bound(statetime);
183 const double& statetime) {
184 typename ListT::iterator it =
stateList.upper_bound(statetime);
196 typename ListT::iterator it_at =
stateList.find(statetime);
206 if (tauMinus == it_end) {
209 if (tauPlus == it_end) {
212 if (fabs(tauPlus->second->time - statetime)
213 < fabs(tauMinus->second->time - statetime)) {
226 typename ListT::iterator it =
stateList.lower_bound(statetime);
228 MSF_WARN_STREAM(
"Requested the first object before time " << statetime <<
229 "but the container is empty");
247 typename ListT::iterator it =
stateList.upper_bound(statetime);
262 typename ListT::iterator it =
stateList.find(statetime);
284 if (tauMinus->time == -1) {
286 }
else if (tauPlus->time == -1) {
290 if (fabs(tauPlus->time - statetime) < fabs(tauMinus->time - statetime)) {
304 double newest =
getLast()->time;
306 if (newest - it->second->time < age)
308 if (it->second->time >
stateList.begin()->second->time)
319 MSF_WARN_STREAM(
"Requested the last object in the sorted container, but "
320 "the container is empty");
323 typename ListT::iterator end =
stateList.end();
324 return (--end)->second;
335 "but the container is empty");
338 typename ListT::iterator start =
stateList.begin();
339 return start->second;
352 inline shared_ptr<T>
updateTime(
double timeOld,
double timeNew)
353 __attribute__ ((warn_unused_result)) {
354 typename ListT::iterator it =
stateList.find(timeOld);
356 std::stringstream ss;
358 <<
"Wanted to update a states/measurements time, but could not find "
359 "the old state, for which the time was asked to be updated. time "
360 << std::fixed << std::setprecision(9) << timeOld << std::endl;
362 ss <<
"Map: " << std::endl;
363 for (
typename ListT::iterator it2 =
stateList.begin();
365 ss << it2->first << std::endl;
372 shared_ptr<T> copy = it->second;
374 copy->time = timeNew;
375 typename ListT::iterator inserted =
insert(copy);
376 return inserted->second;
385 std::stringstream ss;
389 ss << std::fixed << std::setprecision(9) << it->second->time << std::endl;
398 #endif // MSF_SORTEDCONTAINER_H_