1 #ifndef BIGUNSIGNEDINABASE_H
2 #define BIGUNSIGNEDINABASE_H
4 #include "NumberlikeArray.hh"
5 #include "BigUnsigned.hh"
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).
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
21 * BigUnsignedInABase is similar to BigUnsigned. Note the following:
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).
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.)
31 * (3) The only arithmetic operation supported by BigUnsignedInABase
32 * is an equality test. Use BigUnsigned for arithmetic.
35 class BigUnsignedInABase : protected NumberlikeArray<unsigned short> {
39 typedef unsigned short Digit; // The digit type that BigUnsignedInABases are built from
44 Base base; // The base of this BigUnsignedInABase
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++.
51 BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {} // Creates a BigUnsignedInABase with a capacity
53 void zapLeadingZeros() { // Decreases len to eliminate leading zeros
54 while (len > 0 && blk[len - 1] == 0)
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
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
65 void operator =(const BigUnsignedInABase &x) { // Assignment operator
66 NumberlikeArray<Digit>::operator =(x);
70 BigUnsignedInABase(const Digit *d, Index l) : NumberlikeArray<Digit>(d, l) { // Constructor from an array of digits
74 // LINKS TO BIGUNSIGNED
75 BigUnsignedInABase(const BigUnsigned &x, Base base);
76 operator BigUnsigned() const;
80 * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to represent
81 * digits of 0 through 35. When parsing strings, lowercase is also accepted.
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!)
87 * No string representation has a ``base indicator'' like ``0x''.
89 * An exception is made for zero: it is converted to ``0'' and not the empty string.
91 * If you want different conventions, write your
92 * own routines to go between BigUnsignedInABase and strings. It's not hard.
94 operator std::string() const;
95 BigUnsignedInABase(const std::string &s, Base base);
98 // These accessors can be used to get the pieces of the number
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
112 bool operator ==(const BigUnsignedInABase &x) const {
113 return base == x.base && NumberlikeArray<Digit>::operator ==(x);
115 bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); }