Commit | Line | Data |
---|---|---|
e67d6049 MM |
1 | /* |
2 | * Matt McCutchen's Big Integer Library | |
e67d6049 MM |
3 | */ |
4 | ||
5 | #ifndef BIGINTEGER | |
6 | #define BIGINTEGER | |
7 | ||
05780f4b | 8 | #include "BigUnsigned.hh" |
e67d6049 MM |
9 | |
10 | /* | |
e67d6049 MM |
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, | |
05780f4b | 14 | * and many math operations are defined on BigIntegers. |
e67d6049 MM |
15 | * |
16 | * The number is stored as a series of blocks in a | |
4efbb076 MM |
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.** | |
e67d6049 MM |
20 | * |
21 | * This class is derived from BigUnsigned, which represents | |
05780f4b | 22 | * a large nonnegative integer. BigUnsigned should be studied |
e67d6049 MM |
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: | |
05780f4b | 40 | BigInteger(Sign s, Index c) : BigUnsigned(0, c), sign(s) {}; // Creates a BigInteger with a sign and capacity |
e67d6049 | 41 | public: |
05780f4b MM |
42 | |
43 | BigInteger() : BigUnsigned(), sign(zero) {} // Default constructor (value is 0) | |
44 | BigInteger(const BigInteger &x) : BigUnsigned(x), sign(x.sign) {}; // Copy constructor | |
e67d6049 | 45 | void operator=(const BigInteger &x); // Assignment operator |
05780f4b MM |
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 | } | |
e67d6049 MM |
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 | |
05780f4b MM |
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; } | |
e67d6049 MM |
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. | |
8c16728a | 95 | * See explanation of "put-here operations" in BigUnsigned.cc . */ |
e67d6049 MM |
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 | |
05780f4b MM |
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. | |
8c16728a MM |
106 | * `a.divideWithRemainder(b, a)' causes an exception: it doesn't make |
107 | * sense to write quotient and remainder into the same variable. | |
05780f4b MM |
108 | */ |
109 | void divideWithRemainder(const BigInteger &b, BigInteger &q); | |
110 | void divide(const BigInteger &a, const BigInteger &b) { | |
05780f4b MM |
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) { | |
05780f4b MM |
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 | |
e67d6049 MM |
124 | // Some operations are inherently unsigned and are not |
125 | // redefined for BigIntegers. Calling one of these on | |
05780f4b MM |
126 | // a BigInteger will convert it to a BigUnsigned, |
127 | // which takes its absolute value. | |
e67d6049 MM |
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 | ||
e67d6049 MM |
161 | // PICKING APART |
162 | inline BigInteger::Sign BigInteger::getSign() const { return sign; } | |
163 | ||
e67d6049 MM |
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 | ||
8c16728a MM |
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 | */ | |
e67d6049 | 206 | inline void BigInteger::operator +=(const BigInteger &x) { |
8c16728a | 207 | add(*this, x); |
e67d6049 MM |
208 | } |
209 | inline void BigInteger::operator -=(const BigInteger &x) { | |
8c16728a | 210 | subtract(*this, x); |
e67d6049 MM |
211 | } |
212 | inline void BigInteger::operator *=(const BigInteger &x) { | |
8c16728a | 213 | multiply(*this, x); |
e67d6049 MM |
214 | } |
215 | inline void BigInteger::operator /=(const BigInteger &x) { | |
05780f4b | 216 | // Updated for divideWithRemainder |
e67d6049 | 217 | BigInteger thisCopy(*this); |
05780f4b MM |
218 | thisCopy.divideWithRemainder(x, *this); |
219 | // quotient left in *this | |
220 | // don't care about remainder left in thisCopy | |
e67d6049 MM |
221 | } |
222 | inline void BigInteger::operator %=(const BigInteger &x) { | |
05780f4b MM |
223 | // Shortcut (woohoo!) |
224 | BigInteger q; | |
225 | divideWithRemainder(x, q); | |
226 | // remainder left in *this | |
227 | // don't care about quotient left in q | |
e67d6049 MM |
228 | } |
229 | // This one is trivial | |
230 | inline void BigInteger::flipSign() { | |
231 | sign = Sign(-sign); | |
232 | } | |
233 | ||
234 | #endif |