SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helper_modules.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_HELPER_MODULES_HPP
9 #define SIG_UTIL_HELPER_MODULES_HPP
10 
11 #include "../sigutil.hpp"
12 #include "../helper/type_convert.hpp"
13 #include <sstream>
14 
16 
17 namespace sig
18 {
19 
20 template <class B>
21 #if SIG_ENABLE_CONSTEXPR
22 constexpr
23 #endif
24 bool And(B&& cond){
25  return static_cast<bool>(cond);
26 }
27 
29 
39 template <class B1, class... Bs>
40 #if SIG_ENABLE_CONSTEXPR
41 constexpr
42 #endif
43 bool And(B1&& cond, Bs&&... conds){
44  return And(std::forward<Bs>(conds)...) && cond;
45 }
46 
47 
48 template <class B>
49 #if SIG_ENABLE_CONSTEXPR
50 constexpr
51 #endif
52 bool Or(B&& cond){
53  return static_cast<bool>(cond);
54 }
56 
66 template <class B1, class... Bs>
67 #if SIG_ENABLE_CONSTEXPR
68 constexpr
69 #endif
70 bool Or(B1&& cond, Bs&&... conds){
71  return Or(conds...) || cond;
72 }
73 
74 
76 
88 template <class B1, class B2>
89 #if SIG_ENABLE_CONSTEXPR
90 constexpr
91 #endif
92 bool Xor(B1&& a, B2&& b){
93  return (a && !b) || (!a && b);
94 }
95 
97 
109 template <class B1, class B2>
110 #if SIG_ENABLE_CONSTEXPR
111 constexpr
112 #endif
113 bool Consistency(B1&& a, B2&& b){
114  return (a && b) || (!a && !b);
115 }
116 
117 
118 template <class T>
119 #if SIG_ENABLE_CONSTEXPR
120 constexpr
121 #endif
122 auto min(T v) ->T
123 {
124  return v;
125 }
126 
127 template <class T1, class T2>
128 #if SIG_ENABLE_CONSTEXPR
129 constexpr
130 #endif
131 auto min(T1 v1, T2 v2)
132 {
133  return v1 < v2 ? v1 : v2;
134 }
135 
137 
146 template <class T, class... Ts>
147 #if SIG_ENABLE_CONSTEXPR
148 constexpr
149 #endif
150 auto min(T v, Ts... vs)
151 {
152  return min(v, min(vs...));
153 }
154 
155 
156 template <class T>
157 #if SIG_ENABLE_CONSTEXPR
158 constexpr
159 #endif
160 auto max(T v) ->T
161 {
162  return v;
163 }
164 
165 template <class T1, class T2>
166 #if SIG_ENABLE_CONSTEXPR
167 constexpr
168 #endif
169 auto max(T1 v1, T2 v2)
170 {
171  return v1 < v2 ? v2 : v1;
172 }
173 
175 
184 template <class T, class... Ts>
185 #if SIG_ENABLE_CONSTEXPR
186 constexpr
187 #endif
188 auto max(T v, Ts... vs)
189 {
190  return max(v, max(vs...));
191 }
192 
193 
195 
206 template <class T>
207 constexpr bool is_number(T x)
208 {
209  // false if x is a NaN.
210  return (x == x);
211 }
212 
214 
224 template <class T>
225 constexpr bool is_finite_number(T x)
226 {
228 }
229 
230 
231 template <class T>
232 #if SIG_ENABLE_CONSTEXPR
233 constexpr
234 #endif
235 T abs_delta(T v1, T v2)
236 {
237  return v1 < v2 ? v2 - v1 : v1 - v2;
238 }
239 
241 
251 template <class T1, class T2>
252 #if SIG_ENABLE_CONSTEXPR
253 constexpr
254 #endif
255 auto abs_delta(T1 v1, T2 v2) ->decltype(v1 < v2 ? v2 - v1 : v1 - v2)
256 {
257  return v1 < v2 ? v2 - v1 : v1 - v2;
258 }
259 
260 
262 
281 template <class T1, class T2, typename std::enable_if<!(impl::is_string<T1>::value || impl::is_string<T2>::value)>::type*& = enabler>
282 bool equal(T1&& v1, T2&& v2)
283 {
284  constexpr uint tolerant_rate = 10000; //許容範囲の調整 (10^-16 * tolerant_rate)
285 
286  using T = typename std::common_type<T1, T2>::type;
287  const T dmin = std::numeric_limits<T>::epsilon();
288 
289  return !(abs_delta(std::forward<T1>(v1), std::forward<T2>(v2)) > tolerant_rate * dmin);
290 }
291 
292 
294 
304 template <class S1, class S2, typename std::enable_if<(impl::is_string<S1>::value && impl::is_string<S2>::value)>::type*& = enabler>
305 bool equal(S1&& v1, S2&& v2)
306 {
307  return static_cast<impl::string_t<S1>>(std::forward<S1>(v1)) == static_cast<impl::string_t<S2>>(std::forward<S2>(v2));
308 }
309 
310 
312 
323 template <class T1, class T2>
324 bool equal_tolerant(T1 v1, T2 v2, typename std::common_type<T1, T2>::type margin)
325 {
326  return margin ? !(abs_delta(v1, v2) > margin) : equal(v1, v2);
327 }
328 
330 
346 template <class T, class U>
347 constexpr bool check_range(T const& val, U const& min, U const& max)
348 {
349  return (min > val) || (val > max) ? false : true;
350 }
351 
353 
370 template <class T, class U>
371 bool modify_range(T& val, U const& min, U const& max)
372 {
373  if (val<min){ val = min; return false; }
374  if (val>max){ val = max; return false; }
375  return true;
376 }
377 
378 
386 template <class T1, class T2>
387 constexpr bool greater(T1 v1, T2 v2){ return v1 > v2 ? true : false; };
388 
396 template <class T1, class T2>
397 constexpr bool less(T1 v1, T2 v2){ return v1 < v2 ? true : false; };
398 
399 
402 {
403  template <class T>
404 #if SIG_ENABLE_CONSTEXPR
405  constexpr
406 #endif
407  T operator()(T v) const{ return v; }
408 };
409 
412 {
413  template <class T>
414 #if SIG_ENABLE_CONSTEXPR
415  constexpr
416 #endif
417  T operator()(T v) const{ return ++v; }
418 };
419 
422 {
423  template <class T>
424 #if SIG_ENABLE_CONSTEXPR
425  constexpr
426 #endif
427  T operator()(T v) const{ return --v; }
428 };
429 
430 
432 struct plus_t
433 {
434  template <class T1, class T2>
435  auto operator()(T1&& v1, T2&& v2) const
436  ->typename impl::remove_const_reference< decltype(std::forward<T1>(v1) + std::forward<T2>(v2)) >::type
437  {
438  return std::forward<T1>(v1) + std::forward<T2>(v2);
439  }
440 };
441 
443 struct minus_t
444 {
445  template <class T1, class T2>
446  auto operator()(T1&& v1, T2&& v2) const
447  ->typename impl::remove_const_reference< decltype(std::forward<T1>(v1) - std::forward<T2>(v2)) >::type
448  {
449  return std::forward<T1>(v1) - std::forward<T2>(v2);
450  }
451 };
452 
454 struct mult_t
455 {
456  template <class T1, class T2>
457  auto operator()(T1&& v1, T2&& v2) const
458  ->typename impl::remove_const_reference< decltype(std::forward<T1>(v1) * std::forward<T2>(v2)) >::type
459  {
460  return std::forward<T1>(v1) * std::forward<T2>(v2);
461  }
462 };
463 
465 struct div_t
466 {
467  template <class T1, class T2>
468  auto operator()(T1&& v1, T2&& v2) const
469  ->typename impl::remove_const_reference< decltype(std::forward<T1>(v1) / std::forward<T2>(v2)) >::type
470  {
471  return std::forward<T1>(v1) / std::forward<T2>(v2);
472  }
473 };
474 
475 }
476 #endif
auto min(T v, Ts...vs)
可変長 min
T operator()(T v) const
加法を行う関数オブジェクト
constexpr bool is_number(T x)
値が非数(NaN)でないかを確認
auto max(T v, Ts...vs)
可変長 max
引数の値をインクリメントした値を返す関数オブジェクト
void * enabler
T abs_delta(T v1, T v2)
bool Or(B &&cond)
bool And(B &&cond)
bool equal_tolerant(T1 v1, T2 v2, typename std::common_type< T1, T2 >::type margin)
指定範囲内の誤差を許した等値比較
引数の値をデクリメントした値を返す関数オブジェクト
bool equal(T1 &&v1, T2 &&v2)
数値の簡易等値比較(厳密な計算でない場合の使用を想定)
bool Consistency(B1 &&a, B2 &&b)
AとBの真偽一致でtrueを返す (⇔ !xor)
auto operator()(T1 &&v1, T2 &&v2) const -> typename impl::remove_const_reference< decltype(std::forward< T1 >(v1)-std::forward< T2 >(v2)) >::type
T operator()(T v) const
bool Xor(B1 &&a, B2 &&b)
xor
constexpr bool check_range(T const &val, U const &min, U const &max)
値の範囲チェック
auto min(T v) -> T
auto max(T v) -> T
constexpr bool is_finite_number(T x)
値が非数(NaN)でなく、かつ無限大(Inf)でないかを確認
減法を行う関数オブジェクト
bool modify_range(T &val, U const &min, U const &max)
値を範囲内に自動修正
除法を行う関数オブジェクト
引数の変数をそのまま返す関数オブジェクト
T operator()(T v) const
constexpr bool less(T1 v1, T2 v2)
auto operator()(T1 &&v1, T2 &&v2) const -> typename impl::remove_const_reference< decltype(std::forward< T1 >(v1)*std::forward< T2 >(v2)) >::type
Definition: array.hpp:15
typename StringId< typename remove_const_reference< typename std::decay< T >::type >::type >::type string_t
auto operator()(T1 &&v1, T2 &&v2) const -> typename impl::remove_const_reference< decltype(std::forward< T1 >(v1)+std::forward< T2 >(v2)) >::type
auto operator()(T1 &&v1, T2 &&v2) const -> typename impl::remove_const_reference< decltype(std::forward< T1 >(v1)/std::forward< T2 >(v2)) >::type
constexpr bool greater(T1 v1, T2 v2)
乗法を行う関数オブジェクト