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



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