share/Common.cxx

00001 // Copyright (C) 2001-2012 Vivien Mallet
00002 //
00003 // This file is part of the linear-algebra library Seldon,
00004 // http://seldon.sourceforge.net/.
00005 //
00006 // Seldon is free software; you can redistribute it and/or modify it under the
00007 // terms of the GNU Lesser General Public License as published by the Free
00008 // Software Foundation; either version 2.1 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00018 
00019 
00020 #ifndef SELDON_FILE_COMMON_CXX
00021 
00022 #include "Common.hxx"
00023 
00024 template <class T>
00025 void PrintArray(T* v, int lgth)
00026 {
00027   for (int k = 0; k < lgth - 1; k++)
00028     std::cout << v[k] << " | ";
00029   std::cout << v[lgth - 1] << std::endl;
00030 }
00031 
00032 namespace Seldon
00033 {
00034 
00035 
00037   Str::Str()
00038   {
00039   }
00040 
00041 
00043 
00046   Str::Str(const Str& s)
00047   {
00048     output_ << s;
00049   }
00050 
00051 
00053   Str::operator std::string() const
00054   {
00055     return output_.str();
00056   }
00057 
00058 
00060 
00063   template <class T>
00064   Str& Str::operator << (const T& input)
00065   {
00066     output_ << input;
00067     return *this;
00068   }
00069 
00070 
00072 
00076   template <class T>
00077   Str operator + (const Str& s, const T& input)
00078   {
00079     string s_input = s;
00080     Str output;
00081     output << s_input << input;
00082     return output;
00083   }
00084 
00085 
00087   ostream& operator << (ostream& out, Str& in)
00088   {
00089     string output = in;
00090     out << output;
00091     return out;
00092   }
00093 
00094 
00096   ostream& operator << (ostream& out, Str in)
00097   {
00098     string output = in;
00099     out << output;
00100     return out;
00101   }
00102 
00103 
00105 
00109   template<typename T>
00110   std::string to_str(const T& input)
00111   {
00112     std::ostringstream output;
00113     output << input;
00114     return output.str();
00115   }
00116 
00117 
00119 
00123   template <class T>
00124   void to_num(std::string s, T& num)
00125   {
00126     std::istringstream str(s);
00127     str >> num;
00128   }
00129 
00130 
00132 
00136   template <class T>
00137   T to_num(std::string s)
00138   {
00139     T num;
00140     std::istringstream str(s);
00141     str >> num;
00142     return num;
00143   }
00144 
00145 
00147 
00150   template <class T>
00151   inline void SetComplexZero(T& number)
00152   {
00153     number = T(0);
00154   }
00155 
00156 
00158 
00161   template <class T>
00162   inline void SetComplexZero(complex<T>& number)
00163   {
00164     number = complex<T>(T(0), T(0));
00165   }
00166 
00167 
00169 
00172   template <class T>
00173   inline void SetComplexOne(T& number)
00174   {
00175     number = T(1);
00176   }
00177 
00178 
00180 
00183   template <class T>
00184   inline void SetComplexOne(complex<T>& number)
00185   {
00186     number = complex<T>(T(1), T(0));
00187   }
00188 
00189 
00190 #ifdef SELDON_WITH_HDF5
00191 
00192 
00196   template <class T>
00197   hid_t GetH5Type(T& input)
00198   {
00199     double d;
00200     float f;
00201     int i;
00202     long l;
00203     char c;
00204     unsigned char uc;
00205     long long ll;
00206     unsigned int ui;
00207     unsigned short us;
00208     unsigned long ul;
00209     unsigned long long ull;
00210 
00211     if (typeid(input) == typeid(d))
00212       return H5T_NATIVE_DOUBLE;
00213     if (typeid(input) == typeid(f))
00214       return H5T_NATIVE_FLOAT;
00215     if (typeid(input) == typeid(i))
00216       return H5T_NATIVE_INT;
00217     if (typeid(input) == typeid(l))
00218       return H5T_NATIVE_LONG;
00219     if (typeid(input) == typeid(c))
00220       return H5T_NATIVE_CHAR;
00221     if (typeid(input) == typeid(uc))
00222       return H5T_NATIVE_UCHAR;
00223     if (typeid(input) == typeid(ll))
00224       return H5T_NATIVE_LLONG;
00225     if (typeid(input) == typeid(ui))
00226       return H5T_NATIVE_UINT;
00227     if (typeid(input) == typeid(us))
00228       return H5T_NATIVE_USHORT;
00229     if (typeid(input) == typeid(ul))
00230       return H5T_NATIVE_ULONG;
00231     if (typeid(input) == typeid(ull))
00232       return H5T_NATIVE_ULLONG;
00233     else
00234       throw Error("hid_t GetH5Type(T& input)",
00235                   "Type has no corresponding native HDF5 datatype.");
00236   }
00237 #endif
00238 
00239 
00240 }  // namespace Seldon.
00241 
00242 #define SELDON_FILE_COMMON_CXX
00243 #endif