[Gmp-commit] /var/hg/gmp: Add swap functions to gmpxx.
mercurial at gmplib.org
mercurial at gmplib.org
Wed Sep 21 15:05:48 CEST 2011
details: /var/hg/gmp/rev/02b32281da81
changeset: 14238:02b32281da81
user: Marc Glisse <marc.glisse at inria.fr>
date: Wed Sep 21 15:05:45 2011 +0200
description:
Add swap functions to gmpxx.
diffstat:
ChangeLog | 9 +++++
doc/gmp.texi | 11 +++++-
gmpxx.h | 10 +++++
tests/cxx/t-assign.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 118 insertions(+), 2 deletions(-)
diffs (227 lines):
diff -r c749307472b4 -r 02b32281da81 ChangeLog
--- a/ChangeLog Sun Aug 21 20:08:38 2011 +0200
+++ b/ChangeLog Wed Sep 21 15:05:45 2011 +0200
@@ -1,3 +1,12 @@
+2011-09-21 Marc Glisse <marc.glisse at inria.fr>
+
+ * gmpxx.h (mpz_class::swap): New function.
+ (mpq_class::swap): Likewise.
+ (mpf_class::swap): Likewise.
+ (swap): New function.
+ * tests/cxx/t-assign.cc: Test the above.
+ * doc/gmp.texi (swap): Document the above.
+
2011-08-21 Marc Glisse <marc.glisse at inria.fr>
* tests/cxx/t-ops2.cc: check mul-div by 2.
diff -r c749307472b4 -r 02b32281da81 doc/gmp.texi
--- a/doc/gmp.texi Sun Aug 21 20:08:38 2011 +0200
+++ b/doc/gmp.texi Wed Sep 21 15:05:45 2011 +0200
@@ -6540,7 +6540,7 @@
@end example
@end deftypefun
- at deftypefun mpz_class abs (mpz_class @var{op1})
+ at deftypefun mpz_class abs (mpz_class @var{op})
@deftypefunx int cmp (mpz_class @var{op1}, type @var{op2})
@deftypefunx int cmp (type @var{op1}, mpz_class @var{op2})
@maybepagebreak
@@ -6561,6 +6561,9 @@
@deftypefunx int mpz_class::set_str (const string& @var{str}, int @var{base})
@deftypefunx int sgn (mpz_class @var{op})
@deftypefunx mpz_class sqrt (mpz_class @var{op})
+ at maybepagebreak
+ at deftypefunx void mpz_class::swap (mpz_class& @var{op})
+ at deftypefunx void swap (mpz_class& @var{op1}, mpz_class& @var{op2})
These functions provide a C++ class interface to the corresponding GMP C
routines.
@@ -6632,6 +6635,9 @@
@deftypefunx int mpq_class::set_str (const char *@var{str}, int @var{base})
@deftypefunx int mpq_class::set_str (const string& @var{str}, int @var{base})
@deftypefunx int sgn (mpq_class @var{op})
+ at maybepagebreak
+ at deftypefunx void mpq_class::swap (mpq_class& @var{op})
+ at deftypefunx void swap (mpq_class& @var{op1}, mpq_class& @var{op2})
These functions provide a C++ class interface to the corresponding GMP C
routines.
@@ -6774,6 +6780,9 @@
@deftypefunx int mpf_class::set_str (const string& @var{str}, int @var{base})
@deftypefunx int sgn (mpf_class @var{op})
@deftypefunx mpf_class sqrt (mpf_class @var{op})
+ at maybepagebreak
+ at deftypefunx void mpf_class::swap (mpf_class& @var{op})
+ at deftypefunx void swap (mpf_class& @var{op1}, mpf_class& @var{op2})
@deftypefunx mpf_class trunc (mpf_class @var{op})
These functions provide a C++ class interface to the corresponding GMP C
routines.
diff -r c749307472b4 -r 02b32281da81 gmpxx.h
--- a/gmpxx.h Sun Aug 21 20:08:38 2011 +0200
+++ b/gmpxx.h Wed Sep 21 15:05:45 2011 +0200
@@ -35,6 +35,7 @@
#include <iosfwd>
#include <cstring> /* for strlen */
+#include <utility>
#include <string>
#include <stdexcept>
#include <cfloat>
@@ -1450,6 +1451,8 @@
~__gmp_expr() { mpz_clear(mp); }
+ void swap(__gmp_expr& z) { std::swap(*mp, *z.mp); }
+
// assignment operators
__gmp_expr & operator=(const __gmp_expr &z)
{ mpz_set(mp, z.mp); return *this; }
@@ -1623,6 +1626,8 @@
~__gmp_expr() { mpq_clear(mp); }
+ void swap(__gmp_expr& q) { std::swap(*mp, *q.mp); }
+
// assignment operators
__gmp_expr & operator=(const __gmp_expr &q)
{ mpq_set(mp, q.mp); return *this; }
@@ -1828,6 +1833,8 @@
~__gmp_expr() { mpf_clear(mp); }
+ void swap(__gmp_expr& f) { std::swap(*mp, *f.mp); }
+
// assignment operators
__gmp_expr & operator=(const __gmp_expr &f)
{ mpf_set(mp, f.mp); return *this; }
@@ -2963,6 +2970,9 @@
__GMP_DEFINE_UNARY_TYPE_FUNCTION(int, sgn, __gmp_sgn_function)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(int, cmp, __gmp_cmp_function)
+template <class T>
+void swap(__gmp_expr<T, T>& x, __gmp_expr<T, T>& y) { x.swap(y); }
+
// member operators for mpz_class
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator+=, __gmp_binary_plus)
diff -r c749307472b4 -r 02b32281da81 tests/cxx/t-assign.cc
--- a/tests/cxx/t-assign.cc Sun Aug 21 20:08:38 2011 +0200
+++ b/tests/cxx/t-assign.cc Wed Sep 21 15:05:45 2011 +0200
@@ -27,7 +27,8 @@
#include "gmp-impl.h"
#include "tests.h"
-using namespace std;
+using std::string;
+using std::invalid_argument;
void
@@ -185,6 +186,35 @@
} catch (invalid_argument) {
}
}
+
+ // swap(mpz_class &)
+ {
+ mpz_class a(123);
+ mpz_class b(456);
+ a.swap(b);
+ a.swap(a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
+
+ // swap(mpz_class &, mpz_class &)
+ {
+ mpz_class a(123);
+ mpz_class b(456);
+ ::swap(a, b);
+ ::swap(a, a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
+ {
+ using std::swap;
+ mpz_class a(123);
+ mpz_class b(456);
+ swap(a, b);
+ swap(a, a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
}
void
@@ -342,6 +372,35 @@
} catch (invalid_argument) {
}
}
+
+ // swap(mpq_class &)
+ {
+ mpq_class a(3, 2);
+ mpq_class b(-1, 4);
+ a.swap(b);
+ a.swap(a);
+ ASSERT_ALWAYS(a == -.25);
+ ASSERT_ALWAYS(b == 1.5);
+ }
+
+ // swap(mpq_class &, mpq_class &)
+ {
+ mpq_class a(3, 2);
+ mpq_class b(-1, 4);
+ ::swap(a, b);
+ ::swap(a, a);
+ ASSERT_ALWAYS(a == -.25);
+ ASSERT_ALWAYS(b == 1.5);
+ }
+ {
+ using std::swap;
+ mpq_class a(3, 2);
+ mpq_class b(-1, 4);
+ swap(a, b);
+ swap(a, a);
+ ASSERT_ALWAYS(a == -.25);
+ ASSERT_ALWAYS(b == 1.5);
+ }
}
void
@@ -499,6 +558,35 @@
} catch (invalid_argument) {
}
}
+
+ // swap(mpf_class &)
+ {
+ mpf_class a(123);
+ mpf_class b(456);
+ a.swap(b);
+ a.swap(a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
+
+ // swap(mpf_class &, mpf_class &)
+ {
+ mpf_class a(123);
+ mpf_class b(456);
+ ::swap(a, b);
+ ::swap(a, a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
+ {
+ using std::swap;
+ mpf_class a(123);
+ mpf_class b(456);
+ swap(a, b);
+ swap(a, a);
+ ASSERT_ALWAYS(a == 456);
+ ASSERT_ALWAYS(b == 123);
+ }
}
More information about the gmp-commit
mailing list