Main MRPT website > C++ reference for MRPT 1.5.7
xsarray.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 #ifndef XSARRAY_H
10 #define XSARRAY_H
11 
12 #include "xstypesconfig.h"
13 
14 #define XSARRAY_DECL(T)\
15  T* const m_data; /*!< Pointer to contained data buffer */\
16  const XsSize m_size; /*!< Size of used data buffer in number of items */\
17  const XsSize m_reserved; /*!< Size of allocated data buffer in number of items */\
18  const int m_flags; /*!< Flags for data management */\
19  XsArrayDescriptor const* const m_descriptor; /*!< Describes how to handle the items in the array */
20 
21 #define XSARRAY_STRUCT(S,T) struct S { XSARRAY_DECL(T) }
22 #define XSARRAY_INITIALIZER(D) { 0, 0, 0, XSDF_Managed, D }
23 
24 /*! \cond XS_INTERNAL */
25 typedef void (*XsArrayItemSwapFunc)(void*, void*);
26 typedef void (*XsArrayItemStructFunc)(void*);
27 typedef void (*XsArrayItemCopyFunc)(void*, void const*);
28 typedef int (*XsArrayItemCompareFunc)(void const*, void const*);
29 #if defined(__GNUC__)
30 #define XSEXPCASTITEMSWAP (XsArrayItemSwapFunc)
31 #define XSEXPCASTITEMMAKE (XsArrayItemStructFunc)
32 #define XSEXPCASTITEMCOPY (XsArrayItemCopyFunc)
33 #define XSEXPCASTITEMCOMP (XsArrayItemCompareFunc)
34 #else
35 #define XSEXPCASTITEMSWAP
36 #define XSEXPCASTITEMMAKE
37 #define XSEXPCASTITEMCOPY
38 #define XSEXPCASTITEMCOMP
39 #endif
40 /*! \endcond */
41 
42 /*! \brief This object describes how to treat the data in an array.
43  \details Ususally there is one static instance per type of array that will be used by all
44  XsArrays of that type.
45 */
46 struct XsArrayDescriptor {
47 #ifndef __cplusplus
48  const
49 #else
50 protected:
51  template <typename T, XsArrayDescriptor const& D, typename I> friend struct XsArrayImpl;
52 #endif
53  XsSize itemSize; //!< The size of an array item in bytes
54  void (*itemSwap)(void* a, void* b); //!< The function to use for swapping the data of two array items. \param a Pointer to first item to swap. \param b Pointer to second item to swap.
55  void (*itemConstruct)(void* e); //!< The function to use for constructing a new array item. May be 0 for simple types. \param e Pointer to item to construct.
56  void (*itemCopyConstruct)(void* e, void const* s); //!< The function to use for constructing a new array item with a source initializer. This may not be 0. \param e Pointer to item to construct. \param s Pointer to source item to copy from.
57  void (*itemDestruct)(void* e); //!< The function to use for destructing a array item. May be 0 for simple types. \param e Pointer to item to destruct.
58  void (*itemCopy)(void* to, void const* from); //!< The function to use for copying the data of \a from to \a to. \param to Pointer to item to copy to. \param from Pointer to item to copy from.
59  int (*itemCompare)(void const* a, void const* b); //!< The function to use for comparing two items. \param a Left hand side of comparison. \param b Right hand side of comparison. \returns The function will return 0 when the items are equal. When greater/less comparison is possible, the function should return < 0 if a < b and > 0 if a > b.
60 };
62 
63 #ifdef __cplusplus
64 #include <iterator>
65 #include <stdexcept>
66 extern "C" {
67 #endif
68 
69 XSTYPES_DLL_API void XsArray_construct(void* thisPtr, XsArrayDescriptor const* const descriptor, XsSize count, void const* src);
70 XSTYPES_DLL_API void XsArray_copyConstruct(void* thisPtr, void const* src);
71 XSTYPES_DLL_API void XsArray_destruct(void* thisPtr);
72 XSTYPES_DLL_API void XsArray_assign(void* thisPtr, XsSize count, void const* src);
73 XSTYPES_DLL_API void XsArray_resize(void* thisPtr, XsSize count);
74 XSTYPES_DLL_API void XsArray_reserve(void* thisPtr, XsSize count);
75 XSTYPES_DLL_API void XsArray_copy(void* thisPtr, void const* src);
76 XSTYPES_DLL_API void XsArray_append(void* thisPtr, void const* other);
77 XSTYPES_DLL_API void XsArray_insert(void* thisPtr, XsSize index, XsSize count, void const* src);
79 XSTYPES_DLL_API void XsArray_swap(void* a, void* b);
80 XSTYPES_DLL_API int XsArray_compare(void const* a, void const* b);
81 XSTYPES_DLL_API int XsArray_compareSet(void const* a, void const* b);
82 XSTYPES_DLL_API int XsArray_find(void const* thisPtr, void const* needle);
83 XSTYPES_DLL_API void const* XsArray_at(void const* thisPtr, XsSize index);
84 XSTYPES_DLL_API void* XsArray_atIndex(void* thisPtr, XsSize index);
85 XSTYPES_DLL_API void XsArray_removeDuplicates(void* thisPtr);
86 
87 struct XsArray {
88  XSARRAY_DECL(void)
89 #ifdef __cplusplus
90  //! \copydoc XsArray_construct
91  inline XsArray(XsArrayDescriptor const* descriptor, XsSize count = 0, void const* src = 0)
92  : m_data(0)
93  , m_size(0)
94  , m_reserved(0)
95  , m_flags(0)
96  , m_descriptor(0)
97  {
98  XsArray_construct(this, descriptor, count, src);
99  }
100 
101  //! \copydoc XsArray_copyConstruct
102  inline XsArray(const XsArray& src)
103  : m_data(0)
104  , m_size(0)
105  , m_reserved(0)
106  , m_flags(0)
107  , m_descriptor(0)
108  {
109  XsArray_copyConstruct(this, &src);
110  }
111 
112  //! \brief Creates a array that references the data supplied in \a ref without allocating the data itself
113  inline explicit XsArray(XsArrayDescriptor const* descriptor, void* ref, XsSize count, XsDataFlags flags)
114  : m_data(ref)
115  , m_size(count)
116  , m_reserved(count)
117  , m_flags(flags)
118  , m_descriptor(descriptor)
119  {
120  }
121 
122  //! \brief Destructor
123  ~XsArray()
124  {
125  XsArray_destruct(this);
126  }
127 
128  /*! \brief Assignment operator
129  \details Copies the values in \a src into \a this
130  \param src The array to copy from
131  \return A reference to this
132  */
133  inline XsArray const& operator=(const XsArray& src)
134  {
135  if (this != &src)
136  XsArray_copy(this, &src);
137  return *this;
138  }
139 #endif
140 };
141 
142 typedef struct XsArray XsArray;
143 
144 #ifdef __cplusplus
145 } // extern "C"
146 
147 /*! \brief A C++ wrapper for XsArray, this is a template class for the actual specialized classes
148  \tparam T The type of the contained values
149  \tparam D The descriptor to use for this specific array implementation. This must be statically allocated and its lifetime must encompass the lifetime of objects that use it.
150  \tparam I The class that inherits from the XsArrayImpl. Some functions (such as the streaming operator) require the inheriting type to be returned for proper functionality.
151 */
152 template <typename T, XsArrayDescriptor const& D, typename I>
153 struct XsArrayImpl : protected XsArray { // MRPT
154  //! \brief The contained type
155  typedef T value_type;
156 
157  //! \brief A shorthand for the type of this specific implementation
158  typedef XsArrayImpl<T, D, I> ArrayImpl;
159 
160  /*! \brief Construct an XsArray
161  \param count the number of items in src
162  \param src pointer to an array of output configurations
163  \sa XsArray_construct
164  */
165  inline XsArrayImpl<T, D, I>(XsSize count = 0, T const* src = 0)
166  : XsArray(&D, count, src)
167  {
168  }
169 
170  //! \brief Constructs the XsArray as a copy of \a other
171  inline XsArrayImpl<T, D, I>(ArrayImpl const& other)
172  : XsArray(other)
173  {
174  }
175 #ifndef XSENS_NOITERATOR
176  //! \brief Constructs the XsArray with a copy of the array bound by the supplied iterators \a beginIt and \a endIt
177  template <typename Iterator>
178  inline XsArrayImpl<T, D, I>(Iterator const& beginIt, Iterator const& endIt)
179  : XsArray(&D, 0, 0)
180  {
181  ptrdiff_t diff = endIt-beginIt;
182  if (diff > 0)
183  {
184  reserve((XsSize) diff);
185  for (Iterator it = beginIt; it != endIt; ++it)
186  push_back(*it);
187  }
188  }
189 #endif
190  //! \brief Creates the XsArray as a reference to the data supplied in \a ref
191  inline explicit XsArrayImpl<T, D, I>(T* ref, XsSize sz, XsDataFlags flags = XSDF_None)
192  : XsArray(&D, ref, sz, flags)
193  {
194  }
195 
196  //! \copydoc XsArray_destruct
197  inline ~XsArrayImpl()
198  {
199  XsArray_destruct(this);
200  }
201 
202  //! \brief Clears the array \sa XsArray_destruct()
203  inline void clear()
204  {
205  XsArray_destruct(this);
206  }
207 
208  /*! \brief Tests \a other for equality
209  \param other the array to compare against
210  \returns true if the two arrays are equal
211  \sa XsArray_compareArray
212  */
213  inline bool operator == (ArrayImpl const& other) const
214  {
215  return !XsArray_compare(this, &other);
216  }
217 
218  /*! \brief Tests \a other for inequality
219  \param other the array to compare against
220  \returns true if the two arrays are not equal
221  \sa XsArray_compareArray
222  */
223  inline bool operator != (ArrayImpl const& other) const
224  {
225  return !!XsArray_compare(this, &other);
226  }
227 
228  //! \copydoc XsArray_reserve
229  inline void reserve(XsSize count)
230  {
231  XsArray_reserve(this, count);
232  }
233 
234  //! \brief Returns the reserved space in number of items
235  inline XsSize reserved() const
236  {
237  return m_reserved;
238  }
239 
240  //! \brief Returns the XsArrayDescriptor describing the array
241  inline XsArrayDescriptor const& descriptor() const
242  {
243  return *m_descriptor;
244  }
245 
246 protected:
247 #ifndef XSENS_NOITERATOR
248  /*! \brief STL-style iterator */
249  template <ptrdiff_t F, typename R, typename Derived>
250  struct IteratorImplBase {
251  public:
252  //! \brief Difference between two items
253  typedef ptrdiff_t difference_type;
254  //! \brief The contained type
255  typedef T value_type;
256  //! \brief Type of pointer
257  typedef T* pointer;
258  //! \brief Type of reference
259  typedef T& reference;
260 #ifndef XSENS_NO_STL
261  //! \brief The category of this type of iterator (random access)
262  typedef std::random_access_iterator_tag iterator_category;
263 #endif
264  //! \brief The type of the inherited class that is actually this class
265  typedef Derived this_type;
266  //! \brief The direction of the iterator, +1 = forward, -1 = reverse
267  static const ptrdiff_t direction = F;
268  protected:
269  //! \brief Basic constructor
270  inline explicit IteratorImplBase(void* p = 0) : m_ptr((T*) p) {}
271  //! \brief Basic constructor
272  inline explicit IteratorImplBase(T* p) : m_ptr(p) {}
273  //! \brief Copy constructor
274  inline IteratorImplBase(this_type const& i) : m_ptr(i.m_ptr) {}
275  public:
276  //! \brief Assignment operator
277  inline this_type operator =(void* p)
278  {
279  m_ptr = (T*) p;
280  return this_type(m_ptr);
281  }
282  //! \brief Assignment operator
283  inline this_type operator =(T* p)
284  {
285  m_ptr = p;
286  return this_type(m_ptr);
287  }
288  //! \brief Assignment operator
289  inline this_type operator =(this_type const& i)
290  {
291  m_ptr = i.m_ptr;
292  return this_type(m_ptr);
293  }
294  //! \brief Prefix increment by one operator
295  inline this_type operator ++()
296  {
297  m_ptr = (T*) ptrAt(m_ptr, F);
298  return this_type(m_ptr);
299  }
300  //! \brief Postfix increment by one operator
301  inline this_type operator ++(int)
302  {
303  this_type p(m_ptr);
304  m_ptr = (T*) ptrAt(m_ptr, F);
305  return p;
306  }
307  //! \brief Prefix decrement by one operator
308  inline this_type operator --()
309  {
310  m_ptr = (T*) ptrAt(m_ptr, -F);
311  return this_type(m_ptr);
312  }
313  //! \brief Postfix decrement by one operator
314  inline this_type operator --(int)
315  {
316  this_type p(m_ptr);
317  m_ptr = (T*) ptrAt(m_ptr, -F);
318  return p;
319  }
320  //! \brief Increment by \a count operator
321  inline this_type operator +=(ptrdiff_t count)
322  {
323  m_ptr = ptrAt(m_ptr, F*count);
324  return this_type(m_ptr);
325  }
326  //! \brief Decrement by \a count operator
327  inline this_type operator -=(ptrdiff_t count)
328  {
329  m_ptr = ptrAt(m_ptr, -F*count);
330  return this_type(m_ptr);
331  }
332  //! \brief Addition by \a count operator
333  inline this_type operator +(ptrdiff_t count) const
334  {
335  return this_type(ptrAt(m_ptr, F*count));
336  }
337  //! \brief Subtraction by \a count operator
338  inline this_type operator -(ptrdiff_t count) const
339  {
340  return this_type(ptrAt(m_ptr, -F*count));
341  }
342  /*! \brief Returns the difference in number of items between the two iterators
343  \details The function computes the difference between this iterator and \a other. The
344  difference may be negative if \a other is 'ahead' of this. The function takes the direction
345  of the iterator into consideration.
346  \param other The iterator to subtract from this.
347  \returns The difference between the two iterators.
348  */
349  inline difference_type operator - (const this_type& other) const
350  {
351  return (F * (reinterpret_cast<char*>(m_ptr) - reinterpret_cast<char*>(other.m_ptr))) / D.itemSize;
352  }
353  //! \brief Iterator comparison
354  inline bool operator == (this_type const& i) const { return m_ptr == i.m_ptr; }
355  //! \brief Iterator comparison, taking direction into account
356  inline bool operator <= (this_type const& i) const { return (F==1)?(m_ptr <= i.m_ptr):(m_ptr >= i.m_ptr); }
357  //! \brief Iterator comparison, taking direction into account
358  inline bool operator < (this_type const& i) const { return (F==1)?(m_ptr < i.m_ptr):(m_ptr > i.m_ptr); }
359  //! \brief Iterator comparison
360  inline bool operator != (this_type const& i) const { return m_ptr != i.m_ptr; }
361  //! \brief Iterator comparison, taking direction into account
362  inline bool operator >= (this_type const& i) const { return (F==1)?(m_ptr >= i.m_ptr):(m_ptr <= i.m_ptr); }
363  //! \brief Iterator comparison, taking direction into account
364  inline bool operator > (this_type const& i) const { return (F==1)?(m_ptr > i.m_ptr):(m_ptr < i.m_ptr); }
365  //! \brief Dereferencing operator
366  inline R& operator *() const { return *(R*) ptr(); }
367  //! \brief Pointer operator
368  inline R* operator ->() const { return (R*) ptr(); }
369  //! \brief Access to internal pointer object, use should be avoided
370  inline T* ptr() const { return m_ptr; }
371  private:
372  //! \brief The internal pointer
373  T* m_ptr;
374  };
375 #endif
376 
377 public:
378 #ifndef XSENS_NOITERATOR
379  /*! \brief A non-const iterator implementation */
380  template <ptrdiff_t F>
381  struct IteratorImpl : public IteratorImplBase<F, T, IteratorImpl<F> >
382  {
383  private:
384  //! \brief A shorthand for the base object
385  typedef IteratorImplBase<F, T, IteratorImpl<F> > ParentType;
386  public:
387  //! \brief Basic constructor
388  inline IteratorImpl(void* p = 0) : ParentType(p) {}
389  //! \brief Basic constructor
390  inline IteratorImpl(T* p) : ParentType(p) {}
391  //! \brief Copy constructor
392  inline IteratorImpl(const IteratorImpl& i) : ParentType(i) {}
393  };
394 
395  /*! \brief A const iterator implementation */
396  template <ptrdiff_t F>
397  struct IteratorImplConst : public IteratorImplBase<F, T const, IteratorImplConst<F> >
398  {
399  private:
400  //! \brief A shorthand for the base object
401  typedef IteratorImplBase<F, T const, IteratorImplConst<F> > ParentType;
402  public:
403  //! \brief Basic constructor
404  inline IteratorImplConst(void* p = 0) : ParentType(p) {}
405  //! \brief Basic constructor
406  inline IteratorImplConst(T* p) : ParentType(p) {}
407  //! \brief Copy constructor
408  inline IteratorImplConst(IteratorImpl<F> const& i) : ParentType(i.ptr()) {}
409  //! \brief Copy constructor
410  inline IteratorImplConst(IteratorImplConst const& i) : ParentType(i) {}
411  };
412 
413  //! \brief STL-style mutable forward iterator
414  typedef IteratorImpl<1> iterator;
415  //! \brief STL-style mutable reverse iterator
416  typedef IteratorImpl<-1> reverse_iterator;
417  //! \brief STL-style mutable const forward iterator
418  typedef IteratorImplConst<1> const_iterator;
419  //! \brief STL-style mutable const reverse iterator
420  typedef IteratorImplConst<-1> const_reverse_iterator;
421 
422  /*! \brief STL-style const_iterator to the first data item in the array */
423  inline const_iterator begin() const { return const_iterator(m_data); }
424  /*! \brief STL-style const_iterator to the first data item past the end of the array */
425  inline const_iterator end() const { return begin() + (ptrdiff_t) size(); }
426 
427  /*! \brief STL-style const_reverse_iterator to the first data item in the reversed array */
428  inline const_reverse_iterator rbegin() const { return rend() - (ptrdiff_t) size(); }
429  /*! \brief STL-style const_reverse_iterator to the first data item past the end of the reversed array */
430  inline const_reverse_iterator rend() const { return const_reverse_iterator(m_data) + (ptrdiff_t) 1; }
431 
432  /*! \brief STL-style iterator to the first data item in the array */
433  inline iterator begin() { return iterator(m_data); }
434  /*! \brief STL-style iterator to the first data item past the end of the array */
435  inline iterator end() { return begin() + (ptrdiff_t) size(); }
436 
437  /*! \brief STL-style reverse_iterator to the first data item in the reversed array */
438  inline reverse_iterator rbegin() { return rend() - (ptrdiff_t) size(); }
439  /*! \brief STL-style reverse_iterator to the first data item past the end of the reversed array */
440  inline reverse_iterator rend() { return reverse_iterator(m_data) + (ptrdiff_t) 1; }
441 #endif
442  /*! \brief indexed data access operator */
443  inline T & operator[] (XsSize index) const
444  {
445  assert(index < m_size);
446  return *ptrAt(m_data, index);
447  }
448  /*! \brief indexed data access operator */
449  inline T& operator[] (XsSize index)
450  {
451  assert(index < m_size);
452  return *ptrAt(m_data, (ptrdiff_t) index);
453  }
454  /*! \brief indexed data access \sa operator[] \param index Index of item to access. \returns The item at \a index (by value). */
455  inline T const& at(XsSize index) const
456  {
457 #ifdef XSENS_NO_EXCEPTIONS
458  assert(index >= m_size);
459 #else
460  if (index >= m_size)
461  throw std::out_of_range("index out of range");
462 #endif
463  return *ptrAt(m_data, index);
464  }
465  /*! \brief indexed data access \sa operator[] \param index Index of item to access. \returns The item at \a index (by value). */
466  inline T& at(XsSize index)
467  {
468 #ifdef XSENS_NO_EXCEPTIONS
469  assert(index >= m_size);
470 #else
471  if (index >= m_size)
472  throw std::out_of_range("index out of range");
473 #endif
474  return *ptrAt(m_data, index);
475  }
476  /*! \brief Insert \a item at \a index
477  \param item The item to insert
478  \param index The index to insert the item. When beyond the end of the array, the item is appended.
479  \sa XsArray_insert
480  */
481  inline void insert(T const& item, XsSize index)
482  {
483  insert(&item, index, 1);
484  }
485  /*! \brief Insert \a item at \a index
486  \param items The items to insert
487  \param index The index to insert the items. When beyond the end of the array, the items are appended.
488  \param count The number of items to insert
489  \sa XsArray_insert
490  */
491  inline void insert(T const* items, XsSize index, XsSize count)
492  {
493  XsArray_insert(this, index, count, items);
494  }
495 
496 #ifndef XSENS_NOITERATOR
497  /*! \brief Insert \a item before iterator \a it
498  \param item The item to insert
499  \param it The iterator before which to insert the item.
500  \sa XsArray_insert
501  */
502  inline void insert(T const& item, const_iterator it)
503  {
504  insert(&item, indexOf(it), 1);
505  }
506  /*! \brief Insert \a item before iterator \a it
507  \param item The item to insert
508  \param it The iterator before which to insert the item.
509  \sa XsArray_insert
510  */
511  inline void insert(T const& item, const_reverse_iterator it)
512  {
513  insert(&item, indexOf(it), 1);
514  }
515 
516  /*! \brief Insert \a item at \a index
517  \param items The items to insert
518  \param it The iterator before which to insert the item.
519  \param count The number of items to insert
520  \sa XsArray_insert
521  */
522  inline void insert(T const* items, const_iterator it, XsSize count)
523  {
524  insert(items, indexOf(it), count);
525  }
526  /*! \brief Insert \a item at \a index
527  \param items The items to insert
528  \param it The iterator before which to insert the item.
529  \param count The number of items to insert
530  \sa XsArray_insert
531  */
532  inline void insert(T const* items, const_reverse_iterator it, XsSize count)
533  {
534  insert(items, indexOf(it), count);
535  }
536 #endif
537 
538  /*! \brief Adds \a item to the end of the array \sa XsArray_insert \param item The item to append to the array. */
539  inline void push_back(T const& item)
540  {
541  insert(&item, (XsSize) -1, 1);
542  }
543  /*! \brief Removes \a count items from the end of the array \sa XsArray_erase \param count The number items to remove */
544  inline void pop_back(XsSize count = 1)
545  {
546  if (count >= size())
547  erase(0, (XsSize) -1);
548  else
549  erase(size()-count, count);
550  }
551  /*! \brief Adds \a item to the start of the array \sa XsArray_insert \param item The item to insert at the front of the array */
552  inline void push_front(T const& item)
553  {
554  insert(&item, 0, 1);
555  }
556  /*! \brief Removes \a count items from the start of the array \param count The number items to remove \sa XsArray_erase */
557  inline void pop_front(XsSize count = 1)
558  {
559  erase(0, count);
560  }
561  /*! \brief Returns the number of items currently in the array
562  \returns The number of items currently in the array
563  \sa reserved \sa setSize \sa resize
564  */
565  inline XsSize size() const
566  {
567  return m_size;
568  }
569  /*! \brief Removes \a count items from the array starting at \a index. \param index The index of the first item to remove. \param count The number of items to remove. */
570  inline void erase(XsSize index, XsSize count = 1)
571  {
572  XsArray_erase(this, index, count);
573  }
574 #ifndef XSENS_NOITERATOR
575  /*! \brief Removes the item at iterator \a it. \details \param it The item to remove. \returns An iterator pointing to the next item after the erased item. */
576  inline iterator erase(iterator it)
577  {
578  XsSize idx = indexOf(it);
579  erase(idx, 1);
580  return (idx < size()) ? ptrAt(m_data, idx) : end();
581  }
582  /*! \brief Removes the item at iterator \a it. \details \param it The item to remove. \returns An iterator pointing to the next item after the erased item. */
583  inline reverse_iterator erase(reverse_iterator it)
584  {
585  XsSize idx = indexOf(it);
586  erase(idx, 1);
587  return (idx < size()) ? ptrAt(m_data, idx) : rend();
588  }
589 #endif
590  /*! \copydoc XsArray_assign \sa XsArray_assign */
591  inline void assign(XsSize count, T const* src)
592  {
593  XsArray_assign(this, count, src);
594  }
595  /*! \copydoc XsArray_resize \sa XsArray_resize */
596  inline void resize(XsSize count)
597  {
598  XsArray_resize(this, count);
599  }
600  /*! \brief Set the size of the array to \a count. \details The contents of the array after this operation are undefined. \param count The desired new size fo the array. \sa XsArray_assign \sa reserve \sa resize */
601  inline void setSize(XsSize count)
602  {
603  if (count != m_size)
604  XsArray_assign(this, count, 0);
605  }
606  /*! \copydoc XsArray_append \sa XsArray_append */
607  inline void append(ArrayImpl const& other)
608  {
609  XsArray_append(this, &other);
610  }
611  /*! \brief Assignment operator, copies \a other into this, overwriting the old contents \param other The array to copy from \returns A reference to this \sa XsArray_copy */
612  inline ArrayImpl& operator=(ArrayImpl const& other)
613  {
614  if (this != &other)
615  XsArray_copy(this, &other);
616  return *this;
617  }
618  /*! \brief Returns whether the array is empty. \details This differs slightly from a straight check for size() != 0 in that it also works for fixed-size XsArrays. \returns true if the array is empty. */
619  inline bool empty() const
620  {
621  return (size() == 0) || (m_data == 0) || (m_flags & XSDF_Empty);
622  }
623 
624 #ifndef XSENS_NOITERATOR
625  /*! \brief Return the inheriting object */
626  inline I const& inherited() const { return *static_cast<I const*>(this); }
627 
628  /*! \brief Return the inheriting object */
629  inline I& inherited() { return *static_cast<I*>(this); }
630 
631 #endif
632  /*! \brief Swap the contents of the array with those of \a other. \param other The array to swap contents with. \sa XsArray_swap*/
633  inline void swap(ArrayImpl& other)
634  {
635  XsArray_swap(this, &other);
636  }
637 
638  /*! \brief Append \a item in a stream-like manner.
639  \details This allows for constructing XsArrays easily like this:
640  XsInt64Array array = XsInt64Array() << 1 << 2 << 3;
641  \param item The item to append to the array.
642  \returns A reference to the modified array
643  \sa push_back
644  */
645 #ifndef XSENS_NOITERATOR
646  inline I& operator <<(T const& item)
647  {
648  push_back(item);
649  return inherited();
650  }
651 #endif
652 
653  /*! \copydoc XsArray_find */
654  inline int find(T const& needle) const
655  {
656  return XsArray_find(this, &needle);
657  }
658 #ifndef XSENS_NOITERATOR
659  /*! \brief Returns the linear index of \a it in the array
660  \param it The iterator to analyze
661  \returns The linear forward index of the item pointed to by \a it. If \a it points to before the
662  beginning of the array it returns 0. If it points beyond the end, it returns the current size()
663  */
664  template <ptrdiff_t F, typename R, typename Derived>
665  XsSize indexOf(IteratorImplBase<F,R,Derived> const& it) const
666  {
667  ptrdiff_t d = ((char const*) it.ptr() - (char const*) m_data);
668  if (d >= 0)
669  {
670  XsSize r = d/D.itemSize;
671  if (r <= size())
672  return r;
673  return size();
674  }
675  return 0;
676  }
677 #endif
678 
679  /*! \copydoc XsArray_removeDuplicates */
680  inline void removeDuplicates()
681  {
683  }
684 protected: // MRPT
685  /*! \internal
686  \brief Generic pointer movement function
687  \details This function adds \a count items to \a ptr based on the size specified in \a descriptor
688  \param descriptor The array descriptor that contains the size of a array item
689  \param ptr The pointer to start from
690  \param count The number of items to move up or down. count may be negative
691  \returns The requested pointer.
692  \note The return value loses its constness, take care when using this function directly. In most cases
693  it should not be necessary to use this function in user code.
694  */
695  inline static T* ptrAt(void const* ptr, ptrdiff_t count)
696  {
697  return (T*)(void*)(((char*)ptr)+count*D.itemSize);
698  }
699 };
700 #endif
701 
702 #endif // file guard
XSTYPES_DLL_API void const * XsArray_at(void const *thisPtr, XsSize index)
XSTYPES_DLL_API void XsArray_insert(void *thisPtr, XsSize index, XsSize count, void const *src)
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
GLuint GLuint GLsizei count
Definition: glext.h:3512
This object describes how to treat the data in an array.
Definition: xsarray.h:51
EIGEN_STRONG_INLINE bool empty() const
_u32 reserved
Definition: rplidar_cmd.h:22
GLsizei const GLvoid * pointer
Definition: glext.h:3702
GLenum GLint ref
Definition: glext.h:3888
XSTYPES_DLL_API void XsArray_resize(void *thisPtr, XsSize count)
iterator operator++(int)
A thread-safe (ts) container which minimally emulates a std::map<>&#39;s [] and find() methods but which ...
Definition: ts_hash_map.h:97
XSTYPES_DLL_API void XsArray_destruct(void *thisPtr)
The object contains undefined data / should be considered empty. Usually only relevant when XSDF_Fixe...
Definition: xstypedefs.h:41
bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
Scalar * iterator
Definition: eigen_plugins.h:23
void(* itemSwap)(void *a, void *b)
The function to use for swapping the data of two array items.
Definition: xsarray.h:59
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
EIGEN_STRONG_INLINE void push_back(Scalar val)
Insert an element at the end of the container (for 1D vectors/arrays)
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:17
const Scalar * const_iterator
Definition: eigen_plugins.h:24
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:139
GLdouble s
Definition: glext.h:3602
GLuint src
Definition: glext.h:6303
No flag set.
Definition: xstypedefs.h:38
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
XSTYPES_DLL_API int XsArray_compareSet(void const *a, void const *b)
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:89
XSTYPES_DLL_API void XsArray_erase(void *thisPtr, XsSize index, XsSize count)
XSTYPES_DLL_API int XsArray_find(void const *thisPtr, void const *needle)
void(* itemCopyConstruct)(void *e, void const *s)
The function to use for constructing a new array item with a source initializer. This may not be 0...
Definition: xsarray.h:61
GLuint index
Definition: glext.h:3891
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:70
XSTYPES_DLL_API void XsArray_removeDuplicates(void *thisPtr)
GLuint GLuint end
Definition: glext.h:3512
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:123
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
GLubyte GLubyte b
Definition: glext.h:5575
bool operator>(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:289
void(* itemCopy)(void *to, void const *from)
The function to use for copying the data of from to to.
Definition: xsarray.h:63
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:41
size_t m_size
Number of elements accessed with write access so far.
Definition: ts_hash_map.h:103
const XsSize itemSize
The size of an array item in bytes.
Definition: xsarray.h:58
_W64 int ptrdiff_t
Definition: glew.h:133
void(* itemDestruct)(void *e)
The function to use for destructing a array item. May be 0 for simple types.
Definition: xsarray.h:62
std::ostream & operator<<(std::ostream &out, MD5 md5)
Definition: md5.cpp:419
XSTYPES_DLL_API void * XsArray_atIndex(void *thisPtr, XsSize index)
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
XSTYPES_DLL_API int XsArray_compare(void const *a, void const *b)
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
XSTYPES_DLL_API void XsArray_copyConstruct(void *thisPtr, void const *src)
const float R
struct XsArray XsArray
Definition: xsarray.h:147
bool operator<=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:293
bool operator<(const CPoint< DERIVEDCLASS > &a, const CPoint< DERIVEDCLASS > &b)
Used by STL algorithms.
Definition: CPoint.h:116
XSTYPES_DLL_API void XsArray_assign(void *thisPtr, XsSize count, void const *src)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLsizeiptr size
Definition: glext.h:3779
#define XSARRAY_DECL(T)
Definition: xsarray.h:14
XSTYPES_DLL_API void XsArray_reserve(void *thisPtr, XsSize count)
XSTYPES_DLL_API void XsArray_copy(void *thisPtr, void const *src)
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
TPoint3D operator-(const TPoint3D &p1)
Unary minus operator for 3D points.
XSTYPES_DLL_API void XsArray_swap(void *a, void *b)
GLfloat GLfloat p
Definition: glext.h:5587
XSTYPES_DLL_API void XsArray_append(void *thisPtr, void const *other)
XSTYPES_DLL_API void XsArray_construct(void *thisPtr, XsArrayDescriptor const *const descriptor, XsSize count, void const *src)
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:37
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:59
bool operator>=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:297
void(* itemConstruct)(void *e)
The function to use for constructing a new array item. May be 0 for simple types. ...
Definition: xsarray.h:60
int(* itemCompare)(void const *a, void const *b)
The function to use for comparing two items.
Definition: xsarray.h:64



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019