Old snapshot `bigint-2006.05.01'; see the ChangeLog file.
[bigint/bigint.git] / BigInteger.hh
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         * Calls like a.operation(a, b) are unsafe and not allowed. */
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         */
107         void divideWithRemainder(const BigInteger &b, BigInteger &q);
108         void divide(const BigInteger &a, const BigInteger &b) {
109                 // Division, deprecated and provided for compatibility
110                 BigInteger a2(a);
111                 a2.divideWithRemainder(b, *this);
112                 // quotient now in *this
113                 // don't care about remainder left in a2
114         }
115         void modulo(const BigInteger &a, const BigInteger &b) {
116                 // Modular reduction, deprecated and provided for compatibility
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 // ASSIGNMENT OPERATORS
200 // These create a copy of this, then invoke the appropriate
201 // put-here operation on this, passing the copy and x.
202 inline void BigInteger::operator +=(const BigInteger &x) {
203         BigInteger thisCopy(*this);
204         add(thisCopy, x);
205 }
206 inline void BigInteger::operator -=(const BigInteger &x) {
207         BigInteger thisCopy(*this);
208         subtract(thisCopy, x);
209 }
210 inline void BigInteger::operator *=(const BigInteger &x) {
211         BigInteger thisCopy(*this);
212         multiply(thisCopy, x);
213 }
214 inline void BigInteger::operator /=(const BigInteger &x) {
215         // Updated for divideWithRemainder
216         BigInteger thisCopy(*this);
217         thisCopy.divideWithRemainder(x, *this);
218         // quotient left in *this
219         // don't care about remainder left in thisCopy
220 }
221 inline void BigInteger::operator %=(const BigInteger &x) {
222         // Shortcut (woohoo!)
223         BigInteger q;
224         divideWithRemainder(x, q);
225         // remainder left in *this
226         // don't care about quotient left in q
227 }
228 // This one is trivial
229 inline void BigInteger::flipSign() {
230         sign = Sign(-sign);
231 }
232
233 #endif