This is actually going to be version 2007.02.16 .
[bigint/bigint.git] / BigUnsigned.hh
CommitLineData
e67d6049
MM
1/*
2* Matt McCutchen's Big Integer Library
e67d6049
MM
3*/
4
5#ifndef BIGUNSIGNED
6#define BIGUNSIGNED
7
05780f4b
MM
8#include "NumberlikeArray.hh"
9
e67d6049
MM
10/*
11* A BigUnsigned object represents a nonnegative integer of size
12* limited only by available memory. A BigUnsigned can be
13* created from and converted back to most integral types,
05780f4b 14* and many math operations are defined on BigUnsigneds.
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.**
05780f4b
MM
20*
21* The memory-management details that used to be in here have
22* been moved into NumberlikeArray, which BigUnsigned now derives from.
23* `(NlA)' means that member(s) are declared identically in NumberlikeArray.
24* Such members are either redeclared here to make them public or are
25* here, commented out, for reference.
e67d6049
MM
26*/
27
05780f4b 28class BigUnsigned : protected NumberlikeArray<unsigned long> {
e67d6049
MM
29
30 // TYPES & CONSTANTS
31 public:
32 enum CmpRes { less = -1, equal = 0, greater = 1 }; // Enumeration for the result of a comparison
33 typedef unsigned long Blk; // The number block type that BigUnsigneds are built from
05780f4b 34 typedef NumberlikeArray<Blk>::Index Index; // (NlA) Type for the index of a block in the array
4efbb076 35 NumberlikeArray<Blk>::N; // Number of bits in a Blk
e67d6049 36
05780f4b 37 /*
e67d6049
MM
38 // FIELDS
39 protected:
05780f4b
MM
40 Index cap; // (NlA) The current allocated capacity of this BigUnsigned (in blocks)
41 Index len; // (NlA) The actual length of the number stored in this BigUnsigned (in blocks)
42 Blk *blk; // (NlA) Dynamically allocated array of the number blocks
43 */
e67d6049
MM
44
45 // MANAGEMENT
46 protected:
05780f4b
MM
47 // These members generally defer to those in NumberlikeArray, possibly with slight changes.
48 // It might be nice if one could request that constructors be inherited in C++.
49
50 BigUnsigned(int, Index c) : NumberlikeArray<Blk>(0, c) {} // Creates a BigUnsigned with a capacity
51
52 void zapLeadingZeros() { // Decreases len to eliminate leading zeros
53 while (len > 0 && blk[len - 1] == 0)
54 len--;
55 }
56
57 //void allocate(Index c); // (NlA) Ensures the number array has at least the indicated capacity, maybe discarding contents
58 //void allocateAndCopy(Index c); // (NlA) Ensures the number array has at least the indicated capacity, preserving its contents
59
e67d6049 60 public:
05780f4b
MM
61 BigUnsigned() : NumberlikeArray<Blk>() {} // Default constructor (value is 0)
62 BigUnsigned(const BigUnsigned &x) : NumberlikeArray<Blk>(x) {} // Copy constructor
63
64 void operator=(const BigUnsigned &x) { // Assignment operator
65 NumberlikeArray<Blk>::operator =(x);
66 }
67
68 BigUnsigned(const Blk *b, Index l) : NumberlikeArray<Blk>(b, l) { // Constructor from an array of blocks
69 zapLeadingZeros();
70 }
71
e67d6049
MM
72 // Constructors from integral types
73 BigUnsigned(unsigned long x);
74 BigUnsigned( long x);
75 BigUnsigned(unsigned int x);
76 BigUnsigned( int x);
77 BigUnsigned(unsigned short x);
78 BigUnsigned( short x);
05780f4b 79 ~BigUnsigned() {} // Destructor
e67d6049
MM
80
81 // CONVERTERS to integral types
82 public:
83 operator unsigned long () const;
84 operator long () const;
85 operator unsigned int () const;
86 operator int () const;
87 operator unsigned short() const;
88 operator short() const;
89
90 // PICKING APART
91 // These accessors can be used to get the pieces of the number
92 public:
05780f4b
MM
93 NumberlikeArray<Blk>::getCapacity;
94 NumberlikeArray<Blk>::getLength;
95 // Note that getBlock returns 0 if the block index is beyond the length of the number.
96 // A routine that uses this accessor can safely assume a BigUnsigned has 0s infinitely to the left.
97 Blk getBlock(Index i) const { return i >= len ? 0 : blk[i]; }
98 // Note how we replace one level of abstraction with another. Isn't that neat?
99 bool isZero() const { return NumberlikeArray<Blk>::isEmpty(); } // Often convenient for loops
e67d6049
MM
100
101 // COMPARISONS
102 public:
103 // Compares this to x like Perl's <=>
104 CmpRes compareTo(const BigUnsigned &x) const;
105 // Normal comparison operators
918d66f2
MM
106 // Bug fixed 2006.04.24: Only we, not the user, can pass a BigUnsigned off as a
107 // NumberlikeArray, so we have to wrap == and !=.
108 bool operator ==(const BigUnsigned &x) const {
109 return NumberlikeArray<Blk>::operator ==(x);
110 }
111 bool operator !=(const BigUnsigned &x) const {
112 return NumberlikeArray<Blk>::operator !=(x);
113 }
05780f4b
MM
114 bool operator < (const BigUnsigned &x) const { return compareTo(x) == less ; }
115 bool operator <=(const BigUnsigned &x) const { return compareTo(x) != greater; }
116 bool operator >=(const BigUnsigned &x) const { return compareTo(x) != less ; }
117 bool operator > (const BigUnsigned &x) const { return compareTo(x) == greater; }
a8b42b68
MM
118
119 /*
120 * BigUnsigned and BigInteger both provide three kinds of operators.
121 * Here ``big-integer'' refers to BigInteger or BigUnsigned.
122 *
123 * (1) Overloaded ``return-by-value'' operators:
124 * +, -, *, /, %, unary -.
125 * Big-integer code using these operators looks identical to
126 * code using the primitive integer types. These operators take
127 * one or two big-integer inputs and return a big-integer result,
128 * which can then be assigned to a BigInteger variable or used
129 * in an expression. Example:
130 * BigInteger a(1), b = 1;
131 * BigInteger c = a + b;
132 *
133 * (2) Overloaded assignment operators:
134 * +=, -=, *=, /=, %=, &=, |=, ^=, ++, --, flipSign.
135 * Again, these are used on big integers just like on ints.
136 * They take one writable big integer that both provides an
137 * operand and receives a result. The first eight also take
138 * a second read-only operand. Example:
139 * BigInteger a(1), b(1);
140 * a += b;
141 *
142 * (3) ``Put-here'' operations: `add', `subtract', etc.
143 * Using a return-by-value or assignment operator generally involves
144 * copy constructions and/or assignments. The ``put-here'' operations
145 * require none, but they are more of a hassle to use. Most take two
146 * read-only operands and save the result in the calling object `*this',
147 * whose previous value is ignored. `divideWithRemainder' is an exception.
148 * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
149 * Examples:
150 * BigInteger a(43), b(7), c, d;
151 * c = a + b; // Now c == 50.
152 * c.add(a, b); // Same effect but without the two bulk-copies.
153 * c.divideWithRemainder(b, d); // 50 / 7; now d == 7 (quotient) and c == 1 (remainder).
8c16728a
MM
154 * a.add(a, b); // ``Aliased'' calls now do the right thing using a
155 * // temporary copy, but see note on divideWithRemainder.
a8b42b68 156 */
e67d6049
MM
157
158 // PUT-HERE OPERATIONS
e67d6049 159 public:
a8b42b68
MM
160 /* These 3: Two read-only operands as arguments. Result left in *this. */
161 void add(const BigUnsigned &a, const BigUnsigned &b); // Addition
162 void subtract(const BigUnsigned &a, const BigUnsigned &b); // Subtraction
163 void multiply(const BigUnsigned &a, const BigUnsigned &b); // Multiplication
05780f4b
MM
164 /* Divisive stuff
165 * `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'.
166 * Semantics similar to Donald E. Knuth's are used for / and %,
167 * and these differ from the semantics of primitive-type
168 * / and % under division by zero.
169 * Look in `BigUnsigned.cc' for details.
8c16728a
MM
170 * `a.divideWithRemainder(b, a)' causes an exception: it doesn't make
171 * sense to write quotient and remainder into the same variable.
05780f4b
MM
172 */
173 void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q);
174 void divide(const BigUnsigned &a, const BigUnsigned &b) {
05780f4b
MM
175 BigUnsigned a2(a);
176 a2.divideWithRemainder(b, *this);
177 // quotient now in *this
178 // don't care about remainder left in a2
179 }
180 void modulo(const BigUnsigned &a, const BigUnsigned &b) {
05780f4b
MM
181 *this = a;
182 BigUnsigned q;
183 divideWithRemainder(b, q);
184 // remainder now in *this
185 // don't care about quotient left in q
186 }
a8b42b68
MM
187 // Bitwise operations. Two read-only operands as arguments. Result left in *this.
188 // These are not provided for BigIntegers; I think that using them on BigIntegers
189 // will discard the sign first.
190 void bitAnd(const BigUnsigned &a, const BigUnsigned &b); // Bitwise AND
191 void bitOr(const BigUnsigned &a, const BigUnsigned &b); // Bitwise OR
192 void bitXor(const BigUnsigned &a, const BigUnsigned &b); // Bitwise XOR
05780f4b 193
8c16728a
MM
194 // These functions might exist someday.
195 //void bitShiftLeft(const BigUnsigned &a, unsigned int b); // Bitwise left shift
196 //void bitShiftRight(const BigUnsigned &a, unsigned int b); // Bitwise right shift
e67d6049
MM
197
198 // NORMAL OPERATORS
199 // These perform the operation on this (to the left of the operator)
200 // and x (to the right of the operator) and return a new BigUnsigned with the result.
201 public:
202 BigUnsigned operator +(const BigUnsigned &x) const; // Addition
203 BigUnsigned operator -(const BigUnsigned &x) const; // Subtraction
204 BigUnsigned operator *(const BigUnsigned &x) const; // Multiplication
205 BigUnsigned operator /(const BigUnsigned &x) const; // Division
206 BigUnsigned operator %(const BigUnsigned &x) const; // Modular reduction
207 BigUnsigned operator &(const BigUnsigned &x) const; // Bitwise AND
208 BigUnsigned operator |(const BigUnsigned &x) const; // Bitwise OR
209 BigUnsigned operator ^(const BigUnsigned &x) const; // Bitwise XOR
210
211 // ASSIGNMENT OPERATORS
212 // These perform the operation on this and x, storing the result into this.
213 public:
214 void operator +=(const BigUnsigned &x); // Addition
215 void operator -=(const BigUnsigned &x); // Subtraction
216 void operator *=(const BigUnsigned &x); // Multiplication
217 void operator /=(const BigUnsigned &x); // Division
218 void operator %=(const BigUnsigned &x); // Modular reduction
219 void operator &=(const BigUnsigned &x); // Bitwise AND
220 void operator |=(const BigUnsigned &x); // Bitwise OR
221 void operator ^=(const BigUnsigned &x); // Bitwise XOR
222
223 // INCREMENT/DECREMENT OPERATORS
224 // These increase or decrease the number by 1. To discourage side effects,
225 // these do not return *this, so prefix and postfix behave the same.
226 public:
227 void operator ++( ); // Prefix increment
228 void operator ++(int); // Postfix decrement
229 void operator --( ); // Prefix increment
230 void operator --(int); // Postfix decrement
231
4efbb076
MM
232 // Helper function that needs access to BigUnsigned internals
233 friend Blk getShiftedBlock(const BigUnsigned &num, Index x, unsigned int y);
e67d6049
MM
234};
235
e67d6049
MM
236// NORMAL OPERATORS
237/* These create an object to hold the result and invoke
238* the appropriate put-here operation on it, passing
239* this and x. The new object is then returned. */
240inline BigUnsigned BigUnsigned::operator +(const BigUnsigned &x) const {
241 BigUnsigned ans;
242 ans.add(*this, x);
243 return ans;
244}
245inline BigUnsigned BigUnsigned::operator -(const BigUnsigned &x) const {
246 BigUnsigned ans;
247 ans.subtract(*this, x);
248 return ans;
249}
250inline BigUnsigned BigUnsigned::operator *(const BigUnsigned &x) const {
251 BigUnsigned ans;
252 ans.multiply(*this, x);
253 return ans;
254}
255inline BigUnsigned BigUnsigned::operator /(const BigUnsigned &x) const {
256 BigUnsigned ans;
257 ans.divide(*this, x);
258 return ans;
259}
260inline BigUnsigned BigUnsigned::operator %(const BigUnsigned &x) const {
261 BigUnsigned ans;
262 ans.modulo(*this, x);
263 return ans;
264}
265inline BigUnsigned BigUnsigned::operator &(const BigUnsigned &x) const {
266 BigUnsigned ans;
267 ans.bitAnd(*this, x);
268 return ans;
269}
270inline BigUnsigned BigUnsigned::operator |(const BigUnsigned &x) const {
271 BigUnsigned ans;
272 ans.bitOr(*this, x);
273 return ans;
274}
275inline BigUnsigned BigUnsigned::operator ^(const BigUnsigned &x) const {
276 BigUnsigned ans;
277 ans.bitXor(*this, x);
278 return ans;
279}
280
8c16728a
MM
281/*
282 * ASSIGNMENT OPERATORS
283 *
284 * Now the responsibility for making a temporary copy if necessary
285 * belongs to the put-here operations. I made this change on 2007.02.13 after
286 * Boris Dessy pointed out that the old implementation handled calls like
287 * "a *= a" badly: it translated them to essentially "a.multiply(aCopy, a)",
288 * which threw an exception.
289 */
e67d6049 290inline void BigUnsigned::operator +=(const BigUnsigned &x) {
8c16728a 291 add(*this, x);
e67d6049
MM
292}
293inline void BigUnsigned::operator -=(const BigUnsigned &x) {
8c16728a 294 subtract(*this, x);
e67d6049
MM
295}
296inline void BigUnsigned::operator *=(const BigUnsigned &x) {
8c16728a 297 multiply(*this, x);
e67d6049
MM
298}
299inline void BigUnsigned::operator /=(const BigUnsigned &x) {
05780f4b 300 // Updated for divideWithRemainder
e67d6049 301 BigUnsigned thisCopy(*this);
05780f4b
MM
302 thisCopy.divideWithRemainder(x, *this);
303 // quotient left in *this
304 // don't care about remainder left in thisCopy
e67d6049
MM
305}
306inline void BigUnsigned::operator %=(const BigUnsigned &x) {
05780f4b
MM
307 // Shortcut (woohoo!)
308 BigUnsigned q;
309 divideWithRemainder(x, q);
310 // remainder left in *this
311 // don't care about quotient left in q
e67d6049
MM
312}
313inline void BigUnsigned::operator &=(const BigUnsigned &x) {
8c16728a 314 bitAnd(*this, x);
e67d6049
MM
315}
316inline void BigUnsigned::operator |=(const BigUnsigned &x) {
8c16728a 317 bitOr(*this, x);
e67d6049
MM
318}
319inline void BigUnsigned::operator ^=(const BigUnsigned &x) {
8c16728a 320 bitXor(*this, x);
e67d6049
MM
321}
322
323#endif