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