suanPan
utility.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2024 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 #ifdef __cpp_lib_execution
24 #include <execution>
25 #endif
26 
27 namespace suanpan {
28  template<sp_i IT, std::invocable<IT> F> void for_each(const IT start, const IT end, F&& FN) {
29 #ifdef SUANPAN_MT
30  static tbb::affinity_partitioner ap;
31  tbb::parallel_for(start, end, std::forward<F>(FN), ap);
32 #else
33  for(IT I = start; I < end; ++I) FN(I);
34 #endif
35  }
36 
37  template<sp_i IT, std::invocable<IT> F> void for_each(const IT end, F&& FN) { return for_each(static_cast<IT>(0), end, std::forward<F>(FN)); }
38 
39  template<typename T> constexpr T max_element(T start, T end) {
40 #ifdef __cpp_lib_execution
41  return std::max_element(std::execution::par, start, end);
42 #else
43  return std::max_element(start, end);
44 #endif
45  }
46 
47  template<typename T> [[maybe_unused]] const std::vector<T>& unique(std::vector<T>& container) {
48  std::sort(container.begin(), container.end());
49  container.erase(std::unique(container.begin(), container.end()), container.end());
50  container.shrink_to_fit();
51  return container;
52  }
53 
54  template<typename T> constexpr T& hacker(const T& I) { return const_cast<T&>(I); }
55 
56  template<typename T> constexpr T*& hacker(const T* const& I) { return const_cast<T*&>(I); }
57 
58  template<typename T> T sign(const T& I) { return (I > T(0)) - (I < T(0)); }
59 
60  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(); }
61 
62  unsigned long long binomial(unsigned long long, unsigned long long);
63 
64  char to_upper(char);
65  char to_lower(char);
66 
67  void to_upper(string&);
68  void to_lower(string&);
69  string to_upper(const string&);
70  string to_lower(const string&);
71  string to_upper(string&&);
72  string to_lower(string&&);
73 
74  namespace expression {
75  std::vector<std::pair<string, unsigned>> split(const std::string_view& variable_string);
76  } // namespace expression
77 } // namespace suanpan
78 
79 template<typename T> bool get_input(istringstream& I, T& O) { return static_cast<bool>(I >> O); }
80 
81 template<typename T> bool get_input(istringstream& I, Col<T>& O) {
82  auto code = true;
83  for(auto& P : O) code &= static_cast<bool>(I >> P);
84  return code;
85 }
86 
87 template<typename T> bool get_input(istringstream& I, std::vector<T>& O) {
88  T value;
89  while(get_input(I, value)) O.emplace_back(value);
90  return true;
91 }
92 
93 template<typename T, typename... U> bool get_input(istringstream& I, T& O, U&... R) { return static_cast<bool>(I >> O) ? get_input(I, R...) : false; }
94 
95 template<typename T> T get_input(istringstream& I) {
96  T O;
97  I >> O;
98  return O;
99 }
100 
101 void ignore_whitespace(istringstream&);
102 
103 template<typename T> bool get_optional_input(istringstream& I, T& O) {
104  if(I.eof()) return true;
105 
106  return static_cast<bool>(I >> O);
107 }
108 
109 template<typename T> bool get_optional_input(istringstream& I, Col<T>& O) {
110  auto code = true;
111  for(auto& P : O) code &= I.eof() ? true : static_cast<bool>(I >> P);
112  return code;
113 }
114 
115 template<typename T, typename... U> bool get_optional_input(istringstream& I, T& O, U&... R) {
116  if(I.eof()) return true;
117 
118  return static_cast<bool>(I >> O) ? get_optional_input(I, R...) : false;
119 }
120 
121 string get_remaining(istringstream&);
122 
123 bool is_equal(const char*, const char*);
124 bool is_equal(char, char);
125 bool is_equal(int, char);
126 bool is_equal(const string&, const char*);
127 bool is_equal(const char*, const string&);
128 bool is_equal(const string&, const string&);
129 
130 bool if_contain(const string&, const char*);
131 bool if_contain(const string&, const string&);
132 bool if_contain(string&&, string&&);
133 
134 template<std::equality_comparable T> std::pair<bool, std::int64_t> if_contain(const std::vector<T>& container, const T target) {
135  auto position = std::find(container.begin(), container.end(), target);
136 
137  return {position != container.end() && container.size() > 0, position - container.begin()};
138 }
139 
140 bool is_true(const char*);
141 bool is_false(const char*);
142 bool is_true(const string&);
143 bool is_false(const string&);
144 
145 bool is_integer(const string&);
146 
147 double perturb(double, double = 1E-5);
148 
149 #endif
Storage< T >::iterator end(Storage< T > &S)
Definition: Storage.hpp:202
std::vector< std::pair< string, unsigned > > split(const std::string_view &variable_string)
Definition: utility.cpp:69
Definition: MatrixModifier.hpp:36
constexpr T & hacker(const T &I)
Definition: utility.h:54
char to_lower(char)
Definition: utility.cpp:37
T sign(const T &I)
Definition: utility.h:58
unsigned long long binomial(unsigned long long, unsigned long long)
Definition: utility.cpp:21
char to_upper(char)
Definition: utility.cpp:35
constexpr T max_element(T start, T end)
Definition: utility.h:39
const std::vector< T > & unique(std::vector< T > &container)
Definition: utility.h:47
void for_each(const IT start, const IT end, F &&FN)
Definition: utility.h:28
std::enable_if_t<!std::numeric_limits< T >::is_integer, bool > approx_equal(T x, T y, int ulp=2)
Definition: utility.h:60
bool get_input(istringstream &I, T &O)
Definition: utility.h:79
bool is_integer(const string &)
Definition: utility.cpp:129
bool is_false(const char *)
Definition: utility.cpp:123
string get_remaining(istringstream &)
Definition: utility.cpp:98
void ignore_whitespace(istringstream &)
Definition: utility.cpp:92
bool if_contain(const string &, const char *)
Definition: utility.cpp:115
double perturb(double, double=1E-5)
Definition: utility.cpp:131
bool is_equal(const char *, const char *)
Definition: utility.cpp:103
bool get_optional_input(istringstream &I, T &O)
Definition: utility.h:103
bool is_true(const char *)
Definition: utility.cpp:121