Home | History | Annotate | Download | only in Eigen2Support
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2011 Benoit Jacob <jacob.benoit.1 (at) 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 EIGEN2_META_H
     11 #define EIGEN2_META_H
     12 
     13 namespace Eigen {
     14 
     15 template<typename T>
     16 struct ei_traits : internal::traits<T>
     17 {};
     18 
     19 struct ei_meta_true {  enum { ret = 1 }; };
     20 struct ei_meta_false { enum { ret = 0 }; };
     21 
     22 template<bool Condition, typename Then, typename Else>
     23 struct ei_meta_if { typedef Then ret; };
     24 
     25 template<typename Then, typename Else>
     26 struct ei_meta_if <false, Then, Else> { typedef Else ret; };
     27 
     28 template<typename T, typename U> struct ei_is_same_type { enum { ret = 0 }; };
     29 template<typename T> struct ei_is_same_type<T,T> { enum { ret = 1 }; };
     30 
     31 template<typename T> struct ei_unref { typedef T type; };
     32 template<typename T> struct ei_unref<T&> { typedef T type; };
     33 
     34 template<typename T> struct ei_unpointer { typedef T type; };
     35 template<typename T> struct ei_unpointer<T*> { typedef T type; };
     36 template<typename T> struct ei_unpointer<T*const> { typedef T type; };
     37 
     38 template<typename T> struct ei_unconst { typedef T type; };
     39 template<typename T> struct ei_unconst<const T> { typedef T type; };
     40 template<typename T> struct ei_unconst<T const &> { typedef T & type; };
     41 template<typename T> struct ei_unconst<T const *> { typedef T * type; };
     42 
     43 template<typename T> struct ei_cleantype { typedef T type; };
     44 template<typename T> struct ei_cleantype<const T>   { typedef typename ei_cleantype<T>::type type; };
     45 template<typename T> struct ei_cleantype<const T&>  { typedef typename ei_cleantype<T>::type type; };
     46 template<typename T> struct ei_cleantype<T&>        { typedef typename ei_cleantype<T>::type type; };
     47 template<typename T> struct ei_cleantype<const T*>  { typedef typename ei_cleantype<T>::type type; };
     48 template<typename T> struct ei_cleantype<T*>        { typedef typename ei_cleantype<T>::type type; };
     49 
     50 /** \internal In short, it computes int(sqrt(\a Y)) with \a Y an integer.
     51   * Usage example: \code ei_meta_sqrt<1023>::ret \endcode
     52   */
     53 template<int Y,
     54          int InfX = 0,
     55          int SupX = ((Y==1) ? 1 : Y/2),
     56          bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
     57                                 // use ?: instead of || just to shut up a stupid gcc 4.3 warning
     58 class ei_meta_sqrt
     59 {
     60     enum {
     61       MidX = (InfX+SupX)/2,
     62       TakeInf = MidX*MidX > Y ? 1 : 0,
     63       NewInf = int(TakeInf) ? InfX : int(MidX),
     64       NewSup = int(TakeInf) ? int(MidX) : SupX
     65     };
     66   public:
     67     enum { ret = ei_meta_sqrt<Y,NewInf,NewSup>::ret };
     68 };
     69 
     70 template<int Y, int InfX, int SupX>
     71 class ei_meta_sqrt<Y, InfX, SupX, true> { public:  enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
     72 
     73 } // end namespace Eigen
     74 
     75 #endif // EIGEN2_META_H
     76