summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2022-11-29 13:31:43 +0200
committerIgor Pashev <pashev.igor@gmail.com>2022-11-29 13:31:43 +0200
commite8ef97ac2d129d5132cd3ff5240306487bfb2a51 (patch)
treea41380e56046c660ed28132ded8cc1261bbf0575
parent271959a7ea324d833c245cecfc586ef140693578 (diff)
downloadgcd-e8ef97ac2d129d5132cd3ff5240306487bfb2a51.tar.gz
Update C++ with templates
-rw-r--r--gcd.cpp40
1 files changed, 17 insertions, 23 deletions
diff --git a/gcd.cpp b/gcd.cpp
index 15fc4e9..e1d363a 100644
--- a/gcd.cpp
+++ b/gcd.cpp
@@ -27,17 +27,14 @@
#include <gmpxx.h>
typedef mpz_class Number;
#else
-typedef unsigned int Number;
+typedef unsigned long Number;
#endif
-using namespace std;
+using std::vector;
-typedef vector<Number> Numbers;
-
-Number gcd(Number a, Number b) {
- Number c;
+template <class N> N gcd2(N a, N b) {
while (b != 0) {
- c = b;
+ N c = b;
b = a % b;
a = c;
}
@@ -45,38 +42,35 @@ Number gcd(Number a, Number b) {
}
#if __cplusplus >= 201703L
-class GCD {
+template <class N> class GCD {
public:
- Number operator()(Number a, Number b) const { return gcd(a, b); };
-} GCD;
+ N operator()(N a, N b) const { return gcd2(a, b); };
+};
-Number gcd(const Numbers &ns) {
- return reduce(execution::par, begin(ns), end(ns), Number(0), GCD);
+template <class N> N gcd(const vector<N> &ns) {
+ GCD<N> GCD;
+ return reduce(std::execution::par, begin(ns), end(ns), N(0), GCD);
}
#else
-Number gcd(const Numbers &ns) {
- Number r = 0;
- for (Numbers::const_iterator n = ns.begin(); n != ns.end(); ++n)
- r = gcd(*n, r);
+template <class N> N gcd(const vector<N> &ns) {
+ N r = 0;
+ for (typename vector<N>::const_iterator n = ns.begin(); n != ns.end(); ++n)
+ r = gcd2(*n, r);
return r;
}
#endif
int main(int argc, char *argv[]) {
if (argc > 1) {
- Numbers ns(argc - 1);
+ vector<Number> ns(argc - 1);
for (int n = 1; n < argc; ++n) {
- stringstream str;
+ std::stringstream str;
str << argv[n];
str >> ns[n - 1];
- /* NOTE:
- * For GMP we can just assign: ns[n-1] = argv[n],
- * and sstream is not needed.
- */
}
- cout << gcd(ns) << endl;
+ std::cout << gcd(ns) << std::endl;
}
return EXIT_SUCCESS;
}