36 #include <boost/shared_ptr.hpp> 37 #include <boost/bind.hpp> 38 #include <boost/function.hpp> 42 #ifndef SETTLERLIB_SORTED_DEQUE_H_ 43 #define SETTLERLIB_SORTED_DEQUE_H_ 45 #define DEQUE_DEBUG(fmt, ...) \ 46 ROS_DEBUG_NAMED(logger_.c_str(), fmt,##__VA_ARGS__) 62 using std::deque<M>::size;
63 using std::deque<M>::front;
64 using std::deque<M>::back;
65 using std::deque<M>::pop_front;
66 using std::deque<M>::begin;
67 using std::deque<M>::end;
68 using std::deque<M>::rbegin;
69 using std::deque<M>::rend;
70 using std::deque<M>::insert;
71 using std::deque<M>::at;
72 using std::deque<M>::erase;
90 std::string logger =
"deque") :
std::deque<M>(),
logger_(logger)
123 typename std::deque<M>::reverse_iterator rev_it = rbegin();
131 insert(rev_it.base(), msg);
144 unsigned int start_index = 0 ;
145 while(start_index < size() &&
152 unsigned int end_index = start_index ;
153 while(end_index < size() &&
159 std::vector<M> interval_elems ;
160 interval_elems.reserve(end_index - start_index) ;
161 for (
unsigned int i=start_index; i<end_index; i++)
163 interval_elems.push_back(at(i)) ;
166 return interval_elems ;
177 unsigned int start_index = size()-1;
178 while(start_index > 0 &&
183 unsigned int end_index = start_index;
184 while(end_index < size()-1 &&
190 std::vector<M> interval_elems;
191 interval_elems.reserve(end_index - start_index + 1) ;
192 for (
unsigned int i=start_index; i<=end_index; i++)
194 interval_elems.push_back(at(i)) ;
197 return interval_elems;
209 int elem_index = -1 ;
219 out = at(elem_index);
235 int elem_index = -1 ;
245 out = at(elem_index);
263 typename std::deque<M>::iterator it = begin();
264 typename std::deque<M>::iterator best = it;
266 double best_diff = fabs( (time -
getStamp(*best)).toSec());
270 double cur_diff = fabs( (time -
getStamp(*it)).toSec());
271 if (cur_diff < best_diff)
273 best_diff = cur_diff;
291 typename std::deque<M>::iterator it = begin();
293 while (size() > 0 &&
getStamp(front()) < time)
305 return m->header.stamp;
310 return m.header.stamp;
327 boost::function<const ros::Time&(const M&)>
getStamp;
331 DEQUE_DEBUG(
"%sdeque.size(): %u max_size: %u", prefix.c_str(), (
unsigned int) size(),
max_size_);
SortedDeque(boost::function< const ros::Time &(const M &)> getStampFunc, std::string logger="deque")
Advanced constructor, allowing user to specific the timestamp getter method.
static const ros::Time & getStructStamp(const M &m)
Adds helper routines to the STL Deque for holding messages with headers.
void setMaxSize(unsigned int max_size)
Set the maximum # of elements this deque can hold. Older elems are popped once the length is exeeded...
static const ros::Time & getHeaderStamp(const M &m)
bool getClosestElem(const ros::Time &time, M &out)
Get the elem that occurs closest to the specified time.
static const ros::Time & getPtrStamp(const M &m)
bool getElemAfterTime(const ros::Time &time, M &out) const
Grab the oldest element that occurs right after the specified time.
bool getElemBeforeTime(const ros::Time &time, M &out) const
Grab the oldest element that occurs right before the specified time.
void DEQUE_DEBUG_STATS(const std::string &prefix)
void add(const M &msg)
Add a new element to the deque, correctly sorted by timestamp.
#define DEQUE_DEBUG(fmt,...)
SortedDeque(std::string logger="deque")
Assumes that '.header.stamp' can be used to get the stamp used for sorting.
std::vector< M > getSurroundingInterval(const ros::Time &start, const ros::Time &end)
boost::function< const ros::Time &(const M &)> getStamp
void removeAllBeforeTime(const ros::Time &time)
Removes all elements that occur before the specified time.
std::vector< M > getInterval(const ros::Time &start, const ros::Time &end)
Extract all the elements that occur in the interval between the start and end times.