如何序列化GMP有理数?

时间:2014-02-23 16:07:21

标签: c++ gmp

有没有办法以二进制形式序列化GMP有理数?我只看到用于读/写FILE的函数,但即便如此,它也是一种文本形式。我可以转换为分子/分母并使用整数函数,但同样只有FILE输出可用。我需要能够获取原始字节或写入C ++流。

2 个答案:

答案 0 :(得分:1)

我认为你在寻找

完整演示:

#include <cstdio>
#include <gmpxx.h>
#include <iostream>
#include <cassert>

int main ()
{
    mpq_class original("1731181/1762217");

    original *= original; original *= original; original *= original;
    original *= original; original *= original; original *= original; // make it a little more interesting...

    std::cout << original << "\n";

    {
        FILE* output = fopen("serialized.bin", "wb");
        mpz_out_raw(output, original.get_num_mpz_t());
        mpz_out_raw(output, original.get_den_mpz_t());
        fclose(output);
    }

    mpq_class restored;
    {
        FILE* input = fopen("serialized.bin", "rb");
        mpz_inp_raw(restored.get_num_mpz_t(), input);
        mpz_inp_raw(restored.get_den_mpz_t(), input);
        fclose(input);
    }

    assert(restored == original);
}

serialized.bin的内容:

0000000: 0000 00a6 4e66 c747 a50a f69b 331d 1fcf  ....Nf.G....3...
0000010: 5dd1 5d53 a179 7c2f b767 38a7 75d4 7ea0  ].]S.y|/.g8.u.~.
0000020: 0391 3f38 1b65 9ad8 4982 ecff b452 994d  ..?8.e..I....R.M
0000030: b473 0cf3 e949 556a 75e7 f713 83e9 a3c4  .s...IUju.......
0000040: 280b 5ec6 e394 dd85 3a8f 9277 8486 ad58  (.^.....:..w...X
0000050: 3797 7b45 0b06 25f3 b8cd ea5a 2b24 ef16  7.{E..%....Z+$..
0000060: 245a 8941 cf79 8107 c2c9 d471 9b80 c5bb  $Z.A.y.....q....
0000070: c2c2 cb0d 9838 4270 7788 098f 462e ced7  .....8Bpw...F...
0000080: b048 961b be6c c60a 6296 a269 8ff3 7264  .H...l..b..i..rd
0000090: 2e38 b373 ee0c 08b7 1e16 f36b 2562 9ff2  .8.s.......k%b..
00000a0: 22b1 d1bf 5997 17c3 6901 0000 00a6 f475  "...Y...i......u
00000b0: 90ed f827 9644 01f5 f80a b293 119a 212a  ...'.D........!*
00000c0: 3a32 7e76 30de 87a0 9f2d 4652 3a85 f83a  :2~v0....-FR:..:
00000d0: ae91 0755 ba5c 7712 7f60 5210 fa26 798a  ...U.\w..`R..&y.
00000e0: 9c82 f2ae 986f 6a14 3175 010c 50bc a705  .....oj.1u..P...
00000f0: 40ab 0824 9319 18ca 9592 b35c e4f9 7f9d  @..$.......\....
0000100: c25a ca49 a176 2aa1 06a2 399c 64b3 fdaa  .Z.I.v*...9.d...
0000110: 4c08 57fa 4bc5 4861 46ab 18c7 b48c ae91  L.W.K.HaF.......
0000120: ab01 a671 7073 fdae 0584 87e7 8919 f554  ...qps.........T
0000130: 4bfa c3ae 8831 daa7 0d10 ca77 963c b31a  K....1.....w.<..
0000140: 26ad cf1b 5b84 5112 0a93 7784 ca44 916f  &...[.Q...w..D.o
0000150: 81b5 a201                                ....

答案 1 :(得分:1)

使用mpz_exportmpz_import函数(感谢Marc指出这一点),我想出了下面的代码。这是包含number值的mpz_class类的一部分。

这确实表明GMP没有适当的导入/导出功能。以下是一种解决方法,而不是使用功能。

void number::write( std::ostream & out ) const {
    int8_t neg = value.get_num() < 0;
    out.write( (char*)&neg, sizeof(neg) );

    size_t c;
    void * raw = mpz_export( nullptr, &c, 1, 1, 0, 0, value.get_num().get_mpz_t() );
    out.write( (char*)&c, sizeof(c) );
    out.write( (char*)raw, c );
    free(raw);

    raw = mpz_export( nullptr, &c, 1, 1, 0, 0, value.get_den().get_mpz_t() );
    out.write( (char*)&c, sizeof(c) );
    out.write( (char*)raw, c );
    free(raw);
}

void number::read( std::istream & in ) {
    mpz_class num, den;
    size_t s;
    std::vector<uint8_t> v;

    int8_t neg;
    in.read( (char*)&neg, sizeof(neg) );

    in.read( (char*)&s, sizeof(s) );
    v.resize(s);
    in.read( (char*)&v[0], s );
    mpz_import( num.get_mpz_t(), s, 1, 1, 0, 0, &v[0] );

    in.read( (char*)&s, sizeof(s) );
    v.resize(s);
    in.read( (char*)&v[0], s );
    mpz_import( den.get_mpz_t(), s, 1, 1, 0, 0, &v[0] );

    value = mpq_class(num) / mpq_class(den);
    if( neg ) {
        value = -value;
    }
}