Planeshift
|
00001 #ifndef ONCE_FP_GMP_INT_HH_ 00002 #define ONCE_FP_GMP_INT_HH_ 00003 00004 #include <iostream> 00005 00006 class GmpInt 00007 { 00008 public: 00009 /* A default of 256 bits will be used for all newly-instantiated GmpInt 00010 objects. This default can be changed with the function below. 00011 */ 00012 static void setDefaultNumberOfBits(unsigned long); 00013 static unsigned long getDefaultNumberOfBits(); 00014 00015 GmpInt(); 00016 GmpInt(long value); 00017 GmpInt(unsigned long value); 00018 GmpInt(int value); 00019 GmpInt(double value); 00020 GmpInt(long double value); 00021 00022 GmpInt(const GmpInt&); 00023 GmpInt& operator=(const GmpInt&); 00024 GmpInt& operator=(signed long value); 00025 00026 ~GmpInt(); 00027 00028 00029 /* This function can be used to retrieve the raw mpz_t data structure 00030 used by this object. (The template trick is used to avoid a dependency 00031 of this header file with <gmp.h>.) 00032 In other words, it can be called like: 00033 00034 mpz_t raw_mpz_data; 00035 intValue.get_raw_mpz_data(raw_mpz_data); 00036 00037 Note that the returned mpz_t should be considered as read-only and 00038 not be modified from the outside because it may be shared among 00039 several objects. If the calling code needs to modify the data, it 00040 should copy it for itself first with the appropriate GMP library 00041 functions. 00042 */ 00043 template<typename Mpz_t> 00044 void get_raw_mpfr_data(Mpz_t& dest_mpz_t); 00045 00046 00047 // Note that the returned char* points to an internal (shared) buffer 00048 // which will be valid until the next time this function is called 00049 // (by any object). 00050 const char* getAsString(int base = 10) const; 00051 long toInt() const; 00052 00053 GmpInt& operator+=(const GmpInt&); 00054 GmpInt& operator+=(long); 00055 GmpInt& operator-=(const GmpInt&); 00056 GmpInt& operator-=(long); 00057 GmpInt& operator*=(const GmpInt&); 00058 GmpInt& operator*=(long); 00059 GmpInt& operator/=(const GmpInt&); 00060 GmpInt& operator/=(long); 00061 GmpInt& operator%=(const GmpInt&); 00062 GmpInt& operator%=(long); 00063 00064 GmpInt& operator<<=(unsigned long); 00065 GmpInt& operator>>=(unsigned long); 00066 00067 // Equivalent to "+= value1 * value2;" 00068 void addProduct(const GmpInt& value1, const GmpInt& value2); 00069 void addProduct(const GmpInt& value1, unsigned long value2); 00070 00071 // Equivalent to "-= value1 * value2;" 00072 void subProduct(const GmpInt& value1, const GmpInt& value2); 00073 void subProduct(const GmpInt& value1, unsigned long value2); 00074 00075 void negate(); 00076 void abs(); 00077 static GmpInt abs(const GmpInt&); 00078 00079 GmpInt operator+(const GmpInt&) const; 00080 GmpInt operator+(long) const; 00081 GmpInt operator-(const GmpInt&) const; 00082 GmpInt operator-(long) const; 00083 GmpInt operator*(const GmpInt&) const; 00084 GmpInt operator*(long) const; 00085 GmpInt operator/(const GmpInt&) const; 00086 GmpInt operator/(long) const; 00087 GmpInt operator%(const GmpInt&) const; 00088 GmpInt operator%(long) const; 00089 00090 GmpInt operator-() const; 00091 00092 GmpInt operator<<(unsigned long) const; 00093 GmpInt operator>>(unsigned long) const; 00094 00095 bool operator<(const GmpInt&) const; 00096 bool operator<(long) const; 00097 bool operator<=(const GmpInt&) const; 00098 bool operator<=(long) const; 00099 bool operator>(const GmpInt&) const; 00100 bool operator>(long) const; 00101 bool operator>=(const GmpInt&) const; 00102 bool operator>=(long) const; 00103 bool operator==(const GmpInt&) const; 00104 bool operator==(long) const; 00105 bool operator!=(const GmpInt&) const; 00106 bool operator!=(long) const; 00107 00108 void parseValue(const char* value); 00109 void parseValue(const char* value, char** endptr); 00110 static GmpInt parseString(const char* str, char** endptr); 00111 00112 00113 private: 00114 struct GmpIntData; 00115 class GmpIntDataContainer; 00116 00117 GmpIntData* mData; 00118 00119 enum DummyType { kNoInitialization }; 00120 GmpInt(DummyType); 00121 00122 void copyIfShared(); 00123 static GmpIntDataContainer& gmpIntDataContainer(); 00124 00125 friend GmpInt operator+(long lhs, const GmpInt& rhs); 00126 friend GmpInt operator-(long lhs, const GmpInt& rhs); 00127 }; 00128 00129 GmpInt operator+(long lhs, const GmpInt& rhs); 00130 GmpInt operator-(long lhs, const GmpInt& rhs); 00131 GmpInt operator*(long lhs, const GmpInt& rhs); 00132 GmpInt operator/(long lhs, const GmpInt& rhs); 00133 GmpInt operator%(long lhs, const GmpInt& rhs); 00134 00135 inline bool operator<(long lhs, const GmpInt& rhs) { return rhs > lhs; } 00136 inline bool operator<=(long lhs, const GmpInt& rhs) { return rhs >= lhs; } 00137 inline bool operator>(long lhs, const GmpInt& rhs) { return rhs < lhs; } 00138 inline bool operator>=(long lhs, const GmpInt& rhs) { return rhs <= lhs; } 00139 inline bool operator==(long lhs, const GmpInt& rhs) { return rhs == lhs; } 00140 inline bool operator!=(long lhs, const GmpInt& rhs) { return rhs != lhs; } 00141 00142 inline std::ostream& operator<<(std::ostream& os, const GmpInt& value) 00143 { 00144 os << value.getAsString(); 00145 return os; 00146 } 00147 00148 #endif