Eigen  3.2.93
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 #if EIGEN_COMP_ICC>=1600 && __cplusplus >= 201103L
20 #include <cstdint>
21 #endif
22 
23 namespace Eigen {
24 
25 namespace internal {
26 
34 // Only recent versions of ICC complain about using ptrdiff_t to hold pointers,
35 // and older versions do not provide *intptr_t types.
36 #if EIGEN_COMP_ICC>=1600 && __cplusplus >= 201103L
37 typedef std::intptr_t IntPtr;
38 typedef std::uintptr_t UIntPtr;
39 #else
40 typedef std::ptrdiff_t IntPtr;
41 typedef std::size_t UIntPtr;
42 #endif
43 
44 struct true_type { enum { value = 1 }; };
45 struct false_type { enum { value = 0 }; };
46 
47 template<bool Condition, typename Then, typename Else>
48 struct conditional { typedef Then type; };
49 
50 template<typename Then, typename Else>
51 struct conditional <false, Then, Else> { typedef Else type; };
52 
53 template<typename T, typename U> struct is_same { enum { value = 0 }; };
54 template<typename T> struct is_same<T,T> { enum { value = 1 }; };
55 
56 template<typename T> struct remove_reference { typedef T type; };
57 template<typename T> struct remove_reference<T&> { typedef T type; };
58 
59 template<typename T> struct remove_pointer { typedef T type; };
60 template<typename T> struct remove_pointer<T*> { typedef T type; };
61 template<typename T> struct remove_pointer<T*const> { typedef T type; };
62 
63 template <class T> struct remove_const { typedef T type; };
64 template <class T> struct remove_const<const T> { typedef T type; };
65 template <class T> struct remove_const<const T[]> { typedef T type[]; };
66 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };
67 
68 template<typename T> struct remove_all { typedef T type; };
69 template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; };
70 template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; };
71 template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; };
72 template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; };
73 template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; };
74 
75 template<typename T> struct is_arithmetic { enum { value = false }; };
76 template<> struct is_arithmetic<float> { enum { value = true }; };
77 template<> struct is_arithmetic<double> { enum { value = true }; };
78 template<> struct is_arithmetic<long double> { enum { value = true }; };
79 template<> struct is_arithmetic<bool> { enum { value = true }; };
80 template<> struct is_arithmetic<char> { enum { value = true }; };
81 template<> struct is_arithmetic<signed char> { enum { value = true }; };
82 template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
83 template<> struct is_arithmetic<signed short> { enum { value = true }; };
84 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
85 template<> struct is_arithmetic<signed int> { enum { value = true }; };
86 template<> struct is_arithmetic<unsigned int> { enum { value = true }; };
87 template<> struct is_arithmetic<signed long> { enum { value = true }; };
88 template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
89 
90 template<typename T> struct is_integral { enum { value = false }; };
91 template<> struct is_integral<bool> { enum { value = true }; };
92 template<> struct is_integral<char> { enum { value = true }; };
93 template<> struct is_integral<signed char> { enum { value = true }; };
94 template<> struct is_integral<unsigned char> { enum { value = true }; };
95 template<> struct is_integral<signed short> { enum { value = true }; };
96 template<> struct is_integral<unsigned short> { enum { value = true }; };
97 template<> struct is_integral<signed int> { enum { value = true }; };
98 template<> struct is_integral<unsigned int> { enum { value = true }; };
99 template<> struct is_integral<signed long> { enum { value = true }; };
100 template<> struct is_integral<unsigned long> { enum { value = true }; };
101 
102 template <typename T> struct add_const { typedef const T type; };
103 template <typename T> struct add_const<T&> { typedef T& type; };
104 
105 template <typename T> struct is_const { enum { value = 0 }; };
106 template <typename T> struct is_const<T const> { enum { value = 1 }; };
107 
108 template<typename T> struct add_const_on_value_type { typedef const T type; };
109 template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; };
110 template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; };
111 template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; };
112 template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; };
113 
114 
115 template<typename From, typename To>
116 struct is_convertible_impl
117 {
118 private:
119  struct any_conversion
120  {
121  template <typename T> any_conversion(const volatile T&);
122  template <typename T> any_conversion(T&);
123  };
124  struct yes {int a[1];};
125  struct no {int a[2];};
126 
127  static yes test(const To&, int);
128  static no test(any_conversion, ...);
129 
130 public:
131  static From ms_from;
132 #ifdef __INTEL_COMPILER
133  #pragma warning push
134  #pragma warning ( disable : 2259 )
135 #endif
136  enum { value = sizeof(test(ms_from, 0))==sizeof(yes) };
137 #ifdef __INTEL_COMPILER
138  #pragma warning pop
139 #endif
140 };
141 
142 template<typename From, typename To>
143 struct is_convertible
144 {
145  enum { value = is_convertible_impl<typename remove_all<From>::type,
146  typename remove_all<To >::type>::value };
147 };
148 
152 template<bool Condition, typename T=void> struct enable_if;
153 
154 template<typename T> struct enable_if<true,T>
155 { typedef T type; };
156 
157 #if defined(__CUDA_ARCH__)
158 #if !defined(__FLT_EPSILON__)
159 #define __FLT_EPSILON__ FLT_EPSILON
160 #define __DBL_EPSILON__ DBL_EPSILON
161 #endif
162 
163 namespace device {
164 
165 template<typename T> struct numeric_limits
166 {
167  EIGEN_DEVICE_FUNC
168  static T epsilon() { return 0; }
169  static T (max)() { assert(false && "Highest not supported for this type"); }
170  static T (min)() { assert(false && "Lowest not supported for this type"); }
171  static T infinity() { assert(false && "Infinity not supported for this type"); }
172  static T quiet_NaN() { assert(false && "quiet_NaN not supported for this type"); }
173 };
174 template<> struct numeric_limits<float>
175 {
176  EIGEN_DEVICE_FUNC
177  static float epsilon() { return __FLT_EPSILON__; }
178  EIGEN_DEVICE_FUNC
179  static float (max)() { return CUDART_MAX_NORMAL_F; }
180  EIGEN_DEVICE_FUNC
181  static float (min)() { return FLT_MIN; }
182  EIGEN_DEVICE_FUNC
183  static float infinity() { return CUDART_INF_F; }
184  EIGEN_DEVICE_FUNC
185  static float quiet_NaN() { return CUDART_NAN_F; }
186 };
187 template<> struct numeric_limits<double>
188 {
189  EIGEN_DEVICE_FUNC
190  static double epsilon() { return __DBL_EPSILON__; }
191  EIGEN_DEVICE_FUNC
192  static double (max)() { return DBL_MAX; }
193  EIGEN_DEVICE_FUNC
194  static double (min)() { return DBL_MIN; }
195  EIGEN_DEVICE_FUNC
196  static double infinity() { return CUDART_INF; }
197  EIGEN_DEVICE_FUNC
198  static double quiet_NaN() { return CUDART_NAN; }
199 };
200 template<> struct numeric_limits<int>
201 {
202  EIGEN_DEVICE_FUNC
203  static int epsilon() { return 0; }
204  EIGEN_DEVICE_FUNC
205  static int (max)() { return INT_MAX; }
206  EIGEN_DEVICE_FUNC
207  static int (min)() { return INT_MIN; }
208 };
209 template<> struct numeric_limits<unsigned int>
210 {
211  EIGEN_DEVICE_FUNC
212  static unsigned int epsilon() { return 0; }
213  EIGEN_DEVICE_FUNC
214  static unsigned int (max)() { return UINT_MAX; }
215  EIGEN_DEVICE_FUNC
216  static unsigned int (min)() { return 0; }
217 };
218 template<> struct numeric_limits<long>
219 {
220  EIGEN_DEVICE_FUNC
221  static long epsilon() { return 0; }
222  EIGEN_DEVICE_FUNC
223  static long (max)() { return LONG_MAX; }
224  EIGEN_DEVICE_FUNC
225  static long (min)() { return LONG_MIN; }
226 };
227 template<> struct numeric_limits<unsigned long>
228 {
229  EIGEN_DEVICE_FUNC
230  static unsigned long epsilon() { return 0; }
231  EIGEN_DEVICE_FUNC
232  static unsigned long (max)() { return ULONG_MAX; }
233  EIGEN_DEVICE_FUNC
234  static unsigned long (min)() { return 0; }
235 };
236 template<> struct numeric_limits<long long>
237 {
238  EIGEN_DEVICE_FUNC
239  static long long epsilon() { return 0; }
240  EIGEN_DEVICE_FUNC
241  static long long (max)() { return LLONG_MAX; }
242  EIGEN_DEVICE_FUNC
243  static long long (min)() { return LLONG_MIN; }
244 };
245 template<> struct numeric_limits<unsigned long long>
246 {
247  EIGEN_DEVICE_FUNC
248  static unsigned long long epsilon() { return 0; }
249  EIGEN_DEVICE_FUNC
250  static unsigned long long (max)() { return ULLONG_MAX; }
251  EIGEN_DEVICE_FUNC
252  static unsigned long long (min)() { return 0; }
253 };
254 
255 }
256 
257 #endif
258 
262 class noncopyable
263 {
264  EIGEN_DEVICE_FUNC noncopyable(const noncopyable&);
265  EIGEN_DEVICE_FUNC const noncopyable& operator=(const noncopyable&);
266 protected:
267  EIGEN_DEVICE_FUNC noncopyable() {}
268  EIGEN_DEVICE_FUNC ~noncopyable() {}
269 };
270 
278 #if EIGEN_HAS_STD_RESULT_OF
279 template<typename T> struct result_of {
280  typedef typename std::result_of<T>::type type1;
281  typedef typename remove_all<type1>::type type;
282 };
283 #else
284 template<typename T> struct result_of { };
285 
286 struct has_none {int a[1];};
287 struct has_std_result_type {int a[2];};
288 struct has_tr1_result {int a[3];};
289 
290 template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
291 struct unary_result_of_select {typedef typename internal::remove_all<ArgType>::type type;};
292 
293 template<typename Func, typename ArgType>
294 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};
295 
296 template<typename Func, typename ArgType>
297 struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;};
298 
299 template<typename Func, typename ArgType>
300 struct result_of<Func(ArgType)> {
301  template<typename T>
302  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
303  template<typename T>
304  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
305  static has_none testFunctor(...);
306 
307  // note that the following indirection is needed for gcc-3.3
308  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
309  typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
310 };
311 
312 template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
313 struct binary_result_of_select {typedef typename internal::remove_all<ArgType0>::type type;};
314 
315 template<typename Func, typename ArgType0, typename ArgType1>
316 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
317 {typedef typename Func::result_type type;};
318 
319 template<typename Func, typename ArgType0, typename ArgType1>
320 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
321 {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};
322 
323 template<typename Func, typename ArgType0, typename ArgType1>
324 struct result_of<Func(ArgType0,ArgType1)> {
325  template<typename T>
326  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
327  template<typename T>
328  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
329  static has_none testFunctor(...);
330 
331  // note that the following indirection is needed for gcc-3.3
332  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
333  typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
334 };
335 
336 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2, int SizeOf=sizeof(has_none)>
337 struct ternary_result_of_select {typedef typename internal::remove_all<ArgType0>::type type;};
338 
339 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
340 struct ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, sizeof(has_std_result_type)>
341 {typedef typename Func::result_type type;};
342 
343 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
344 struct ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, sizeof(has_tr1_result)>
345 {typedef typename Func::template result<Func(ArgType0,ArgType1,ArgType2)>::type type;};
346 
347 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
348 struct result_of<Func(ArgType0,ArgType1,ArgType2)> {
349  template<typename T>
350  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
351  template<typename T>
352  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1,ArgType2)>::type const * = 0);
353  static has_none testFunctor(...);
354 
355  // note that the following indirection is needed for gcc-3.3
356  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
357  typedef typename ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, FunctorType>::type type;
358 };
359 #endif
360 
361 // Check whether T::ReturnType does exist
362 template <typename T>
363 struct has_ReturnType
364 {
365  typedef char yes[1];
366  typedef char no[2];
367 
368  template <typename C> static yes& testFunctor(C const *, typename C::ReturnType const * = 0);
369  static no& testFunctor(...);
370 
371  static const bool value = sizeof(testFunctor(static_cast<T*>(0))) == sizeof(yes);
372 };
373 
377 template<int Y,
378  int InfX = 0,
379  int SupX = ((Y==1) ? 1 : Y/2),
380  bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
381  // use ?: instead of || just to shut up a stupid gcc 4.3 warning
382 class meta_sqrt
383 {
384  enum {
385  MidX = (InfX+SupX)/2,
386  TakeInf = MidX*MidX > Y ? 1 : 0,
387  NewInf = int(TakeInf) ? InfX : int(MidX),
388  NewSup = int(TakeInf) ? int(MidX) : SupX
389  };
390  public:
391  enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
392 };
393 
394 template<int Y, int InfX, int SupX>
395 class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
396 
397 
402 template<int A, int B, int K=1, bool Done = ((A*K)%B)==0>
403 struct meta_least_common_multiple
404 {
405  enum { ret = meta_least_common_multiple<A,B,K+1>::ret };
406 };
407 template<int A, int B, int K>
408 struct meta_least_common_multiple<A,B,K,true>
409 {
410  enum { ret = A*K };
411 };
412 
414 template<typename T, typename U> struct scalar_product_traits
415 {
416  enum { Defined = 0 };
417 };
418 
419 // FIXME quick workaround around current limitation of result_of
420 // template<typename Scalar, typename ArgType0, typename ArgType1>
421 // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
422 // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
423 // };
424 
425 } // end namespace internal
426 
427 namespace numext {
428 
429 #if defined(__CUDA_ARCH__)
430 template<typename T> EIGEN_DEVICE_FUNC void swap(T &a, T &b) { T tmp = b; b = a; a = tmp; }
431 #else
432 template<typename T> EIGEN_STRONG_INLINE void swap(T &a, T &b) { std::swap(a,b); }
433 #endif
434 
435 #if defined(__CUDA_ARCH__)
436 using internal::device::numeric_limits;
437 #else
438 using std::numeric_limits;
439 #endif
440 
441 // Integer division with rounding up.
442 // T is assumed to be an integer type with a>=0, and b>0
443 template<typename T>
444 T div_ceil(const T &a, const T &b)
445 {
446  return (a+b-1) / b;
447 }
448 
449 } // end namespace numext
450 
451 } // end namespace Eigen
452 
453 #endif // EIGEN_META_H
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Definition: Eigen_Colamd.h:50