| 1 | #ifndef BIGUNSIGNEDINABASE_H |
| 2 | #define BIGUNSIGNEDINABASE_H |
| 3 | |
| 4 | #include "NumberlikeArray.hh" |
| 5 | #include "BigUnsigned.hh" |
| 6 | #include <string> |
| 7 | |
| 8 | /* |
| 9 | * A BigUnsignedInABase object represents a nonnegative |
| 10 | * integer of size limited only by available memory, |
| 11 | * represented in a user-specified base that can fit in |
| 12 | * an `unsigned short' (most can, and this saves memory). |
| 13 | * |
| 14 | * BigUnsignedInABase is intended as an intermediary class |
| 15 | * with little functionality of its own. BigUnsignedInABase |
| 16 | * objects can be constructed from, and converted to, |
| 17 | * BigUnsigneds (requiring multiplication, mods, etc.) and |
| 18 | * `std::string's (by switching digit values for appropriate |
| 19 | * characters). |
| 20 | * |
| 21 | * BigUnsignedInABase is similar to BigUnsigned. Note the following: |
| 22 | * |
| 23 | * (1) They represent the number in exactly the same way, except |
| 24 | * that BigUnsignedInABase uses ``digits'' (or Digit) where BigUnsigned uses |
| 25 | * ``blocks'' (or Blk). |
| 26 | * |
| 27 | * (2) Both use the management features of NumberlikeArray. (In fact, |
| 28 | * my desire to add a BigUnsignedInABase class without duplicating a |
| 29 | * lot of code led me to introduce NumberlikeArray.) |
| 30 | * |
| 31 | * (3) The only arithmetic operation supported by BigUnsignedInABase |
| 32 | * is an equality test. Use BigUnsigned for arithmetic. |
| 33 | */ |
| 34 | |
| 35 | class BigUnsignedInABase : protected NumberlikeArray<unsigned short> { |
| 36 | |
| 37 | // TYPES |
| 38 | public: |
| 39 | typedef unsigned short Digit; // The digit type that BigUnsignedInABases are built from |
| 40 | typedef Digit Base; |
| 41 | |
| 42 | // FIELDS |
| 43 | protected: |
| 44 | Base base; // The base of this BigUnsignedInABase |
| 45 | |
| 46 | // MANAGEMENT |
| 47 | protected: |
| 48 | // These members generally defer to those in NumberlikeArray, possibly with slight changes. |
| 49 | // It might be nice if one could request that constructors be inherited in C++. |
| 50 | |
| 51 | BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {} // Creates a BigUnsignedInABase with a capacity |
| 52 | |
| 53 | void zapLeadingZeros() { // Decreases len to eliminate leading zeros |
| 54 | while (len > 0 && blk[len - 1] == 0) |
| 55 | len--; |
| 56 | } |
| 57 | |
| 58 | //void allocate(Index c); // (NlA) Ensures the number array has at least the indicated capacity, maybe discarding contents |
| 59 | //void allocateAndCopy(Index c); // (NlA) Ensures the number array has at least the indicated capacity, preserving its contents |
| 60 | |
| 61 | public: |
| 62 | BigUnsignedInABase() : NumberlikeArray<Digit>(), base(2) {} // Default constructor (value is 0 in base 2) |
| 63 | BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray<Digit>(x), base(x.base) {} // Copy constructor |
| 64 | |
| 65 | void operator =(const BigUnsignedInABase &x) { // Assignment operator |
| 66 | NumberlikeArray<Digit>::operator =(x); |
| 67 | base = x.base; |
| 68 | } |
| 69 | |
| 70 | BigUnsignedInABase(const Digit *d, Index l) : NumberlikeArray<Digit>(d, l) { // Constructor from an array of digits |
| 71 | zapLeadingZeros(); |
| 72 | } |
| 73 | |
| 74 | // LINKS TO BIGUNSIGNED |
| 75 | BigUnsignedInABase(const BigUnsigned &x, Base base); |
| 76 | operator BigUnsigned() const; |
| 77 | |
| 78 | /* LINKS TO STRINGS |
| 79 | * |
| 80 | * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to represent |
| 81 | * digits of 0 through 35. When parsing strings, lowercase is also accepted. |
| 82 | * |
| 83 | * All string representations are big-endian (big-place-value digits first). |
| 84 | * (Computer scientists have adopted zero-based counting; why can't they |
| 85 | * tolerate little-endian numbers? It makes a lot of sense!) |
| 86 | * |
| 87 | * No string representation has a ``base indicator'' like ``0x''. |
| 88 | * |
| 89 | * An exception is made for zero: it is converted to ``0'' and not the empty string. |
| 90 | * |
| 91 | * If you want different conventions, write your |
| 92 | * own routines to go between BigUnsignedInABase and strings. It's not hard. |
| 93 | */ |
| 94 | operator std::string() const; |
| 95 | BigUnsignedInABase(const std::string &s, Base base); |
| 96 | |
| 97 | // PICKING APART |
| 98 | // These accessors can be used to get the pieces of the number |
| 99 | public: |
| 100 | Base getBase() const { return base; } |
| 101 | NumberlikeArray<Digit>::getCapacity; // (NlA) |
| 102 | NumberlikeArray<Digit>::getLength; // (NlA) |
| 103 | // Note that getDigit returns 0 if the digit index is beyond the length of the number. |
| 104 | // A routine that uses this accessor can safely assume a BigUnsigned has 0s infinitely to the left. |
| 105 | Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; } |
| 106 | // Note how we replace one level of abstraction with another. |
| 107 | bool isZero() const { return NumberlikeArray<Digit>::isEmpty(); } // Often convenient for loops |
| 108 | |
| 109 | // EQUALITY TEST |
| 110 | public: |
| 111 | // Equality test |
| 112 | bool operator ==(const BigUnsignedInABase &x) const { |
| 113 | return base == x.base && NumberlikeArray<Digit>::operator ==(x); |
| 114 | } |
| 115 | bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); } |
| 116 | |
| 117 | }; |
| 118 | |
| 119 | #endif |