Old snapshot `BigIntegerLibrary-2005.01.16'; see the ChangeLog file.
[bigint/bigint.git] / BigUnsignedInABase.cc
1 /*
2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
4 */
5
6 /*
7 * Milan Tomic had trouble compiling this file on Microsoft
8 * Visual C++ 6 because, in the libraries that come with
9 * Visual C++ 6, the `std::string::push_back' method apparently
10 * does not exist.  To get around the problem, I rewrote
11 * `BigUnsignedInABase::operator std::string' (at the bottom
12 * of this file) so it doesn't use `push_back'.
13 */
14
15 #include "BigUnsignedInABase.hh"
16
17 namespace {
18         unsigned int bitLen(unsigned int x) {
19                 unsigned int len = 0;
20                 while (x > 0) {
21                         x >>= 1;
22                         len++;
23                 }
24                 return len;
25         }
26         unsigned int ceilingDiv(unsigned int a, unsigned int b) {
27                 return (a + b - 1) / b;
28         }
29 }
30
31 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
32
33         // Check the base
34         if (base < 2)
35                 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
36         // Save the base.
37         // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
38         this->base = base;
39         
40         // Get an upper bound on how much space we need
41         int maxBitLenOfX = x.getLength() * BigUnsigned::N;
42         int minBitsPerDigit = bitLen(base) - 1;
43         int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
44         len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'.
45         allocate(len); // Get the space
46         
47         BigUnsigned x2(x), buBase(base);
48         Index digitNum = 0;
49         
50         while (!x2.isZero()) {
51                 // Get last digit.  This is like `lastDigit = x2 % buBase, x2 /= buBase'.
52                 BigUnsigned lastDigit(x2);
53                 lastDigit.divideWithRemainder(buBase, x2);
54                 // Save the digit.
55                 blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter
56                 // Move on.  We can't run out of room: we figured it out above.
57                 digitNum++;
58         }
59         
60         // Save the actual length.
61         len = digitNum;
62 }
63
64 BigUnsignedInABase::operator BigUnsigned() const {
65         BigUnsigned ans(0), buBase(base), temp;
66         Index digitNum = len;
67         while (digitNum > 0) {
68                 digitNum--;
69                 temp.multiply(ans, buBase);
70                 ans.add(temp, BigUnsigned(blk[digitNum]));
71         }
72         return ans;
73 }
74
75 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
76         // Check the base.
77         if (base > 36)
78                 throw "BigUnsignedInABase(std::string, Base): The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36.  You tried a conversion with a base over 36; write your own string conversion routine.";
79         // Save the base.
80         // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
81         this->base = base;
82         
83         // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
84         // also known as an `unsigned int'.  Some compilers warn without this cast.
85         len = Index(s.length());
86         allocate(len);
87         
88         Index digitNum, symbolNumInString;
89         for (digitNum = 0; digitNum < len; digitNum++) {
90                 symbolNumInString = len - 1 - digitNum;
91                 char theSymbol = s[symbolNumInString];
92                 if (theSymbol >= '0' && theSymbol <= '9')
93                         blk[digitNum] = theSymbol - '0';
94                 else if (theSymbol >= 'A' && theSymbol <= 'Z')
95                         blk[digitNum] = theSymbol - 'A' + 10;
96                 else if (theSymbol >= 'a' && theSymbol <= 'z')
97                         blk[digitNum] = theSymbol - 'a' + 10;
98                 else
99                         throw "BigUnsignedInABase(std::string, Base): Bad symbol in input.  Only 0-9, A-Z, a-z are accepted.";
100         }
101         zapLeadingZeros();
102 }
103
104 BigUnsignedInABase::operator std::string() const {
105         if (base > 36)
106                 throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36.  You tried a conversion with a base over 36; write your own string conversion routine.";
107         if (len == 0)
108                 return std::string("0");
109         char *s = new char[len + 1];
110         s[len] = '\0';
111         Index digitNum, symbolNumInString;
112         for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
113                 digitNum = len - 1 - symbolNumInString;
114                 Digit theDigit = blk[digitNum];
115                 if (theDigit < 10)
116                         s[symbolNumInString] = char('0' + theDigit);
117                 else
118                         s[symbolNumInString] = char('A' + theDigit - 10);
119         }
120         std::string s2(s);
121         delete s;
122         return s2;
123 }