Old snapshot `BigIntegerLibrary-2005.01.06'; 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         // Check the base
33         if (base < 2)
34                 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
35         // Save the base.
36         // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
37         this->base = base;
38         
39         // Get an upper bound on how much space we need
40         int maxBitLenOfX = x.getLength() * 8 * sizeof(BigUnsigned::Blk);
41         int minBitsPerDigit = bitLen(base) - 1;
42         int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
43         allocate(maxDigitLenOfX); // Get the space
44         
45         BigUnsigned x2(x), buBase(base);
46         Index digitNum = 0;
47         
48         while (!x2.isZero()) {
49                 // Get last digit.  This is like `lastDigit = x2 % buBase, x2 /= buBase'.
50                 BigUnsigned lastDigit(x2);
51                 lastDigit.divideWithRemainder(buBase, x2);
52                 // Save the digit.
53                 blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter
54                 // Move on.  We can't run out of room: we figured it out above.
55                 digitNum++;
56         }
57         
58         // Save the eventual length.
59         len = digitNum;
60 }
61
62 BigUnsignedInABase::operator BigUnsigned() const {
63         BigUnsigned ans(0), buBase(base), temp;
64         Index digitNum = len;
65         while (digitNum > 0) {
66                 digitNum--;
67                 temp.multiply(ans, buBase);
68                 ans.add(temp, BigUnsigned(blk[digitNum]));
69         }
70         return ans;
71 }
72
73 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
74         // Check the base.
75         if (base > 36)
76                 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.";
77         // Save the base.
78         // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
79         this->base = base;
80         
81         len = s.length();
82         allocate(len);
83         
84         Index digitNum, symbolNumInString;
85         for (digitNum = 0; digitNum < len; digitNum++) {
86                 symbolNumInString = len - 1 - digitNum;
87                 char theSymbol = s[symbolNumInString];
88                 if (theSymbol >= '0' && theSymbol <= '9')
89                         blk[digitNum] = theSymbol - '0';
90                 else if (theSymbol >= 'A' && theSymbol <= 'Z')
91                         blk[digitNum] = theSymbol - 'A' + 10;
92                 else if (theSymbol >= 'a' && theSymbol <= 'z')
93                         blk[digitNum] = theSymbol - 'a' + 10;
94                 else
95                         throw "BigUnsignedInABase(std::string, Base): Bad symbol in input.  Only 0-9, A-Z, a-z are accepted.";
96         }
97         zapLeadingZeros();
98 }
99
100 BigUnsignedInABase::operator std::string() const {
101         if (base > 36)
102                 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.";
103         if (len == 0)
104                 return std::string("0");
105         char *s = new char[len + 1];
106         s[len] = '\0';
107         Index digitNum, symbolNumInString;
108         for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
109                 digitNum = len - 1 - symbolNumInString;
110                 Digit theDigit = blk[digitNum];
111                 if (theDigit < 10)
112                         s[symbolNumInString] = char('0' + theDigit);
113                 else
114                         s[symbolNumInString] = char('A' + theDigit - 10);
115         }
116         return std::string(s);
117 }