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 "constants.h"
00043
00044
00045
00046
00047
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
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
00062 inline int intfloor (double arg)
00063 {
00064 return (arg>=0) ? int(arg) : int(arg)-1;
00065 }
00066
00067
00068 inline int planck_nint (double arg)
00069 {
00070 arg += 0.5;
00071 return (arg>=0) ? int(arg) : int(arg)-1;
00072 }
00073
00074
00075 inline long nlong (double arg)
00076 {
00077 arg += 0.5;
00078 return (arg>=0) ? long(arg) : long(arg)-1;
00079 }
00080
00081
00082
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
00087
00088
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
00096
00097
00098 inline int modulo (int v1, int v2)
00099 { return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2); }
00100
00101
00102
00103
00104 inline long modulo (long v1, long v2)
00105 { return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2); }
00106
00107
00108
00109 template<typename T> inline T sign (const T& signvalue)
00110 { return (signvalue>=0) ? 1 : -1; }
00111
00112
00113 inline unsigned int isqrt (unsigned int arg)
00114 {
00115 using namespace std;
00116 return unsigned (sqrt(arg+0.5));
00117 }
00118
00119
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
00127
00128
00129
00130
00131
00132
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
00144
00145
00146
00147
00148
00149
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
00163
00164
00165
00166 bool file_present (const std::string &filename);
00167
00168
00169 void remove_file (const std::string &filename);
00170
00171
00172
00173
00174
00175
00176
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
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
00190
00191 void assert_present (const std::string &filename);
00192
00193
00194
00195 void assert_not_present (const std::string &filename);
00196
00197
00198
00199
00200
00201
00202
00203 std::string trim (const std::string &orig);
00204
00205
00206
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
00214
00215 std::string intToString(int x, int width);
00216
00217
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
00223 template<typename T> inline T stringToData (const std::string &x)
00224 { T result; stringToData(x,result); return result; }
00225
00226
00227 void parse_file (const std::string &filename,
00228 std::map<std::string,std::string> &dict);
00229
00230
00231
00232
00233
00234
00235 void announce_progress (int now, int total);
00236
00237
00238
00239 void announce_progress (double now, double last, double total);
00240
00241
00242 void announce (const std::string &name);
00243
00244
00245
00246 void module_startup (const std::string &name, int argc, const char **argv,
00247 int argc_expected, const std::string &argv_expected);
00248
00249
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