Planeshift
GmpInt.h
Go to the documentation of this file.
1 #ifndef ONCE_FP_GMP_INT_HH_
2 #define ONCE_FP_GMP_INT_HH_
3 
4 #include <iostream>
5 
6 class GmpInt
7 {
8  public:
9  /* A default of 256 bits will be used for all newly-instantiated GmpInt
10  objects. This default can be changed with the function below.
11  */
12  static void setDefaultNumberOfBits(unsigned long);
13  static unsigned long getDefaultNumberOfBits();
14 
15  GmpInt();
16  GmpInt(long value);
17  GmpInt(unsigned long value);
18  GmpInt(int value);
19  GmpInt(double value);
20  GmpInt(long double value);
21 
22  GmpInt(const GmpInt&);
23  GmpInt& operator=(const GmpInt&);
24  GmpInt& operator=(signed long value);
25 
26  ~GmpInt();
27 
28 
29  /* This function can be used to retrieve the raw mpz_t data structure
30  used by this object. (The template trick is used to avoid a dependency
31  of this header file with <gmp.h>.)
32  In other words, it can be called like:
33 
34  mpz_t raw_mpz_data;
35  intValue.get_raw_mpz_data(raw_mpz_data);
36 
37  Note that the returned mpz_t should be considered as read-only and
38  not be modified from the outside because it may be shared among
39  several objects. If the calling code needs to modify the data, it
40  should copy it for itself first with the appropriate GMP library
41  functions.
42  */
43  template<typename Mpz_t>
44  void get_raw_mpfr_data(Mpz_t& dest_mpz_t);
45 
46 
47  // Note that the returned char* points to an internal (shared) buffer
48  // which will be valid until the next time this function is called
49  // (by any object).
50  const char* getAsString(int base = 10) const;
51  long toInt() const;
52 
53  GmpInt& operator+=(const GmpInt&);
54  GmpInt& operator+=(long);
55  GmpInt& operator-=(const GmpInt&);
56  GmpInt& operator-=(long);
57  GmpInt& operator*=(const GmpInt&);
58  GmpInt& operator*=(long);
59  GmpInt& operator/=(const GmpInt&);
60  GmpInt& operator/=(long);
61  GmpInt& operator%=(const GmpInt&);
62  GmpInt& operator%=(long);
63 
64  GmpInt& operator<<=(unsigned long);
65  GmpInt& operator>>=(unsigned long);
66 
67  // Equivalent to "+= value1 * value2;"
68  void addProduct(const GmpInt& value1, const GmpInt& value2);
69  void addProduct(const GmpInt& value1, unsigned long value2);
70 
71  // Equivalent to "-= value1 * value2;"
72  void subProduct(const GmpInt& value1, const GmpInt& value2);
73  void subProduct(const GmpInt& value1, unsigned long value2);
74 
75  void negate();
76  void abs();
77  static GmpInt abs(const GmpInt&);
78 
79  GmpInt operator+(const GmpInt&) const;
80  GmpInt operator+(long) const;
81  GmpInt operator-(const GmpInt&) const;
82  GmpInt operator-(long) const;
83  GmpInt operator*(const GmpInt&) const;
84  GmpInt operator*(long) const;
85  GmpInt operator/(const GmpInt&) const;
86  GmpInt operator/(long) const;
87  GmpInt operator%(const GmpInt&) const;
88  GmpInt operator%(long) const;
89 
90  GmpInt operator-() const;
91 
92  GmpInt operator<<(unsigned long) const;
93  GmpInt operator>>(unsigned long) const;
94 
95  bool operator<(const GmpInt&) const;
96  bool operator<(long) const;
97  bool operator<=(const GmpInt&) const;
98  bool operator<=(long) const;
99  bool operator>(const GmpInt&) const;
100  bool operator>(long) const;
101  bool operator>=(const GmpInt&) const;
102  bool operator>=(long) const;
103  bool operator==(const GmpInt&) const;
104  bool operator==(long) const;
105  bool operator!=(const GmpInt&) const;
106  bool operator!=(long) const;
107 
108  void parseValue(const char* value);
109  void parseValue(const char* value, char** endptr);
110  static GmpInt parseString(const char* str, char** endptr);
111 
112 
113  private:
114  struct GmpIntData;
115  class GmpIntDataContainer;
116 
117  GmpIntData* mData;
118 
119  enum DummyType { kNoInitialization };
120  GmpInt(DummyType);
121 
122  void copyIfShared();
123  static GmpIntDataContainer& gmpIntDataContainer();
124 
125  friend GmpInt operator+(long lhs, const GmpInt& rhs);
126  friend GmpInt operator-(long lhs, const GmpInt& rhs);
127 };
128 
129 GmpInt operator+(long lhs, const GmpInt& rhs);
130 GmpInt operator-(long lhs, const GmpInt& rhs);
131 GmpInt operator*(long lhs, const GmpInt& rhs);
132 GmpInt operator/(long lhs, const GmpInt& rhs);
133 GmpInt operator%(long lhs, const GmpInt& rhs);
134 
135 inline bool operator<(long lhs, const GmpInt& rhs) { return rhs > lhs; }
136 inline bool operator<=(long lhs, const GmpInt& rhs) { return rhs >= lhs; }
137 inline bool operator>(long lhs, const GmpInt& rhs) { return rhs < lhs; }
138 inline bool operator>=(long lhs, const GmpInt& rhs) { return rhs <= lhs; }
139 inline bool operator==(long lhs, const GmpInt& rhs) { return rhs == lhs; }
140 inline bool operator!=(long lhs, const GmpInt& rhs) { return rhs != lhs; }
141 
142 inline std::ostream& operator<<(std::ostream& os, const GmpInt& value)
143 {
144  os << value.getAsString();
145  return os;
146 }
147 
148 #endif
GmpInt & operator>>=(unsigned long)
GmpInt & operator/=(const GmpInt &)
GmpInt operator>>(unsigned long) const
GmpInt operator<<(unsigned long) const
bool operator<=(const GmpInt &) const
long toInt() const
GmpInt & operator+=(const GmpInt &)
GmpInt operator*(const GmpInt &) const
GmpInt & operator*=(const GmpInt &)
GmpInt operator%(const GmpInt &) const
GmpInt & operator=(const GmpInt &)
void negate()
GmpInt operator-() const
GmpInt operator/(const GmpInt &) const
bool operator==(const GmpInt &) const
void subProduct(const GmpInt &value1, const GmpInt &value2)
void get_raw_mpfr_data(Mpz_t &dest_mpz_t)
bool operator>(const GmpInt &) const
Definition: GmpInt.h:6
bool operator<(const GmpInt &) const
GmpInt operator+(const GmpInt &) const
GmpInt & operator%=(const GmpInt &)
GmpInt & operator-=(const GmpInt &)
static unsigned long getDefaultNumberOfBits()
static GmpInt parseString(const char *str, char **endptr)
void abs()
void parseValue(const char *value)
GmpInt & operator<<=(unsigned long)
bool operator>=(const GmpInt &) const
void addProduct(const GmpInt &value1, const GmpInt &value2)
bool operator!=(const GmpInt &) const
const char * getAsString(int base=10) const
static void setDefaultNumberOfBits(unsigned long)