X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fgmpint.h;fp=src%2Fgmpint.h;h=11b53c53981457cc31d56d995f3d3866467b6c72;hp=0000000000000000000000000000000000000000;hb=8c88a35a1fb0dfc9e542bb1a1c13c5f3e36eb3e5;hpb=bf91437612eb9f28fce17553d41a61b39b19b3fc diff --git a/src/gmpint.h b/src/gmpint.h new file mode 100644 index 0000000..11b53c5 --- /dev/null +++ b/src/gmpint.h @@ -0,0 +1,66 @@ +/** + * @file gmpint.h + * @brief Wraps to GMP mpz_t type using inlines + */ + +#ifndef _GMPINT_H +#define _GMPINT_H + +#include +#include + +class Gmpint +{ + public: + Gmpint(int64_t i) {mpz_init_set_si(m_op, i);} + Gmpint(const std::string & str, int base=10) {mpz_init_set_str(m_op, str.c_str(), base);} + Gmpint(const Gmpint & cpy) {mpz_init(m_op); mpz_set(m_op, cpy.m_op);} + virtual ~Gmpint() {} //TODO: Do we need to delete m_op somehow? + + + operator int64_t() {return mpz_get_si(m_op);} + operator uint64_t() {return mpz_get_ui(m_op);} + operator double() {return mpz_get_d(m_op);} + std::string Str(int base = 10) const + { + //TODO: Make less hacky, if we care. + char * buff = mpz_get_str(NULL, 10, m_op); + std::string result(buff); + free(buff); + return result; + } + + Gmpint & operator=(const Gmpint & equ) {mpz_set(m_op, equ.m_op); return *this;} + Gmpint & operator+=(const Gmpint & add) {mpz_add(m_op, m_op, add.m_op); return *this;} + Gmpint & operator-=(const Gmpint & sub) {mpz_sub(m_op, m_op, sub.m_op); return *this;} + Gmpint & operator*=(const Gmpint & mul) {mpz_mul(m_op, m_op, mul.m_op); return *this;} + Gmpint & operator/=(const Gmpint & div) {mpz_div(m_op, m_op, div.m_op); return *this;} + + Gmpint operator+(const Gmpint & add) const {Gmpint a(*this); a += add; return a;} + Gmpint operator-(const Gmpint & sub) const {Gmpint a(*this); a -= sub; return a;} + Gmpint operator*(const Gmpint & mul) const {Gmpint a(*this); a *= mul; return a;} + Gmpint operator/(const Gmpint & div) const {Gmpint a(*this); a /= div; return a;} + Gmpint operator%(const Gmpint & div) const + {Gmpint a(*this); mpz_mod(a.m_op, a.m_op, div.m_op); return a;} + Gmpint operator-() const {return (Gmpint(0L)-*this);} + + + bool operator==(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) == 0;} + bool operator!=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) != 0;} + bool operator<(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) < 0;} + bool operator>(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) > 0;} + bool operator<=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) <= 0;} + bool operator>=(const Gmpint & cmp) const {return mpz_cmp(m_op, cmp.m_op) >= 0;} + + Gmpint Abs() const {Gmpint a(*this); mpz_abs(a.m_op, a.m_op); return a;} + + + private: + Gmpint(const mpz_t & op) : m_op(op) {} + mpz_t m_op; +}; + + + + +#endif //_GMPINT_H