suanPan
utility.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2022 Theodore Chang
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  ******************************************************************************/
17 
18 #ifndef UTILITY_H
19 #define UTILITY_H
20 
21 #include <suanPan.h>
22 #include <concepts>
23 
24 template<sp_i IT, typename F> void suanpan_for(const IT start, const IT end, F&& FN) {
25 #ifdef SUANPAN_MT
26  tbb::parallel_for(start, end, std::forward<F>(FN));
27 #else
28  for(IT I = start; I < end; ++I) FN(I);
29 #endif
30 }
31 
32 namespace suanpan {
33  template<typename T> [[maybe_unused]] const std::vector<T>& unique(std::vector<T>& container) {
34  std::sort(container.begin(), container.end());
35  container.erase(std::unique(container.begin(), container.end()), container.end());
36  container.shrink_to_fit();
37  return container;
38  }
39 
40  template<typename T> constexpr T& hacker(const T& I) { return const_cast<T&>(I); }
41 
42  template<typename T> constexpr T*& hacker(const T* const& I) { return const_cast<T*&>(I); }
43 
44  template<typename T> T sign(const T& I) { return (I > T(0)) - (I < T(0)); }
45 
46  template<typename T> std::enable_if_t<!std::numeric_limits<T>::is_integer, bool> approx_equal(T x, T y, int ulp = 2) { return fabs(x - y) <= std::numeric_limits<T>::epsilon() * fabs(x + y) * ulp || fabs(x - y) < std::numeric_limits<T>::min(); }
47 
48  unsigned long long binomial(unsigned long long, unsigned long long);
49 
50  char to_upper(char);
51  char to_lower(char);
52 
53  void to_upper(string&);
54  void to_lower(string&);
55  string to_upper(const string&);
56  string to_lower(const string&);
57  string to_upper(string&&);
58  string to_lower(string&&);
59 } // namespace suanpan
60 
61 template<typename T> bool get_input(istringstream& I, T& O) { return static_cast<bool>(I >> O); }
62 
63 template<typename T> T get_input(istringstream& I) {
64  T O;
65  I >> O;
66  return O;
67 }
68 
69 void ignore_whitespace(istringstream&);
70 
71 template<typename T> bool get_optional_input(istringstream& I, T& O) {
72  if(I.eof()) return true;
73 
74  return static_cast<bool>(I >> O);
75 }
76 
77 bool is_equal(const char*, const char*);
78 bool is_equal(char, char);
79 bool is_equal(int, char);
80 bool is_equal(const string&, const char*);
81 bool is_equal(const string&, const string&);
82 
83 bool if_contain(const string&, const char*);
84 bool if_contain(const string&, const string&);
85 bool if_contain(string&&, string&&);
86 
87 template<std::equality_comparable T> std::pair<bool, std::int64_t> if_contain(const std::vector<T>& container, const T target) {
88  auto position = std::find(container.begin(), container.end(), target);
89 
90  return {position != container.end() && container.size() > 0, position - container.begin()};
91 }
92 
93 bool is_true(const char*);
94 bool is_false(const char*);
95 bool is_true(const string&);
96 bool is_false(const string&);
97 
98 #endif
T sign(const T &I)
Definition: utility.h:44
void ignore_whitespace(istringstream &)
Definition: utility.cpp:69
bool is_equal(const char *, const char *)
Definition: utility.cpp:75
std::enable_if_t<!std::numeric_limits< T >::is_integer, bool > approx_equal(T x, T y, int ulp=2)
Definition: utility.h:46
bool get_optional_input(istringstream &I, T &O)
Definition: utility.h:71
char to_lower(char)
Definition: utility.cpp:37
Definition: MatrixModifier.hpp:36
unsigned long long binomial(unsigned long long, unsigned long long)
Definition: utility.cpp:21
bool if_contain(const string &, const char *)
Definition: utility.cpp:85
bool is_false(const char *)
Definition: utility.cpp:93
bool is_true(const char *)
Definition: utility.cpp:91
void suanpan_for(const IT start, const IT end, F &&FN)
Definition: utility.h:24
const std::vector< T > & unique(std::vector< T > &container)
Definition: utility.h:33
char to_upper(char)
Definition: utility.cpp:35
Storage< T >::iterator end(Storage< T > &S)
Definition: Storage.hpp:202
bool get_input(istringstream &I, T &O)
Definition: utility.h:61
constexpr T & hacker(const T &I)
Definition: utility.h:40