X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/2f145f11d5d5ab979a7f5a5e3b26fc9882dc345c..6e1e0f2f3c2fee3e1e9df39c6d4816566c10aafb:/BigUnsignedInABase.cc diff --git a/BigUnsignedInABase.cc b/BigUnsignedInABase.cc index 8852575..c9b2906 100644 --- a/BigUnsignedInABase.cc +++ b/BigUnsignedInABase.cc @@ -1,19 +1,17 @@ /* -* Matt McCutchen's Big Integer Library -* http://mysite.verizon.net/mccutchen/bigint/ -*/ + * Matt McCutchen's Big Integer Library + */ /* -* Milan Tomic had trouble compiling this file on Microsoft -* Visual C++ 6 because, in the libraries that come with -* Visual C++ 6, the `std::string::push_back' method apparently -* does not exist. To get around the problem, I rewrote -* `BigUnsignedInABase::operator std::string' (at the bottom -* of this file) so it doesn't use `push_back'. -*/ + * Milan Tomic had trouble compiling this file on Microsoft + * Visual C++ 6 because, in the libraries that come with + * Visual C++ 6, the `std::string::push_back' method apparently + * does not exist. To get around the problem, I rewrote + * `BigUnsignedInABase::operator std::string' (at the bottom + * of this file) so it doesn't use `push_back'. + */ #include "BigUnsignedInABase.hh" -#include namespace { unsigned int bitLen(unsigned int x) { @@ -28,9 +26,7 @@ namespace { return (a + b - 1) / b; } } - /*std::cout << "((( BigUnsigned ==> BigUnsignedInABase\n"; - std::cout << "[ Parameter BigUnsigned @ " << (void *)(NumberlikeArray *)(&x) - << ",\nresulting BigUnsignedInABase @ " << (void *)(NumberlikeArray *)(this) << "]" << std::endl;*/ + BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { // Check the base @@ -39,17 +35,17 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { // Save the base. // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java. this->base = base; - + // Get an upper bound on how much space we need - int maxBitLenOfX = x.getLength() * 8 * sizeof(BigUnsigned::Blk); + int maxBitLenOfX = x.getLength() * BigUnsigned::N; int minBitsPerDigit = bitLen(base) - 1; int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit); len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'. allocate(len); // Get the space - + BigUnsigned x2(x), buBase(base); Index digitNum = 0; - + while (!x2.isZero()) { // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'. BigUnsigned lastDigit(x2); @@ -59,10 +55,9 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { // Move on. We can't run out of room: we figured it out above. digitNum++; } - + // Save the actual length. len = digitNum; - /*std::cout << "BigUnsigned ==> BigUnsignedInABase )))\n";*/ } BigUnsignedInABase::operator BigUnsigned() const { @@ -83,10 +78,12 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { // Save the base. // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java. this->base = base; - - len = s.length(); + + // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index', + // also known as an `unsigned int'. Some compilers warn without this cast. + len = Index(s.length()); allocate(len); - + Index digitNum, symbolNumInString; for (digitNum = 0; digitNum < len; digitNum++) { symbolNumInString = len - 1 - digitNum; @@ -104,7 +101,6 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { } BigUnsignedInABase::operator std::string() const { - //std::cout << "((( BigUnsignedInABase ==> std::string\n"; if (base > 36) 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."; if (len == 0) @@ -121,7 +117,7 @@ BigUnsignedInABase::operator std::string() const { s[symbolNumInString] = char('A' + theDigit - 10); } std::string s2(s); - delete s; - //std::cout << "BigUnsignedInABase ==> std::string )))\n"; + // 2006.05.03: This needs to be [] to match the allocation + delete [] s; return s2; }