TensorMeta.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 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_CXX11_TENSOR_TENSOR_META_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_META_H
12 
13 namespace Eigen {
14 
15 template<bool cond> struct Cond {};
16 
17 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
18 const T1& choose(Cond<true>, const T1& first, const T2&) {
19  return first;
20 }
21 
22 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
23 const T2& choose(Cond<false>, const T1&, const T2& second) {
24  return second;
25 }
26 
27 template <size_t n> struct max_n_1 {
28  static const size_t size = n;
29 };
30 template <> struct max_n_1<0> {
31  static const size_t size = 1;
32 };
33 
34 
35 // Default packet types
36 template <typename Scalar, typename Device>
37 struct PacketType {
38  typedef typename internal::packet_traits<Scalar>::type type;
39  static const int size = internal::unpacket_traits<type>::size;
40 };
41 
42 // For CUDA packet types when using a GpuDevice
43 #if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
44 template <>
45 struct PacketType<float, GpuDevice> {
46  typedef float4 type;
47  static const int size = 4;
48 };
49 template <>
50 struct PacketType<double, GpuDevice> {
51  typedef double2 type;
52  static const int size = 2;
53 };
54 #endif
55 
56 
57 
58 // Tuple mimics std::pair but works on e.g. nvcc.
59 template <typename U, typename V> struct Tuple {
60  public:
61  U first;
62  V second;
63 
64  typedef U first_type;
65  typedef V second_type;
66 
67  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
68  Tuple() : first(), second() {}
69 
70  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
71  Tuple(const U& f, const V& s) : first(f), second(s) {}
72 
73  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
74  Tuple& operator= (const Tuple& rhs) {
75  if (&rhs == this) return *this;
76  first = rhs.first;
77  second = rhs.second;
78  return *this;
79  }
80 
81  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
82  void swap(Tuple& rhs) {
83  using numext::swap;
84  swap(first, rhs.first);
85  swap(second, rhs.second);
86  }
87 };
88 
89 template <typename U, typename V>
90 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
91 bool operator==(const Tuple<U, V>& x, const Tuple<U, V>& y) {
92  return (x.first == y.first && x.second == y.second);
93 }
94 
95 template <typename U, typename V>
96 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
97 bool operator!=(const Tuple<U, V>& x, const Tuple<U, V>& y) {
98  return !(x == y);
99 }
100 
101 
102 
103 #ifdef EIGEN_HAS_SFINAE
104 namespace internal{
105 
106  template<typename IndexType, Index... Is>
107  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
108  array<Index, sizeof...(Is)> customIndices2Array(IndexType& idx, numeric_list<Index, Is...>) {
109  return { idx[Is]... };
110  }
111 
113  template<typename Index, std::size_t NumIndices, typename IndexType>
114  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
115  array<Index, NumIndices> customIndices2Array(IndexType& idx) {
116  return customIndices2Array(idx, typename gen_numeric_list<Index, NumIndices>::type{});
117  }
118 
119 
120  template <typename B, typename D>
121  struct is_base_of
122  {
123 
124  typedef char (&yes)[1];
125  typedef char (&no)[2];
126 
127  template <typename BB, typename DD>
128  struct Host
129  {
130  operator BB*() const;
131  operator DD*();
132  };
133 
134  template<typename T>
135  static yes check(D*, T);
136  static no check(B*, int);
137 
138  static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
139  };
140 
141 }
142 #endif
143 
144 
145 
146 } // namespace Eigen
147 
148 #endif // EIGEN_CXX11_TENSOR_TENSOR_META_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13