| 1 | /* |
| 2 | * Matt McCutchen's Big Integer Library |
| 3 | */ |
| 4 | |
| 5 | #ifndef BIGINTEGER |
| 6 | #define BIGINTEGER |
| 7 | |
| 8 | #include "BigUnsigned.hh" |
| 9 | |
| 10 | /* |
| 11 | * A BigInteger object represents a signed integer of size |
| 12 | * limited only by available memory. A BigInteger can be |
| 13 | * created from and converted back to most integral types, |
| 14 | * and many math operations are defined on BigIntegers. |
| 15 | * |
| 16 | * The number is stored as a series of blocks in a |
| 17 | * dynamically allocated array. It is as if the number |
| 18 | * were written digit by digit in base 2 ^ N, **where N is the |
| 19 | * number of bits in an unsigned long.** |
| 20 | * |
| 21 | * This class is derived from BigUnsigned, which represents |
| 22 | * a large nonnegative integer. BigUnsigned should be studied |
| 23 | * first, as only new or different things are declared here. |
| 24 | * Some things are redeclared so that they use the BigInteger |
| 25 | * versions of methods, rather than the BigUnsigned versions. |
| 26 | */ |
| 27 | |
| 28 | class BigInteger : public BigUnsigned { |
| 29 | |
| 30 | // TYPES & CONSTANTS |
| 31 | public: |
| 32 | enum Sign { negative = -1, zero = 0, positive = 1 }; // Enumeration for the sign of a BigInteger |
| 33 | |
| 34 | // FIELDS |
| 35 | protected: |
| 36 | Sign sign; // The sign of this BigInteger |
| 37 | |
| 38 | // MANAGEMENT |
| 39 | protected: |
| 40 | BigInteger(Sign s, Index c) : BigUnsigned(0, c), sign(s) {}; // Creates a BigInteger with a sign and capacity |
| 41 | public: |
| 42 | |
| 43 | BigInteger() : BigUnsigned(), sign(zero) {} // Default constructor (value is 0) |
| 44 | BigInteger(const BigInteger &x) : BigUnsigned(x), sign(x.sign) {}; // Copy constructor |
| 45 | void operator=(const BigInteger &x); // Assignment operator |
| 46 | BigInteger(const Blk *b, Index l) : BigUnsigned(b, l) { // Constructor from an array of blocks |
| 47 | sign = (len == 0) ? zero : positive; |
| 48 | } |
| 49 | BigInteger(const Blk *b, Index l, Sign s); // Constructor from an array of blocks and a sign |
| 50 | BigInteger(const BigUnsigned &x) : BigUnsigned(x) { // Constructor from a BigUnsigned |
| 51 | sign = (len == 0) ? zero : positive; |
| 52 | } |
| 53 | BigInteger(const BigUnsigned &x, Sign s); // Constructor from a BigUnsigned and a sign |
| 54 | // Constructors from integral types |
| 55 | BigInteger(unsigned long x); |
| 56 | BigInteger( long x); |
| 57 | BigInteger(unsigned int x); |
| 58 | BigInteger( int x); |
| 59 | BigInteger(unsigned short x); |
| 60 | BigInteger( short x); |
| 61 | // Note that a BigInteger can be converted to a BigUnsigned |
| 62 | // automatically; this takes its absolute value. |
| 63 | |
| 64 | // CONVERTERS to integral types |
| 65 | public: |
| 66 | operator unsigned long () const; |
| 67 | operator long () const; |
| 68 | operator unsigned int () const; |
| 69 | operator int () const; |
| 70 | operator unsigned short() const; |
| 71 | operator short() const; |
| 72 | |
| 73 | // PICKING APART |
| 74 | // These accessors can be used to get the pieces of the number |
| 75 | public: |
| 76 | Sign getSign() const; |
| 77 | |
| 78 | // COMPARISONS |
| 79 | public: |
| 80 | // Compares this to x like Perl's <=> |
| 81 | CmpRes compareTo(const BigInteger &x) const; |
| 82 | // Normal comparison operators |
| 83 | bool operator ==(const BigInteger &x) const { |
| 84 | return sign == x.sign && BigUnsigned::operator ==(x); |
| 85 | } |
| 86 | bool operator !=(const BigInteger &x) const { return !operator ==(x); }; |
| 87 | bool operator < (const BigInteger &x) const { return compareTo(x) == less ; } |
| 88 | bool operator <=(const BigInteger &x) const { return compareTo(x) != greater; } |
| 89 | bool operator >=(const BigInteger &x) const { return compareTo(x) != less ; } |
| 90 | bool operator > (const BigInteger &x) const { return compareTo(x) == greater; } |
| 91 | |
| 92 | // PUT-HERE OPERATIONS |
| 93 | /* These store the result of the operation on the arguments into this. |
| 94 | * a.add(b, c) is equivalent to, but faster than, a = b + c. |
| 95 | * See explanation of "put-here operations" in BigUnsigned.cc . */ |
| 96 | public: |
| 97 | void add (const BigInteger &a, const BigInteger &b); // Addition |
| 98 | void subtract(const BigInteger &a, const BigInteger &b); // Subtraction |
| 99 | void multiply(const BigInteger &a, const BigInteger &b); // Multiplication |
| 100 | /* Divisive stuff |
| 101 | * `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'. |
| 102 | * Semantics similar to Donald E. Knuth's are used for / and %, |
| 103 | * and these usually differ from the semantics of primitive-type |
| 104 | * / and % when negatives and/or zeroes are involved. |
| 105 | * Look in `BigInteger.cc' for details. |
| 106 | * `a.divideWithRemainder(b, a)' causes an exception: it doesn't make |
| 107 | * sense to write quotient and remainder into the same variable. |
| 108 | */ |
| 109 | void divideWithRemainder(const BigInteger &b, BigInteger &q); |
| 110 | void divide(const BigInteger &a, const BigInteger &b) { |
| 111 | BigInteger a2(a); |
| 112 | a2.divideWithRemainder(b, *this); |
| 113 | // quotient now in *this |
| 114 | // don't care about remainder left in a2 |
| 115 | } |
| 116 | void modulo(const BigInteger &a, const BigInteger &b) { |
| 117 | *this = a; |
| 118 | BigInteger q; |
| 119 | divideWithRemainder(b, q); |
| 120 | // remainder now in *this |
| 121 | // don't care about quotient left in q |
| 122 | } |
| 123 | void negate(const BigInteger &a); // Negative |
| 124 | // Some operations are inherently unsigned and are not |
| 125 | // redefined for BigIntegers. Calling one of these on |
| 126 | // a BigInteger will convert it to a BigUnsigned, |
| 127 | // which takes its absolute value. |
| 128 | |
| 129 | // NORMAL OPERATORS |
| 130 | // These perform the operation on this (to the left of the operator) |
| 131 | // and x (to the right of the operator) and return a new BigInteger with the result. |
| 132 | public: |
| 133 | BigInteger operator +(const BigInteger &x) const; // Addition |
| 134 | BigInteger operator -(const BigInteger &x) const; // Subtraction |
| 135 | BigInteger operator *(const BigInteger &x) const; // Multiplication |
| 136 | BigInteger operator /(const BigInteger &x) const; // Division |
| 137 | BigInteger operator %(const BigInteger &x) const; // Modular reduction |
| 138 | BigInteger operator -( ) const; // Negative |
| 139 | |
| 140 | // ASSIGNMENT OPERATORS |
| 141 | // These perform the operation on this and x, storing the result into this. |
| 142 | public: |
| 143 | void operator +=(const BigInteger &x); // Addition |
| 144 | void operator -=(const BigInteger &x); // Subtraction |
| 145 | void operator *=(const BigInteger &x); // Multiplication |
| 146 | void operator /=(const BigInteger &x); // Division |
| 147 | void operator %=(const BigInteger &x); // Modular reduction |
| 148 | void flipSign(); // Negative |
| 149 | |
| 150 | // INCREMENT/DECREMENT OPERATORS |
| 151 | // These increase or decrease the number by 1. To discourage side effects, |
| 152 | // these do not return *this, so prefix and postfix behave the same. |
| 153 | public: |
| 154 | void operator ++( ); // Prefix increment |
| 155 | void operator ++(int); // Postfix decrement |
| 156 | void operator --( ); // Prefix increment |
| 157 | void operator --(int); // Postfix decrement |
| 158 | |
| 159 | }; |
| 160 | |
| 161 | // PICKING APART |
| 162 | inline BigInteger::Sign BigInteger::getSign() const { return sign; } |
| 163 | |
| 164 | // NORMAL OPERATORS |
| 165 | /* These create an object to hold the result and invoke |
| 166 | * the appropriate put-here operation on it, passing |
| 167 | * this and x. The new object is then returned. */ |
| 168 | inline BigInteger BigInteger::operator +(const BigInteger &x) const { |
| 169 | BigInteger ans; |
| 170 | ans.add(*this, x); |
| 171 | return ans; |
| 172 | } |
| 173 | inline BigInteger BigInteger::operator -(const BigInteger &x) const { |
| 174 | BigInteger ans; |
| 175 | ans.subtract(*this, x); |
| 176 | return ans; |
| 177 | } |
| 178 | inline BigInteger BigInteger::operator *(const BigInteger &x) const { |
| 179 | BigInteger ans; |
| 180 | ans.multiply(*this, x); |
| 181 | return ans; |
| 182 | } |
| 183 | inline BigInteger BigInteger::operator /(const BigInteger &x) const { |
| 184 | BigInteger ans; |
| 185 | ans.divide(*this, x); |
| 186 | return ans; |
| 187 | } |
| 188 | inline BigInteger BigInteger::operator %(const BigInteger &x) const { |
| 189 | BigInteger ans; |
| 190 | ans.modulo(*this, x); |
| 191 | return ans; |
| 192 | } |
| 193 | inline BigInteger BigInteger::operator -() const { |
| 194 | BigInteger ans; |
| 195 | ans.negate(*this); |
| 196 | return ans; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * ASSIGNMENT OPERATORS |
| 201 | * |
| 202 | * Now the responsibility for making a temporary copy if necessary |
| 203 | * belongs to the put-here operations. See Assignment Operators in |
| 204 | * BigUnsigned.hh. |
| 205 | */ |
| 206 | inline void BigInteger::operator +=(const BigInteger &x) { |
| 207 | add(*this, x); |
| 208 | } |
| 209 | inline void BigInteger::operator -=(const BigInteger &x) { |
| 210 | subtract(*this, x); |
| 211 | } |
| 212 | inline void BigInteger::operator *=(const BigInteger &x) { |
| 213 | multiply(*this, x); |
| 214 | } |
| 215 | inline void BigInteger::operator /=(const BigInteger &x) { |
| 216 | // Updated for divideWithRemainder |
| 217 | BigInteger thisCopy(*this); |
| 218 | thisCopy.divideWithRemainder(x, *this); |
| 219 | // quotient left in *this |
| 220 | // don't care about remainder left in thisCopy |
| 221 | } |
| 222 | inline void BigInteger::operator %=(const BigInteger &x) { |
| 223 | // Shortcut (woohoo!) |
| 224 | BigInteger q; |
| 225 | divideWithRemainder(x, q); |
| 226 | // remainder left in *this |
| 227 | // don't care about quotient left in q |
| 228 | } |
| 229 | // This one is trivial |
| 230 | inline void BigInteger::flipSign() { |
| 231 | sign = Sign(-sign); |
| 232 | } |
| 233 | |
| 234 | #endif |