Change Rational<Arbint> -> Rational<Gmpint>
[ipdf/code.git] / src / gmpint.h
1 /**
2  * @file gmpint.h
3  * @brief Wraps to GMP mpz_t type using inlines
4  */
5
6 #ifndef _GMPINT_H
7 #define _GMPINT_H
8
9 #include <gmp.h>
10 #include <string>
11
12 class Gmpint
13 {
14         public:
15                 Gmpint(int64_t i) {mpz_init_set_si(m_op, i);}
16                 Gmpint(const std::string & str, int base=10) {mpz_init_set_str(m_op, str.c_str(), base);}
17                 Gmpint(const Gmpint & cpy) {mpz_init(m_op); mpz_set(m_op, cpy.m_op);}
18                 virtual ~Gmpint() {} //TODO: Do we need to delete m_op somehow?
19                 
20                 
21                 operator int64_t() const {return mpz_get_si(m_op);}
22                 operator uint64_t() const {return mpz_get_ui(m_op);}
23                 operator double() const {return mpz_get_d(m_op);}
24                 std::string Str(int base = 10) const
25                 {
26                         //TODO: Make less hacky, if we care.
27                         char * buff = mpz_get_str(NULL, 10, m_op);
28                         std::string result(buff);
29                         free(buff);
30                         return result;
31                 }
32                 
33                 Gmpint & operator=(const Gmpint & equ) {mpz_set(m_op, equ.m_op); return *this;}
34                 Gmpint & operator+=(const Gmpint & add) {mpz_add(m_op, m_op, add.m_op); return *this;}
35                 Gmpint & operator-=(const Gmpint & sub) {mpz_sub(m_op, m_op, sub.m_op); return *this;}
36                 Gmpint & operator*=(const Gmpint & mul) {mpz_mul(m_op, m_op, mul.m_op); return *this;}
37                 Gmpint & operator/=(const Gmpint & div) {mpz_div(m_op, m_op, div.m_op); return *this;}
38                 
39                 Gmpint operator+(const Gmpint & add) const {Gmpint a(*this); a += add; return a;}
40                 Gmpint operator-(const Gmpint & sub) const {Gmpint a(*this); a -= sub; return a;}
41                 Gmpint operator*(const Gmpint & mul) const {Gmpint a(*this); a *= mul; return a;}
42                 Gmpint operator/(const Gmpint & div) const {Gmpint a(*this); a /= div; return a;}
43                 Gmpint operator%(const Gmpint & div) const {Gmpint a(*this); mpz_mod(a.m_op, a.m_op, div.m_op); return a;}
44                 Gmpint operator-() const {return (Gmpint(0L)-*this);}
45                 
46                 
47                 bool operator==(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) == 0;}
48                 bool operator!=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) != 0;}
49                 bool operator<(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) < 0;}
50                 bool operator>(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) > 0;}
51                 bool operator<=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) <= 0;}
52                 bool operator>=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) >= 0;}
53                 
54                 Gmpint Abs() const {Gmpint a(*this); mpz_abs(a.m_op, a.m_op); return a;}
55                 
56                 
57         private:
58                 Gmpint(const mpz_t & op) : m_op(op) {}
59                 mpz_t m_op;
60 };      
61
62
63
64
65 #endif //_GMPINT_H

UCC git Repository :: git.ucc.asn.au