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



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018