feat(util/numerics): serialization for mpz and mpq

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-12-27 18:32:01 -08:00
parent 49df0c435d
commit abc370a011
6 changed files with 75 additions and 0 deletions

View file

@ -7,6 +7,7 @@ Author: Leonardo de Moura
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "util/test.h" #include "util/test.h"
#include "util/serializer.h"
#include "util/numerics/mpq.h" #include "util/numerics/mpq.h"
using namespace lean; using namespace lean;
@ -173,6 +174,27 @@ static void tst6() {
lean_assert(cmp(mpq(-3, 2), mpz(-1)) < 0); lean_assert(cmp(mpq(-3, 2), mpz(-1)) < 0);
} }
static void tst7() {
std::ostringstream out;
serializer s(out);
mpq n1("-100000000000000000000000000000000000/3");
lean_assert(n1.is_neg());
mpq n2("-3/4");
mpq n3("1200/2131");
mpq n4("321/345");
mpq n5(1, 3);
s << n1 << n2 << n3 << n4 << n5;
std::istringstream in(out.str());
deserializer d(in);
mpq m1, m2, m3, m4, m5;
d >> m1 >> m2 >> m3 >> m4 >> m5;
lean_assert(n1 == m1);
lean_assert(n2 == m2);
lean_assert(n3 == m3);
lean_assert(n4 == m4);
lean_assert(n5 == m5);
}
int main() { int main() {
tst0(); tst0();
tst1(); tst1();
@ -181,5 +203,6 @@ int main() {
tst4(); tst4();
tst5(); tst5();
tst6(); tst6();
tst7();
return has_violations() ? 1 : 0; return has_violations() ? 1 : 0;
} }

View file

@ -8,6 +8,7 @@ Author: Leonardo de Moura
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "util/test.h" #include "util/test.h"
#include "util/serializer.h"
#include "util/numerics/mpz.h" #include "util/numerics/mpz.h"
using namespace lean; using namespace lean;
@ -43,7 +44,27 @@ static void tst1() {
} }
} }
static void tst2() {
std::ostringstream out;
serializer s(out);
mpz n1("-100000000000000000000000000000000000");
lean_assert(n1.is_neg());
mpz n2("0");
mpz n3("1200");
mpz n4("321");
s << n1 << n2 << n3 << n4;
std::istringstream in(out.str());
deserializer d(in);
mpz m1, m2, m3, m4;
d >> m1 >> m2 >> m3 >> m4;
lean_assert(n1 == m1);
lean_assert(n2 == m2);
lean_assert(n3 == m3);
lean_assert(n4 == m4);
}
int main() { int main() {
tst1(); tst1();
tst2();
return has_violations() ? 1 : 0; return has_violations() ? 1 : 0;
} }

View file

@ -125,6 +125,17 @@ mpq const & numeric_traits<mpq>::zero() {
return g_zero; return g_zero;
} }
serializer & operator<<(serializer & s, mpq const & n) {
std::ostringstream out;
out << n;
s << out.str();
return s;
}
mpq read_mpq(deserializer & d) {
return mpq(d.read_string().c_str());
}
DECL_UDATA(mpq) DECL_UDATA(mpq)
template<int idx> template<int idx>

View file

@ -277,6 +277,10 @@ public:
static void atanh(mpq & ) { lean_unreachable(); } // LCOV_EXCL_LINE static void atanh(mpq & ) { lean_unreachable(); } // LCOV_EXCL_LINE
}; };
serializer & operator<<(serializer & s, mpq const & n);
mpq read_mpq(deserializer & d);
inline deserializer & operator>>(deserializer & d, mpq & n) { n = read_mpq(d); return d; }
UDATA_DEFS(mpq) UDATA_DEFS(mpq)
mpq to_mpq_ext(lua_State * L, int idx); mpq to_mpq_ext(lua_State * L, int idx);
void open_mpq(lua_State * L); void open_mpq(lua_State * L);

View file

@ -84,6 +84,17 @@ mpz const & numeric_traits<mpz>::zero() {
return g_zero; return g_zero;
} }
serializer & operator<<(serializer & s, mpz const & n) {
std::ostringstream out;
out << n;
s << out.str();
return s;
}
mpz read_mpz(deserializer & d) {
return mpz(d.read_string().c_str());
}
DECL_UDATA(mpz) DECL_UDATA(mpz)
template<int idx> template<int idx>

View file

@ -9,6 +9,7 @@ Author: Leonardo de Moura
#include <iostream> #include <iostream>
#include "util/debug.h" #include "util/debug.h"
#include "util/lua.h" #include "util/lua.h"
#include "util/serializer.h"
#include "util/numerics/numeric_traits.h" #include "util/numerics/numeric_traits.h"
namespace lean { namespace lean {
@ -236,6 +237,10 @@ public:
static mpz const & zero(); static mpz const & zero();
}; };
serializer & operator<<(serializer & s, mpz const & n);
mpz read_mpz(deserializer & d);
inline deserializer & operator>>(deserializer & d, mpz & n) { n = read_mpz(d); return d; }
UDATA_DEFS(mpz) UDATA_DEFS(mpz)
mpz to_mpz_ext(lua_State * L, int idx); mpz to_mpz_ext(lua_State * L, int idx);
void open_mpz(lua_State * L); void open_mpz(lua_State * L);