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