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
13#include <stdexcept>
15
16namespace mrpt
17{
18namespace 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
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
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 >
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
const_iterator end() const
Definition CArray.h:196
const_iterator begin() const
Definition CArray.h:194
static size_type max_size()
Definition CArray.h:236
const_reference operator[](size_type) const
Definition CArray.h:260
reference at(size_type i)
Definition CArray.h:224
const_reference front() const
Definition CArray.h:264
reverse_iterator rbegin()
Definition CArray.h:214
const_reverse_iterator rbegin() const
Definition CArray.h:215
reference operator[](size_type)
Definition CArray.h:259
static void makes_no_sense()
Definition CArray.h:271
static bool empty()
Definition CArray.h:235
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition CArray.h:201
std::ptrdiff_t difference_type
Definition CArray.h:190
reverse_iterator rend()
Definition CArray.h:218
static size_type size()
Definition CArray.h:234
const_reverse_iterator rend() const
Definition CArray.h:219
const_reference at(size_type i) const
Definition CArray.h:228
std::reverse_iterator< iterator > reverse_iterator
Definition CArray.h:200
const_reference back() const
Definition CArray.h:266
void swap(CArray< T, 0 > &y)
Definition CArray.h:240
const T * data() const
Definition CArray.h:246
A STL container (as wrapper) for arrays of constant size defined at compile time - Users will most li...
Definition CArray.h:55
reference front()
Definition CArray.h:109
const T & const_reference
Definition CArray.h:65
iterator begin()
Definition CArray.h:70
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition CArray.h:78
static size_type max_size()
Definition CArray.h:117
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
void swap(CArray< T, N > &y)
Definition CArray.h:127
static void rangecheck(size_type i)
Definition CArray.h:168
const T * const_iterator
Definition CArray.h:63
const_reference front() const
Definition CArray.h:110
CArray< T, N > & operator=(const CArray< T2, N > &rhs)
Definition CArray.h:139
const_iterator end() const
Definition CArray.h:73
void assign(const size_t n, const T &value)
Definition CArray.h:150
const_reverse_iterator rend() const
Definition CArray.h:96
static size_type size()
Definition CArray.h:115
reference at(size_type i)
Definition CArray.h:105
const_reference at(size_type i) const
Definition CArray.h:106
const_reference operator[](size_type i) const
Definition CArray.h:102
reference back()
Definition CArray.h:111
reverse_iterator rend()
Definition CArray.h:95
iterator end()
Definition CArray.h:72
const_reference back() const
Definition CArray.h:112
void assign(I b, const I &e)
Definition CArray.h:159
const T * data() const
Definition CArray.h:132
reverse_iterator rbegin()
Definition CArray.h:91
static bool empty()
Definition CArray.h:116
std::size_t size_type
Definition CArray.h:66
void assign(const T &value)
Definition CArray.h:145
reference operator[](size_type i)
Definition CArray.h:101
const_iterator begin() const
Definition CArray.h:71
const_reverse_iterator rbegin() const
Definition CArray.h:92
std::ptrdiff_t difference_type
Definition CArray.h:67
std::reverse_iterator< iterator > reverse_iterator
Definition CArray.h:77
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
bool operator<=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:295
bool operator<(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:283
bool operator!=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:287
bool operator>(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:291
bool operator>=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:299
bool operator==(const CArray< T, N > &x, const CArray< T, N > &y)
Definition CArray.h:279
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(), CMatrixFixedNumeric::size(...
Definition CArray.h:305
CMatrixTemplateSize(const size_t *d)
Definition CArray.h:310
bool operator!=(const CMatrixTemplateSize &o) const
Definition CArray.h:313
CMatrixTemplateSize mrpt_autotype
Definition CArray.h:307
CArray< size_t, 2 > Base
Definition CArray.h:306
bool operator==(const CMatrixTemplateSize &o) const
Definition CArray.h:312



Page generated by Doxygen 1.9.8 for MRPT 1.4.0 SVN: at Thu Dec 14 17:13:25 UTC 2023