Documentation of CSL
support.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
21 #ifndef SUPPORT_H_INCLUDED
22 #define SUPPORT_H_INCLUDED
23 
24 #include <cmath>
25 #include <string>
26 #include <string_view>
27 #include <sstream>
28 #include <vector>
29 #include <functional>
30 
31 
32 template<class T>
33 std::string toString(T obj)
34 {
35  std::ostringstream sout;
36  sout << obj;
37  return sout.str();
38 }
39 
43 namespace csl {
44 
54 int PGCD(double a, double b);
55 
64 long long int PGCD(long long int a, long long int b);
65 
66 int PGCD(int a, int b);
67 
80 int internal_PGCD(long long int a, long long int b);
81 
87 long long int sgn(long long int a);
88 
89 int sgn(int a);
90 
96 int sgn(double a);
97 
106 double factorial(int n);
107 
113 long long int internal_factorial(long long int n);
114 
115 int compare(std::string_view a, std::string_view b);
116 
117 bool operator>(const std::string& a, const std::string& b);
118 
119 bool operator<(const std::string& a, const std::string& b);
120 
121 bool operator>=(const std::string& a, const std::string& b);
122 
123 bool operator<=(const std::string& a, const std::string& b);
124 
125 std::vector<size_t> range(size_t n);
126 
127 std::vector<size_t> range(size_t i, size_t n);
128 
129 std::vector<size_t> range(size_t i, size_t n, size_t step);
130 
142 template <typename T>
143 bool comparePlaceIndependant(std::vector<T> A,
144  std::vector<T> B)
145 {
146  const size_t size = A.size();
147  if (size != B.size())
148  return false;
149  std::vector<int> indicesLeft(size);
150  for (size_t i = 0; i != size; ++i)
151  indicesLeft[i] = i;
152 
153  for (size_t iA = 0; iA != size; ++iA) {
154  bool matched = false;
155  for (size_t iLeft = 0; iLeft != indicesLeft.size(); ++iLeft) {
156  size_t iB = indicesLeft[iLeft];
157  if (A[iA] == B[iB]) {
158  matched = true;
159  indicesLeft.erase(indicesLeft.begin()+iLeft);
160  break;
161  }
162  }
163  if (not matched)
164  return false;
165  }
166 
167  return true;
168 }
169 
182 template <typename T>
183 bool partialComparePlaceIndependant(std::vector<T> A, std::vector<T> B)
184 {
185  if (A.size() < B.size())
186  return false;
187  std::vector<int> indicesLeft(B.size());
188  for (size_t i = 0; i != B.size(); ++i)
189  indicesLeft[i] = i;
190 
191  for (size_t iA = 0; iA != A.size(); ++iA) {
192  for (size_t iLeft = 0; iLeft != indicesLeft.size(); ++iLeft) {
193  size_t iB = indicesLeft[iLeft];
194  if (A[iA] == B[iB]) {
195  indicesLeft.erase(indicesLeft.begin()+iLeft);
196  break;
197  }
198  }
199  }
200 
201  return (indicesLeft.empty());
202 }
203 
204 } // End of namespace csl
205 
206 #endif
int PGCD(double a, double b)
Returns the PGCD of a and b.
Definition: support.cpp:23
Namespace for csl library.
Definition: abreviation.h:34
bool operator<=(const Expr &a, const Expr &b)
see Abstract::operator<=()
Definition: abstract.cpp:1416
bool operator>(const Expr &a, const Expr &b)
see Abstract::operator>()
Definition: abstract.cpp:1419
bool comparePlaceIndependant(std::vector< T > A, std::vector< T > B)
Template function that compares the elements in two vectors A and B, independently on their order...
Definition: support.h:143
bool operator>=(const Expr &a, const Expr &b)
see Abstract::operator>=()
Definition: abstract.cpp:1413
int internal_PGCD(long long int a, long long int b)
Returns the PGCD of a and b.
Definition: support.cpp:48
bool partialComparePlaceIndependant(std::vector< T > A, std::vector< T > B)
Template function that compares the elements in two vectors A and B, independently on their order...
Definition: support.h:183
long long int internal_factorial(long long int n)
Returns the factorial of a.
Definition: support.cpp:77
long long int sgn(long long int a)
Returns the sign of a.
Definition: support.cpp:56
bool operator<(const Expr &a, const Expr &b)
see Abstract::operator<()
Definition: abstract.cpp:1423