- Reconcile BigUnsignedInABase.hh with standards established in BigUnsigned.hh.
[bigint/bigint.git] / BigUnsignedInABase.hh
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 integer of size limited
10  * only by available memory, represented in a user-specified base that can fit
11  * in an `unsigned short' (most can, and this saves memory).
12  *
13  * BigUnsignedInABase is intended as an intermediary class with little
14  * functionality of its own.  BigUnsignedInABase objects can be constructed
15  * from, and converted to, BigUnsigneds (requiring multiplication, mods, etc.)
16  * and `std::string's (by switching digit values for appropriate characters).
17  *
18  * BigUnsignedInABase is similar to BigUnsigned.  Note the following:
19  *
20  * (1) They represent the number in exactly the same way, except that
21  * BigUnsignedInABase uses ``digits'' (or Digit) where BigUnsigned uses
22  * ``blocks'' (or Blk).
23  *
24  * (2) Both use the management features of NumberlikeArray.  (In fact, my desire
25  * to add a BigUnsignedInABase class without duplicating a lot of code led me to
26  * introduce NumberlikeArray.)
27  *
28  * (3) The only arithmetic operation supported by BigUnsignedInABase is an
29  * equality test.  Use BigUnsigned for arithmetic.
30  */
31
32 class BigUnsignedInABase : protected NumberlikeArray<unsigned short> {
33
34 public:
35         // The digits of a BigUnsignedInABase are unsigned shorts.
36         typedef unsigned short Digit;
37         // That's also the type of a base.
38         typedef Digit Base;
39
40 protected:
41         // The base in which this BigUnsignedInABase is expressed
42         Base base;
43
44         // Creates a BigUnsignedInABase with a capacity; for internal use.
45         BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {}
46
47         // Decreases len to eliminate any leading zero digits.
48         void zapLeadingZeros() { 
49                 while (len > 0 && blk[len - 1] == 0)
50                         len--;
51         }
52
53 public:
54         // Constructs zero in base 2.
55         BigUnsignedInABase() : NumberlikeArray<Digit>(), base(2) {}
56
57         // Copy constructor
58         BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray<Digit>(x), base(x.base) {}
59
60         // Assignment operator
61         void operator =(const BigUnsignedInABase &x) {
62                 NumberlikeArray<Digit>::operator =(x);
63                 base = x.base;
64         }
65
66         // Constructor that copies from a given array of digits.
67         BigUnsignedInABase(const Digit *d, Index l, Base base);
68
69         // Destructor.  NumberlikeArray does the delete for us.
70         ~BigUnsignedInABase() {}
71
72         // LINKS TO BIGUNSIGNED
73         BigUnsignedInABase(const BigUnsigned &x, Base base);
74         operator BigUnsigned() const;
75
76         /* LINKS TO STRINGS
77          *
78          * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to
79          * represent digits of 0 through 35.  When parsing strings, lowercase is
80          * also accepted.
81          *
82          * All string representations are big-endian (big-place-value digits
83          * first).  (Computer scientists have adopted zero-based counting; why
84          * can't they tolerate little-endian numbers?)
85          *
86          * No string representation has a ``base indicator'' like ``0x''.
87          *
88          * An exception is made for zero: it is converted to ``0'' and not the
89          * empty string.
90          *
91          * If you want different conventions, write your own routines to go
92          * between BigUnsignedInABase and strings.  It's not hard.
93          */
94         operator std::string() const;
95         BigUnsignedInABase(const std::string &s, Base base);
96
97 public:
98
99         // ACCESSORS
100         Base getBase() const { return base; }
101
102         // Expose these from NumberlikeArray directly.
103         NumberlikeArray<Digit>::getCapacity;
104         NumberlikeArray<Digit>::getLength;
105
106         /* Returns the requested digit, or 0 if it is beyond the length (as if
107          * the number had 0s infinitely to the left). */
108         Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; }
109
110         // The number is zero if and only if the canonical length is zero.
111         bool isZero() const { return NumberlikeArray<Digit>::isEmpty(); }
112
113         /* Equality test.  For the purposes of this test, two BigUnsignedInABase
114          * values must have the same base to be equal. */ 
115         bool operator ==(const BigUnsignedInABase &x) const {
116                 return base == x.base && NumberlikeArray<Digit>::operator ==(x);
117         }
118         bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); }
119
120 };
121
122 #endif