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> constexpr T clamp_unit(T c) { return clamp(c, T(0), T(1)); }
66
67 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(); }
68
69 unsigned long long binomial(unsigned long long, unsigned long long);
70
71 char to_upper(char);
72 char to_lower(char);
73
74 void to_upper(std::string&);
75 void to_lower(std::string&);
76 std::string to_upper(const std::string&);
77 std::string to_lower(const std::string&);
78 std::string to_upper(std::string&&);
79 std::string to_lower(std::string&&);
80
81 namespace expression {
82 std::vector<std::pair<std::string, unsigned>> split(std::string_view variable_string);
83 } // namespace expression
84} // namespace suanpan
85
86template<typename T> bool get_input(std::istringstream& I, T& O) { return static_cast<bool>(I >> O); }
87
88template<typename T> bool get_input(std::istringstream& I, Col<T>& O) {
89 auto code = true;
90 for(auto& P : O) code &= static_cast<bool>(I >> P);
91 return code;
92}
93
94template<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; }
95
96template<typename T> T get_input(std::istringstream& I) {
97 T O;
98 I >> O;
99 return O;
100}
101
102void ignore_whitespace(std::istringstream&);
103
104template<typename T> bool get_optional_input(std::istringstream& I, T& O) {
105 if(I.eof()) return true;
106
107 return static_cast<bool>(I >> O);
108}
109
110template<typename T> bool get_optional_input(std::istringstream& I, Col<T>& O) {
111 auto code = true;
112 for(auto& P : O) code &= I.eof() ? true : static_cast<bool>(I >> P);
113 return code;
114}
115
116template<typename T, typename... U> bool get_optional_input(std::istringstream& I, T& O, U&... R) {
117 if(I.eof()) return true;
118
119 return static_cast<bool>(I >> O) ? get_optional_input(I, R...) : false;
120}
121
122template<typename T> auto get_remaining(std::istringstream& I) {
123 std::vector<T> O;
124 T value;
125 while(get_input(I, value)) O.emplace_back(value);
126 return O;
127}
128
129template<typename T1, typename T2> auto get_remaining(std::istringstream& I) {
130 std::vector<T1> O1;
131 std::vector<T2> O2;
132 T1 V1;
133 T2 V2;
134 while(get_input(I, V1, V2)) {
135 O1.emplace_back(V1);
136 O2.emplace_back(V2);
137 }
138 return std::make_tuple(O1, O2);
139}
140
141template<typename T1, typename T2, typename T3> auto get_remaining(std::istringstream& I) {
142 std::vector<T1> O1;
143 std::vector<T2> O2;
144 std::vector<T3> O3;
145 T1 V1;
146 T2 V2;
147 T3 V3;
148 while(get_input(I, V1, V2, V3)) {
149 O1.emplace_back(V1);
150 O2.emplace_back(V2);
151 O3.emplace_back(V3);
152 }
153 return std::make_tuple(O1, O2, O3);
154}
155
156template<typename T1, typename T2, typename T3, typename T4> auto get_remaining(std::istringstream& I) {
157 std::vector<T1> O1;
158 std::vector<T2> O2;
159 std::vector<T3> O3;
160 std::vector<T4> O4;
161 T1 V1;
162 T2 V2;
163 T3 V3;
164 T4 V4;
165 while(get_input(I, V1, V2, V3, V4)) {
166 O1.emplace_back(V1);
167 O2.emplace_back(V2);
168 O3.emplace_back(V3);
169 O4.emplace_back(V4);
170 }
171 return std::make_tuple(O1, O2, O3, O4);
172}
173
174template<typename T1, typename T2, typename T3, typename T4, typename T5> auto get_remaining(std::istringstream& I) {
175 std::vector<T1> O1;
176 std::vector<T2> O2;
177 std::vector<T3> O3;
178 std::vector<T4> O4;
179 std::vector<T5> O5;
180 T1 V1;
181 T2 V2;
182 T3 V3;
183 T4 V4;
184 T5 V5;
185 while(get_input(I, V1, V2, V3, V4, V5)) {
186 O1.emplace_back(V1);
187 O2.emplace_back(V2);
188 O3.emplace_back(V3);
189 O4.emplace_back(V4);
190 O5.emplace_back(V5);
191 }
192 return std::make_tuple(O1, O2, O3, O4, O5);
193}
194
195std::string get_remaining(std::istringstream&);
196
197bool is_equal(const char*, const char*);
198bool is_equal(char, char);
199bool is_equal(int, char);
200bool is_equal(const std::string&, const char*);
201bool is_equal(const char*, const std::string&);
202bool is_equal(const std::string&, const std::string&);
203bool is_equal(std::string_view, const char*);
204bool is_equal(const char*, std::string_view);
205
206bool if_contain(const std::string&, const char*);
207bool if_contain(const std::string&, const std::string&);
208bool if_contain(std::string&&, std::string&&);
209
210bool if_startswith(std::string_view, std::string_view);
211
212template<std::equality_comparable T> std::pair<bool, std::int64_t> if_contain(const std::vector<T>& container, const T target) {
213 auto position = std::find(container.begin(), container.end(), target);
214
215 return {position != container.end() && container.size() > 0, position - container.begin()};
216}
217
218bool is_true(const char*);
219bool is_false(const char*);
220bool is_true(const std::string&);
221bool is_false(const std::string&);
222
223bool is_integer(const std::string&);
224
225double perturb(double, double = 1E-5);
226
227#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
constexpr T clamp_unit(T c)
Definition utility.h:65
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:67
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
bool if_startswith(std::string_view, std::string_view)
Definition utility.cpp:126
void ignore_whitespace(std::istringstream &)
Definition utility.cpp:93
bool is_integer(const std::string &)
Definition utility.cpp:143
bool get_optional_input(std::istringstream &I, T &O)
Definition utility.h:104
bool get_input(std::istringstream &I, T &O)
Definition utility.h:86
bool is_false(const char *)
Definition utility.cpp:137
auto get_remaining(std::istringstream &I)
Definition utility.h:122
double perturb(double, double=1E-5)
Definition utility.cpp:145
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:135