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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020