TR-mbed 1.0
Loading...
Searching...
No Matches
EmulateArray.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_EMULATE_ARRAY_H
11#define EIGEN_EMULATE_ARRAY_H
12
13
14
15// The array class is only available starting with cxx11. Emulate our own here
16// if needed. Beware, msvc still doesn't advertise itself as a c++11 compiler!
17// Moreover, CUDA doesn't support the STL containers, so we use our own instead.
18#if (__cplusplus <= 199711L && EIGEN_COMP_MSVC < 1900) || defined(EIGEN_GPUCC) || defined(EIGEN_AVOID_STL_ARRAY)
19
20namespace Eigen {
21template <typename T, size_t n> class array {
22 public:
24 EIGEN_STRONG_INLINE T& operator[] (size_t index) { eigen_internal_assert(index < size()); return values[index]; }
26 EIGEN_STRONG_INLINE const T& operator[] (size_t index) const { eigen_internal_assert(index < size()); return values[index]; }
27
29 EIGEN_STRONG_INLINE T& at(size_t index) { eigen_assert(index < size()); return values[index]; }
31 EIGEN_STRONG_INLINE const T& at(size_t index) const { eigen_assert(index < size()); return values[index]; }
32
36 EIGEN_STRONG_INLINE const T& front() const { return values[0]; }
37
39 EIGEN_STRONG_INLINE T& back() { return values[n-1]; }
41 EIGEN_STRONG_INLINE const T& back() const { return values[n-1]; }
42
44 static std::size_t size() { return n; }
45
47
52 EIGEN_STATIC_ASSERT(n==1, YOU_MADE_A_PROGRAMMING_MISTAKE)
53 values[0] = v;
54 }
56 EIGEN_STRONG_INLINE array(const T& v1, const T& v2) {
57 EIGEN_STATIC_ASSERT(n==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
58 values[0] = v1;
59 values[1] = v2;
60 }
62 EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3) {
63 EIGEN_STATIC_ASSERT(n==3, YOU_MADE_A_PROGRAMMING_MISTAKE)
64 values[0] = v1;
65 values[1] = v2;
66 values[2] = v3;
67 }
69 EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3,
70 const T& v4) {
71 EIGEN_STATIC_ASSERT(n==4, YOU_MADE_A_PROGRAMMING_MISTAKE)
72 values[0] = v1;
73 values[1] = v2;
74 values[2] = v3;
75 values[3] = v4;
76 }
78 EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
79 const T& v5) {
80 EIGEN_STATIC_ASSERT(n==5, YOU_MADE_A_PROGRAMMING_MISTAKE)
81 values[0] = v1;
82 values[1] = v2;
83 values[2] = v3;
84 values[3] = v4;
85 values[4] = v5;
86 }
88 EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
89 const T& v5, const T& v6) {
90 EIGEN_STATIC_ASSERT(n==6, YOU_MADE_A_PROGRAMMING_MISTAKE)
91 values[0] = v1;
92 values[1] = v2;
93 values[2] = v3;
94 values[3] = v4;
95 values[4] = v5;
96 values[5] = v6;
97 }
99 EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
100 const T& v5, const T& v6, const T& v7) {
101 EIGEN_STATIC_ASSERT(n==7, YOU_MADE_A_PROGRAMMING_MISTAKE)
102 values[0] = v1;
103 values[1] = v2;
104 values[2] = v3;
105 values[3] = v4;
106 values[4] = v5;
107 values[5] = v6;
108 values[6] = v7;
109 }
112 const T& v1, const T& v2, const T& v3, const T& v4,
113 const T& v5, const T& v6, const T& v7, const T& v8) {
114 EIGEN_STATIC_ASSERT(n==8, YOU_MADE_A_PROGRAMMING_MISTAKE)
115 values[0] = v1;
116 values[1] = v2;
117 values[2] = v3;
118 values[3] = v4;
119 values[4] = v5;
120 values[5] = v6;
121 values[6] = v7;
122 values[7] = v8;
123 }
124
125#if EIGEN_HAS_VARIADIC_TEMPLATES
127 EIGEN_STRONG_INLINE array(std::initializer_list<T> l) {
128 eigen_assert(l.size() == n);
129 internal::smart_copy(l.begin(), l.end(), values);
130 }
131#endif
132};
133
134
135// Specialize array for zero size
136template <typename T> class array<T, 0> {
137 public:
140 eigen_assert(false && "Can't index a zero size array");
141 return dummy;
142 }
144 EIGEN_STRONG_INLINE const T& operator[] (size_t) const {
145 eigen_assert(false && "Can't index a zero size array");
146 return dummy;
147 }
148
151 eigen_assert(false && "Can't index a zero size array");
152 return dummy;
153 }
155 EIGEN_STRONG_INLINE const T& front() const {
156 eigen_assert(false && "Can't index a zero size array");
157 return dummy;
158 }
161 eigen_assert(false && "Can't index a zero size array");
162 return dummy;
163 }
165 EIGEN_STRONG_INLINE const T& back() const {
166 eigen_assert(false && "Can't index a zero size array");
167 return dummy;
168 }
169
170 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::size_t size() { return 0; }
171
174
175#if EIGEN_HAS_VARIADIC_TEMPLATES
176 EIGEN_DEVICE_FUNC array(std::initializer_list<T> l) : dummy() {
178 eigen_assert(l.size() == 0);
179 }
180#endif
181
182 private:
183 T dummy;
184};
185
186// Comparison operator
187// Todo: implement !=, <, <=, >, and >=
188template<class T, std::size_t N>
189EIGEN_DEVICE_FUNC bool operator==(const array<T,N>& lhs, const array<T,N>& rhs) {
190 for (std::size_t i = 0; i < N; ++i) {
191 if (lhs[i] != rhs[i]) {
192 return false;
193 }
194 }
195 return true;
196}
197
198
199namespace internal {
200template<std::size_t I_, class T, std::size_t N>
202 return a[I_];
203}
204template<std::size_t I_, class T, std::size_t N>
205EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& array_get(const array<T,N>& a) {
206 return a[I_];
207}
208
209template<class T, std::size_t N> struct array_size<array<T,N> > {
210 enum { value = N };
211};
212template<class T, std::size_t N> struct array_size<array<T,N>& > {
213 enum { value = N };
214};
215template<class T, std::size_t N> struct array_size<const array<T,N> > {
216 enum { value = N };
217};
218template<class T, std::size_t N> struct array_size<const array<T,N>& > {
219 enum { value = N };
220};
221
222} // end namespace internal
223} // end namespace Eigen
224
225#else
226
227// The compiler supports c++11, and we're not targeting cuda: use std::array as Eigen::array
228#include <array>
229namespace Eigen {
230
231template <typename T, std::size_t N> using array = std::array<T, N>;
232
233namespace internal {
234/* std::get is only constexpr in C++14, not yet in C++11
235 * - libstdc++ from version 4.7 onwards has it nevertheless,
236 * so use that
237 * - libstdc++ older versions: use _M_instance directly
238 * - libc++ all versions so far: use __elems_ directly
239 * - all other libs: use std::get to be portable, but
240 * this may not be constexpr
241 */
242#if defined(__GLIBCXX__) && __GLIBCXX__ < 20120322
243#define STD_GET_ARR_HACK a._M_instance[I_]
244#elif defined(_LIBCPP_VERSION)
245#define STD_GET_ARR_HACK a.__elems_[I_]
246#else
247#define STD_GET_ARR_HACK std::template get<I_, T, N>(a)
248#endif
249
250template<std::size_t I_, class T, std::size_t N> constexpr inline T& array_get(std::array<T,N>& a) { return (T&) STD_GET_ARR_HACK; }
251template<std::size_t I_, class T, std::size_t N> constexpr inline T&& array_get(std::array<T,N>&& a) { return (T&&) STD_GET_ARR_HACK; }
252template<std::size_t I_, class T, std::size_t N> constexpr inline T const& array_get(std::array<T,N> const& a) { return (T const&) STD_GET_ARR_HACK; }
253
254#undef STD_GET_ARR_HACK
255
256} // end namespace internal
257} // end namespace Eigen
258
259#endif
260
261#endif // EIGEN_EMULATE_ARRAY_H
ArrayXXi a
Definition Array_initializer_list_23_cxx11.cpp:1
Array< int, Dynamic, 1 > v
Definition Array_initializer_list_vector_cxx11.cpp:1
int n
Definition BiCGSTAB_simple.cpp:1
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define EIGEN_ALWAYS_INLINE
Definition Macros.h:932
#define eigen_internal_assert(x)
Definition Macros.h:1043
#define EIGEN_UNUSED_VARIABLE(var)
Definition Macros.h:1076
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define eigen_assert(x)
Definition Macros.h:1037
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
int array[24]
Definition Map_general_stride.cpp:1
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition StaticAssert.h:127
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
Map< RowVectorXf > v2(M2.data(), M2.size())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition EmulateArray.h:160
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & front() const
Definition EmulateArray.h:155
static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::size_t size()
Definition EmulateArray.h:170
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & front()
Definition EmulateArray.h:150
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array()
Definition EmulateArray.h:173
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & back() const
Definition EmulateArray.h:165
Definition EmulateArray.h:21
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3)
Definition EmulateArray.h:62
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5, const T &v6, const T &v7)
Definition EmulateArray.h:99
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v)
Definition EmulateArray.h:51
EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE std::size_t size()
Definition EmulateArray.h:44
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5)
Definition EmulateArray.h:78
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2)
Definition EmulateArray.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & operator[](size_t index)
Definition EmulateArray.h:24
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & at(size_t index)
Definition EmulateArray.h:29
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition EmulateArray.h:39
T values[n]
Definition EmulateArray.h:46
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array()
Definition EmulateArray.h:49
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & front() const
Definition EmulateArray.h:36
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & front()
Definition EmulateArray.h:34
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5, const T &v6, const T &v7, const T &v8)
Definition EmulateArray.h:111
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3, const T &v4)
Definition EmulateArray.h:69
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE array(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5, const T &v6)
Definition EmulateArray.h:88
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & back() const
Definition EmulateArray.h:41
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T & at(size_t index) const
Definition EmulateArray.h:31
@ N
Definition constructor.cpp:23
const Index array_get(DimensionList< Index, Rank > &)
Definition TensorDimensionList.h:39
EIGEN_DEVICE_FUNC void smart_copy(const T *start, const T *end, T *target)
Definition Memory.h:515
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator==(const Tuple< U, V > &x, const Tuple< U, V > &y)
Definition TensorMeta.h:235
Definition BandTriangularSolver.h:13
@ value
Definition Meta.h:446