Update the "Running the sample program produces this output:" comment in
[bigint/bigint.git] / BigUnsignedInABase.cc
CommitLineData
05780f4b
MM
1/*
2* Matt McCutchen's Big Integer Library
05780f4b
MM
3*/
4
b3fe29df
MM
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
05780f4b
MM
14#include "BigUnsignedInABase.hh"
15
16namespace {
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}
a8b42b68 29
05780f4b 30BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
2f145f11 31
05780f4b
MM
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
4efbb076 40 int maxBitLenOfX = x.getLength() * BigUnsigned::N;
05780f4b
MM
41 int minBitsPerDigit = bitLen(base) - 1;
42 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
2f145f11
MM
43 len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'.
44 allocate(len); // Get the space
05780f4b
MM
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
2f145f11 59 // Save the actual length.
05780f4b
MM
60 len = digitNum;
61}
62
63BigUnsignedInABase::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
74BigUnsignedInABase::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
a8b42b68
MM
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());
05780f4b
MM
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
103BigUnsignedInABase::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");
b3fe29df
MM
108 char *s = new char[len + 1];
109 s[len] = '\0';
05780f4b
MM
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)
b3fe29df 115 s[symbolNumInString] = char('0' + theDigit);
05780f4b 116 else
b3fe29df 117 s[symbolNumInString] = char('A' + theDigit - 10);
05780f4b 118 }
2f145f11 119 std::string s2(s);
be1bdfe2
MM
120 // 2006.05.03: This needs to be [] to match the allocation
121 delete [] s;
2f145f11 122 return s2;
05780f4b 123}