Eigen  3.2.92
Meta.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 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 #if defined(__CUDA_ARCH__)
15 #include <cfloat>
16 #include <math_constants.h>
17 #endif
18 
19 namespace Eigen {
20 
21 namespace internal {
22 
30 struct true_type { enum { value = 1 }; };
31 struct false_type { enum { value = 0 }; };
32 
33 template<bool Condition, typename Then, typename Else>
34 struct conditional { typedef Then type; };
35 
36 template<typename Then, typename Else>
37 struct conditional <false, Then, Else> { typedef Else type; };
38 
39 template<typename T, typename U> struct is_same { enum { value = 0 }; };
40 template<typename T> struct is_same<T,T> { enum { value = 1 }; };
41 
42 template<typename T> struct remove_reference { typedef T type; };
43 template<typename T> struct remove_reference<T&> { typedef T type; };
44 
45 template<typename T> struct remove_pointer { typedef T type; };
46 template<typename T> struct remove_pointer<T*> { typedef T type; };
47 template<typename T> struct remove_pointer<T*const> { typedef T type; };
48 
49 template <class T> struct remove_const { typedef T type; };
50 template <class T> struct remove_const<const T> { typedef T type; };
51 template <class T> struct remove_const<const T[]> { typedef T type[]; };
52 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };
53 
54 template<typename T> struct remove_all { typedef T type; };
55 template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; };
56 template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; };
57 template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; };
58 template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; };
59 template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; };
60 
61 template<typename T> struct is_arithmetic { enum { value = false }; };
62 template<> struct is_arithmetic<float> { enum { value = true }; };
63 template<> struct is_arithmetic<double> { enum { value = true }; };
64 template<> struct is_arithmetic<long double> { enum { value = true }; };
65 template<> struct is_arithmetic<bool> { enum { value = true }; };
66 template<> struct is_arithmetic<char> { enum { value = true }; };
67 template<> struct is_arithmetic<signed char> { enum { value = true }; };
68 template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
69 template<> struct is_arithmetic<signed short> { enum { value = true }; };
70 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
71 template<> struct is_arithmetic<signed int> { enum { value = true }; };
72 template<> struct is_arithmetic<unsigned int> { enum { value = true }; };
73 template<> struct is_arithmetic<signed long> { enum { value = true }; };
74 template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
75 
76 template<typename T> struct is_integral { enum { value = false }; };
77 template<> struct is_integral<bool> { enum { value = true }; };
78 template<> struct is_integral<char> { enum { value = true }; };
79 template<> struct is_integral<signed char> { enum { value = true }; };
80 template<> struct is_integral<unsigned char> { enum { value = true }; };
81 template<> struct is_integral<signed short> { enum { value = true }; };
82 template<> struct is_integral<unsigned short> { enum { value = true }; };
83 template<> struct is_integral<signed int> { enum { value = true }; };
84 template<> struct is_integral<unsigned int> { enum { value = true }; };
85 template<> struct is_integral<signed long> { enum { value = true }; };
86 template<> struct is_integral<unsigned long> { enum { value = true }; };
87 
88 template <typename T> struct add_const { typedef const T type; };
89 template <typename T> struct add_const<T&> { typedef T& type; };
90 
91 template <typename T> struct is_const { enum { value = 0 }; };
92 template <typename T> struct is_const<T const> { enum { value = 1 }; };
93 
94 template<typename T> struct add_const_on_value_type { typedef const T type; };
95 template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; };
96 template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; };
97 template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; };
98 template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; };
99 
100 
101 template<typename From, typename To>
102 struct is_convertible_impl
103 {
104 private:
105  struct any_conversion
106  {
107  template <typename T> any_conversion(const volatile T&);
108  template <typename T> any_conversion(T&);
109  };
110  struct yes {int a[1];};
111  struct no {int a[2];};
112 
113  static yes test(const To&, int);
114  static no test(any_conversion, ...);
115 
116 public:
117  static From ms_from;
118  enum { value = sizeof(test(ms_from, 0))==sizeof(yes) };
119 };
120 
121 template<typename From, typename To>
122 struct is_convertible
123 {
124  enum { value = is_convertible_impl<typename remove_all<From>::type,
125  typename remove_all<To >::type>::value };
126 };
127 
131 template<bool Condition, typename T> struct enable_if;
132 
133 template<typename T> struct enable_if<true,T>
134 { typedef T type; };
135 
136 #if defined(__CUDA_ARCH__)
137 #if !defined(__FLT_EPSILON__)
138 #define __FLT_EPSILON__ FLT_EPSILON
139 #define __DBL_EPSILON__ DBL_EPSILON
140 #endif
141 
142 namespace device {
143 
144 template<typename T> struct numeric_limits
145 {
146  EIGEN_DEVICE_FUNC
147  static T epsilon() { return 0; }
148  static T (max)() { assert(false && "Highest not supported for this type"); }
149  static T (min)() { assert(false && "Lowest not supported for this type"); }
150 };
151 template<> struct numeric_limits<float>
152 {
153  EIGEN_DEVICE_FUNC
154  static float epsilon() { return __FLT_EPSILON__; }
155  EIGEN_DEVICE_FUNC
156  static float (max)() { return CUDART_MAX_NORMAL_F; }
157  EIGEN_DEVICE_FUNC
158  static float (min)() { return FLT_MIN; }
159 };
160 template<> struct numeric_limits<double>
161 {
162  EIGEN_DEVICE_FUNC
163  static double epsilon() { return __DBL_EPSILON__; }
164  EIGEN_DEVICE_FUNC
165  static double (max)() { return DBL_MAX; }
166  EIGEN_DEVICE_FUNC
167  static double (min)() { return DBL_MIN; }
168 };
169 template<> struct numeric_limits<int>
170 {
171  EIGEN_DEVICE_FUNC
172  static int epsilon() { return 0; }
173  EIGEN_DEVICE_FUNC
174  static int (max)() { return INT_MAX; }
175  EIGEN_DEVICE_FUNC
176  static int (min)() { return INT_MIN; }
177 };
178 template<> struct numeric_limits<unsigned int>
179 {
180  EIGEN_DEVICE_FUNC
181  static unsigned int epsilon() { return 0; }
182  EIGEN_DEVICE_FUNC
183  static unsigned int (max)() { return UINT_MAX; }
184  EIGEN_DEVICE_FUNC
185  static unsigned int (min)() { return 0; }
186 };
187 template<> struct numeric_limits<long>
188 {
189  EIGEN_DEVICE_FUNC
190  static long epsilon() { return 0; }
191  EIGEN_DEVICE_FUNC
192  static long (max)() { return LONG_MAX; }
193  EIGEN_DEVICE_FUNC
194  static long (min)() { return LONG_MIN; }
195 };
196 template<> struct numeric_limits<unsigned long>
197 {
198  EIGEN_DEVICE_FUNC
199  static unsigned long epsilon() { return 0; }
200  EIGEN_DEVICE_FUNC
201  static unsigned long (max)() { return ULONG_MAX; }
202  EIGEN_DEVICE_FUNC
203  static unsigned long (min)() { return 0; }
204 };
205 template<> struct numeric_limits<long long>
206 {
207  EIGEN_DEVICE_FUNC
208  static long long epsilon() { return 0; }
209  EIGEN_DEVICE_FUNC
210  static long long (max)() { return LLONG_MAX; }
211  EIGEN_DEVICE_FUNC
212  static long long (min)() { return LLONG_MIN; }
213 };
214 template<> struct numeric_limits<unsigned long long>
215 {
216  EIGEN_DEVICE_FUNC
217  static unsigned long long epsilon() { return 0; }
218  EIGEN_DEVICE_FUNC
219  static unsigned long long (max)() { return ULLONG_MAX; }
220  EIGEN_DEVICE_FUNC
221  static unsigned long long (min)() { return 0; }
222 };
223 
224 }
225 
226 #endif
227 
231 class noncopyable
232 {
233  EIGEN_DEVICE_FUNC noncopyable(const noncopyable&);
234  EIGEN_DEVICE_FUNC const noncopyable& operator=(const noncopyable&);
235 protected:
236  EIGEN_DEVICE_FUNC noncopyable() {}
237  EIGEN_DEVICE_FUNC ~noncopyable() {}
238 };
239 
247 #ifdef EIGEN_HAS_STD_RESULT_OF
248 template<typename T> struct result_of {
249  typedef typename std::result_of<T>::type type1;
250  typedef typename remove_all<type1>::type type;
251 };
252 #else
253 template<typename T> struct result_of { };
254 
255 struct has_none {int a[1];};
256 struct has_std_result_type {int a[2];};
257 struct has_tr1_result {int a[3];};
258 
259 template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
260 struct unary_result_of_select {typedef ArgType type;};
261 
262 template<typename Func, typename ArgType>
263 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};
264 
265 template<typename Func, typename ArgType>
266 struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;};
267 
268 template<typename Func, typename ArgType>
269 struct result_of<Func(ArgType)> {
270  template<typename T>
271  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
272  template<typename T>
273  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
274  static has_none testFunctor(...);
275 
276  // note that the following indirection is needed for gcc-3.3
277  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
278  typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
279 };
280 
281 template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
282 struct binary_result_of_select {typedef ArgType0 type;};
283 
284 template<typename Func, typename ArgType0, typename ArgType1>
285 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
286 {typedef typename Func::result_type type;};
287 
288 template<typename Func, typename ArgType0, typename ArgType1>
289 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
290 {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};
291 
292 template<typename Func, typename ArgType0, typename ArgType1>
293 struct result_of<Func(ArgType0,ArgType1)> {
294  template<typename T>
295  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
296  template<typename T>
297  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
298  static has_none testFunctor(...);
299 
300  // note that the following indirection is needed for gcc-3.3
301  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
302  typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
303 };
304 #endif
305 
309 template<int Y,
310  int InfX = 0,
311  int SupX = ((Y==1) ? 1 : Y/2),
312  bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
313  // use ?: instead of || just to shut up a stupid gcc 4.3 warning
314 class meta_sqrt
315 {
316  enum {
317  MidX = (InfX+SupX)/2,
318  TakeInf = MidX*MidX > Y ? 1 : 0,
319  NewInf = int(TakeInf) ? InfX : int(MidX),
320  NewSup = int(TakeInf) ? int(MidX) : SupX
321  };
322  public:
323  enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
324 };
325 
326 template<int Y, int InfX, int SupX>
327 class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
328 
330 template<typename T, typename U> struct scalar_product_traits
331 {
332  enum { Defined = 0 };
333 };
334 
335 template<typename T> struct scalar_product_traits<T,T>
336 {
337  enum {
338  // Cost = NumTraits<T>::MulCost,
339  Defined = 1
340  };
341  typedef T ReturnType;
342 };
343 
344 template<typename T> struct scalar_product_traits<T,std::complex<T> >
345 {
346  enum {
347  // Cost = 2*NumTraits<T>::MulCost,
348  Defined = 1
349  };
350  typedef std::complex<T> ReturnType;
351 };
352 
353 template<typename T> struct scalar_product_traits<std::complex<T>, T>
354 {
355  enum {
356  // Cost = 2*NumTraits<T>::MulCost,
357  Defined = 1
358  };
359  typedef std::complex<T> ReturnType;
360 };
361 
362 // FIXME quick workaround around current limitation of result_of
363 // template<typename Scalar, typename ArgType0, typename ArgType1>
364 // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
365 // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
366 // };
367 
368 } // end namespace internal
369 
370 namespace numext {
371 
372 #if defined(__CUDA_ARCH__)
373 template<typename T> EIGEN_DEVICE_FUNC void swap(T &a, T &b) { T tmp = b; b = a; a = tmp; }
374 #else
375 template<typename T> EIGEN_STRONG_INLINE void swap(T &a, T &b) { std::swap(a,b); }
376 #endif
377 
378 // Integer division with rounding up.
379 // T is assumed to be an integer type with a>=0, and b>0
380 template<typename T>
381 T div_ceil(const T &a, const T &b)
382 {
383  return (a+b-1) / b;
384 }
385 
386 } // end namespace numext
387 
388 } // end namespace Eigen
389 
390 #endif // EIGEN_META_H
Definition: LDLT.h:16
Definition: StdDeque.h:58
Definition: Eigen_Colamd.h:54