Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
utility.hpp
Go to the documentation of this file.
1 #include <string>
2 #include <stack>
3 #include <cstdio>
4 #include <cstdarg>
5 #include <algorithm>
6 #include <cstdint>
7 #include <cctype>
8 #include <cstring>
9 #include <cmath>
10 
11 namespace meow{
12 
13  inline std::string stringPrintf(char const * fmt, ...){
14  char str[8192];
15  va_list args;
16  va_start(args, fmt);
17  vsnprintf(str, 8192, fmt, args);
18  va_end(args);
19  return std::string(str);
20  }
21 
22  inline std::string stringReplace(std::string str,
23  std::string const& from,
24  std::string const& to){
25  std::string out = str;
26  int len = from.length();
27  for(size_t pos; (pos = out.find(from)) != std::string::npos; ){
28  out.replace(pos, len, to);
29  }
30  return out;
31  }
32 
33  inline bool cstringEndWith(char const* str, int n, ...){
34  int len = strlen(str);
35  va_list args;
36  va_start(args, n);
37  for(int i = 0; i < n; i++){
38  char const* arg = va_arg(args, char const*);
39  int arglen = strlen(arg);
40  if(arglen <= len && strcmp(str + len - arglen, arg) == 0){
41  return true;
42  }
43  }
44  va_end(args);
45  return false;
46  }
47 
48  inline void debugPrintf_(char const* file,
49  char const* func,
50  int32_t line,
51  char const* msg){
52 #ifdef DEBUG
53  fprintf(stderr, "%s[%d] %s >> %s", file, line, func, msg);
54 #endif // DEBUG
55  }
56 
57  inline void messagePrintf(int32_t level_change, char const* fmt, ...){
58  static int32_t level = 0;
59  static int32_t last_level = -5;
60  char str[8192];
61  va_list args;
62  va_start(args, fmt);
63  vsnprintf(str, 8192, fmt, args);
64  va_end(args);
65  if(last_level == 1 && level_change == -1){
66  printf(" ...%s\n", str);
67  }else{
68  if(last_level == 1) printf("\n");
69  int32_t level2 = level + (level_change == -1 ? -1 : 0);
70  for(int i = 0; i < level2; i++) printf("| ");
71  printf("%s%s", (level_change == -1 ? "..." : ""), str);
72  if(level_change != 1) printf("\n");
73  }
74  level += level_change;
75  last_level = level_change;
76  fflush(stdout);
77  }
78 
79  inline double noEPS(double value, double eps){
80  return (fabs(value) <= fabs(eps) ? 0 : value);
81  }
82 
83  inline double normalize(double lower, double upper, double value){
84  return (value - lower) / (upper - lower);
85  }
86 
87  inline double denormalize(double lower, double upper, double ratio){
88  return lower + ratio * (upper - lower);
89  }
90 
91  inline double ratioMapping(double l1, double u1, double m1,
92  double l2, double u2){
93  return denormalize(l2, u2, normalize(l1, u1, m1));
94  }
95 
96  inline bool filenameCompare(std::string const& f1, std::string const& f2){
97  char const* s1 = f1.c_str();
98  char const* s2 = f2.c_str();
99  int l1 = f1.length();
100  int l2 = f2.length();
101  int i1, i2;
102  for(i1 = i2 = 0; i1 < l1 || i2 < l2; i1++, i2++){
103  if(isdigit(s1[i1]) && isdigit(s2[i2])){
104  int n1 = atoi(s1 + i1);
105  int n2 = atoi(s2 + i2);
106  if(n1 != n2){
107  return (n1 < n2);
108  }
109  while(i1 + 1 < l1 && isdigit(s1[i1 + 1])) i1++;
110  while(i2 + 1 < l2 && isdigit(s2[i2 + 1])) i2++;
111  }else{
112  if(s1[i1] != s2[i2]){
113  return s1[i1] < s2[i2];
114  }
115  }
116  }
117  return false;
118  }
119  template<class T> inline T inRange(T const& mn, T const& mx, T const& v){
120  return std::min(mx, std::max(mn, v));
121  }
122  template<class T> inline double average(
123  T const& beg, T const& end,
124  double sigs){
125  int N = 0;
126  double av = 0;
127  for(T it = beg; it != end; it++, N++){
128  av += *it;
129  }
130  av /= N;
131  double sig = 0;
132  for(T it = beg; it != end; it++){
133  sig += (*it - av) * (*it - av);
134  }
135  sig = sqrt(sig / N);
136  double lower = av - sig * sigs, upper = av + sig * sigs;
137  double ret = 0, retn = 0;
138  for(T it = beg; it != end; it++){
139  if(lower <= *it && *it <= upper){
140  ret += *it;
141  retn++;
142  }
143  }
144  return ret / retn;
145  }
146  template<class T> inline double average(
147  T const& beg, T const& end,
148  T const& p,
149  double sigs){
150  int N = 0;
151  double ps = 0;
152  for(T it = beg, ip = p; it != end; it++, N++, ip++){
153  ps += *ip;
154  }
155  double av = 0;
156  for(T it = beg, ip = p; it != end; it++, ip++){
157  av += *it * *ip / ps;
158  }
159  double sig = 0;
160  for(T it = beg, ip = p; it != end; it++, ip++){
161  sig += *ip / ps * (*it - av) * (*it - av);
162  }
163  sig = sqrt(sig);
164  double lower = av - sig * sigs, upper = av + sig * sigs;
165  double ret = 0, retn = 0;
166  for(T it = beg, ip = p; it != end; it++, ip++){
167  if(lower <= *it && *it <= upper){
168  ret += *it * *ip;
169  retn += *ip;
170  }
171  }
172  if(retn <= 1e-10) return av;
173  return ret / retn;
174  }
175 }
T normalize(T lower, T upper, T value)
(value-lower)/(upper-lower)
Definition: utility.h:27
bool filenameCompare(std::string const &f1, std::string const &f2)
將兩個字串用人類習慣的檔名排序方式排序
Definition: utility.h:178
bool cstringEndWith(char const *str, int n,...)
檢查給定字串的結尾是否符合給定的數個patterns中的一個
Definition: utility.h:81
T ratioMapping(T l1, T u1, T m1, T l2, T u2)
denormalize(l2,u2,normalize(l1,u1,m1))
Definition: utility.h:43
T denormalize(T lower, T upper, T _ratio)
(lower+_ratio*(upper-lower))
Definition: utility.h:35
void debugPrintf_(char const *file, char const *func, size_t line, char const *msg)
Definition: utility.h:109
T noEPS(T value, T eps=1e-9)
如果abs(輸入的數值) < eps, 則回傳0, 否則回傳輸入的數值
Definition: utility.h:18
double average(T const &beg, T const &end, double sigs)
只將 sigs 個標準差以內的數據拿來取平均
Definition: utility.h:83
std::string stringReplace(std::string str, std::string const &from, std::string const &to)
將輸入字串中的某個pattern取代成另一個pattern
Definition: utility.h:60
T inRange(T const &mn, T const &mx, T const &v)
std::min(mx,std::max(mn,v))
Definition: utility.h:51
std::string stringPrintf(char const *fmt,...)
類似C的printf, 不過是將格式化的字串丟到 std::string 裡回傳
Definition: utility.h:42
void messagePrintf(int level_change, char const *fmt,...)
階層式輸出
Definition: utility.h:145