cxxutils.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 "lsconstants.h"
00043
00044
00045
00046
00047
00048 template<typename F> inline bool approx (F a, F b, F epsilon=1e-5)
00049 {
00050 using namespace std;
00051 return abs(a-b) < (epsilon*abs(b));
00052 }
00053
00054
00055 template<typename F> inline bool abs_approx (F a, F b, F epsilon=1e-5)
00056 {
00057 using namespace std;
00058 return abs(a-b) < epsilon;
00059 }
00060
00061
00062 template<typename I, typename F> inline I ifloor (F arg)
00063 {
00064 return (arg>=0) ? I(arg) : I(arg)-1;
00065 }
00066
00067
00068 template<typename I, typename F> inline I nearest (F arg)
00069 {
00070 arg += 0.5;
00071 return (arg>=0) ? I(arg) : I(arg)-1;
00072 }
00073
00074
00075
00076 template<typename T> inline T weak_modulo (T v1, T v2)
00077 { return (v1>=0) ? ((v1<v2) ? v1 : (v1-v2)) : (v1+v2); }
00078
00079
00080
00081
00082 inline double fmodulo (double v1, double v2)
00083 {
00084 using namespace std;
00085 return (v1>=0) ? ((v1<v2) ? v1 : fmod(v1,v2)) : (fmod(v1,v2)+v2);
00086 }
00087
00088
00089
00090
00091 template<typename I> inline I imodulo (I v1, I v2)
00092 { return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2); }
00093
00094
00095 template<typename T> inline T sign (const T& signvalue)
00096 { return (signvalue>=0) ? 1 : -1; }
00097
00098
00099 template<typename I> inline unsigned int isqrt (I arg)
00100 {
00101 using namespace std;
00102 if (sizeof(I)<=4)
00103 return unsigned (sqrt(arg+0.5));
00104 long double arg2 = arg;
00105 return unsigned (sqrt(arg2+0.5));
00106 }
00107
00108
00109 template<typename I> inline unsigned int ilog2 (I arg)
00110 {
00111 unsigned int res=0;
00112 while (arg > 0x0000FFFF) { res+=16; arg>>=16; }
00113 if (arg > 0x000000FF) { res|=8; arg>>=8; }
00114 if (arg > 0x0000000F) { res|=4; arg>>=4; }
00115 if (arg > 0x00000003) { res|=2; arg>>=2; }
00116 if (arg > 0x00000001) { res|=1; }
00117 return res;
00118 }
00119
00120
00121 inline double safe_atan2 (double y, double x)
00122 {
00123 using namespace std;
00124 return ((x==0.) && (y==0.)) ? 0.0 : atan2(y,x);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 template<typename T> inline int interpol_left
00135 (const T *begin, int len, const T &val)
00136 {
00137 const T *end = begin+len;
00138 const T *iter = std::lower_bound (begin, end, val);
00139 if (iter==begin) return 0;
00140 if (iter==end) return len-2;
00141 return (iter-begin)-1;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 template<typename T> inline int interpol_nearest
00152 (const T *begin, int len, const T &val)
00153 {
00154 int left = interpol_left(begin, len, val);
00155 T delleft = val-(*(begin+left));
00156 T delright = (*(begin+left+1))-val;
00157 if (delright<0) return left+1;
00158 return (delright<delleft) ? (left+1) : left;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 bool file_present (const std::string &filename);
00168
00169
00170 void remove_file (const std::string &filename);
00171
00172
00173
00174
00175
00176
00177
00178 inline void planck_assert (bool testval, const std::string &msg)
00179 {
00180 if (testval) return;
00181 throw Message_error ("Assertion failed: "+msg);
00182 }
00183
00184 inline void planck_assert (bool testval, const char *msg)
00185 {
00186 if (testval) return;
00187 throw Message_error ("Assertion failed: "+std::string(msg));
00188 }
00189
00190
00191
00192 void assert_present (const std::string &filename);
00193
00194
00195
00196 void assert_not_present (const std::string &filename);
00197
00198
00199
00200
00201
00202
00203
00204 std::string trim (const std::string &orig);
00205
00206
00207
00208 template<typename T> std::string dataToString(const T &x);
00209 template<> std::string dataToString (const bool &x);
00210 template<> std::string dataToString (const std::string &x);
00211 template<> std::string dataToString (const float &x);
00212 template<> std::string dataToString (const double &x);
00213
00214
00215
00216 std::string intToString(int x, int width);
00217
00218
00219 template<typename T> void stringToData (const std::string &x, T &value);
00220 template<> void stringToData (const std::string &x, std::string &value);
00221 template<> void stringToData (const std::string &x, bool &value);
00222
00223
00224 template<typename T> inline T stringToData (const std::string &x)
00225 { T result; stringToData(x,result); return result; }
00226
00227
00228 void parse_file (const std::string &filename,
00229 std::map<std::string,std::string> &dict);
00230
00231
00232
00233
00234 bool equal_nocase (const std::string &a, const std::string &b);
00235
00236
00237 std::string tolower(const std::string &input);
00238
00239
00240
00241
00242
00243
00244 void announce_progress (int now, int total);
00245
00246
00247
00248 void announce_progress (double now, double last, double total);
00249
00250
00251 void end_announce_progress ();
00252
00253
00254 void announce (const std::string &name);
00255
00256
00257
00258 void module_startup (const std::string &name, int argc, const char **argv,
00259 int argc_expected, const std::string &argv_expected);
00260
00261
00262 inline unsigned int healpix_repcount (int npix)
00263 {
00264 if (npix<1024) return 1;
00265 if ((npix%1024)==0) return 1024;
00266 return isqrt (npix/12);
00267 }
00268 #endif