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