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> bool get_input(istringstream& I, Col<T>& O) {
64  auto code = true;
65  for(auto& P : O) code &= static_cast<bool>(I >> P);
66  return code;
67 }
68 
69 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; }
70 
71 template<typename T> T get_input(istringstream& I) {
72  T O;
73  I >> O;
74  return O;
75 }
76 
77 void ignore_whitespace(istringstream&);
78 
79 template<typename T> bool get_optional_input(istringstream& I, T& O) {
80  if(I.eof()) return true;
81 
82  return static_cast<bool>(I >> O);
83 }
84 
85 template<typename T> bool get_optional_input(istringstream& I, Col<T>& O) {
86  auto code = true;
87  for(auto& P : O) code &= I.eof() ? true : static_cast<bool>(I >> P);
88  return code;
89 }
90 
91 template<typename T, typename...U> bool get_optional_input(istringstream& I, T& O, U&...R) {
92  if(I.eof()) return true;
93 
94  return static_cast<bool>(I >> O) ? get_optional_input(I, R...) : false;
95 }
96 
97 bool is_equal(const char*, const char*);
98 bool is_equal(char, char);
99 bool is_equal(int, char);
100 bool is_equal(const string&, const char*);
101 bool is_equal(const string&, const string&);
102 
103 bool if_contain(const string&, const char*);
104 bool if_contain(const string&, const string&);
105 bool if_contain(string&&, string&&);
106 
107 template<std::equality_comparable T> std::pair<bool, std::int64_t> if_contain(const std::vector<T>& container, const T target) {
108  auto position = std::find(container.begin(), container.end(), target);
109 
110  return {position != container.end() && container.size() > 0, position - container.begin()};
111 }
112 
113 bool is_true(const char*);
114 bool is_false(const char*);
115 bool is_true(const string&);
116 bool is_false(const string&);
117 
118 #endif
Storage< T >::iterator end(Storage< T > &S)
Definition: Storage.hpp:202
Definition: MatrixModifier.hpp:36
constexpr T & hacker(const T &I)
Definition: utility.h:40
char to_lower(char)
Definition: utility.cpp:37
T sign(const T &I)
Definition: utility.h:44
unsigned long long binomial(unsigned long long, unsigned long long)
Definition: utility.cpp:21
char to_upper(char)
Definition: utility.cpp:35
const std::vector< T > & unique(std::vector< T > &container)
Definition: utility.h:33
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_input(istringstream &I, T &O)
Definition: utility.h:61
bool is_false(const char *)
Definition: utility.cpp:93
void ignore_whitespace(istringstream &)
Definition: utility.cpp:69
bool if_contain(const string &, const char *)
Definition: utility.cpp:85
bool is_equal(const char *, const char *)
Definition: utility.cpp:75
void suanpan_for(const IT start, const IT end, F &&FN)
Definition: utility.h:24
bool get_optional_input(istringstream &I, T &O)
Definition: utility.h:79
bool is_true(const char *)
Definition: utility.cpp:91