[def863] | 1 | /* |
---|
| 2 | * lib_z.h |
---|
| 3 | * |
---|
| 4 | * Created on: Sep 28, 2010 |
---|
| 5 | * Author: anders |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #ifndef LIB_Z_H_ |
---|
| 9 | #define LIB_Z_H_ |
---|
| 10 | |
---|
| 11 | #include <string.h> |
---|
| 12 | #include <ostream> |
---|
| 13 | #include "gmp.h" |
---|
| 14 | |
---|
| 15 | namespace gfan{ |
---|
| 16 | class Rational; |
---|
| 17 | class Integer |
---|
| 18 | { |
---|
| 19 | friend class Rational; |
---|
| 20 | mpz_t value; |
---|
| 21 | public: |
---|
| 22 | static bool isField() |
---|
| 23 | { |
---|
| 24 | return false; |
---|
| 25 | } |
---|
| 26 | Integer() |
---|
| 27 | { |
---|
| 28 | mpz_init(value); |
---|
| 29 | } |
---|
| 30 | Integer(signed long int value_) |
---|
| 31 | { |
---|
| 32 | mpz_init(value); |
---|
| 33 | mpz_set_si(value,value_); |
---|
| 34 | } |
---|
| 35 | Integer(Integer const & value_) |
---|
| 36 | { |
---|
| 37 | mpz_init_set(value,value_.value); |
---|
| 38 | } |
---|
| 39 | Integer(mpz_t value_) |
---|
| 40 | { |
---|
| 41 | mpz_init_set(value,value_); |
---|
| 42 | } |
---|
| 43 | ~Integer() |
---|
| 44 | { |
---|
| 45 | mpz_clear(value); |
---|
| 46 | } |
---|
| 47 | Integer& operator=(const Integer& a) |
---|
| 48 | { |
---|
| 49 | const Integer *A=(const Integer*)&a; |
---|
| 50 | if (this != A) { |
---|
| 51 | mpz_clear(value); |
---|
| 52 | mpz_init_set(value, a.value); |
---|
| 53 | } |
---|
| 54 | return *this; |
---|
| 55 | } |
---|
| 56 | bool isZero()const{ |
---|
| 57 | return mpz_sgn(value)==0; |
---|
| 58 | } |
---|
| 59 | friend std::ostream &operator<<(std::ostream &f, Integer const &a) |
---|
| 60 | { |
---|
| 61 | void (*freefunc)(void *, size_t); |
---|
| 62 | mp_get_memory_functions(0,0,&freefunc); |
---|
| 63 | char *str=mpz_get_str(0,10,a.value); |
---|
| 64 | f<<str; |
---|
| 65 | freefunc(str,strlen(str)+1); |
---|
| 66 | return f; |
---|
| 67 | } |
---|
| 68 | Integer& operator+=(const Integer& a) |
---|
| 69 | { |
---|
| 70 | mpz_add(value,value,a.value); |
---|
| 71 | return *this; |
---|
| 72 | } |
---|
| 73 | Integer& operator-=(const Integer& a) |
---|
| 74 | { |
---|
| 75 | mpz_sub(value,value,a.value); |
---|
| 76 | return *this; |
---|
| 77 | } |
---|
| 78 | Integer& operator*=(const Integer& a) |
---|
| 79 | { |
---|
| 80 | mpz_mul(value,value,a.value); |
---|
| 81 | return *this; |
---|
| 82 | } |
---|
| 83 | Integer& operator/=(const Integer& a) |
---|
| 84 | { |
---|
| 85 | mpz_div(value,value,a.value); |
---|
| 86 | return *this; |
---|
| 87 | } |
---|
| 88 | friend Integer operator-(const Integer &b) |
---|
| 89 | { |
---|
| 90 | Integer ret; |
---|
| 91 | ret-=b; |
---|
| 92 | return ret; |
---|
| 93 | } |
---|
| 94 | Integer operator+(const Integer &a)const |
---|
| 95 | { |
---|
| 96 | Integer ret(*this); |
---|
| 97 | ret+=a; |
---|
| 98 | return ret; |
---|
| 99 | } |
---|
| 100 | Integer operator-(const Integer &a)const |
---|
| 101 | { |
---|
| 102 | Integer ret(*this); |
---|
| 103 | ret-=a; |
---|
| 104 | return ret; |
---|
| 105 | } |
---|
| 106 | Integer operator*(const Integer &a)const |
---|
| 107 | { |
---|
| 108 | Integer ret(*this); |
---|
| 109 | ret*=a; |
---|
| 110 | return ret; |
---|
| 111 | } |
---|
| 112 | Integer operator/(const Integer &a)const |
---|
| 113 | { |
---|
| 114 | Integer ret(*this); |
---|
| 115 | ret/=a; |
---|
| 116 | return ret; |
---|
| 117 | } |
---|
| 118 | void madd(const Integer &a,const Integer &b) |
---|
| 119 | { |
---|
| 120 | mpz_t temp; |
---|
| 121 | mpz_init(temp); |
---|
| 122 | mpz_mul(temp,a.value,b.value); |
---|
| 123 | mpz_add(value,value,temp); |
---|
| 124 | mpz_clear(temp); |
---|
| 125 | } |
---|
| 126 | bool operator<(const Integer &a)const |
---|
| 127 | { |
---|
| 128 | return mpz_cmp(value,a.value)<0; |
---|
| 129 | } |
---|
| 130 | bool operator==(const Integer &a)const |
---|
| 131 | { |
---|
| 132 | return mpz_cmp(value,a.value)==0; |
---|
| 133 | } |
---|
| 134 | bool operator!=(const Integer &a)const |
---|
| 135 | { |
---|
| 136 | return mpz_cmp(value,a.value)!=0; |
---|
| 137 | } |
---|
| 138 | int sign()const |
---|
| 139 | { |
---|
| 140 | return mpz_sgn(value); |
---|
| 141 | } |
---|
| 142 | static Integer gcd(Integer const &a, Integer const &b, Integer &s, Integer &t) |
---|
| 143 | { |
---|
| 144 | mpz_t r; |
---|
| 145 | mpz_init(r); |
---|
| 146 | mpz_gcdext(r,s.value,t.value,a.value,b.value); |
---|
| 147 | Integer ret(r); |
---|
| 148 | mpz_clear(r); |
---|
| 149 | return ret; |
---|
| 150 | } |
---|
| 151 | /** |
---|
| 152 | * Assigns the value to z. z must have been initialized as a gmp variable. |
---|
| 153 | */ |
---|
| 154 | void setGmp(mpz_t z)const |
---|
| 155 | { |
---|
| 156 | mpz_set(z,value); |
---|
| 157 | } |
---|
| 158 | /** |
---|
| 159 | * Returns a value which is useful for computing hash functions. |
---|
| 160 | */ |
---|
| 161 | signed long int hashValue()const |
---|
| 162 | { |
---|
| 163 | return mpz_get_si(value); |
---|
| 164 | } |
---|
[c4d065] | 165 | bool fitsInInt()const |
---|
| 166 | { |
---|
| 167 | mpz_t v; |
---|
| 168 | mpz_init(v); |
---|
| 169 | this->setGmp(v); |
---|
| 170 | bool ret=(mpz_fits_sint_p(v)!=0); |
---|
| 171 | mpz_clear(v); |
---|
| 172 | return ret; |
---|
| 173 | } |
---|
| 174 | int toInt()const |
---|
| 175 | { |
---|
| 176 | mpz_t v; |
---|
| 177 | mpz_init(v); |
---|
| 178 | this->setGmp(v); |
---|
| 179 | int ret=0; |
---|
| 180 | if(mpz_fits_sint_p(v)) |
---|
| 181 | ret=mpz_get_si(v); |
---|
| 182 | // else |
---|
| 183 | // ok=false; |
---|
| 184 | mpz_clear(v); |
---|
| 185 | return ret; |
---|
| 186 | } |
---|
[def863] | 187 | }; |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | #endif /* LIB_Z_H_ */ |
---|
| 195 | |
---|