Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.3.9
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 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex;
26 
33 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE Index;
34 
35 namespace internal {
36 
44 // Only recent versions of ICC complain about using ptrdiff_t to hold pointers,
45 // and older versions do not provide *intptr_t types.
46 #if EIGEN_COMP_ICC>=1600 && __cplusplus >= 201103L
47 typedef std::intptr_t IntPtr;
48 typedef std::uintptr_t UIntPtr;
49 #else
50 typedef std::ptrdiff_t IntPtr;
51 typedef std::size_t UIntPtr;
52 #endif
53 
54 struct true_type { enum { value = 1 }; };
55 struct false_type { enum { value = 0 }; };
56 
57 template<bool Condition, typename Then, typename Else>
58 struct conditional { typedef Then type; };
59 
60 template<typename Then, typename Else>
61 struct conditional <false, Then, Else> { typedef Else type; };
62 
63 template<typename T, typename U> struct is_same { enum { value = 0 }; };
64 template<typename T> struct is_same<T,T> { enum { value = 1 }; };
65 
66 template<typename T> struct remove_reference { typedef T type; };
67 template<typename T> struct remove_reference<T&> { typedef T type; };
68 
69 template<typename T> struct remove_pointer { typedef T type; };
70 template<typename T> struct remove_pointer<T*> { typedef T type; };
71 template<typename T> struct remove_pointer<T*const> { typedef T type; };
72 
73 template <class T> struct remove_const { typedef T type; };
74 template <class T> struct remove_const<const T> { typedef T type; };
75 template <class T> struct remove_const<const T[]> { typedef T type[]; };
76 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };
77 
78 template<typename T> struct remove_all { typedef T type; };
79 template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; };
80 template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; };
81 template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; };
82 template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; };
83 template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; };
84 
85 template<typename T> struct is_arithmetic { enum { value = false }; };
86 template<> struct is_arithmetic<float> { enum { value = true }; };
87 template<> struct is_arithmetic<double> { enum { value = true }; };
88 template<> struct is_arithmetic<long double> { enum { value = true }; };
89 template<> struct is_arithmetic<bool> { enum { value = true }; };
90 template<> struct is_arithmetic<char> { enum { value = true }; };
91 template<> struct is_arithmetic<signed char> { enum { value = true }; };
92 template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
93 template<> struct is_arithmetic<signed short> { enum { value = true }; };
94 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
95 template<> struct is_arithmetic<signed int> { enum { value = true }; };
96 template<> struct is_arithmetic<unsigned int> { enum { value = true }; };
97 template<> struct is_arithmetic<signed long> { enum { value = true }; };
98 template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
99 
100 #if EIGEN_HAS_CXX11
101 using std::is_integral;
102 #else
103 template<typename T> struct is_integral { enum { value = false }; };
104 template<> struct is_integral<bool> { enum { value = true }; };
105 template<> struct is_integral<char> { enum { value = true }; };
106 template<> struct is_integral<signed char> { enum { value = true }; };
107 template<> struct is_integral<unsigned char> { enum { value = true }; };
108 template<> struct is_integral<signed short> { enum { value = true }; };
109 template<> struct is_integral<unsigned short> { enum { value = true }; };
110 template<> struct is_integral<signed int> { enum { value = true }; };
111 template<> struct is_integral<unsigned int> { enum { value = true }; };
112 template<> struct is_integral<signed long> { enum { value = true }; };
113 template<> struct is_integral<unsigned long> { enum { value = true }; };
114 #if EIGEN_COMP_MSVC
115 template<> struct is_integral<signed __int64> { enum { value = true }; };
116 template<> struct is_integral<unsigned __int64>{ enum { value = true }; };
117 #endif
118 #endif
119 
120 #if EIGEN_HAS_CXX11
121 using std::make_unsigned;
122 #else
123 // TODO: Possibly improve this implementation of make_unsigned.
124 // It is currently used only by
125 // template<typename Scalar> struct random_default_impl<Scalar, false, true>.
126 template<typename> struct make_unsigned;
127 template<> struct make_unsigned<char> { typedef unsigned char type; };
128 template<> struct make_unsigned<signed char> { typedef unsigned char type; };
129 template<> struct make_unsigned<unsigned char> { typedef unsigned char type; };
130 template<> struct make_unsigned<signed short> { typedef unsigned short type; };
131 template<> struct make_unsigned<unsigned short> { typedef unsigned short type; };
132 template<> struct make_unsigned<signed int> { typedef unsigned int type; };
133 template<> struct make_unsigned<unsigned int> { typedef unsigned int type; };
134 template<> struct make_unsigned<signed long> { typedef unsigned long type; };
135 template<> struct make_unsigned<unsigned long> { typedef unsigned long type; };
136 #if EIGEN_COMP_MSVC
137 template<> struct make_unsigned<signed __int64> { typedef unsigned __int64 type; };
138 template<> struct make_unsigned<unsigned __int64> { typedef unsigned __int64 type; };
139 #endif
140 #endif
141 
142 template <typename T> struct add_const { typedef const T type; };
143 template <typename T> struct add_const<T&> { typedef T& type; };
144 
145 template <typename T> struct is_const { enum { value = 0 }; };
146 template <typename T> struct is_const<T const> { enum { value = 1 }; };
147 
148 template<typename T> struct add_const_on_value_type { typedef const T type; };
149 template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; };
150 template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; };
151 template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; };
152 template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; };
153 
154 
155 template<typename From, typename To>
156 struct is_convertible_impl
157 {
158 private:
159  struct any_conversion
160  {
161  template <typename T> any_conversion(const volatile T&);
162  template <typename T> any_conversion(T&);
163  };
164  struct yes {int a[1];};
165  struct no {int a[2];};
166 
167  static yes test(const To&, int);
168  static no test(any_conversion, ...);
169 
170 public:
171  static From ms_from;
172 #ifdef __INTEL_COMPILER
173  #pragma warning push
174  #pragma warning ( disable : 2259 )
175 #endif
176  enum { value = sizeof(test(ms_from, 0))==sizeof(yes) };
177 #ifdef __INTEL_COMPILER
178  #pragma warning pop
179 #endif
180 };
181 
182 template<typename From, typename To>
183 struct is_convertible
184 {
185  enum { value = is_convertible_impl<typename remove_all<From>::type,
186  typename remove_all<To >::type>::value };
187 };
188 
192 template<bool Condition, typename T=void> struct enable_if;
193 
194 template<typename T> struct enable_if<true,T>
195 { typedef T type; };
196 
197 #if defined(__CUDA_ARCH__)
198 #if !defined(__FLT_EPSILON__)
199 #define __FLT_EPSILON__ FLT_EPSILON
200 #define __DBL_EPSILON__ DBL_EPSILON
201 #endif
202 
203 namespace device {
204 
205 template<typename T> struct numeric_limits
206 {
207  EIGEN_DEVICE_FUNC
208  static T epsilon() { return 0; }
209  static T (max)() { assert(false && "Highest not supported for this type"); }
210  static T (min)() { assert(false && "Lowest not supported for this type"); }
211  static T infinity() { assert(false && "Infinity not supported for this type"); }
212  static T quiet_NaN() { assert(false && "quiet_NaN not supported for this type"); }
213 };
214 template<> struct numeric_limits<float>
215 {
216  EIGEN_DEVICE_FUNC
217  static float epsilon() { return __FLT_EPSILON__; }
218  EIGEN_DEVICE_FUNC
219  static float (max)() { return CUDART_MAX_NORMAL_F; }
220  EIGEN_DEVICE_FUNC
221  static float (min)() { return FLT_MIN; }
222  EIGEN_DEVICE_FUNC
223  static float infinity() { return CUDART_INF_F; }
224  EIGEN_DEVICE_FUNC
225  static float quiet_NaN() { return CUDART_NAN_F; }
226 };
227 template<> struct numeric_limits<double>
228 {
229  EIGEN_DEVICE_FUNC
230  static double epsilon() { return __DBL_EPSILON__; }
231  EIGEN_DEVICE_FUNC
232  static double (max)() { return DBL_MAX; }
233  EIGEN_DEVICE_FUNC
234  static double (min)() { return DBL_MIN; }
235  EIGEN_DEVICE_FUNC
236  static double infinity() { return CUDART_INF; }
237  EIGEN_DEVICE_FUNC
238  static double quiet_NaN() { return CUDART_NAN; }
239 };
240 template<> struct numeric_limits<int>
241 {
242  EIGEN_DEVICE_FUNC
243  static int epsilon() { return 0; }
244  EIGEN_DEVICE_FUNC
245  static int (max)() { return INT_MAX; }
246  EIGEN_DEVICE_FUNC
247  static int (min)() { return INT_MIN; }
248 };
249 template<> struct numeric_limits<unsigned int>
250 {
251  EIGEN_DEVICE_FUNC
252  static unsigned int epsilon() { return 0; }
253  EIGEN_DEVICE_FUNC
254  static unsigned int (max)() { return UINT_MAX; }
255  EIGEN_DEVICE_FUNC
256  static unsigned int (min)() { return 0; }
257 };
258 template<> struct numeric_limits<long>
259 {
260  EIGEN_DEVICE_FUNC
261  static long epsilon() { return 0; }
262  EIGEN_DEVICE_FUNC
263  static long (max)() { return LONG_MAX; }
264  EIGEN_DEVICE_FUNC
265  static long (min)() { return LONG_MIN; }
266 };
267 template<> struct numeric_limits<unsigned long>
268 {
269  EIGEN_DEVICE_FUNC
270  static unsigned long epsilon() { return 0; }
271  EIGEN_DEVICE_FUNC
272  static unsigned long (max)() { return ULONG_MAX; }
273  EIGEN_DEVICE_FUNC
274  static unsigned long (min)() { return 0; }
275 };
276 template<> struct numeric_limits<long long>
277 {
278  EIGEN_DEVICE_FUNC
279  static long long epsilon() { return 0; }
280  EIGEN_DEVICE_FUNC
281  static long long (max)() { return LLONG_MAX; }
282  EIGEN_DEVICE_FUNC
283  static long long (min)() { return LLONG_MIN; }
284 };
285 template<> struct numeric_limits<unsigned long long>
286 {
287  EIGEN_DEVICE_FUNC
288  static unsigned long long epsilon() { return 0; }
289  EIGEN_DEVICE_FUNC
290  static unsigned long long (max)() { return ULLONG_MAX; }
291  EIGEN_DEVICE_FUNC
292  static unsigned long long (min)() { return 0; }
293 };
294 
295 }
296 
297 #endif
298 
302 class noncopyable
303 {
304  EIGEN_DEVICE_FUNC noncopyable(const noncopyable&);
305  EIGEN_DEVICE_FUNC const noncopyable& operator=(const noncopyable&);
306 protected:
307  EIGEN_DEVICE_FUNC noncopyable() {}
308  EIGEN_DEVICE_FUNC ~noncopyable() {}
309 };
310 
318 #if EIGEN_HAS_STD_RESULT_OF
319 template<typename T> struct result_of {
320  typedef typename std::result_of<T>::type type1;
321  typedef typename remove_all<type1>::type type;
322 };
323 #else
324 template<typename T> struct result_of { };
325 
326 struct has_none {int a[1];};
327 struct has_std_result_type {int a[2];};
328 struct has_tr1_result {int a[3];};
329 
330 template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
331 struct unary_result_of_select {typedef typename internal::remove_all<ArgType>::type type;};
332 
333 template<typename Func, typename ArgType>
334 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};
335 
336 template<typename Func, typename ArgType>
337 struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;};
338 
339 template<typename Func, typename ArgType>
340 struct result_of<Func(ArgType)> {
341  template<typename T>
342  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
343  template<typename T>
344  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
345  static has_none testFunctor(...);
346 
347  // note that the following indirection is needed for gcc-3.3
348  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
349  typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
350 };
351 
352 template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
353 struct binary_result_of_select {typedef typename internal::remove_all<ArgType0>::type type;};
354 
355 template<typename Func, typename ArgType0, typename ArgType1>
356 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
357 {typedef typename Func::result_type type;};
358 
359 template<typename Func, typename ArgType0, typename ArgType1>
360 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
361 {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};
362 
363 template<typename Func, typename ArgType0, typename ArgType1>
364 struct result_of<Func(ArgType0,ArgType1)> {
365  template<typename T>
366  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
367  template<typename T>
368  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
369  static has_none testFunctor(...);
370 
371  // note that the following indirection is needed for gcc-3.3
372  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
373  typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
374 };
375 
376 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2, int SizeOf=sizeof(has_none)>
377 struct ternary_result_of_select {typedef typename internal::remove_all<ArgType0>::type type;};
378 
379 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
380 struct ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, sizeof(has_std_result_type)>
381 {typedef typename Func::result_type type;};
382 
383 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
384 struct ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, sizeof(has_tr1_result)>
385 {typedef typename Func::template result<Func(ArgType0,ArgType1,ArgType2)>::type type;};
386 
387 template<typename Func, typename ArgType0, typename ArgType1, typename ArgType2>
388 struct result_of<Func(ArgType0,ArgType1,ArgType2)> {
389  template<typename T>
390  static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
391  template<typename T>
392  static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1,ArgType2)>::type const * = 0);
393  static has_none testFunctor(...);
394 
395  // note that the following indirection is needed for gcc-3.3
396  enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
397  typedef typename ternary_result_of_select<Func, ArgType0, ArgType1, ArgType2, FunctorType>::type type;
398 };
399 #endif
400 
401 struct meta_yes { char a[1]; };
402 struct meta_no { char a[2]; };
403 
404 // Check whether T::ReturnType does exist
405 template <typename T>
406 struct has_ReturnType
407 {
408  template <typename C> static meta_yes testFunctor(typename C::ReturnType const *);
409  template <typename C> static meta_no testFunctor(...);
410 
411  enum { value = sizeof(testFunctor<T>(0)) == sizeof(meta_yes) };
412 };
413 
414 template<typename T> const T* return_ptr();
415 
416 template <typename T, typename IndexType=Index>
417 struct has_nullary_operator
418 {
419  template <typename C> static meta_yes testFunctor(C const *,typename enable_if<(sizeof(return_ptr<C>()->operator()())>0)>::type * = 0);
420  static meta_no testFunctor(...);
421 
422  enum { value = sizeof(testFunctor(static_cast<T*>(0))) == sizeof(meta_yes) };
423 };
424 
425 template <typename T, typename IndexType=Index>
426 struct has_unary_operator
427 {
428  template <typename C> static meta_yes testFunctor(C const *,typename enable_if<(sizeof(return_ptr<C>()->operator()(IndexType(0)))>0)>::type * = 0);
429  static meta_no testFunctor(...);
430 
431  enum { value = sizeof(testFunctor(static_cast<T*>(0))) == sizeof(meta_yes) };
432 };
433 
434 template <typename T, typename IndexType=Index>
435 struct has_binary_operator
436 {
437  template <typename C> static meta_yes testFunctor(C const *,typename enable_if<(sizeof(return_ptr<C>()->operator()(IndexType(0),IndexType(0)))>0)>::type * = 0);
438  static meta_no testFunctor(...);
439 
440  enum { value = sizeof(testFunctor(static_cast<T*>(0))) == sizeof(meta_yes) };
441 };
442 
446 template<int Y,
447  int InfX = 0,
448  int SupX = ((Y==1) ? 1 : Y/2),
449  bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
450  // use ?: instead of || just to shut up a stupid gcc 4.3 warning
451 class meta_sqrt
452 {
453  enum {
454  MidX = (InfX+SupX)/2,
455  TakeInf = MidX*MidX > Y ? 1 : 0,
456  NewInf = int(TakeInf) ? InfX : int(MidX),
457  NewSup = int(TakeInf) ? int(MidX) : SupX
458  };
459  public:
460  enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
461 };
462 
463 template<int Y, int InfX, int SupX>
464 class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
465 
466 
471 template<int A, int B, int K=1, bool Done = ((A*K)%B)==0>
472 struct meta_least_common_multiple
473 {
474  enum { ret = meta_least_common_multiple<A,B,K+1>::ret };
475 };
476 template<int A, int B, int K>
477 struct meta_least_common_multiple<A,B,K,true>
478 {
479  enum { ret = A*K };
480 };
481 
483 template<typename T, typename U> struct scalar_product_traits
484 {
485  enum { Defined = 0 };
486 };
487 
488 // FIXME quick workaround around current limitation of result_of
489 // template<typename Scalar, typename ArgType0, typename ArgType1>
490 // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
491 // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
492 // };
493 
494 } // end namespace internal
495 
496 namespace numext {
497 
498 #if defined(__CUDA_ARCH__)
499 template<typename T> EIGEN_DEVICE_FUNC void swap(T &a, T &b) { T tmp = b; b = a; a = tmp; }
500 #else
501 template<typename T> EIGEN_STRONG_INLINE void swap(T &a, T &b) { std::swap(a,b); }
502 #endif
503 
504 #if defined(__CUDA_ARCH__)
505 using internal::device::numeric_limits;
506 #else
507 using std::numeric_limits;
508 #endif
509 
510 // Integer division with rounding up.
511 // T is assumed to be an integer type with a>=0, and b>0
512 template<typename T>
513 T div_ceil(const T &a, const T &b)
514 {
515  return (a+b-1) / b;
516 }
517 
518 // The aim of the following functions is to bypass -Wfloat-equal warnings
519 // when we really want a strict equality comparison on floating points.
520 template<typename X, typename Y> EIGEN_STRONG_INLINE
521 bool equal_strict(const X& x,const Y& y) { return x == y; }
522 
523 template<> EIGEN_STRONG_INLINE
524 bool equal_strict(const float& x,const float& y) { return std::equal_to<float>()(x,y); }
525 
526 template<> EIGEN_STRONG_INLINE
527 bool equal_strict(const double& x,const double& y) { return std::equal_to<double>()(x,y); }
528 
529 template<typename X, typename Y> EIGEN_STRONG_INLINE
530 bool not_equal_strict(const X& x,const Y& y) { return x != y; }
531 
532 template<> EIGEN_STRONG_INLINE
533 bool not_equal_strict(const float& x,const float& y) { return std::not_equal_to<float>()(x,y); }
534 
535 template<> EIGEN_STRONG_INLINE
536 bool not_equal_strict(const double& x,const double& y) { return std::not_equal_to<double>()(x,y); }
537 
538 } // end namespace numext
539 
540 } // end namespace Eigen
541 
542 // Define portable (u)int{32,64} types
543 #if EIGEN_HAS_CXX11
544 #include <cstdint>
545 namespace Eigen {
546 namespace numext {
547 typedef std::uint32_t uint32_t;
548 typedef std::int32_t int32_t;
549 typedef std::uint64_t uint64_t;
550 typedef std::int64_t int64_t;
551 }
552 }
553 #else
554 // Without c++11, all compilers able to compile Eigen also
555 // provides the C99 stdint.h header file.
556 #include <stdint.h>
557 namespace Eigen {
558 namespace numext {
559 typedef ::uint32_t uint32_t;
560 typedef ::int32_t int32_t;
561 typedef ::uint64_t uint64_t;
562 typedef ::int64_t int64_t;
563 }
564 }
565 #endif
566 
567 
568 #endif // EIGEN_META_H
Eigen
Namespace containing all symbols from the Eigen library.
Definition: Core:309
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33