00001
00002
00003
00004
00005
00006 #ifndef FITYK__COMMON__H__
00007 #define FITYK__COMMON__H__
00008
00009 #if HAVE_CONFIG_H
00010 # include <config.h>
00011 #endif
00012
00013 #ifndef VERSION
00014 # define VERSION "unknown"
00015 #endif
00016
00017 #include <string>
00018 #include <vector>
00019 #include <math.h>
00020 #include <assert.h>
00021 #include <float.h>
00022
00023 #include "fityk.h"
00024 using fityk::ExecuteError;
00025 using fityk::ExitRequestedException;
00026
00027
00028 #ifdef _MSC_VER
00029 #include <boost/math/special_functions/erf.hpp>
00030 using boost::math::erf;
00031 using boost::math:erfc;
00032 inline double trunc(double a) { return a >= 0 ? floor(a) : ceil(a); }
00033 #define snprintf sprintf_s
00034
00035 #pragma warning( disable : 4996 )
00036 #endif
00037
00038
00039
00040 struct RealRange
00041 {
00042 double from, to;
00043
00044 RealRange() : from(-DBL_MAX), to(DBL_MAX) {}
00045 RealRange(double from_, double to_) : from(from_), to(to_) {}
00046 bool from_inf() const { return from == -DBL_MAX; }
00047 bool to_inf() const { return to == DBL_MAX; }
00048 };
00049
00050
00051
00052
00053
00054 extern double epsilon;
00055
00056 inline bool is_eq(double a, double b) { return fabs(a-b) <= epsilon; }
00057 inline bool is_neq(double a, double b) { return fabs(a-b) > epsilon; }
00058 inline bool is_lt(double a, double b) { return a < b - epsilon; }
00059 inline bool is_gt(double a, double b) { return a > b + epsilon; }
00060 inline bool is_le(double a, double b) { return a <= b + epsilon; }
00061 inline bool is_ge(double a, double b) { return a >= b - epsilon; }
00062 inline bool is_zero(double a) { return fabs(a) <= epsilon; }
00063
00064 inline bool is_finite(double a)
00065 #if HAVE_FINITE
00066 { return finite(a); }
00067 #else
00068 { return a == a; }
00069 #endif
00070
00071
00072 #ifndef M_PI
00073 # define M_PI 3.1415926535897932384626433832795029 // pi
00074 #endif
00075 #ifndef M_LN2
00076 # define M_LN2 0.6931471805599453094172321214581766 // log_e 2
00077 #endif
00078 #ifndef M_SQRT2
00079 # define M_SQRT2 1.4142135623730950488016887242096981 // sqrt(2)
00080 #endif
00081
00082
00083 inline int iround(double d) { return static_cast<int>(floor(d+0.5)); }
00084
00085
00086
00087 template <typename T, int N>
00088 std::string format1(const char* fmt, T t)
00089 {
00090 char buffer[N];
00091 snprintf(buffer, N, fmt, t);
00092 buffer[N-1] = '\0';
00093 return std::string(buffer);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 inline std::string S(bool b) { return b ? "true" : "false"; }
00103 inline std::string S(const char *k) { return std::string(k); }
00104 inline std::string S(char k) { return std::string(1, k); }
00105 inline std::string S(const std::string& k) { return k; }
00106 inline std::string S() { return std::string(); }
00107 inline std::string S(int n) { return format1<int, 16>("%d", n); }
00108 inline std::string S(long n) { return format1<int, 16>("%d", n); }
00109 inline std::string S(size_t n)
00110 { return format1<size_t, 16>("%lu", (unsigned long) n); }
00111 inline std::string S(double d) { return format1<double, 16>("%g", d); }
00112
00113 inline std::string eS(double d) { return format1<double, 24>("%.12g", d); }
00114 inline std::string S(long double d) { return S((double) d); }
00115
00116
00117 bool is_double (std::string const& s);
00118
00119
00120 bool is_int (std::string const& s);
00121
00122
00123 void replace_all(std::string &s, std::string const &old,
00124 std::string const &new_);
00125
00126 void replace_words(std::string &t, std::string const &old_word,
00127 std::string const &new_word);
00128
00129
00130 template<typename T>
00131 std::vector<std::string> split_string(std::string const &s, T delim) {
00132 std::vector<std::string> v;
00133 std::string::size_type start_pos = 0, pos=0;
00134 while (pos != std::string::npos) {
00135 pos = s.find_first_of(delim, start_pos);
00136 v.push_back(std::string(s, start_pos, pos-start_pos));
00137 start_pos = pos+1;
00138 }
00139 return v;
00140 }
00141
00142
00143 inline std::string strip_string(std::string const &s) {
00144 char const *blank = " \r\n\t";
00145 std::string::size_type first = s.find_first_not_of(blank);
00146 if (first == std::string::npos)
00147 return std::string();
00148 std::string::size_type last = s.find_last_not_of(blank);
00149 if (first == 0 && last == s.size() - 1)
00150 return s;
00151 else
00152 return std::string(s, first, last-first+1);
00153 }
00154
00155
00156 inline bool startswith(std::string const& s, std::string const& p) {
00157 return p.size() <= s.size() && std::string(s, 0, p.size()) == p;
00158 }
00159
00160 inline bool endswith(std::string const& s, std::string const& p) {
00161 return p.size() <= s.size() && std::string(s, s.size() - p.size()) == p;
00162 }
00163
00164 std::string::size_type find_matching_bracket(std::string const& formula,
00165 std::string::size_type left_pos);
00166
00167 template<typename T, typename T2>
00168 bool contains_element(std::basic_string<T> const& str, T2 const& t)
00169 {
00170 return (str.find(t) != std::basic_string<T>::npos);
00171 }
00172
00173
00174 bool match_glob(const char* name, const char* pattern);
00175
00176
00177
00178
00179
00180 #define v_foreach(type, iter, vec) \
00181 for (vector<type>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter)
00182
00183 #define vm_foreach(type, iter, vec) \
00184 for (vector<type>::iterator iter = vec.begin(); iter != vec.end(); ++iter)
00185
00186
00187 template <typename T>
00188 inline std::vector<T> vector1 (T a) { return std::vector<T>(1, a); }
00189
00190
00191 template <typename T>
00192 inline std::vector<T> vector2 (T a, T b)
00193 { std::vector<T> v = std::vector<T>(2, a); v[1] = b; return v;}
00194
00195
00196 template <typename T>
00197 inline std::vector<T> vector3 (T a, T b, T c)
00198 { std::vector<T> v = std::vector<T>(3); v[0]=a; v[1]=b; v[2]=c; return v;}
00199
00200
00201 template <typename T>
00202 inline std::vector<T> vector4 (T a, T b, T c, T d) {
00203 std::vector<T> v = std::vector<T>(4); v[0]=a; v[1]=b; v[2]=c; v[3]=d;
00204 return v;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 std::vector<int> range_vector(int l, int u);
00218
00219
00220
00221
00222 template <typename T>
00223 inline int size(std::vector<T> const& v) { return static_cast<int>(v.size()); }
00224
00225
00226 template <typename T>
00227 inline bool is_index (int idx, std::vector<T> const& v)
00228 {
00229 return idx >= 0 && idx < static_cast<int>(v.size());
00230 }
00231
00232
00233 template <typename RandomAccessIterator>
00234 inline std::string join(RandomAccessIterator first, RandomAccessIterator last,
00235 std::string const& sep)
00236 {
00237 if (last - first <= 0)
00238 return "";
00239 std::string s = S(*first);
00240 for (RandomAccessIterator i = first + 1; i != last; ++i)
00241 s += sep + S(*i);
00242 return s;
00243 }
00244
00245 template <typename T>
00246 inline std::string join_vector(std::vector<T> const& v, std::string const& sep)
00247 {
00248 return join(v.begin(), v.end(), sep);
00249 }
00250
00251
00252 template<typename T>
00253 void purge_all_elements(std::vector<T*> &vec)
00254 {
00255 for (typename std::vector<T*>::iterator i=vec.begin(); i!=vec.end(); ++i)
00256 delete *i;
00257 vec.clear();
00258 }
00259
00260
00261 template<typename T, typename T2>
00262 bool contains_element(T const& vec, T2 const& t)
00263 {
00264 return (find(vec.begin(), vec.end(), t) != vec.end());
00265 }
00266
00267
00268 template<typename T, typename T2>
00269 int index_of_element(std::vector<T> const& vec, T2 const& t)
00270 {
00271 typename std::vector<T>::const_iterator p = find(vec.begin(), vec.end(), t);
00272 if (p != vec.end())
00273 return p - vec.begin();
00274 else
00275 return -1;
00276 }
00277
00278
00279 #if defined(_WIN32) || defined(WIN32) || defined(__NT__) || defined(__WIN32__) || defined(__OS2__)
00280 #define PATH_COMPONENT_SEP '\\'
00281 #else
00282 #define PATH_COMPONENT_SEP '/'
00283 #endif
00284
00285 inline std::string get_directory(std::string const& filename)
00286 {
00287 std::string::size_type i = filename.rfind(PATH_COMPONENT_SEP);
00288 return i==std::string::npos ? std::string() : std::string(filename, 0, i+1);
00289 }
00290
00291
00292
00293
00294
00295
00296 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
00297 TypeName(const TypeName&); \
00298 void operator=(const TypeName&)
00299
00300 extern const char* fityk_version_line;
00301
00302
00303 extern volatile bool user_interrupt;
00304
00305 extern const std::string help_filename;
00306
00307
00308
00309 std::string time_now();
00310
00311 #endif
00312