This is actually going to be version 2007.02.16 .
[bigint/bigint.git] / BigUnsigned.hh
index 101f697..1eb056f 100644 (file)
@@ -1,6 +1,5 @@
 /*
 * Matt McCutchen's Big Integer Library
-* http://mysite.verizon.net/mccutchen/bigint/
 */
 
 #ifndef BIGUNSIGNED
@@ -15,8 +14,9 @@
 * and many math operations are defined on BigUnsigneds.
 *
 * The number is stored as a series of blocks in a
-* dynamically allocated array.  It is as if the numbers
-* were written digit by digit in base 256 ^ sizeof(unsigned long).
+* dynamically allocated array.  It is as if the number
+* were written digit by digit in base 2 ^ N, **where N is the
+* number of bits in an unsigned long.**
 *
 * The memory-management details that used to be in here have
 * been moved into NumberlikeArray, which BigUnsigned now derives from.
@@ -32,6 +32,7 @@ class BigUnsigned : protected NumberlikeArray<unsigned long> {
        enum CmpRes { less = -1, equal = 0, greater = 1 }; // Enumeration for the result of a comparison
        typedef unsigned long Blk; // The number block type that BigUnsigneds are built from
        typedef NumberlikeArray<Blk>::Index Index; // (NlA) Type for the index of a block in the array
+       NumberlikeArray<Blk>::N; // Number of bits in a Blk
        
        /*
        // FIELDS
@@ -102,54 +103,97 @@ class BigUnsigned : protected NumberlikeArray<unsigned long> {
        // Compares this to x like Perl's <=>
        CmpRes compareTo(const BigUnsigned &x) const;
        // Normal comparison operators
-       NumberlikeArray<Blk>::operator ==; // (NlA) The body used to be `{ return compareTo(x) == equal; }'.  For performance reasons we use NumberlikeArray code that only worries about (in)equality and doesn't waste time determining which is bigger
-       NumberlikeArray<Blk>::operator !=; // (NlA) Ditto.
+       // Bug fixed 2006.04.24: Only we, not the user, can pass a BigUnsigned off as a
+       // NumberlikeArray, so we have to wrap == and !=.
+       bool operator ==(const BigUnsigned &x) const {
+               return NumberlikeArray<Blk>::operator ==(x);
+       }
+       bool operator !=(const BigUnsigned &x) const {
+               return NumberlikeArray<Blk>::operator !=(x);
+       }
        bool operator < (const BigUnsigned &x) const { return compareTo(x) == less   ; }
        bool operator <=(const BigUnsigned &x) const { return compareTo(x) != greater; }
        bool operator >=(const BigUnsigned &x) const { return compareTo(x) != less   ; }
        bool operator > (const BigUnsigned &x) const { return compareTo(x) == greater; }
+
+       /*
+       * BigUnsigned and BigInteger both provide three kinds of operators.
+       * Here ``big-integer'' refers to BigInteger or BigUnsigned.
+       *
+       * (1) Overloaded ``return-by-value'' operators:
+       *     +, -, *, /, %, unary -.
+       * Big-integer code using these operators looks identical to
+       * code using the primitive integer types.  These operators take
+       * one or two big-integer inputs and return a big-integer result,
+       * which can then be assigned to a BigInteger variable or used
+       * in an expression.  Example:
+       *     BigInteger a(1), b = 1;
+       *     BigInteger c = a + b;
+       *
+       * (2) Overloaded assignment operators:
+       *     +=, -=, *=, /=, %=, &=, |=, ^=, ++, --, flipSign.
+       * Again, these are used on big integers just like on ints.
+       * They take one writable big integer that both provides an
+       * operand and receives a result.  The first eight also take
+       * a second read-only operand.  Example:
+       *     BigInteger a(1), b(1);
+       *     a += b;
+       *
+       * (3) ``Put-here'' operations: `add', `subtract', etc.
+       * Using a return-by-value or assignment operator generally involves
+       * copy constructions and/or assignments.  The ``put-here'' operations
+       * require none, but they are more of a hassle to use.  Most take two
+       * read-only operands and save the result in the calling object `*this',
+       * whose previous value is ignored.  `divideWithRemainder' is an exception.
+       * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
+       * Examples:
+       *     BigInteger a(43), b(7), c, d;
+       *     c = a + b;   // Now c == 50.
+       *     c.add(a, b); // Same effect but without the two bulk-copies.
+       *     c.divideWithRemainder(b, d); // 50 / 7; now d == 7 (quotient) and c == 1 (remainder).
+       *     a.add(a, b); // ``Aliased'' calls now do the right thing using a
+       *              // temporary copy, but see note on divideWithRemainder.
+       */
        
        // PUT-HERE OPERATIONS
-       /* These store the result of the operation on the arguments into this.
-       * a.add(b, c) is equivalent to, but faster than, a = b + c.
-       * Calls like a.operation(a, b) are unsafe and not allowed. */
        public:
-       // Easy 3
-       void add          (const BigUnsigned &a, const BigUnsigned &b); // Addition
-       void subtract     (const BigUnsigned &a, const BigUnsigned &b); // Subtraction
-       void multiply     (const BigUnsigned &a, const BigUnsigned &b); // Multiplication
+       /* These 3: Two read-only operands as arguments.  Result left in *this. */
+       void add(const BigUnsigned &a, const BigUnsigned &b); // Addition
+       void subtract(const BigUnsigned &a, const BigUnsigned &b); // Subtraction
+       void multiply(const BigUnsigned &a, const BigUnsigned &b); // Multiplication
        /* Divisive stuff
        * `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'.
        * Semantics similar to Donald E. Knuth's are used for / and %,
        * and these differ from the semantics of primitive-type
        * / and % under division by zero.
        * Look in `BigUnsigned.cc' for details.
+       * `a.divideWithRemainder(b, a)' causes an exception: it doesn't make
+       * sense to write quotient and remainder into the same variable.
        */
        void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q);
        void divide(const BigUnsigned &a, const BigUnsigned &b) {
-               // Division, deprecated and provided for compatibility
                BigUnsigned a2(a);
                a2.divideWithRemainder(b, *this);
                // quotient now in *this
                // don't care about remainder left in a2
        }
        void modulo(const BigUnsigned &a, const BigUnsigned &b) {
-               // Modular reduction, deprecated and provided for compatibility
                *this = a;
                BigUnsigned q;
                divideWithRemainder(b, q);
                // remainder now in *this
                // don't care about quotient left in q
        }
