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 
00062 template <int N>
00063 struct DUNE_DEPRECATED Int2Type :
00064     public integral_constant<int,N>
00065 {
00071   static const int DUNE_DEPRECATED value = N;
00072 
00078   DUNE_DEPRECATED
00079   Int2Type() { }
00080 };
00081 
00082 // need definition here in case sombody passes "Int2Type<N>::value" as a
00083 // function argument or similar (otherwise the linker will barf)
00084 template <int N> const int Int2Type<N>::value;
00085 
00091 
00092 // conjugate complex does nothing for non-complex types
00093 template<class K>
00094 inline K conjugateComplex (const K& x)
00095 {
00096     return x;
00097 }
00098 
00099 #ifndef DOXYGEN
00100 // specialization for complex
00101 template<class K>
00102 inline std::complex<K> conjugateComplex (const std::complex<K>& c)
00103 {
00104     return std::complex<K>(c.real(),-c.imag());
00105 }
00106 #endif
00107 
00109 template <class T>
00110 int sign(const T& val) 
00111 {
00112   return (val < 0 ? -1 : 1);
00113 }
00114 
00121 template<class T>
00122 T SQR (T t)
00123 {
00124   return t*t;
00125 }
00126 
00128 template <int m, int p> 
00129 struct Power_m_p
00130 {
00131   // power stores m^p
00132   enum { power = (m * Power_m_p<m,p-1>::power ) };
00133 };
00134 
00136 template <int m> 
00137 struct Power_m_p< m , 0>
00138 {
00139   // m^0 = 1
00140   enum { power = 1 };
00141 };
00142 
00144 template <int m> 
00145 struct Factorial
00146 {
00148   enum { factorial = m * Factorial<m-1>::factorial };
00149 };
00150 
00152 template <> 
00153 struct Factorial<0>
00154 {
00155   // 0! = 1
00156   enum { factorial = 1 };
00157 };
00158 
00159 //********************************************************************
00160 //
00161 // generate filenames with timestep number in it 
00162 //
00163 //********************************************************************
00164 
00166 inline std::string genFilename(const std::string& path, 
00167                                const std::string& fn, 
00168                                int ntime, 
00169                                int precision = 6)
00170 {
00171   std::ostringstream name;
00172 
00173   if(path.size() > 0)
00174   {
00175     name << path; 
00176     name << "/"; 
00177   }
00178   name << fn << std::setw(precision) << std::setfill('0') << ntime;
00179  
00180   // Return the string corresponding to the stringstream
00181   return name.str();
00182 }
00183     
00184 
00185 //********************************************************************
00186 //
00187 //  check whether a given container has a prefix/suffix
00188 //
00189 //********************************************************************
00190 
00192 
00195 template<typename C>
00196 bool hasPrefix(const C& c, const char* prefix) {
00197   std::size_t len = std::strlen(prefix);
00198   return c.size() >= len &&
00199     std::equal(prefix, prefix+len, c.begin());
00200 }
00201 
00203 
00211 template<typename C>
00212 bool hasSuffix(const C& c, const char* suffix) {
00213   std::size_t len = std::strlen(suffix);
00214   if(c.size() < len) return false;
00215   typename C::const_iterator it = c.begin();
00216   std::advance(it, c.size() - len);
00217   return std::equal(suffix, suffix+len, it);
00218 }
00221 }
00222 
00223 
00224 #endif

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].