- Improve comments for the new BigUnsigned accessors.
[bigint/bigint.git] / BigUnsignedInABase.hh
CommitLineData
b35b6967
MM
1#ifndef BIGUNSIGNEDINABASE_H
2#define BIGUNSIGNEDINABASE_H
05780f4b
MM
3
4#include "NumberlikeArray.hh"
5#include "BigUnsigned.hh"
6#include <string>
7
8/*
6e1e0f2f
MM
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 */
05780f4b
MM
34
35class BigUnsignedInABase : protected NumberlikeArray<unsigned short> {
5ff40cf5 36
05780f4b 37 // TYPES
2301f99c 38public:
05780f4b
MM
39 typedef unsigned short Digit; // The digit type that BigUnsignedInABases are built from
40 typedef Digit Base;
5ff40cf5 41
05780f4b 42 // FIELDS
2301f99c 43protected:
05780f4b 44 Base base; // The base of this BigUnsignedInABase
5ff40cf5 45
05780f4b 46 // MANAGEMENT
2301f99c 47protected:
05780f4b
MM
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++.
5ff40cf5 50
05780f4b 51 BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {} // Creates a BigUnsignedInABase with a capacity
5ff40cf5 52
05780f4b
MM
53 void zapLeadingZeros() { // Decreases len to eliminate leading zeros
54 while (len > 0 && blk[len - 1] == 0)
55 len--;
56 }
5ff40cf5 57
05780f4b
MM
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
5ff40cf5 60
2301f99c 61public:
05780f4b
MM
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
5ff40cf5 64
05780f4b
MM
65 void operator =(const BigUnsignedInABase &x) { // Assignment operator
66 NumberlikeArray<Digit>::operator =(x);
67 base = x.base;
68 }
5ff40cf5 69
05780f4b
MM
70 BigUnsignedInABase(const Digit *d, Index l) : NumberlikeArray<Digit>(d, l) { // Constructor from an array of digits
71 zapLeadingZeros();
72 }
5ff40cf5 73
05780f4b
MM
74 // LINKS TO BIGUNSIGNED
75 BigUnsignedInABase(const BigUnsigned &x, Base base);
76 operator BigUnsigned() const;
5ff40cf5 77
05780f4b 78 /* LINKS TO STRINGS
6e1e0f2f
MM
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 */
05780f4b
MM
94 operator std::string() const;
95 BigUnsignedInABase(const std::string &s, Base base);
5ff40cf5 96
05780f4b
MM
97 // PICKING APART
98 // These accessors can be used to get the pieces of the number
2301f99c 99public:
05780f4b
MM
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
5ff40cf5 108
05780f4b 109 // EQUALITY TEST
2301f99c 110public:
05780f4b
MM
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); }
5ff40cf5 116
05780f4b
MM
117};
118
119#endif