class BigInteger : public BigUnsigned {
// TYPES & CONSTANTS
- public:
+public:
enum Sign { negative = -1, zero = 0, positive = 1 }; // Enumeration for the sign of a BigInteger
// FIELDS
- protected:
+protected:
Sign sign; // The sign of this BigInteger
// MANAGEMENT
- protected:
+protected:
BigInteger(Sign s, Index c) : BigUnsigned(0, c), sign(s) {}; // Creates a BigInteger with a sign and capacity
- public:
+public:
BigInteger() : BigUnsigned(), sign(zero) {} // Default constructor (value is 0)
BigInteger(const BigInteger &x) : BigUnsigned(x), sign(x.sign) {}; // Copy constructor
// automatically; this takes its absolute value.
// CONVERTERS to integral types
- public:
+public:
operator unsigned long () const;
operator long () const;
operator unsigned int () const;
// PICKING APART
// These accessors can be used to get the pieces of the number
- public:
+public:
Sign getSign() const;
// COMPARISONS
- public:
+public:
// Compares this to x like Perl's <=>
CmpRes compareTo(const BigInteger &x) const;
// Normal comparison operators
/* 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.
* See explanation of "put-here operations" in BigUnsigned.cc . */
- public:
+public:
void add (const BigInteger &a, const BigInteger &b); // Addition
void subtract(const BigInteger &a, const BigInteger &b); // Subtraction
void multiply(const BigInteger &a, const BigInteger &b); // Multiplication
// NORMAL OPERATORS
// These perform the operation on this (to the left of the operator)
// and x (to the right of the operator) and return a new BigInteger with the result.
- public:
+public:
BigInteger operator +(const BigInteger &x) const; // Addition
BigInteger operator -(const BigInteger &x) const; // Subtraction
BigInteger operator *(const BigInteger &x) const; // Multiplication
// ASSIGNMENT OPERATORS
// These perform the operation on this and x, storing the result into this.
- public:
+public:
void operator +=(const BigInteger &x); // Addition
void operator -=(const BigInteger &x); // Subtraction
void operator *=(const BigInteger &x); // Multiplication
// INCREMENT/DECREMENT OPERATORS
// These increase or decrease the number by 1. To discourage side effects,
// these do not return *this, so prefix and postfix behave the same.
- public:
+public:
void operator ++( ); // Prefix increment
void operator ++(int); // Postfix decrement
void operator --( ); // Prefix increment
class BigUnsigned : protected NumberlikeArray<unsigned long> {
// TYPES & CONSTANTS
- public:
+public:
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
/*
// FIELDS
- protected:
+protected:
Index cap; // (NlA) The current allocated capacity of this BigUnsigned (in blocks)
Index len; // (NlA) The actual length of the number stored in this BigUnsigned (in blocks)
Blk *blk; // (NlA) Dynamically allocated array of the number blocks
*/
// MANAGEMENT
- protected:
+protected:
// These members generally defer to those in NumberlikeArray, possibly with slight changes.
// It might be nice if one could request that constructors be inherited in C++.
//void allocate(Index c); // (NlA) Ensures the number array has at least the indicated capacity, maybe discarding contents
//void allocateAndCopy(Index c); // (NlA) Ensures the number array has at least the indicated capacity, preserving its contents
- public:
+public:
BigUnsigned() : NumberlikeArray<Blk>() {} // Default constructor (value is 0)
BigUnsigned(const BigUnsigned &x) : NumberlikeArray<Blk>(x) {} // Copy constructor
~BigUnsigned() {} // Destructor
// CONVERTERS to integral types
- public:
+public:
operator unsigned long () const;
operator long () const;
operator unsigned int () const;
// PICKING APART
// These accessors can be used to get the pieces of the number
- public:
+public:
NumberlikeArray<Blk>::getCapacity;
NumberlikeArray<Blk>::getLength;
// Note that getBlock returns 0 if the block index is beyond the length of the number.
bool isZero() const { return NumberlikeArray<Blk>::isEmpty(); } // Often convenient for loops
// COMPARISONS
- public:
+public:
// Compares this to x like Perl's <=>
CmpRes compareTo(const BigUnsigned &x) const;
// Normal comparison operators
*/
// PUT-HERE OPERATIONS
- public:
+public:
/* 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
// NORMAL OPERATORS
// These perform the operation on this (to the left of the operator)
// and x (to the right of the operator) and return a new BigUnsigned with the result.
- public:
+public:
BigUnsigned operator +(const BigUnsigned &x) const; // Addition
BigUnsigned operator -(const BigUnsigned &x) const; // Subtraction
BigUnsigned operator *(const BigUnsigned &x) const; // Multiplication
// ASSIGNMENT OPERATORS
// These perform the operation on this and x, storing the result into this.
- public:
+public:
void operator +=(const BigUnsigned &x); // Addition
void operator -=(const BigUnsigned &x); // Subtraction
void operator *=(const BigUnsigned &x); // Multiplication
// INCREMENT/DECREMENT OPERATORS
// These increase or decrease the number by 1. To discourage side effects,
// these do not return *this, so prefix and postfix behave the same.
- public:
+public:
void operator ++( ); // Prefix increment
void operator ++(int); // Postfix decrement
void operator --( ); // Prefix increment
class BigUnsignedInABase : protected NumberlikeArray<unsigned short> {
// TYPES
- public:
+public:
typedef unsigned short Digit; // The digit type that BigUnsignedInABases are built from
typedef Digit Base;
// FIELDS
- protected:
+protected:
Base base; // The base of this BigUnsignedInABase
// MANAGEMENT
- protected:
+protected:
// These members generally defer to those in NumberlikeArray, possibly with slight changes.
// It might be nice if one could request that constructors be inherited in C++.
//void allocate(Index c); // (NlA) Ensures the number array has at least the indicated capacity, maybe discarding contents
//void allocateAndCopy(Index c); // (NlA) Ensures the number array has at least the indicated capacity, preserving its contents
- public:
+public:
BigUnsignedInABase() : NumberlikeArray<Digit>(), base(2) {} // Default constructor (value is 0 in base 2)
BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray<Digit>(x), base(x.base) {} // Copy constructor
// PICKING APART
// These accessors can be used to get the pieces of the number
- public:
+public:
Base getBase() const { return base; }
NumberlikeArray<Digit>::getCapacity; // (NlA)
NumberlikeArray<Digit>::getLength; // (NlA)
bool isZero() const { return NumberlikeArray<Digit>::isEmpty(); } // Often convenient for loops
// EQUALITY TEST
- public:
+public:
// Equality test
bool operator ==(const BigUnsignedInABase &x) const {
return base == x.base && NumberlikeArray<Digit>::operator ==(x);
template <class Blk>
class NumberlikeArray {
- public:
+public:
typedef unsigned int Index; // Type for the index of a block in the array
static const unsigned int N; // The number of bits in a block, defined below.