Main MRPT website > C++ reference for MRPT 1.4.0
CArray.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-2016, 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 _MRPT_CArray_H
10 #define _MRPT_CArray_H
11 
12 #include <mrpt/utils/core_defs.h>
13 #include <stdexcept>
14 #include <mrpt/utils/TTypeName.h>
15 
16 namespace mrpt
17 {
18 namespace math
19 {
20  // ---------------- CArray -------------------------
21  /** A STL container (as wrapper) for arrays of constant size defined at compile time - <b>Users will most likely prefer to use CArrayPOD and its derived classes instead</b>.
22  *
23  * This code is an adapted version from Boost, modifed for its integration
24  * within MRPT (JLBC, Dec/2009) (Renamed array -> CArray to avoid possible potential conflicts).
25  *
26  * See
27  * http://www.josuttis.com/cppcode
28  * for details and the latest version.
29  * See
30  * http://www.boost.org/libs/array for Documentation.
31  * for documentation.
32  *
33  * (C) Copyright Nicolai M. Josuttis 2001.
34  * Permission to copy, use, modify, sell and distribute this software
35  * is granted provided this copyright notice appears in all copies.
36  * This software is provided "as is" without express or implied
37  * warranty, and with no claim as to its suitability for any purpose.
38  *
39  * 29 Jan 2004 - minor fixes (Nico Josuttis)
40  * 04 Dec 2003 - update to synch with library TR1 (Alisdair Meredith)
41  * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
42  * 05 Aug 2001 - minor update (Nico Josuttis)
43  * 20 Jan 2001 - STLport fix (Beman Dawes)
44  * 29 Sep 2000 - Initial Revision (Nico Josuttis)
45  *
46  * Jan 30, 2004
47  *
48  * \note This class DOES NOT support mathematical operations on its elements: it's a generic container, it doesn't assume they are numerical.
49  * \note For a summary and classification of all MRPT vector, array and matrix classes see: http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
50  *
51  * \sa CArrayNumeric (for another, non-related base template class that DOES support maths)
52  * \ingroup mrpt_base_grp
53  */
54  template <typename T, std::size_t N>
55  class CArray {
56  public:
57  T elems[N]; // fixed-size array of elements of type T
58 
59  public:
60  // type definitions
61  typedef T value_type;
62  typedef T* iterator;
63  typedef const T* const_iterator;
64  typedef T& reference;
65  typedef const T& const_reference;
66  typedef std::size_t size_type;
67  typedef std::ptrdiff_t difference_type;
68 
69  // iterator support
70  inline iterator begin() { return elems; }
71  inline const_iterator begin() const { return elems; }
72  inline iterator end() { return elems+N; }
73  inline const_iterator end() const { return elems+N; }
74 
75  // reverse iterator support
76 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
77  typedef std::reverse_iterator<iterator> reverse_iterator;
78  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
80  // workaround for broken reverse_iterator in VC7
81  typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
83  typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
85 #else
86  // workaround for broken reverse_iterator implementations
87  typedef std::reverse_iterator<iterator,T> reverse_iterator;
88  typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
89 #endif
90 
93  return const_reverse_iterator(end());
94  }
97  return const_reverse_iterator(begin());
98  }
99 
100  // operator[]
101  inline reference operator[](size_type i) { return elems[i]; }
102  inline const_reference operator[](size_type i) const { return elems[i]; }
103 
104  // at() with range check
105  reference at(size_type i) { rangecheck(i); return elems[i]; }
106  const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
107 
108  // front() and back()
109  reference front() { return elems[0]; }
110  const_reference front() const { return elems[0]; }
111  reference back() { return elems[N-1]; }
112  const_reference back() const { return elems[N-1]; }
113 
114  // size is constant
115  static inline size_type size() { return N; }
116  static bool empty() { return false; }
117  static size_type max_size() { return N; }
118  enum { static_size = N };
119 
120  /** This method has no effects in this class, but raises an exception if the expected size does not match */
121  inline void resize(const size_t nElements) {
122  if (nElements!=N)
123  throw std::logic_error(format("Try to change the size of a %u-CArray to %u.",static_cast<unsigned>(N),static_cast<unsigned>(nElements)));
124  }
125 
126  // swap (note: linear complexity in N, constant for given instantiation)
127  void swap (CArray<T,N>& y) {
128  std::swap_ranges(begin(),end(),y.begin());
129  }
130 
131  // direct access to data (read-only)
132  const T* data() const { return elems; }
133 
134  // use array as C array (direct read/write access to data)
135  T* data() { return elems; }
136 
137  // assignment with type conversion
138  template <typename T2>
140  std::copy(rhs.begin(),rhs.end(), begin());
141  return *this;
142  }
143 
144  // assign one value to all elements
145  inline void assign (const T& value)
146  {
147  for (size_t i=0;i<N;i++) elems[i]=value;
148  }
149  // assign (compatible with std::vector's one) (by JLBC for MRPT)
150  void assign (const size_t n, const T& value)
151  {
152  #ifdef _DEBUG
153  if (N!=n) throw std::out_of_range("CArray<>: assign() of incorrect length");
154  #endif
155  for (size_t i=0;i<N;i++) elems[i]=value;
156  }
157 
158  //assign a range of values corresponding to a pair of iterators (by PMO for MRPT)
159  template<typename I> void assign(I b,const I &e) {
160  #ifdef _DEBUG
161  if (std::distance(b,e)!=N) throw std::out_of_range("CArray<>: assign() of incorrect length");
162  #endif
163  for (iterator i=begin();i<end();++i) *i=*(b++);
164  }
165 
166  private:
167  // check range (may be private because it is static)
168  static void rangecheck (size_type i) {
169  if (i >= size()) {
170  throw std::out_of_range("CArray<>: index out of range");
171  }
172  }
173 
174  };
175 
176 // partial specialization for arrays of size 0
177  template <typename T>
178  class CArray<T,0> {
179  public:
180  char c; // to ensure different array intances return unique values for begin/end
181 
182  public:
183  // type definitions
184  typedef T value_type;
185  typedef T* iterator;
186  typedef const T* const_iterator;
187  typedef T& reference;
188  typedef const T& const_reference;
189  typedef std::size_t size_type;
190  typedef std::ptrdiff_t difference_type;
191 
192  // iterator support
193  iterator begin() { return reinterpret_cast< iterator >( &c ); }
194  const_iterator begin() const { return reinterpret_cast< const_iterator >( &c ); }
195  iterator end() { return reinterpret_cast< iterator >( &c ); } //-V524
196  const_iterator end() const { return reinterpret_cast< const_iterator >( &c ); } //-V524
197 
198  // reverse iterator support
199 #if !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
200  typedef std::reverse_iterator<iterator> reverse_iterator;
201  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
202 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
203  // workaround for broken reverse_iterator in VC7
204  typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
206  typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
208 #else
209  // workaround for broken reverse_iterator implementations
210  typedef std::reverse_iterator<iterator,T> reverse_iterator;
211  typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
212 #endif
213 
216  return const_reverse_iterator(end());
217  }
220  return const_reverse_iterator(begin());
221  }
222 
223  // at() with range check
226  throw std::out_of_range("CArray<0>: index out of range");
227  }
230  throw std::out_of_range("<0>: index out of range");
231  }
232 
233  // size is constant
234  static size_type size() { return 0; }
235  static bool empty() { return true; }
236  static size_type max_size() { return 0; }
237  enum { static_size = 0 };
238 
239  // swap
240  void swap (CArray<T,0>& y) {
242  // could swap value of c, but value is not part of documented array state
243  }
244 
245  // direct access to data
246  const T* data() const { return NULL; }
247  T* data() { return NULL; }
248 
249  // assignment with type conversion
250  template < typename T2 >
252  MRPT_UNUSED_PARAM(rhs);
253  return *this;
254  }
255 
256  // Calling these operations are undefined behaviour for 0-size arrays,
257  // but Library TR1 requires their presence.
258  // operator[]
259  inline reference operator[](size_type ) { makes_no_sense(); static T dumm=0; return dumm; }
260  inline const_reference operator[](size_type ) const { makes_no_sense(); static T dumm=0; return dumm; }
261 
262  // front() and back()
263  reference front() { makes_no_sense(); }
264  const_reference front() const { makes_no_sense(); }
265  reference back() { makes_no_sense(); }
266  const_reference back() const { makes_no_sense(); }
267 
268  private:
269  // helper for operations that have undefined behaviour for 0-size arrays,
270  // assert( false ); added to make lack of support clear
271  static void makes_no_sense () {
272  //assert(true);
273  throw std::out_of_range("CArray<0>: index out of range");
274  }
275  };
276 
277  // comparisons
278  template<class T, std::size_t N>
279  bool operator== (const CArray<T,N>& x, const CArray<T,N>& y) {
280  return std::equal(x.begin(), x.end(), y.begin());
281  }
282  template<class T, std::size_t N>
283  bool operator< (const CArray<T,N>& x, const CArray<T,N>& y) {
284  return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
285  }
286  template<class T, std::size_t N>
287  bool operator!= (const CArray<T,N>& x, const CArray<T,N>& y) {
288  return !(x==y);
289  }
290  template<class T, std::size_t N>
291  bool operator> (const CArray<T,N>& x, const CArray<T,N>& y) {
292  return y<x;
293  }
294  template<class T, std::size_t N>
295  bool operator<= (const CArray<T,N>& x, const CArray<T,N>& y) {
296  return !(y<x);
297  }
298  template<class T, std::size_t N>
299  bool operator>= (const CArray<T,N>& x, const CArray<T,N>& y) {
300  return !(x<y);
301  }
302 
303  /** Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(), CMatrixFixedNumeric::size(), CMatrixFixedNumeric::resize(), to mimic the behavior of STL-containers */
304  struct CMatrixTemplateSize : public CArray<size_t,2>
305  {
308 
309  inline CMatrixTemplateSize() : CArray<size_t,2>() {}
310  inline CMatrixTemplateSize(const size_t *d) { (*this)[0]=d[0]; (*this)[1]=d[1]; }
311 
312  inline bool operator==(const CMatrixTemplateSize&o) const { return Base::operator[](0)==o[0] && Base::operator[](1)==o[1]; }
313  inline bool operator!=(const CMatrixTemplateSize&o) const { return !(*this==o); }
314  /** This operator allows the size(N,M) to be compared with a plain size_t N*M */
315  inline operator size_t(void) const { return 2; }
316  };
317 
318 } // End of namespace
319 
320 } // End of namespace
321 
322 
323 #endif
mrpt::math::CArray< T, 0 >::front
reference front()
Definition: CArray.h:263
mrpt::math::operator==
bool operator==(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:279
mrpt::math::CArray::end
iterator end()
Definition: CArray.h:72
mrpt::math::CArray::size_type
std::size_t size_type
Definition: CArray.h:66
mrpt::math::operator<
bool operator<(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:283
mrpt::math::CArray::assign
void assign(const size_t n, const T &value)
Definition: CArray.h:150
mrpt::math::CArray::reference
T & reference
Definition: CArray.h:64
mrpt::math::CArray::difference_type
std::ptrdiff_t difference_type
Definition: CArray.h:67
mrpt::math::CArray< T, 0 >::begin
const_iterator begin() const
Definition: CArray.h:194
mrpt::math::CArray< T, 0 >::operator[]
const_reference operator[](size_type) const
Definition: CArray.h:260
mrpt::math::CArray::value_type
T value_type
Definition: CArray.h:61
mrpt::math::CArray< T, 0 >::at
reference at(size_type i)
Definition: CArray.h:224
mrpt::math::CArray< T, 0 >::value_type
T value_type
Definition: CArray.h:184
mrpt::math::CArray< T, 0 >::data
const T * data() const
Definition: CArray.h:246
mrpt::math::CArray< T, 0 >::size
static size_type size()
Definition: CArray.h:234
mrpt::math::CArray::elems
T elems[N]
Definition: CArray.h:57
mrpt::math::CArray::assign
void assign(const T &value)
Definition: CArray.h:145
mrpt::math::CArray::at
const_reference at(size_type i) const
Definition: CArray.h:106
mrpt::math::operator<=
bool operator<=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:295
mrpt::math::CArray< T, 0 >::rbegin
reverse_iterator rbegin()
Definition: CArray.h:214
mrpt::math::CArray::rangecheck
static void rangecheck(size_type i)
Definition: CArray.h:168
mrpt::math::CArray::const_iterator
const typedef T * const_iterator
Definition: CArray.h:63
mrpt::math::CArray::at
reference at(size_type i)
Definition: CArray.h:105
mrpt::math::CMatrixTemplateSize
Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(), CMatrixFixedNumeric::size(...
Definition: CArray.h:304
mrpt::math::CArray::back
const_reference back() const
Definition: CArray.h:112
mrpt::math::CArray< T, 0 >::end
iterator end()
Definition: CArray.h:195
mrpt::math::distance
double BASE_IMPEXP distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
mrpt::math::CMatrixTemplateSize::CMatrixTemplateSize
CMatrixTemplateSize()
Definition: CArray.h:309
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Definition: mrpt_macros.h:290
mrpt::math::CArray< T, 0 >::back
const_reference back() const
Definition: CArray.h:266
mrpt::math::CArray::data
const T * data() const
Definition: CArray.h:132
mrpt::math::CArray< T, 0 >::data
T * data()
Definition: CArray.h:247
mrpt::math::CArray< T, 0 >::back
reference back()
Definition: CArray.h:265
mrpt::math::CArray::rbegin
reverse_iterator rbegin()
Definition: CArray.h:91
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:16
mrpt::math::CArray::assign
void assign(I b, const I &e)
Definition: CArray.h:159
mrpt::math::CArray::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: CArray.h:78
mrpt::math::CArray< T, 0 >::begin
iterator begin()
Definition: CArray.h:193
mrpt::math::CMatrixTemplateSize::operator!=
bool operator!=(const CMatrixTemplateSize &o) const
Definition: CArray.h:313
mrpt::math::CArray< T, 0 >::end
const_iterator end() const
Definition: CArray.h:196
mrpt::math::CArray::size
static size_type size()
Definition: CArray.h:115
mrpt::math::CArray::swap
void swap(CArray< T, N > &y)
Definition: CArray.h:127
mrpt::math::CArray
A STL container (as wrapper) for arrays of constant size defined at compile time - Users will most li...
Definition: CArray.h:55
mrpt::math::CArray< T, 0 >::front
const_reference front() const
Definition: CArray.h:264
mrpt::math::CArray::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: CArray.h:77
core_defs.h
mrpt::math::CArray< T, 0 >::operator[]
reference operator[](size_type)
Definition: CArray.h:259
mrpt::math::CArray::const_reference
const typedef T & const_reference
Definition: CArray.h:65
mrpt::math::CArray::max_size
static size_type max_size()
Definition: CArray.h:117
TTypeName.h
mrpt::math::CArray< T, 0 >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: CArray.h:201
mrpt::math::CArray::iterator
T * iterator
Definition: CArray.h:62
mrpt::math::CArray::resize
void resize(const size_t nElements)
This method has no effects in this class, but raises an exception if the expected size does not match...
Definition: CArray.h:121
mrpt::math::CMatrixTemplateSize::Base
CArray< size_t, 2 > Base
Definition: CArray.h:306
mrpt::math::CArray::rend
const_reverse_iterator rend() const
Definition: CArray.h:96
mrpt::math::CArray< T, 0 >::size_type
std::size_t size_type
Definition: CArray.h:189
mrpt::math::operator!=
bool operator!=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:287
mrpt::math::CArray< T, 0 >::at
const_reference at(size_type i) const
Definition: CArray.h:228
mrpt::math::CArray::operator[]
const_reference operator[](size_type i) const
Definition: CArray.h:102
mrpt::math::CArray::operator=
CArray< T, N > & operator=(const CArray< T2, N > &rhs)
Definition: CArray.h:139
mrpt::math::CMatrixTemplateSize::mrpt_autotype
CMatrixTemplateSize mrpt_autotype
Definition: CArray.h:307
mrpt::math::CArray< T, 0 >::c
char c
Definition: CArray.h:180
mrpt::math::CArray::front
const_reference front() const
Definition: CArray.h:110
mrpt::math::CArray< T, 0 >::reference
T & reference
Definition: CArray.h:187
mrpt::math::CArray< T, 0 >::max_size
static size_type max_size()
Definition: CArray.h:236
mrpt::math::CArray::static_size
@ static_size
Definition: CArray.h:118
mrpt::math::CArray< T, 0 >::difference_type
std::ptrdiff_t difference_type
Definition: CArray.h:190
mrpt::math::CArray< T, 0 >::const_reference
const typedef T & const_reference
Definition: CArray.h:188
mrpt::math::CArray< T, 0 >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: CArray.h:200
mrpt::math::CArray< T, 0 >::const_iterator
const typedef T * const_iterator
Definition: CArray.h:186
mrpt::math::CArray< T, 0 >::makes_no_sense
static void makes_no_sense()
Definition: CArray.h:271
mrpt::math::CArray::operator[]
reference operator[](size_type i)
Definition: CArray.h:101
mrpt::math::CArray::begin
iterator begin()
Definition: CArray.h:70
mrpt::math::CMatrixTemplateSize::operator==
bool operator==(const CMatrixTemplateSize &o) const
Definition: CArray.h:312
mrpt::math::CArray< T, 0 >::rend
reverse_iterator rend()
Definition: CArray.h:218
mrpt::math::operator>
bool operator>(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:291
mrpt::math::CArray< T, 0 >::rbegin
const_reverse_iterator rbegin() const
Definition: CArray.h:215
mrpt::math::CMatrixTemplateSize::CMatrixTemplateSize
CMatrixTemplateSize(const size_t *d)
Definition: CArray.h:310
mrpt::math::CArray< T, 0 >
Definition: CArray.h:178
mrpt::math::CArray< T, 0 >::swap
void swap(CArray< T, 0 > &y)
Definition: CArray.h:240
mrpt::math::CArray::rbegin
const_reverse_iterator rbegin() const
Definition: CArray.h:92
mrpt::math::operator>=
bool operator>=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:299
mrpt::math::CArray::back
reference back()
Definition: CArray.h:111
mrpt::math::CArray< T, 0 >::empty
static bool empty()
Definition: CArray.h:235
mrpt::math::CArray::begin
const_iterator begin() const
Definition: CArray.h:71
mrpt::math::CArray::empty
static bool empty()
Definition: CArray.h:116
mrpt::math::CArray::front
reference front()
Definition: CArray.h:109
mrpt::format
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
mrpt::math::CArray< T, 0 >::iterator
T * iterator
Definition: CArray.h:185
mrpt::math::CArray::data
T * data()
Definition: CArray.h:135
mrpt::math::CArray::end
const_iterator end() const
Definition: CArray.h:73
mrpt::math::CArray< T, 0 >::rend
const_reverse_iterator rend() const
Definition: CArray.h:219
mrpt::math::CArray::rend
reverse_iterator rend()
Definition: CArray.h:95



Page generated by Doxygen 1.8.17 for MRPT 1.4.0 SVN: at Tue Mar 3 09:15:16 UTC 2020