Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/CXX11/src/util/EmulateArray.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_EMULATE_ARRAY_H
0011 #define EIGEN_EMULATE_ARRAY_H
0012 
0013 
0014 
0015 // The array class is only available starting with cxx11. Emulate our own here
0016 // if needed. Beware, msvc still doesn't advertise itself as a c++11 compiler!
0017 // Moreover, CUDA doesn't support the STL containers, so we use our own instead.
0018 #if (__cplusplus <= 199711L && EIGEN_COMP_MSVC < 1900) || defined(EIGEN_GPUCC) || defined(EIGEN_AVOID_STL_ARRAY)
0019 
0020 namespace Eigen {
0021 template <typename T, size_t n> class array {
0022  public:
0023   EIGEN_DEVICE_FUNC
0024   EIGEN_STRONG_INLINE T& operator[] (size_t index) { eigen_internal_assert(index < size()); return values[index]; }
0025   EIGEN_DEVICE_FUNC
0026   EIGEN_STRONG_INLINE const T& operator[] (size_t index) const { eigen_internal_assert(index < size()); return values[index]; }
0027 
0028   EIGEN_DEVICE_FUNC
0029   EIGEN_STRONG_INLINE T& at(size_t index) { eigen_assert(index < size()); return values[index]; }
0030   EIGEN_DEVICE_FUNC
0031   EIGEN_STRONG_INLINE const T& at(size_t index) const { eigen_assert(index < size()); return values[index]; }
0032 
0033   EIGEN_DEVICE_FUNC
0034   EIGEN_STRONG_INLINE T& front() { return values[0]; }
0035   EIGEN_DEVICE_FUNC
0036   EIGEN_STRONG_INLINE const T& front() const { return values[0]; }
0037 
0038   EIGEN_DEVICE_FUNC
0039   EIGEN_STRONG_INLINE T& back() { return values[n-1]; }
0040   EIGEN_DEVICE_FUNC
0041   EIGEN_STRONG_INLINE const T& back() const { return values[n-1]; }
0042 
0043   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
0044   static std::size_t size() { return n; }
0045 
0046   T values[n];
0047 
0048   EIGEN_DEVICE_FUNC
0049   EIGEN_STRONG_INLINE array() { }
0050   EIGEN_DEVICE_FUNC
0051   EIGEN_STRONG_INLINE array(const T& v) {
0052     EIGEN_STATIC_ASSERT(n==1, YOU_MADE_A_PROGRAMMING_MISTAKE)
0053     values[0] = v;
0054   }
0055   EIGEN_DEVICE_FUNC
0056   EIGEN_STRONG_INLINE array(const T& v1, const T& v2) {
0057     EIGEN_STATIC_ASSERT(n==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
0058     values[0] = v1;
0059     values[1] = v2;
0060   }
0061   EIGEN_DEVICE_FUNC
0062   EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3) {
0063     EIGEN_STATIC_ASSERT(n==3, YOU_MADE_A_PROGRAMMING_MISTAKE)
0064     values[0] = v1;
0065     values[1] = v2;
0066     values[2] = v3;
0067   }
0068   EIGEN_DEVICE_FUNC
0069   EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3,
0070                             const T& v4) {
0071     EIGEN_STATIC_ASSERT(n==4, YOU_MADE_A_PROGRAMMING_MISTAKE)
0072     values[0] = v1;
0073     values[1] = v2;
0074     values[2] = v3;
0075     values[3] = v4;
0076   }
0077   EIGEN_DEVICE_FUNC
0078   EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
0079                             const T& v5) {
0080     EIGEN_STATIC_ASSERT(n==5, YOU_MADE_A_PROGRAMMING_MISTAKE)
0081     values[0] = v1;
0082     values[1] = v2;
0083     values[2] = v3;
0084     values[3] = v4;
0085     values[4] = v5;
0086   }
0087   EIGEN_DEVICE_FUNC
0088   EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
0089                             const T& v5, const T& v6) {
0090     EIGEN_STATIC_ASSERT(n==6, YOU_MADE_A_PROGRAMMING_MISTAKE)
0091     values[0] = v1;
0092     values[1] = v2;
0093     values[2] = v3;
0094     values[3] = v4;
0095     values[4] = v5;
0096     values[5] = v6;
0097   }
0098   EIGEN_DEVICE_FUNC
0099   EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
0100                             const T& v5, const T& v6, const T& v7) {
0101     EIGEN_STATIC_ASSERT(n==7, YOU_MADE_A_PROGRAMMING_MISTAKE)
0102     values[0] = v1;
0103     values[1] = v2;
0104     values[2] = v3;
0105     values[3] = v4;
0106     values[4] = v5;
0107     values[5] = v6;
0108     values[6] = v7;
0109   }
0110   EIGEN_DEVICE_FUNC
0111   EIGEN_STRONG_INLINE array(
0112       const T& v1, const T& v2, const T& v3, const T& v4,
0113       const T& v5, const T& v6, const T& v7, const T& v8) {
0114     EIGEN_STATIC_ASSERT(n==8, YOU_MADE_A_PROGRAMMING_MISTAKE)
0115     values[0] = v1;
0116     values[1] = v2;
0117     values[2] = v3;
0118     values[3] = v4;
0119     values[4] = v5;
0120     values[5] = v6;
0121     values[6] = v7;
0122     values[7] = v8;
0123   }
0124 
0125 #if EIGEN_HAS_VARIADIC_TEMPLATES
0126   EIGEN_DEVICE_FUNC
0127   EIGEN_STRONG_INLINE array(std::initializer_list<T> l) {
0128     eigen_assert(l.size() == n);
0129     internal::smart_copy(l.begin(), l.end(), values);
0130   }
0131 #endif
0132 };
0133 
0134 
0135 // Specialize array for zero size
0136 template <typename T> class array<T, 0> {
0137  public:
0138   EIGEN_DEVICE_FUNC
0139   EIGEN_STRONG_INLINE T& operator[] (size_t) {
0140     eigen_assert(false && "Can't index a zero size array");
0141     return dummy;
0142   }
0143   EIGEN_DEVICE_FUNC
0144   EIGEN_STRONG_INLINE const T& operator[] (size_t) const {
0145     eigen_assert(false && "Can't index a zero size array");
0146     return dummy;
0147   }
0148 
0149   EIGEN_DEVICE_FUNC
0150   EIGEN_STRONG_INLINE T& front() {
0151     eigen_assert(false && "Can't index a zero size array");
0152     return dummy;
0153   }
0154   EIGEN_DEVICE_FUNC
0155   EIGEN_STRONG_INLINE const T& front() const {
0156     eigen_assert(false && "Can't index a zero size array");
0157     return dummy;
0158   }
0159   EIGEN_DEVICE_FUNC
0160   EIGEN_STRONG_INLINE T& back() {
0161     eigen_assert(false && "Can't index a zero size array");
0162     return dummy;
0163   }
0164   EIGEN_DEVICE_FUNC
0165   EIGEN_STRONG_INLINE const T& back() const {
0166     eigen_assert(false && "Can't index a zero size array");
0167     return dummy;
0168   }
0169 
0170   static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::size_t size() { return 0; }
0171 
0172   EIGEN_DEVICE_FUNC
0173   EIGEN_STRONG_INLINE array() : dummy() { }
0174 
0175 #if EIGEN_HAS_VARIADIC_TEMPLATES
0176   EIGEN_DEVICE_FUNC array(std::initializer_list<T> l) : dummy() {
0177     EIGEN_UNUSED_VARIABLE(l);
0178     eigen_assert(l.size() == 0);
0179   }
0180 #endif
0181 
0182  private:
0183   T dummy;
0184 };
0185 
0186 // Comparison operator
0187 // Todo: implement !=, <, <=, >,  and >=
0188 template<class T, std::size_t N>
0189 EIGEN_DEVICE_FUNC bool operator==(const array<T,N>& lhs, const array<T,N>& rhs) {
0190   for (std::size_t i = 0; i < N; ++i) {
0191     if (lhs[i] != rhs[i]) {
0192       return false;
0193     }
0194   }
0195   return true;
0196 }
0197 
0198 
0199 namespace internal {
0200 template<std::size_t I_, class T, std::size_t N>
0201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& array_get(array<T,N>& a) {
0202   return a[I_];
0203 }
0204 template<std::size_t I_, class T, std::size_t N>
0205 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& array_get(const array<T,N>& a) {
0206   return a[I_];
0207 }
0208 
0209 template<class T, std::size_t N> struct array_size<array<T,N> > {
0210   enum { value = N };
0211 };
0212 template<class T, std::size_t N> struct array_size<array<T,N>& > {
0213   enum { value = N };
0214 };
0215 template<class T, std::size_t N> struct array_size<const array<T,N> > {
0216   enum { value = N };
0217 };
0218 template<class T, std::size_t N> struct array_size<const array<T,N>& > {
0219   enum { value = N };
0220 };
0221 
0222 }  // end namespace internal
0223 }  // end namespace Eigen
0224 
0225 #else
0226 
0227 // The compiler supports c++11, and we're not targeting cuda: use std::array as Eigen::array
0228 #include <array>
0229 namespace Eigen {
0230 
0231 template <typename T, std::size_t N> using array = std::array<T, N>;
0232 
0233 namespace internal {
0234 /* std::get is only constexpr in C++14, not yet in C++11
0235  *     - libstdc++ from version 4.7 onwards has it nevertheless,
0236  *                                          so use that
0237  *     - libstdc++ older versions: use _M_instance directly
0238  *     - libc++ all versions so far: use __elems_ directly
0239  *     - all other libs: use std::get to be portable, but
0240  *                       this may not be constexpr
0241  */
0242 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20120322
0243 #define STD_GET_ARR_HACK             a._M_instance[I_]
0244 #elif defined(_LIBCPP_VERSION)
0245 #define STD_GET_ARR_HACK             a.__elems_[I_]
0246 #else
0247 #define STD_GET_ARR_HACK             std::template get<I_, T, N>(a)
0248 #endif
0249 
0250 template<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; }
0251 template<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; }
0252 template<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; }
0253 
0254 #undef STD_GET_ARR_HACK
0255 
0256 }  // end namespace internal
0257 }  // end namespace Eigen
0258 
0259 #endif
0260 
0261 #endif  // EIGEN_EMULATE_ARRAY_H