NGSolve  5.3
ngstd/templates.hpp
00001 #ifndef FILE_NGSTD_TEMPLATES
00002 #define FILE_NGSTD_TEMPLATES
00003 
00004 /*********************************************************************/
00005 /* File:   templates.hpp                                             */
00006 /* Author: Joachim Schoeberl                                         */
00007 /* Date:   25. Mar. 2000                                             */
00008 /*********************************************************************/
00009 
00010 namespace ngstd 
00011 {
00012 
00014 template <class T>
00015 INLINE T min2 (T a, T b)
00016 {
00017   return (a < b) ? a : b;
00018 }
00019 
00021 template <class T>
00022 INLINE T max2 (T a, T b)
00023 {
00024   return (a > b) ? a : b;
00025 }
00026 
00028 template <class T>
00029 INLINE T min3 (T a, T b, T c)
00030 {
00031   return (a < b) ? (a < c) ? a : c
00032     : (b < c) ? b : c;
00033 }
00034 
00036 template <class T>
00037 INLINE T max3 (T a, T b, T c)
00038 {
00040   return (a > b) ? ((a > c) ? a : c)
00041     : ((b > c) ? b : c);
00042 }
00043 
00044 
00046 template <class T>
00047 INLINE void Swap (T & a, T & b)
00048 {
00049   T temp = a;
00050   a = b;
00051   b = temp;
00052 }
00053 
00054 
00056 template <class T>
00057 INLINE int sgn (T a)
00058 {
00059   return (a > 0) ? 1 : ( ( a < 0) ? -1 : 0 );
00060 }
00061 
00063 template <class T>
00064 INLINE T sqr (const T a)
00065 {
00066   return a * a; 
00067 }
00068 
00070 template <class T>
00071 INLINE T pow3 (const T a)
00072 {
00073   return a * a * a; 
00074 }
00075 
00076 
00077 
00078 template <class T>
00079 inline string ToString (const T& t)
00080 {
00081   stringstream ss;
00082   ss << t;
00083   return ss.str();
00084 }
00085 
00086 
00087 
00088 template <class T>
00089 void SaveBin (ostream & ost, const T & val)
00090 {
00091   const char * cp = reinterpret_cast<const char*> (&val);
00092   for (unsigned j = 0; j < sizeof(T); j++)
00093     ost.put(cp[j]);
00094 }
00095 
00096 
00097 template <class T>
00098 void LoadBin (istream & ist, T & val)
00099 {
00100   char * cp = reinterpret_cast<char*> (&val);
00101   for (unsigned j = 0; j < sizeof(T); j++)
00102     ist.get(cp[j]);
00103 }
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 template <int NUM>
00112 class Cl_Iterate
00113 {
00114 public:
00115   template <typename FUNC>
00116   static INLINE void Do (FUNC f)
00117   {
00118     Cl_Iterate<NUM-1>::Do(f);
00119     f(NUM);
00120   }
00121 };
00122 
00123 template <>
00124 class Cl_Iterate<0>
00125 {
00126 public:
00127   template <typename FUNC>
00128   static INLINE void Do (FUNC f)  { f(0); }
00129 };
00130 
00131 template <int NUM, typename FUNC>
00132 INLINE void Iterate (FUNC f)
00133 {
00134   Cl_Iterate<NUM-1>::Do(f);
00135 }
00136 
00137 
00138 
00139 
00140 
00141 
00142 }
00143 
00144 #endif