00001
00002
00003 #ifndef DUNE_COMMON_POWER_HH
00004 #define DUNE_COMMON_POWER_HH
00005
00010 namespace Dune {
00011
00017
00018 template <int m, int p>
00019 struct StaticPower
00020 {
00022 enum { power = (m * StaticPower<m,p-1>::power ) };
00023 };
00024
00026 template <int m>
00027 struct StaticPower< m , 0>
00028 {
00030 enum { power = 1 };
00031 };
00032
00033
00034 #ifndef DOXYGEN
00035 template <int p, bool odd = p%2>
00036 struct PowerImp {};
00037 #endif
00038
00045 template <int p>
00046 struct Power
00047 {
00048 template <typename T>
00049 static T eval(const T & a)
00050 {
00051 return PowerImp<p>::eval(a);
00052 }
00053 };
00054
00055 #ifndef DOXYGEN
00056 template <int p>
00057 struct PowerImp<p,false>
00058 {
00059 template <typename T>
00060 static T eval(const T & a)
00061 {
00062 T t = Power<p/2>::eval(a);
00063 return t*t;
00064 }
00065 };
00066
00067 template <int p>
00068 struct PowerImp<p,true>
00069 {
00070 template <typename T>
00071 static T eval(const T & a)
00072 {
00073 return a*Power<p-1>::eval(a);;
00074 }
00075 };
00076
00077 template <>
00078 struct PowerImp<1,true>
00079 {
00080 template <typename T>
00081 static T eval(const T & a)
00082 {
00083 return a;
00084 }
00085 };
00086 #endif
00087
00088 }
00089
00090 #endif