MRPT  1.9.9
xsens_fifoqueue.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef _FIFOQUEUE_H_2006_05_03
11 #define _FIFOQUEUE_H_2006_05_03
12 
13 //#define _LOG_QUEUE
14 #if (defined(_DEBUG) || defined(_LOG_ALWAYS)) && defined(_LOG_QUEUE)
15 #include <typeinfo.h>
16 #else
17 #define QUEUELOG(...)
18 #endif
19 
20 namespace xsens
21 {
22 //////////////////////////////////////////////////////////////////////////////////////////
23 
24 /*! \brief A FIFO queue with limited length (cyclic).
25 
26  The class is based on the STL queue class, but has a limited size. If more
27  items are
28  inserted than would fit, the oldest item is overwritten. The class can only
29  handle
30  pointer types.
31 */
32 template <class T, bool E = true>
33 class FifoQueue
34 {
35  private:
36 #if !defined(QUEUELOG)
37  // write to screen/debug-stream
38  void QUEUELOG(const char* str, ...)
39  {
40 #if defined(_LOG_TO_STDOUT)
41  va_list ptr;
42  va_start(ptr, str);
43  vprintf(str, ptr);
44 #else
45  //#ifdef _LOG_TO_DBVIEW
46  char buf[2048];
47 
48  va_list ptr;
49  va_start(ptr, str);
50  vsprintf(buf, str, ptr);
51 
52  OutputDebugString(buf);
53 #endif
54  }
55 #endif
56  protected:
57  size_t m_maxCount;
59  size_t m_first;
61 
62  T* m_list;
63 
64  public:
65  /** The type of the value stored in this queue. */
66  typedef T value_type;
67  /** The type of a 'size' value. */
68  typedef size_t size_type;
69 
70  //! Create an empty queue with capacity size.
71  FifoQueue(size_type size = 16, bool delOnOverwrite = true)
72  {
73  QUEUELOG(
74  "[%p]FifoQueue.new (base), T=%s, size=%d, del=%d\n", this,
75  typeid(T).name(), size, delOnOverwrite);
76 
77  if (size > 0)
78  m_maxCount = size;
79  else
80  m_maxCount = 1;
81  m_list = new T[m_maxCount];
82  m_currentCount = 0;
83  m_first = 0;
84  m_deleteOnOverwrite = delOnOverwrite;
85  }
86 
87  //! The copy constructor.
88  template <bool E2>
90  {
91  QUEUELOG(
92  "[%p]FifoQueue.new (copy), T=%s, size=%d, count=%d, del=%d\n", this,
93  typeid(T).name(), q.m_maxCount, q.m_currentCount,
94  q.m_deleteOnOverwrite);
95  m_maxCount = q.m_maxCount;
96  m_list = new T[m_maxCount];
97  m_currentCount = q.m_currentCount;
98  m_deleteOnOverwrite = q.m_deleteOnOverwrite;
99  m_first = 0;
100  for (size_t i = 0; i < m_currentCount; ++i)
101  m_list[i] = q.m_list[(i + q.m_first) % m_maxCount];
102  }
103 
104  void eraseAndClear(void)
105  {
106  QUEUELOG(
107  "[%p]FifoQueue.eraseAndClear, T=%s, count=%d\n", this,
108  typeid(T).name(), m_currentCount);
109  for (size_t i = 0; i < m_currentCount; ++i)
110  delete m_list[(i + m_first) % m_maxCount];
111  m_currentCount = 0;
112  m_first = 0;
113  }
114 
115  //! The destructor.
117  {
118  QUEUELOG(
119  "[%p]FifoQueue.delete, T=%s, count=%d\n", this, typeid(T).name(),
121  if (E) eraseAndClear();
122  m_maxCount = 0;
123  delete[] m_list;
124  }
125 
126  //! The assignment operator.
127  template <bool E2>
129  {
130  QUEUELOG(
131  "[%p]FifoQueue.operator =, T=%s, max=%d, count=%d, smax=%d, "
132  "scount=%d\n",
133  this, typeid(T).name(), m_maxCount, m_currentCount, q.m_maxCount,
134  q.m_currentCount);
135  if (m_maxCount != q.m_maxCount)
136  {
137  delete[] m_list;
138  m_maxCount = q.m_maxCount;
139  m_list = new T[m_maxCount];
140  }
141  m_currentCount = q.m_currentCount;
142  m_first = 0;
143  for (size_t i = 0; i < m_currentCount; ++i)
144  m_list[i] = q.m_list[(i + q.m_first) % m_maxCount];
145 
146  return *this;
147  }
148 
149  //! Resize the queue, note that this function clears the queue.
150  void resize(const size_t size)
151  {
152  QUEUELOG(
153  "[%p]FifoQueue.resize, T=%s, max=%d, count=%d, size=%d\n", this,
154  typeid(T).name(), m_maxCount, m_currentCount, size);
155  if (E) eraseAndClear();
156  delete[] m_list;
157  if (size > 0)
158  m_maxCount = size;
159  else
160  m_maxCount = 1;
161  m_list = new T[m_maxCount];
162  m_currentCount = 0;
163  m_first = 0;
164  }
165 
166  //! Return true if the queue is empty.
167  bool empty() const { return (m_currentCount == 0); }
168  //! Return the maximum number of elements in the queue.
169  size_type size() const { return m_maxCount; }
170  //! Return the number of elements currnetly in the queue.
171  size_type length() const { return m_currentCount; }
172  //! Return the oldest element in the queue.
173  value_type& front() { return m_list[m_first]; }
174  //! Return the oldest element in the queue.
175  const value_type& front() const { return m_list[m_first]; }
176  //! Return the newest element in the queue.
178  {
179  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
180  }
181 
182  //! Return the newest element in the queue.
183  const value_type& back() const
184  {
185  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
186  }
187 
188  //! Insert x at the back of the queue.
189  void push(const value_type& x)
190  {
191  QUEUELOG(
192  "[%p]FifoQueue.push, T=%s, max=%d, count=%d, x=%p\n", this,
193  typeid(T).name(), m_maxCount, m_currentCount, x);
194  if (m_currentCount == m_maxCount)
195  {
196  if (m_deleteOnOverwrite) delete (m_list[m_first]);
197 
198  m_list[m_first] = x;
199  m_first = (m_first + 1) % m_maxCount;
200  }
201  else
202  {
204  }
205  }
206 
207  //! Remove the element at the front of the queue.
208  void pop(void)
209  {
210  QUEUELOG(
211  "[%p]FifoQueue.pop, T=%s, max=%d, count=%d\n", this,
212  typeid(T).name(), m_maxCount, m_currentCount);
213  m_first = (m_first + 1) % m_maxCount;
214  --m_currentCount;
215  }
216 
217  //! Remove the element at the back of the queue.
218  void popBack(void)
219  {
220  QUEUELOG(
221  "[%p]FifoQueue.popBack, T=%s, max=%d, count=%d\n", this,
222  typeid(T).name(), m_maxCount, m_currentCount);
223  --m_currentCount;
224  }
225 
226  //! Return the index'th oldest item from the queue
227  const value_type& operator[](size_t index) const
228  {
229  if (index >= m_currentCount)
230  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
231  else
232  return m_list[(m_first + index) % m_maxCount];
233  }
234 
235  //! Return the index'th oldest item from the queue
237  {
238  if (index >= m_currentCount)
239  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
240  else
241  return m_list[(m_first + index) % m_maxCount];
242  }
243 
244  void clear(void)
245  {
246  QUEUELOG(
247  "[%p]FifoQueue.clear, T=%s, max=%d, count=%d\n", this,
248  typeid(T).name(), m_maxCount, m_currentCount);
249  m_currentCount = 0;
250  m_first = 0;
251  }
252 
253  void remove(size_t index)
254  {
255  QUEUELOG(
256  "[%p]FifoQueue.remove, T=%s, max=%d, count=%d, index=%d\n", this,
257  typeid(T).name(), m_maxCount, m_currentCount, index);
258  if (index >= m_currentCount) return;
259  if (index == 0)
260  pop();
261  else
262  {
263  --m_currentCount;
264  for (size_t i = index; i < m_currentCount; ++i)
265  m_list[(m_first + i) % m_maxCount] =
266  m_list[(1 + m_first + i) % m_maxCount];
267  }
268  }
269 };
270 
271 //////////////////////////////////////////////////////////////////////////////////////////
272 
273 /*! \brief A FIFO queue with limited length (cyclic).
274 
275  The class is based on the STL queue class, but has a limited size. If more
276  items are
277  inserted than would fit, the oldest item is overwritten. The class can only
278  handle
279  non-pointer types.
280 */
281 template <class T>
283 {
284  private:
285 #if !defined(QUEUELOG)
286  // write to screen/debug-stream
287  void QUEUELOG(const char* str, ...)
288  {
289 #if defined(_LOG_TO_STDOUT)
290  va_list ptr;
291  va_start(ptr, str);
292  vprintf(str, ptr);
293 #else
294  //#ifdef _LOG_TO_DBVIEW
295  char buf[2048];
296 
297  va_list ptr;
298  va_start(ptr, str);
299  vsprintf(buf, str, ptr);
300 
301  OutputDebugString(buf);
302 #endif
303  }
304 #endif
305  protected:
306  size_t m_maxCount;
308  size_t m_first;
309 
310  T* m_list;
311 
312  public:
313  /** The type of the value stored in this queue. */
314  typedef T value_type;
315  /** The type of a 'size' value. */
316  typedef size_t size_type;
317 
318  //! Create an empty queue with capacity size.
320  {
321  QUEUELOG(
322  "[%p]FifoQueueBase.new (base), T=%s, size=%d\n", this,
323  typeid(T).name(), size);
324  if (size > 0)
325  m_maxCount = size;
326  else
327  m_maxCount = 1;
328  m_list = new T[m_maxCount];
329  m_currentCount = 0;
330  m_first = 0;
331  }
332 
333  //! The copy constructor.
335  {
336  QUEUELOG(
337  "[%p]FifoQueueBase.new (copy), T=%s, size=%d\n", this,
338  typeid(T).name(), q.m_maxCount);
339  m_maxCount = q.m_maxCount;
340  m_list = new T[m_maxCount];
341  m_currentCount = q.m_currentCount;
342  m_first = 0;
343  for (size_t i = 0; i < m_currentCount; ++i)
344  m_list[i] = q.m_list[(i + q.m_first) % m_maxCount];
345  }
346 
347  void eraseAndClear(void)
348  {
349  QUEUELOG(
350  "[%p]FifoQueueBase.eraseAndClear, T=%s, count=%d\n", this,
351  typeid(T).name(), m_currentCount);
352  for (size_t i = 0; i < m_currentCount; ++i)
353  delete m_list[(i + m_first) % m_maxCount];
354  m_currentCount = 0;
355  m_first = 0;
356  }
357 
358  //! The destructor.
360  {
361  QUEUELOG(
362  "[%p]FifoQueueBase.delete, T=%s, count=%d\n", this,
363  typeid(T).name(), m_currentCount);
364  m_maxCount = 0;
365  delete[] m_list;
366  }
367 
368  //! The assignment operator.
370  {
371  QUEUELOG(
372  "[%p]FifoQueueBase.operator =, T=%s, max=%d, count=%d, smax=%d, "
373  "scount=%d\n",
374  this, typeid(T).name(), m_maxCount, m_currentCount, q.m_maxCount,
375  q.m_currentCount);
376  if (m_maxCount != q.m_maxCount)
377  {
378  delete[] m_list;
379  m_maxCount = q.m_maxCount;
380  m_list = new T[m_maxCount];
381  }
382  m_currentCount = q.m_currentCount;
383  m_first = 0;
384  for (size_t i = 0; i < m_currentCount; ++i)
385  m_list[i] = q.m_list[(i + q.m_first) % m_maxCount];
386 
387  return *this;
388  }
389 
390  //! Resize the queue, note that this function clears the queue.
391  void resize(const size_t size)
392  {
393  QUEUELOG(
394  "[%p]FifoQueueBase.resize, T=%s, max=%d, count=%d, size=%d\n", this,
395  typeid(T).name(), m_maxCount, m_currentCount, size);
396  delete[] m_list;
397  if (size > 0)
398  m_maxCount = size;
399  else
400  m_maxCount = 1;
401  m_list = new T[m_maxCount];
402  m_currentCount = 0;
403  m_first = 0;
404  }
405 
406  //! Return true if the queue is empty.
407  bool empty() const { return (m_currentCount == 0); }
408  //! Return the maximum number of elements in the queue.
409  size_type size() const { return m_maxCount; }
410  //! Return the number of elements currnetly in the queue.
411  size_type length() const { return m_currentCount; }
412  //! Return the oldest element in the queue.
413  value_type& front() { return m_list[m_first]; }
414  //! Return the oldest element in the queue.
415  const value_type& front() const { return m_list[m_first]; }
416  //! Return the newest element in the queue.
418  {
419  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
420  }
421 
422  //! Return the newest element in the queue.
423  const value_type& back() const
424  {
425  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
426  }
427 
428  //! Insert x at the back of the queue.
429  void push(const value_type& x)
430  {
431  QUEUELOG(
432  "[%p]FifoQueueBase.push, T=%s, max=%d, count=%d, x=%p\n", this,
433  typeid(T).name(), m_maxCount, m_currentCount, x);
434  if (m_currentCount == m_maxCount)
435  {
436  m_list[m_first] = x;
437  m_first = (m_first + 1) % m_maxCount;
438  }
439  else
440  {
442  }
443  }
444 
445  //! Insert x at the front of the queue (LIFO operation).
446  void push_front(const value_type& x)
447  {
448  QUEUELOG(
449  "[%p]FifoQueueBase.push_front, T=%s, max=%d, count=%d, x=%p\n",
450  this, typeid(T).name(), m_maxCount, m_currentCount, x);
451  m_first = (m_first + m_maxCount - 1) % m_maxCount;
452  if (m_currentCount == 0) m_first = 0;
453  m_list[m_first] = x;
455  }
456 
457  //! Remove the element at the front of the queue.
458  void pop(void)
459  {
460  QUEUELOG(
461  "[%p]FifoQueueBase.pop, T=%s, max=%d, count=%d\n", this,
462  typeid(T).name(), m_maxCount, m_currentCount);
463  if (m_currentCount > 0)
464  {
465  m_first = (m_first + 1) % m_maxCount;
466  --m_currentCount;
467  }
468  }
469 
470  //! Remove the element at the back of the queue.
471  void popBack(void)
472  {
473  QUEUELOG(
474  "[%p]FifoQueueBase.popBack, T=%s, max=%d, count=%d\n", this,
475  typeid(T).name(), m_maxCount, m_currentCount);
476  if (m_currentCount > 0) --m_currentCount;
477  }
478 
479  //! Return the index'th oldest item from the queue
480  const value_type& operator[](size_t index) const
481  {
482  if (index >= m_currentCount)
483  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
484  else
485  return m_list[(m_first + index) % m_maxCount];
486  }
487 
488  //! Return the index'th oldest item from the queue
490  {
491  if (index >= m_currentCount)
492  return m_list[(m_first + m_currentCount - 1) % m_maxCount];
493  else
494  return m_list[(m_first + index) % m_maxCount];
495  }
496 
497  void clear(void)
498  {
499  QUEUELOG(
500  "[%p]FifoQueueBase.clear, T=%s, max=%d, count=%d\n", this,
501  typeid(T).name(), m_maxCount, m_currentCount);
502  m_currentCount = 0;
503  m_first = 0;
504  }
505 
506  void remove(size_t index)
507  {
508  QUEUELOG(
509  "[%p]FifoQueueBase.remove, T=%s, max=%d, count=%d, index=%d\n",
510  this, typeid(T).name(), m_maxCount, m_currentCount, index);
511  if (index >= m_currentCount) return;
512  if (index == 0)
513  pop();
514  else
515  {
516  --m_currentCount;
517  for (size_t i = index; i < m_currentCount; ++i)
518  m_list[(m_first + i) % m_maxCount] =
519  m_list[(1 + m_first + i) % m_maxCount];
520  }
521  }
522 };
523 
524 } // namespace xsens
525 
526 #endif // _FIFOQUEUE_H_2006_05_03
size_type size() const
Return the maximum number of elements in the queue.
FifoQueueBasic(const FifoQueueBasic< T > &q)
The copy constructor.
FifoQueue(const FifoQueue< T, E2 > &q)
The copy constructor.
size_type size() const
Return the maximum number of elements in the queue.
value_type & back()
Return the newest element in the queue.
const value_type & back() const
Return the newest element in the queue.
size_type length() const
Return the number of elements currnetly in the queue.
const value_type & operator[](size_t index) const
Return the index&#39;th oldest item from the queue.
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3727
bool empty() const
Return true if the queue is empty.
const value_type & front() const
Return the oldest element in the queue.
void pop(void)
Remove the element at the front of the queue.
void eraseAndClear(void)
size_t size_type
The type of a &#39;size&#39; value.
T value_type
The type of the value stored in this queue.
void push(const value_type &x)
Insert x at the back of the queue.
FifoQueue< T, E > & operator=(const FifoQueue< T, E2 > &q)
The assignment operator.
void push(const value_type &x)
Insert x at the back of the queue.
const value_type & operator[](size_t index) const
Return the index&#39;th oldest item from the queue.
void resize(const size_t size)
Resize the queue, note that this function clears the queue.
~FifoQueue()
The destructor.
FifoQueue(size_type size=16, bool delOnOverwrite=true)
Create an empty queue with capacity size.
GLuint index
Definition: glext.h:4068
#define QUEUELOG(...)
value_type & front()
Return the oldest element in the queue.
void popBack(void)
Remove the element at the back of the queue.
T value_type
The type of the value stored in this queue.
const value_type & front() const
Return the oldest element in the queue.
const value_type & back() const
Return the newest element in the queue.
size_t size_type
The type of a &#39;size&#39; value.
FifoQueueBasic(size_type size=16)
Create an empty queue with capacity size.
value_type & operator[](size_t index)
Return the index&#39;th oldest item from the queue.
bool empty() const
Return true if the queue is empty.
int int vsprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compile...
Definition: os.cpp:214
value_type & front()
Return the oldest element in the queue.
A FIFO queue with limited length (cyclic).
A FIFO queue with limited length (cyclic).
FifoQueueBasic< T > & operator=(const FifoQueueBasic< T > &q)
The assignment operator.
GLuint const GLchar * name
Definition: glext.h:4068
void resize(const size_t size)
Resize the queue, note that this function clears the queue.
void pop(void)
Remove the element at the front of the queue.
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
GLsizeiptr size
Definition: glext.h:3934
size_type length() const
Return the number of elements currnetly in the queue.
GLenum GLint x
Definition: glext.h:3542
void popBack(void)
Remove the element at the back of the queue.
~FifoQueueBasic()
The destructor.
void push_front(const value_type &x)
Insert x at the front of the queue (LIFO operation).
value_type & operator[](size_t index)
Return the index&#39;th oldest item from the queue.
value_type & back()
Return the newest element in the queue.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019