dune-common  2.2.0
misc.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set ts=8 sw=2 et sts=2:
00003 #ifndef MISC_HH
00004 #define MISC_HH
00005 
00010 #include <algorithm>
00011 #include <cstddef>
00012 #include <cstring>
00013 #include <cstdio>
00014 #include <iostream>
00015 #include <iomanip>
00016 #include <iterator>
00017 #include <sstream>
00018 #include <complex>
00019 
00020 #include <dune/common/deprecated.hh>
00021 #include "exceptions.hh"
00022 #include <dune/common/typetraits.hh>
00023 
00024 namespace Dune {
00025 
00026 
00032 
00033 // conjugate complex does nothing for non-complex types
00034 template<class K>
00035 inline K conjugateComplex (const K& x)
00036 {
00037     return x;
00038 }
00039 
00040 #ifndef DOXYGEN
00041 // specialization for complex
00042 template<class K>
00043 inline std::complex<K> conjugateComplex (const std::complex<K>& c)
00044 {
00045     return std::complex<K>(c.real(),-c.imag());
00046 }
00047 #endif
00048 
00050 template <class T>
00051 int sign(const T& val) 
00052 {
00053   return (val < 0 ? -1 : 1);
00054 }
00055 
00062 template<class T>
00063 T SQR (T t)
00064 {
00065   return t*t;
00066 }
00067 
00069 template <int m, int p> 
00070 struct Power_m_p
00071 {
00072   // power stores m^p
00073   enum { power = (m * Power_m_p<m,p-1>::power ) };
00074 };
00075 
00077 template <int m> 
00078 struct Power_m_p< m , 0>
00079 {
00080   // m^0 = 1
00081   enum { power = 1 };
00082 };
00083 
00085 template <int m> 
00086 struct Factorial
00087 {
00089   enum { factorial = m * Factorial<m-1>::factorial };
00090 };
00091 
00093 template <> 
00094 struct Factorial<0>
00095 {
00096   // 0! = 1
00097   enum { factorial = 1 };
00098 };
00099 
00100 //********************************************************************
00101 //
00102 // generate filenames with timestep number in it 
00103 //
00104 //********************************************************************
00105 
00107 inline std::string genFilename(const std::string& path, 
00108                                const std::string& fn, 
00109                                int ntime, 
00110                                int precision = 6)
00111 {
00112   std::ostringstream name;
00113 
00114   if(path.size() > 0)
00115   {
00116     name << path; 
00117     name << "/"; 
00118   }
00119   name << fn << std::setw(precision) << std::setfill('0') << ntime;
00120  
00121   // Return the string corresponding to the stringstream
00122   return name.str();
00123 }
00124     
00125 
00126 //********************************************************************
00127 //
00128 //  check whether a given container has a prefix/suffix
00129 //
00130 //********************************************************************
00131 
00133 
00136 template<typename C>
00137 bool hasPrefix(const C& c, const char* prefix) {
00138   std::size_t len = std::strlen(prefix);
00139   return c.size() >= len &&
00140     std::equal(prefix, prefix+len, c.begin());
00141 }
00142 
00144 
00152 template<typename C>
00153 bool hasSuffix(const C& c, const char* suffix) {
00154   std::size_t len = std::strlen(suffix);
00155   if(c.size() < len) return false;
00156   typename C::const_iterator it = c.begin();
00157   std::advance(it, c.size() - len);
00158   return std::equal(suffix, suffix+len, it);
00159 }
00162 }
00163 
00164 
00165 #endif