BigUnsignedInABase.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "BigUnsignedInABase.hh"
  2. BigUnsignedInABase::BigUnsignedInABase(const Digit *d, Index l, Base base)
  3. : NumberlikeArray<Digit>(d, l), base(base) {
  4. // Check the base
  5. if (base < 2)
  6. throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): The base must be at least 2";
  7. // Validate the digits.
  8. for (Index i = 0; i < l; i++)
  9. if (blk[i] >= base)
  10. throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base";
  11. // Eliminate any leading zeros we may have been passed.
  12. zapLeadingZeros();
  13. }
  14. namespace {
  15. unsigned int bitLen(unsigned int x) {
  16. unsigned int len = 0;
  17. while (x > 0) {
  18. x >>= 1;
  19. len++;
  20. }
  21. return len;
  22. }
  23. unsigned int ceilingDiv(unsigned int a, unsigned int b) {
  24. return (a + b - 1) / b;
  25. }
  26. }
  27. BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
  28. // Check the base
  29. if (base < 2)
  30. throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
  31. this->base = base;
  32. // Get an upper bound on how much space we need
  33. int maxBitLenOfX = x.getLength() * BigUnsigned::N;
  34. int minBitsPerDigit = bitLen(base) - 1;
  35. int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
  36. len = maxDigitLenOfX; // Another change to comply with `staying in bounds'.
  37. allocate(len); // Get the space
  38. BigUnsigned x2(x), buBase(base);
  39. Index digitNum = 0;
  40. while (!x2.isZero()) {
  41. // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
  42. BigUnsigned lastDigit(x2);
  43. lastDigit.divideWithRemainder(buBase, x2);
  44. // Save the digit.
  45. blk[digitNum] = lastDigit.toUnsignedShort();
  46. // Move on. We can't run out of room: we figured it out above.
  47. digitNum++;
  48. }
  49. // Save the actual length.
  50. len = digitNum;
  51. }
  52. BigUnsignedInABase::operator BigUnsigned() const {
  53. BigUnsigned ans(0), buBase(base), temp;
  54. Index digitNum = len;
  55. while (digitNum > 0) {
  56. digitNum--;
  57. temp.multiply(ans, buBase);
  58. ans.add(temp, BigUnsigned(blk[digitNum]));
  59. }
  60. return ans;
  61. }
  62. BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
  63. // Check the base.
  64. if (base > 36)
  65. 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.";
  66. // Save the base.
  67. // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
  68. this->base = base;
  69. // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
  70. // also known as an `unsigned int'. Some compilers warn without this cast.
  71. len = Index(s.length());
  72. allocate(len);
  73. Index digitNum, symbolNumInString;
  74. for (digitNum = 0; digitNum < len; digitNum++) {
  75. symbolNumInString = len - 1 - digitNum;
  76. char theSymbol = s[symbolNumInString];
  77. if (theSymbol >= '0' && theSymbol <= '9')
  78. blk[digitNum] = theSymbol - '0';
  79. else if (theSymbol >= 'A' && theSymbol <= 'Z')
  80. blk[digitNum] = theSymbol - 'A' + 10;
  81. else if (theSymbol >= 'a' && theSymbol <= 'z')
  82. blk[digitNum] = theSymbol - 'a' + 10;
  83. else
  84. throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
  85. if (blk[digitNum] >= base)
  86. throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base";
  87. }
  88. zapLeadingZeros();
  89. }
  90. BigUnsignedInABase::operator std::string() const {
  91. if (base > 36)
  92. 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.";
  93. if (len == 0)
  94. return std::string("0");
  95. // Some compilers don't have push_back, so use a char * buffer instead.
  96. char *s = new char[len + 1];
  97. s[len] = '\0';
  98. Index digitNum, symbolNumInString;
  99. for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
  100. digitNum = len - 1 - symbolNumInString;
  101. Digit theDigit = blk[digitNum];
  102. if (theDigit < 10)
  103. s[symbolNumInString] = char('0' + theDigit);
  104. else
  105. s[symbolNumInString] = char('A' + theDigit - 10);
  106. }
  107. std::string s2(s);
  108. delete [] s;
  109. return s2;
  110. }