Main Page | Modules | Alphabetical List | Class List | File List | Class Members | File Members

cxxutils.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  Healpix_cxx is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*! \file cxxutils.h
00028  *  Various convenience functions used by the Planck LevelS package.
00029  *
00030  *  Copyright (C) 2002, 2003, 2004 Max-Planck-Society
00031  *  \author Martin Reinecke \author Reinhard Hell
00032  */
00033 
00034 #ifndef PLANCK_CXXUTILS_H
00035 #define PLANCK_CXXUTILS_H
00036 
00037 #include <algorithm>
00038 #include <string>
00039 #include <map>
00040 #include <cmath>
00041 #include "message_error.h"
00042 #include "constants.h"
00043 
00044 /*! \defgroup mathutilsgroup Mathematical helper functions */
00045 /*! \{ */
00046 
00047 //! Returns \e true if | \a a-b | < \a epsilon * | \a b |, else \e false.
00048 inline bool approx (double a, double b, double epsilon=1e-5)
00049   {
00050   using namespace std;
00051   return abs(a-b) < (epsilon*abs(b));
00052   }
00053 
00054 //! Returns \e true if | \a a-b | < \a epsilon, else \e false.
00055 inline bool abs_approx (double a, double b, double epsilon=1e-5)
00056   {
00057   using namespace std;
00058   return abs(a-b) < epsilon;
00059   }
00060 
00061 //! Returns the largest integer which is smaller than (or equal to) \a arg.
00062 inline int intfloor (double arg)
00063   {
00064   return (arg>=0) ? int(arg) : int(arg)-1;
00065   }
00066 
00067 //! Returns the integer which is nearest to \a arg.
00068 inline int planck_nint (double arg)
00069   {
00070   arg += 0.5;
00071   return (arg>=0) ? int(arg) : int(arg)-1;
00072   }
00073 
00074 //! Returns the long integer which is nearest to \a arg.
00075 inline long nlong (double arg)
00076   {
00077   arg += 0.5;
00078   return (arg>=0) ? long(arg) : long(arg)-1;
00079   }
00080 
00081 //! Returns \a v1+v2 if \a v1<0, \a v1-v2 if \a v1>=v2, else \a v1.
00082 /*! \a v1 can be positive or negative; \a v2 must be positive. */
00083 template<typename T> inline T weak_modulo (T v1, T v2)
00084   { return (v1>=0) ? ((v1<v2) ? v1 : (v1-v2)) : (v1+v2); }
00085 
00086 //! Returns the remainder of the division \a v1/v2.
00087 /*! The result is non-negative.
00088     \a v1 can be positive or negative; \a v2 must be positive. */
00089 inline double modulo (double v1, double v2)
00090   {
00091   using namespace std;
00092   return (v1>=0) ? ((v1<v2) ? v1 : fmod(v1,v2)) : (fmod(v1,v2)+v2);
00093   }
00094 
00095 //! Returns the remainder of the division \a v1/v2.
00096 /*! The result is non-negative.
00097     \a v1 can be positive or negative; \a v2 must be positive. */
00098 inline int modulo (int v1, int v2)
00099   { return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2); }
00100 
00101 //! Returns the remainder of the division \a v1/v2.
00102 /*! The result is non-negative.
00103     \a v1 can be positive or negative; \a v2 must be positive. */
00104 inline long modulo (long v1, long v2)
00105   { return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2); }
00106 
00107 
00108 //! Returns -1 if \a signvalue is negative, else +1.
00109 template<typename T> inline T sign (const T& signvalue)
00110   { return (signvalue>=0) ? 1 : -1; }
00111 
00112 //! Returns the integer \a n, which fulfills \a n*n<=arg<(n+1)*(n+1).
00113 inline unsigned int isqrt (unsigned int arg)
00114   {
00115   using namespace std;
00116   return unsigned (sqrt(arg+0.5));
00117   }
00118 
00119 //! Returns \a atan2(y,x) if \a x!=0 or \a y!=0; else returns 0.
00120 inline double safe_atan2 (double y, double x)
00121   {
00122   using namespace std;
00123   return ((x==0.) && (y==0.)) ? 0.0 : atan2(y,x);
00124   }
00125 
00126 //! Returns an index to the left of two interpolation values.
00127 /*! \a begin points to an array containing a sequence of values
00128     sorted in ascending order. The length of the array is \a len.
00129     If \a val is lower than the first element, 0 is returned.
00130     If \a val is higher than the last element, \a len-2
00131     is returned. Else, the index of the largest element smaller
00132     than \a val is returned. */
00133 template<typename T> inline int interpol_left
00134   (const T *begin, int len, const T &val)
00135   {
00136   const T *end = begin+len;
00137   const T *iter = std::lower_bound (begin, end, val);
00138   if (iter==begin) return 0;
00139   if (iter==end) return len-2;
00140   return (iter-begin)-1;
00141   }
00142 
00143 //! Returns an index to the nearest interpolation value.
00144 /*! \a begin points to an array containing a sequence of values
00145     sorted in ascending order. The length of the array is \a len.
00146     If \a val is lower than the first element, 0 is returned.
00147     If \a val is higher than the last element, \a len-1 is returned.
00148     Else, the index of the nearest element within the sequence of
00149     values is returned. */
00150 template<typename T> inline int interpol_nearest
00151   (const T *begin, int len, const T &val)
00152   {
00153   int left = interpol_left(begin, len, val);
00154   T delleft = val-(*(begin+left));
00155   T delright = (*(begin+left+1))-val;
00156   if (delright<0) return left+1;
00157   return (delright<delleft) ? (left+1) : left;
00158   }
00159 
00160 /*! \} */
00161 
00162 /*! \defgroup fileutilsgroup File-handling helper functions */
00163 /*! \{ */
00164 
00165 //! If the file \a filename is present, return \p true, else \p false.
00166 bool file_present (const std::string &filename);
00167 
00168 //! Removes the file \a filename
00169 void remove_file (const std::string &filename);
00170 
00171 /*! \} */
00172 
00173 /*! \defgroup assertgroup Assertions */
00174 /*! \{ */
00175 
00176 //! Throws a Message_error containing \a msg if \a testval is false.
00177 inline void planck_assert (bool testval, const std::string &msg)
00178   {
00179   if (testval) return;
00180   throw Message_error ("Assertion failed: "+msg);
00181   }
00182 //! Throws a Message_error containing \a msg if \a testval is false.
00183 inline void planck_assert (bool testval, const char *msg)
00184   {
00185   if (testval) return;
00186   throw Message_error ("Assertion failed: "+std::string(msg));
00187   }
00188 
00189 //! Checks the presence of the file \a filename.
00190 /*! If the file is not present, a Message_error is thrown. */
00191 void assert_present (const std::string &filename);
00192 
00193 //! Checks the absence of the file \a filename.
00194 /*! If the file is present, a Message_error is thrown. */
00195 void assert_not_present (const std::string &filename);
00196 
00197 /*! \} */
00198 
00199 /*! \defgroup stringutilsgroup String handling helper functions */
00200 /*! \{ */
00201 
00202 //! Returns the string \a orig without leading and trailing whitespace.
00203 std::string trim (const std::string &orig);
00204 
00205 //! Returns a string containing the text representation of \a x.
00206 /*! Care is taken that no information is lost in the conversion. */
00207 template<typename T> std::string dataToString(const T &x);
00208 template<> std::string dataToString (const bool &x);
00209 template<> std::string dataToString (const std::string &x);
00210 template<> std::string dataToString (const float &x);
00211 template<> std::string dataToString (const double &x);
00212 
00213 /*! Returns a string containing the text representation of \a x, padded
00214     with leading zeroes to \a width characters. */
00215 std::string intToString(int x, int width);
00216 
00217 //! Reads a value of a given datatype from a string
00218 template<typename T> void stringToData (const std::string &x, T &value);
00219 template<> void stringToData (const std::string &x, std::string &value);
00220 template<> void stringToData (const std::string &x, bool &value);
00221 
00222 //! Reads a value of a given datatype from a string
00223 template<typename T> inline T stringToData (const std::string &x)
00224   { T result; stringToData(x,result); return result; }
00225 
00226 //! Parses the file \a filename and returns the key/value pairs in \a dict.
00227 void parse_file (const std::string &filename,
00228   std::map<std::string,std::string> &dict);
00229 
00230 /*! \} */
00231 
00232 //! Indicates progress by printing the percentage of \a now/total.
00233 /*! A message is only printed if it has changed since \a now-1/total.
00234     The output is followed by a carriage return, not a newline. */
00235 void announce_progress (int now, int total);
00236 //! Indicates progress by printing the percentage of \a now/total.
00237 /*! A message is only printed if it has changed since \a last/total.
00238     The output is followed by a carriage return, not a newline. */
00239 void announce_progress (double now, double last, double total);
00240 
00241 //! Prints a banner containing \a name. Useful for displaying program names.
00242 void announce (const std::string &name);
00243 
00244 /*! Prints a banner containing \a name and checks if \a argc==argc_expected.
00245     If not, a usage description is given and the program is terminated. */
00246 void module_startup (const std::string &name, int argc, const char **argv,
00247   int argc_expected, const std::string &argv_expected);
00248 
00249 //! Returns an appropriate FITS repetition count for a map with \a npix pixels.
00250 inline int healpix_repcount (int npix)
00251   {
00252   if (npix<1024) return 1;
00253   if ((npix%1024)==0) return 1024;
00254   return isqrt (npix/12);
00255   }
00256 #endif

Generated on Fri Jul 8 09:37:14 2005 for LevelS C++ support library