suanPan
Loading...
Searching...
No Matches
utility.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2025 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 <concepts>
22#include <suanPan.h>
23#ifdef __cpp_lib_execution
24#include <execution>
25#endif
26
27namespace 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> constexpr T clamp(T c, T a, T b) {
61 if(a > b) std::swap(a, b);
62 return std::max(a, std::min(b, c));
63 }
64
65 template<typename T> bool approx_equal(T x, T y, int ulp = 2) requires(!std::numeric_limits<T>::is_integer) { return fabs(x - y) <= std::numeric_limits<T>::epsilon() * fabs(x + y) * ulp || fabs(x - y) < std::numeric_limits<T>::min(); }
66
67 unsigned long long binomial(unsigned long long, unsigned long long);
68
69 char to_upper(char);
70 char to_lower(char);
71
72 void to_upper(std::string&);
73 void to_lower(std::string&);
74 std::string to_upper(const std::string&);
75 std::string to_lower(const std::string&);
76 std::string to_upper(std::string&&);
77 std::string to_lower(std::string&&);
78
79 namespace expression {
80 std::vector<std::pair<std::string, unsigned>> split(std::string_view variable_string);
81 } // namespace expression
82} // namespace suanpan
83
84template<typename T> bool get_input(std::istringstream& I, T& O) { return static_cast<bool>(I >> O); }
85
86template<typename T> bool get_input(std::istringstream& I, Col<T>& O) {
87 auto code = true;
88 for(auto& P : O) code &= static_cast<bool>(I >> P);
89 return code;
90}
91
92template<typename T, typename... U> bool get_input(std::istringstream& I, T& O, U&... R) { return static_cast<bool>(I >> O) ? get_input(I, R...) : false; }
93
94template<typename T> T get_input(std::istringstream& I) {
95 T O;
96 I >> O;
97 return O;
98}
99
100void ignore_whitespace(std::istringstream&);
101
102template<typename T> bool get_optional_input(std::istringstream& I, T& O) {
103 if(I.eof()) return true;
104
105 return static_cast<bool>(I >> O);
106}
107
108template<typename T> bool get_optional_input(std::istringstream& I, Col<T>& O) {
109 auto code = true;
110 for(auto& P : O) code &= I.eof() ? true : static_cast<bool>(I >> P);
111 return code;
112}
113
114template<typename T, typename... U> bool get_optional_input(std::istringstream& I, T& O, U&... R) {
115 if(I.eof()) return true;
116
117 return static_cast<bool>(I >> O) ? get_optional_input(I, R...) : false;
118}
119
120template<typename T> auto get_remaining(std::istringstream& I) {
121 std::vector<T> O;
122 T value;
123 while(get_input(I, value)) O.emplace_back(value);
124 return O;
125}
126
127template<typename T1, typename T2> auto get_remaining(std::istringstream& I) {
128 std::vector<T1> O1;
129 std::vector<T2> O2;
130 T1 V1;
131 T2 V2;
132 while(get_input(I, V1, V2)) {
133 O1.emplace_back(V1);
134 O2.emplace_back(V2);
135 }
136 return std::make_tuple(O1, O2);
137}
138
139template<typename T1, typename T2, typename T3> auto get_remaining(std::istringstream& I) {
140 std::vector<T1> O1;
141 std::vector<T2> O2;
142 std::vector<T3> O3;
143 T1 V1;
144 T2 V2;
145 T3 V3;
146 while(get_input(I, V1, V2, V3)) {
147 O1.emplace_back(V1);
148 O2.emplace_back(V2);
149 O3.emplace_back(V3);
150 }
151 return std::make_tuple(O1, O2, O3);
152}
153
154template<typename T1, typename T2, typename T3, typename T4> auto get_remaining(std::istringstream& I) {
155 std::vector<T1> O1;
156 std::vector<T2> O2;
157 std::vector<T3> O3;
158 std::vector<T4> O4;
159 T1 V1;
160 T2 V2;
161 T3 V3;
162 T4 V4;
163 while(get_input(I, V1, V2, V3, V4)) {
164 O1.emplace_back(V1);
165 O2.emplace_back(V2);
166 O3.emplace_back(V3);
167 O4.emplace_back(V4);
168 }
169 return std::make_tuple(O1, O2, O3, O4);
170}
171
172template<typename T1, typename T2, typename T3, typename T4, typename T5> auto get_remaining(std::istringstream& I) {
173 std::vector<T1> O1;
174 std::vector<T2> O2;
175 std::vector<T3> O3;
176 std::vector<T4> O4;
177 std::vector<T5> O5;
178 T1 V1;
179 T2 V2;
180 T3 V3;
181 T4 V4;
182 T5 V5;
183 while(get_input(I, V1, V2, V3, V4, V5)) {
184 O1.emplace_back(V1);
185 O2.emplace_back(V2);
186 O3.emplace_back(V3);
187 O4.emplace_back(V4);
188 O5.emplace_back(V5);
189 }
190 return std::make_tuple(O1, O2, O3, O4, O5);
191}
192
193std::string get_remaining(std::istringstream&);
194
195bool is_equal(const char*, const char*);
196bool is_equal(char, char);
197bool is_equal(int, char);
198bool is_equal(const std::string&, const char*);
199bool is_equal(const char*, const std::string&);
200bool is_equal(const std::string&, const std::string&);
201bool is_equal(std::string_view, const char*);
202bool is_equal(const char*, std::string_view);
203
204bool if_contain(const std::string&, const char*);
205bool if_contain(const std::string&, const std::string&);
206bool if_contain(std::string&&, std::string&&);
207
208template<std::equality_comparable T> std::pair<bool, std::int64_t> if_contain(const std::vector<T>& container, const T target) {
209 auto position = std::find(container.begin(), container.end(), target);
210
211 return {position != container.end() && container.size() > 0, position - container.begin()};
212}
213
214bool is_true(const char*);
215bool is_false(const char*);
216bool is_true(const std::string&);
217bool is_false(const std::string&);
218
219bool is_integer(const std::string&);
220
221double perturb(double, double = 1E-5);
222
223#endif
Storage< T >::iterator end(Storage< T > &S)
Definition Storage.hpp:208
std::vector< std::pair< std::string, unsigned > > split(std::string_view variable_string)
Definition utility.cpp:70
Definition SparseMatMAGMA.hpp:43
const std::vector< T > & unique(std::vector< T > &container)
Definition utility.h:47
char to_lower(char)
Definition utility.cpp:38
constexpr T clamp(T c, T a, T b)
Definition utility.h:60
T sign(const T &I)
Definition utility.h:58
unsigned long long binomial(unsigned long long, unsigned long long)
Definition utility.cpp:22
char to_upper(char)
Definition utility.cpp:36
constexpr T max_element(T start, T end)
Definition utility.h:39
bool approx_equal(T x, T y, int ulp=2)
Definition utility.h:65
void for_each(const IT start, const IT end, F &&FN)
Definition utility.h:28
constexpr T & hacker(const T &I)
Definition utility.h:54
void ignore_whitespace(std::istringstream &)
Definition utility.cpp:93
bool is_integer(const std::string &)
Definition utility.cpp:134
bool get_optional_input(std::istringstream &I, T &O)
Definition utility.h:102
bool get_input(std::istringstream &I, T &O)
Definition utility.h:84
bool is_false(const char *)
Definition utility.cpp:128
auto get_remaining(std::istringstream &I)
Definition utility.h:120
double perturb(double, double=1E-5)
Definition utility.cpp:136
bool is_equal(const char *, const char *)
Definition utility.cpp:104
bool if_contain(const std::string &, const char *)
Definition utility.cpp:120
bool is_true(const char *)
Definition utility.cpp:126