4 #include "NumberlikeArray.hh"
6 /* A BigUnsigned object represents a nonnegative integer of size limited only by
7 * available memory. BigUnsigneds support most mathematical operators and can
8 * be converted to and from most primitive integer types.
10 * The number is stored as a NumberlikeArray of unsigned longs as if it were
11 * written in base 256^sizeof(unsigned long). The least significant block is
12 * first, and the length is such that the most significant block is nonzero. */
13 class BigUnsigned : protected NumberlikeArray<unsigned long> {
16 // Enumeration for the result of a comparison.
17 enum CmpRes { less = -1, equal = 0, greater = 1 };
19 // BigUnsigneds are built with a Blk type of unsigned long.
20 typedef unsigned long Blk;
22 typedef NumberlikeArray<Blk>::Index Index;
23 NumberlikeArray<Blk>::N;
26 // Creates a BigUnsigned with a capacity; for internal use.
27 BigUnsigned(int, Index c) : NumberlikeArray<Blk>(0, c) {}
29 // Decreases len to eliminate any leading zero blocks.
30 void zapLeadingZeros() {
31 while (len > 0 && blk[len - 1] == 0)
37 BigUnsigned() : NumberlikeArray<Blk>() {}
40 BigUnsigned(const BigUnsigned &x) : NumberlikeArray<Blk>(x) {}
42 // Assignment operator
43 void operator=(const BigUnsigned &x) {
44 NumberlikeArray<Blk>::operator =(x);
47 // Constructor that copies from a given array of blocks.
48 BigUnsigned(const Blk *b, Index blen) : NumberlikeArray<Blk>(b, blen) {
49 // Eliminate any leading zeros we may have been passed.
53 // Destructor. NumberlikeArray does the delete for us.
56 // Constructors from primitive integer types
57 BigUnsigned(unsigned long x);
59 BigUnsigned(unsigned int x);
61 BigUnsigned(unsigned short x);
62 BigUnsigned( short x);
65 template <class X> void initFromPrimitive(X x);
66 template <class X> void initFromSignedPrimitive(X x);
69 /* Converters to primitive integer types
70 * The implicit conversion operators caused trouble, so these are now
72 unsigned long toUnsignedLong () const;
74 unsigned int toUnsignedInt () const;
76 unsigned short toUnsignedShort() const;
77 short toShort () const;
80 template <class X> X convertToSignedPrimitive() const;
81 template <class X> X convertToPrimitive() const;
86 // Expose these from NumberlikeArray directly.
87 NumberlikeArray<Blk>::getCapacity;
88 NumberlikeArray<Blk>::getLength;
90 /* Returns the requested block, or 0 if it is beyond the length (as if
91 * the number had 0s infinitely to the left). */
92 Blk getBlock(Index i) const { return i >= len ? 0 : blk[i]; }
94 // The number is zero if and only if the canonical length is zero.
95 bool isZero() const { return NumberlikeArray<Blk>::isEmpty(); }
99 // Compares this to x like Perl's <=>
100 CmpRes compareTo(const BigUnsigned &x) const;
102 // Ordinary comparison operators
103 bool operator ==(const BigUnsigned &x) const {
104 return NumberlikeArray<Blk>::operator ==(x);
106 bool operator !=(const BigUnsigned &x) const {
107 return NumberlikeArray<Blk>::operator !=(x);
109 bool operator < (const BigUnsigned &x) const { return compareTo(x) == less ; }
110 bool operator <=(const BigUnsigned &x) const { return compareTo(x) != greater; }
111 bool operator >=(const BigUnsigned &x) const { return compareTo(x) != less ; }
112 bool operator > (const BigUnsigned &x) const { return compareTo(x) == greater; }
115 * BigUnsigned and BigInteger both provide three kinds of operators.
116 * Here ``big-integer'' refers to BigInteger or BigUnsigned.
118 * (1) Overloaded ``return-by-value'' operators:
119 * +, -, *, /, %, unary -, &, |, ^, <<, >>.
120 * Big-integer code using these operators looks identical to code using
121 * the primitive integer types. These operators take one or two
122 * big-integer inputs and return a big-integer result, which can then
123 * be assigned to a BigInteger variable or used in an expression.
125 * BigInteger a(1), b = 1;
126 * BigInteger c = a + b;
128 * (2) Overloaded assignment operators:
129 * +=, -=, *=, /=, %=, flipSign, &=, |=, ^=, <<=, >>=, ++, --.
130 * Again, these are used on big integers just like on ints. They take
131 * one writable big integer that both provides an operand and receives a
132 * result. Most also take a second read-only operand.
134 * BigInteger a(1), b(1);
137 * (3) Copy-less operations: `add', `subtract', etc.
138 * These named methods take operands as arguments and store the result
139 * in the receiver (*this), avoiding unnecessary copies and allocations.
140 * `divideWithRemainder' is special: it both takes the dividend from and
141 * stores the remainder into the receiver, and it takes a separate
142 * object in which to store the quotient. NOTE: If you are wondering
143 * why these don't return a value, you probably mean to use the
144 * overloaded return-by-value operators instead.
147 * BigInteger a(43), b(7), c, d;
149 * c = a + b; // Now c == 50.
150 * c.add(a, b); // Same effect but without the two copies.
152 * c.divideWithRemainder(b, d);
153 * // 50 / 7; now d == 7 (quotient) and c == 1 (remainder).
155 * // ``Aliased'' calls now do the right thing using a temporary
156 * // copy, but see note on `divideWithRemainder'.
160 // COPY-LESS OPERATIONS
162 // These 8: Arguments are read-only operands, result is saved in *this.
163 void add(const BigUnsigned &a, const BigUnsigned &b);
164 void subtract(const BigUnsigned &a, const BigUnsigned &b);
165 void multiply(const BigUnsigned &a, const BigUnsigned &b);
166 void bitAnd(const BigUnsigned &a, const BigUnsigned &b);
167 void bitOr(const BigUnsigned &a, const BigUnsigned &b);
168 void bitXor(const BigUnsigned &a, const BigUnsigned &b);
169 void bitShiftLeft(const BigUnsigned &a, unsigned int b);
170 void bitShiftRight(const BigUnsigned &a, unsigned int b);
172 /* `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'.
173 * / and % use semantics similar to Knuth's, which differ from the
174 * primitive integer semantics under division by zero. See the
175 * implementation in BigUnsigned.cc for details.
176 * `a.divideWithRemainder(b, a)' throws an exception: it doesn't make
177 * sense to write quotient and remainder into the same variable. */
178 void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q);
180 /* `divide' and `modulo' are no longer offered. Use
181 * `divideWithRemainder' instead. */
183 // OVERLOADED RETURN-BY-VALUE OPERATORS
184 BigUnsigned operator +(const BigUnsigned &x) const;
185 BigUnsigned operator -(const BigUnsigned &x) const;
186 BigUnsigned operator *(const BigUnsigned &x) const;
187 BigUnsigned operator /(const BigUnsigned &x) const;
188 BigUnsigned operator %(const BigUnsigned &x) const;
189 /* OK, maybe unary minus could succeed in one case, but it really
190 * shouldn't be used, so it isn't provided. */
191 BigUnsigned operator &(const BigUnsigned &x) const;
192 BigUnsigned operator |(const BigUnsigned &x) const;
193 BigUnsigned operator ^(const BigUnsigned &x) const;
194 BigUnsigned operator <<(unsigned int b) const;
195 BigUnsigned operator >>(unsigned int b) const;
196 // Additional operators in an attempt to avoid overloading tangles.
197 // XXX Why exactly are these needed?
198 BigUnsigned operator <<(int b) const;
199 BigUnsigned operator >>(int b) const;
201 // OVERLOADED ASSIGNMENT OPERATORS
202 void operator +=(const BigUnsigned &x);
203 void operator -=(const BigUnsigned &x);
204 void operator *=(const BigUnsigned &x);
205 void operator /=(const BigUnsigned &x);
206 void operator %=(const BigUnsigned &x);
207 void operator &=(const BigUnsigned &x);
208 void operator |=(const BigUnsigned &x);
209 void operator ^=(const BigUnsigned &x);
210 void operator <<=(unsigned int b);
211 void operator >>=(unsigned int b);
212 // Additional operators in an attempt to avoid overloading tangles.
213 // XXX Why exactly are these needed?
214 void operator <<=(int b);
215 void operator >>=(int b);
217 /* INCREMENT/DECREMENT OPERATORS
218 * To discourage messy coding, these do not return *this, so prefix
219 * and postfix behave the same. */
221 void operator ++(int);
223 void operator --(int);
225 // Helper function that needs access to BigUnsigned internals
226 friend Blk getShiftedBlock(const BigUnsigned &num, Index x,
229 // See BigInteger.cc.
231 friend X convertBigUnsignedToPrimitiveAccess(const BigUnsigned &a);
234 /* Implementing the return-by-value and assignment operators in terms of the
235 * copy-less operations. The copy-less operations are responsible for making
236 * any necessary temporary copies to work around aliasing. */
238 inline BigUnsigned BigUnsigned::operator +(const BigUnsigned &x) const {
243 inline BigUnsigned BigUnsigned::operator -(const BigUnsigned &x) const {
245 ans.subtract(*this, x);
248 inline BigUnsigned BigUnsigned::operator *(const BigUnsigned &x) const {
250 ans.multiply(*this, x);
253 inline BigUnsigned BigUnsigned::operator /(const BigUnsigned &x) const {
256 r.divideWithRemainder(x, q);
259 inline BigUnsigned BigUnsigned::operator %(const BigUnsigned &x) const {
262 r.divideWithRemainder(x, q);
265 inline BigUnsigned BigUnsigned::operator &(const BigUnsigned &x) const {
267 ans.bitAnd(*this, x);
270 inline BigUnsigned BigUnsigned::operator |(const BigUnsigned &x) const {
275 inline BigUnsigned BigUnsigned::operator ^(const BigUnsigned &x) const {
277 ans.bitXor(*this, x);
280 inline BigUnsigned BigUnsigned::operator <<(unsigned int b) const {
282 ans.bitShiftLeft(*this, b);
285 inline BigUnsigned BigUnsigned::operator >>(unsigned int b) const {
287 ans.bitShiftRight(*this, b);
290 inline BigUnsigned BigUnsigned::operator <<(int b) const {
292 throw "BigUnsigned::operator <<(int): Negative shift amounts are not allowed";
293 return *this << (unsigned int)(b);
295 inline BigUnsigned BigUnsigned::operator >>(int b) const {
297 throw "BigUnsigned::operator >>(int): Negative shift amounts are not allowed";
298 return *this >> (unsigned int)(b);
301 inline void BigUnsigned::operator +=(const BigUnsigned &x) {
304 inline void BigUnsigned::operator -=(const BigUnsigned &x) {
307 inline void BigUnsigned::operator *=(const BigUnsigned &x) {
310 inline void BigUnsigned::operator /=(const BigUnsigned &x) {
311 /* The following technique is slightly faster than copying *this first
312 * when x is large. */
314 divideWithRemainder(x, q);
315 // *this contains the remainder, but we overwrite it with the quotient.
318 inline void BigUnsigned::operator %=(const BigUnsigned &x) {
320 // Mods *this by x. Don't care about quotient left in q.
321 divideWithRemainder(x, q);
323 inline void BigUnsigned::operator &=(const BigUnsigned &x) {
326 inline void BigUnsigned::operator |=(const BigUnsigned &x) {
329 inline void BigUnsigned::operator ^=(const BigUnsigned &x) {
332 inline void BigUnsigned::operator <<=(unsigned int b) {
333 bitShiftLeft(*this, b);
335 inline void BigUnsigned::operator >>=(unsigned int b) {
336 bitShiftRight(*this, b);
338 inline void BigUnsigned::operator <<=(int b) {
340 throw "BigUnsigned::operator <<=(int): Negative shift amounts are not supported";
341 *this <<= (unsigned int)(b);
343 inline void BigUnsigned::operator >>=(int b) {
345 throw "BigUnsigned::operator >>=(int): Negative shift amounts are not supported";
346 *this >>= (unsigned int)(b);