Eigen  3.2.91
Meta.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_META_H
12 #define EIGEN_META_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
25 struct true_type { enum { value = 1 }; };
26 struct false_type { enum { value = 0 }; };
27 
28 template<bool Condition, typename Then, typename Else>
29 struct conditional { typedef Then type; };
30 
31 template<typename Then, typename Else>
32 struct conditional <false, Then, Else> { typedef Else type; };
33 
34 template<typename T, typename U> struct is_same { enum { value = 0 }; };
35 template<typename T> struct is_same<T,T> { enum { value = 1 }; };
36 
37 template<typename T> struct remove_reference { typedef T type; };
38 template<typename T> struct remove_reference<T&> { typedef T type; };
39 
40 template<typename T> struct remove_pointer { typedef T type; };
41 template<typename T> struct remove_pointer<T*> { typedef T type; };
42 template<typename T> struct remove_pointer<T*const> { typedef T type; };
43 
44 template <class T> struct remove_const { typedef T type; };
45 template <class T> struct remove_const<const T> { typedef T type; };
46 template <class T> struct remove_const<const T[]> { typedef T type[]; };
47 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };
48 
49 template<typename T> struct remove_all { typedef T type; };
50 template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; };
51 template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; };
52 template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; };
53 template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; };
54 template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; };
55 
56 template<typename T> struct is_arithmetic { enum { value = false }; };
57 template<> struct is_arithmetic<float> { enum { value = true }; };
58 template<> struct is_arithmetic<double> { enum { value = true }; };
59 template<> struct is_arithmetic<long double> { enum { value = true }; };
60 template<> struct is_arithmetic<bool> { enum { value = true }; };
61 template<> struct is_arithmetic<char> { enum { value = true }; };
62 template<> struct is_arithmetic<signed char> { enum { value = true }; };
63 template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
64 template<> struct is_arithmetic<signed short> { enum { value = true }; };
65 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
66 template<> struct is_arithmetic<signed int> { enum { value = true }; };
67 template<> struct is_arithmetic<unsigned int> { enum { value = true }; };
68 template<> struct is_arithmetic<signed long> { enum { value = true }; };
69 template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
70 
71 template <typename T> struct add_const { typedef const T type; };
72 template <typename T> struct add_const<T&> { typedef T& type; };
73 
74 template <typename T> struct is_const { enum { value = 0 }; };
75 template <typename T> struct is_const<T const> { enum { value = 1 }; };
76 
77 template<typename T> struct add_const_on_value_type { typedef const T type; };
78 template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; };
79 template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; };
80 template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; };
81 template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; };
82 
83 
84 template<typename From, typename To>
85 struct is_convertible_impl
86 {
87 private:
88  struct any_conversion
89  {
90  template <typename T> any_conversion(const volatile T&);
91  template <typename T> any_conversion(T&);
92  };
93  struct yes {int a[1];};
94  struct no {int a[2];};
95 
96  static yes test(const To&, int);
97  static no test(any_conversion, ...);
98 
99 public:
100  static From ms_from;
101  enum { value = sizeof(test(ms_from, 0))==sizeof(yes) };
102 };
103 
104 template<typename From, typename To>
105 struct is_convertible
106 {
107  enum { value = is_convertible_impl<typename remove_all<From>::type,
108  typename remove_all<To >::type>::value };
109 };
110 
114 template<bool Condition, typename T> struct enable_if;
115 
116 template<typename T> struct enable_if<true,T>
117 { typedef T type; };
118 
119 #if defined(__CUDA_ARCH__)
120 #if !defined(__FLT_EPSILON__)
121 #define __FLT_EPSILON__ FLT_EPSILON
122 #define __DBL_EPSILON__ DBL_EPSILON
123 #endif
124 
125 namespace device {
126 
127 template<typename T> struct numeric_limits
128 {
129  EIGEN_DEVICE_FUNC
130  static T epsilon() { return 0; }
131  static T (max)() { assert(false && "Highest not supported for this type"); }
132  static T (min)() { assert(false && "Lowest not supported for this type"); }
133 };
134 template<> struct numeric_limits<float>
135 {
136  EIGEN_DEVICE_FUNC
137  static float epsilon() { return __FLT_EPSILON__; }
138  EIGEN_DEVICE_FUNC
139  static float (max)() { return CUDART_MAX_NORMAL_F; }
140  EIGEN_DEVICE_FUNC
141  static float (min)() { return __FLT_EPSILON__; }
142 };
143 template<> struct numeric_limits<double>
144 {
145  EIGEN_DEVICE_FUNC
146  static double epsilon() { return __DBL_EPSILON__; }
147  EIGEN_DEVICE_FUNC
148  static double (max)() { return CUDART_INF; }
149  EIGEN_DEVICE_FUNC
150  static double (min)() { return __DBL_EPSILON__; }
151 };
152 template<> struct numeric_limits<int>
153 {
154  EIGEN_DEVICE_FUNC
155  static int epsilon() { return 0; }
156  EIGEN_DEVICE_FUNC
157  static int (max)() { return INT_MAX; }
158  EIGEN_DEVICE_FUNC
159  static int (min)() { return INT_MIN; }
160 };
161 template<> struct numeric_limits<long>
162 {
163  EIGEN_DEVICE_FUNC
164  static long epsilon() { return 0; }
165  EIGEN_DEVICE_FUNC
166  static long (max)() { return LONG_MAX; }
167  EIGEN_DEVICE_FUNC
168  static long (min)() { return LONG_MIN; }
169 };
170 template<> struct numeric_limits<long long>
171 {
172  EIGEN_DEVICE_FUNC
173  static long long epsilon() { return 0; }
174  EIGEN_DEVICE_FUNC
175  static long long (max)() { return LLONG_MAX; }
176  EIGEN_DEVICE_FUNC
177  static long long (min)() { return LLONG_MIN; }
178 };
179 
180 }
181 
182 #endif
183 
187 class noncopyable
188 {
189  EIGEN_DEVICE_FUNC noncopyable(const noncopyable&);
190  EIGEN_DEVICE_FUNC const noncopyable& operator=(const noncopyable&);
191 protected:
192  EIGEN_DEVICE_FUNC noncopyable() {}
193  EIGEN_DEVICE_FUNC ~noncopyable() {}
194 };
195 
196 
204 #ifdef EIGEN_HAS_STD_RESULT_OF
205 template<typename T> struct result_of {
206  typedef typename std::result_of<T>::type type1;
207  typedef typename remove_all<type1>::type type;
208 };
209 #else
210 template<typename T> struct result_of { };
211 
212 struct has_none {int a[1];};
213 struct has_std_result_type {int a[2];};
214 struct has_tr1_result {int a[3];};
215 
216 template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
217 struct unary_result_of_select {typedef ArgType type;};
218 
219 template<typename Func, typename ArgType>
220 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};
221 
222 template<typename Func, typename ArgType>
223 struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;};
224 
225 template<typename Func, typename ArgType>
226 struct result_of<Func(ArgType)> {
227  template<typename T>
228  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
229  template<typename T>
230  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
231  static has_none testFunctor(...);
232 
233  // note that the following indirection is needed for gcc-3.3
234  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
235  typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
236 };
237 
238 template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
239 struct binary_result_of_select {typedef ArgType0 type;};
240 
241 template<typename Func, typename ArgType0, typename ArgType1>
242 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
243 {typedef typename Func::result_type type;};
244 
245 template<typename Func, typename ArgType0, typename ArgType1>
246 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
247 {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};
248 
249 template<typename Func, typename ArgType0, typename ArgType1>
250 struct result_of<Func(ArgType0,ArgType1)> {
251  template<typename T>
252  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
253  template<typename T>
254  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
255  static has_none testFunctor(...);
256 
257  // note that the following indirection is needed for gcc-3.3
258  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
259  typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
260 };
261 #endif
262 
266 template<int Y,
267  int InfX = 0,
268  int SupX = ((Y==1) ? 1 : Y/2),
269  bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
270  // use ?: instead of || just to shut up a stupid gcc 4.3 warning
271 class meta_sqrt
272 {
273  enum {
274  MidX = (InfX+SupX)/2,
275  TakeInf = MidX*MidX > Y ? 1 : 0,
276  NewInf = int(TakeInf) ? InfX : int(MidX),
277  NewSup = int(TakeInf) ? int(MidX) : SupX
278  };
279  public:
280  enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
281 };
282 
283 template<int Y, int InfX, int SupX>
284 class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
285 
287 template<typename T, typename U> struct scalar_product_traits
288 {
289  enum { Defined = 0 };
290 };
291 
292 template<typename T> struct scalar_product_traits<T,T>
293 {
294  enum {
295  // Cost = NumTraits<T>::MulCost,
296  Defined = 1
297  };
298  typedef T ReturnType;
299 };
300 
301 template<typename T> struct scalar_product_traits<T,std::complex<T> >
302 {
303  enum {
304  // Cost = 2*NumTraits<T>::MulCost,
305  Defined = 1
306  };
307  typedef std::complex<T> ReturnType;
308 };
309 
310 template<typename T> struct scalar_product_traits<std::complex<T>, T>
311 {
312  enum {
313  // Cost = 2*NumTraits<T>::MulCost,
314  Defined = 1
315  };
316  typedef std::complex<T> ReturnType;
317 };
318 
319 // FIXME quick workaround around current limitation of result_of
320 // template<typename Scalar, typename ArgType0, typename ArgType1>
321 // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
322 // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
323 // };
324 
325 } // end namespace internal
326 
327 namespace numext {
328 
329 #if defined(__CUDA_ARCH__)
330 template<typename T> EIGEN_DEVICE_FUNC void swap(T &a, T &b) { T tmp = b; b = a; a = tmp; }
331 #else
332 template<typename T> EIGEN_STRONG_INLINE void swap(T &a, T &b) { std::swap(a,b); }
333 #endif
334 
335 // Integer division with rounding up.
336 // T is assumed to be an integer type with a>=0, and b>0
337 template<typename T>
338 T div_ceil(const T &a, const T &b)
339 {
340  return (a+b-1) / b;
341 }
342 
343 } // end namespace numext
344 
345 } // end namespace Eigen
346 
347 #endif // EIGEN_META_H
Definition: LDLT.h:16
Definition: StdDeque.h:58
Definition: Eigen_Colamd.h:54