-       // Bitwise
-       void bitAnd       (const BigUnsigned &a, const BigUnsigned &b); // Bitwise AND
-       void bitOr        (const BigUnsigned &a, const BigUnsigned &b); // Bitwise OR
-       void bitXor       (const BigUnsigned &a, const BigUnsigned &b); // Bitwise XOR
+       // Bitwise operations.  Two read-only operands as arguments.  Result left in *this.
+       // These are not provided for BigIntegers; I think that using them on BigIntegers
+       // will discard the sign first.
+       void bitAnd(const BigUnsigned &a, const BigUnsigned &b); // Bitwise AND
+       void bitOr(const BigUnsigned &a, const BigUnsigned &b); // Bitwise OR
+       void bitXor(const BigUnsigned &a, const BigUnsigned &b); // Bitwise XOR
        
-       // These functions are declared but not defined.  (Sorry.)
-       // Trying to call either will result in a link-time error.
-       void bitShiftLeft (const BigUnsigned &a, unsigned int b); // Bitwise left shift
-       void bitShiftRight(const BigUnsigned &a, unsigned int b); // Bitwise right shift
+       // These functions might exist someday.
+       //void bitShiftLeft(const BigUnsigned &a, unsigned int b); // Bitwise left shift
+       //void bitShiftRight(const BigUnsigned &a, unsigned int b); // Bitwise right shift
        
        // NORMAL OPERATORS
        // These perform the operation on this (to the left of the operator)
@@ -185,6 +229,8 @@ class BigUnsigned : protected NumberlikeArray<unsigned long> {
        void operator --(   ); // Prefix  increment
        void operator --(int); // Postfix decrement
        
+       // Helper function that needs access to BigUnsigned internals
+       friend Blk getShiftedBlock(const BigUnsigned &num, Index x, unsigned int y);
 };
 
 // NORMAL OPERATORS
@@ -232,21 +278,23 @@ inline BigUnsigned BigUnsigned::operator ^(const BigUnsigned &x) const {
        return ans;
 }
 
-// ASSIGNMENT OPERATORS
-// These create a copy of this, then invoke the appropriate
-// put-here operation on this, passing the copy and x.
-// Exception: those updated for divideWithRemainder.
+/*
+ * ASSIGNMENT OPERATORS
+ * 
+ * Now the responsibility for making a temporary copy if necessary
+ * belongs to the put-here operations.  I made this change on 2007.02.13 after
+ * Boris Dessy pointed out that the old implementation handled calls like
+ * "a *= a" badly: it translated them to essentially "a.multiply(aCopy, a)",
+ * which threw an exception.
+ */
 inline void BigUnsigned::operator +=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       add(thisCopy, x);
+       add(*this, x);
 }
 inline void BigUnsigned::operator -=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       subtract(thisCopy, x);
+       subtract(*this, x);
 }
 inline void BigUnsigned::operator *=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       multiply(thisCopy, x);
+       multiply(*this, x);
 }
 inline void BigUnsigned::operator /=(const BigUnsigned &x) {
        // Updated for divideWithRemainder
@@ -263,16 +311,13 @@ inline void BigUnsigned::operator %=(const BigUnsigned &x) {
        // don't care about quotient left in q
 }
 inline void BigUnsigned::operator &=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       bitAnd(thisCopy, x);
+       bitAnd(*this, x);
 }
 inline void BigUnsigned::operator |=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       bitOr(thisCopy, x);
+       bitOr(*this, x);
 }
 inline void BigUnsigned::operator ^=(const BigUnsigned &x) {
-       BigUnsigned thisCopy(*this);
-       bitXor(thisCopy, x);
+       bitXor(*this, x);
 }
 
 #endif