SVG text and line elements
[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() {mpz_clear(m_op);}
19                 
20                 operator int64_t() const {return mpz_get_si(m_op);}
21                 operator uint64_t() const {return mpz_get_ui(m_op);}
22                 operator double() const {return mpz_get_d(m_op);}
23                 std::string Str(int base = 10) const
24                 {
25                         //TODO: Make less hacky, if we care.
26                         char * buff = mpz_get_str(NULL, 10, m_op);
27                         std::string result(buff);
28                         free(buff);
29                         return result;
30                 }
31                 
32                 Gmpint & operator=(const Gmpint & equ) {mpz_set(m_op, equ.m_op); return *this;}
33                 Gmpint & operator+=(const Gmpint & add) {mpz_add(m_op, m_op, add.m_op); return *this;}
34                 Gmpint & operator-=(const Gmpint & sub) {mpz_sub(m_op, m_op, sub.m_op); return *this;}
35                 Gmpint & operator*=(const Gmpint & mul) {mpz_mul(m_op, m_op, mul.m_op); return *this;}
36                 Gmpint & operator/=(const Gmpint & div) {mpz_div(m_op, m_op, div.m_op); return *this;}
37                 
38                 Gmpint operator+(const Gmpint & add) const {Gmpint a(*this); a += add; return a;}
39                 Gmpint operator-(const Gmpint & sub) const {Gmpint a(*this); a -= sub; return a;}
40                 Gmpint operator*(const Gmpint & mul) const {Gmpint a(*this); a *= mul; return a;}
41                 Gmpint operator/(const Gmpint & div) const {Gmpint a(*this); a /= div; return a;}
42                 Gmpint operator%(const Gmpint & div) const {Gmpint a(*this); mpz_mod(a.m_op, a.m_op, div.m_op); return a;}
43                 Gmpint operator-() const {return (Gmpint(0L)-*this);}
44                 
45                 
46                 bool operator==(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) == 0;}
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                 
53                 Gmpint Abs() const {Gmpint a(*this); mpz_abs(a.m_op, a.m_op); return a;}
54                 
55                 
56         private:
57                 mpz_t m_op;
58 };      
59
60
61
62
63 #endif //_GMPINT_H

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