BigUnsignedInABase.hh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef BIGUNSIGNEDINABASE_H
  2. #define BIGUNSIGNEDINABASE_H
  3. #include "NumberlikeArray.hh"
  4. #include "BigUnsigned.hh"
  5. #include <string>
  6. /*
  7. * A BigUnsignedInABase object represents a nonnegative integer of size limited
  8. * only by available memory, represented in a user-specified base that can fit
  9. * in an `unsigned short' (most can, and this saves memory).
  10. *
  11. * BigUnsignedInABase is intended as an intermediary class with little
  12. * functionality of its own. BigUnsignedInABase objects can be constructed
  13. * from, and converted to, BigUnsigneds (requiring multiplication, mods, etc.)
  14. * and `std::string's (by switching digit values for appropriate characters).
  15. *
  16. * BigUnsignedInABase is similar to BigUnsigned. Note the following:
  17. *
  18. * (1) They represent the number in exactly the same way, except that
  19. * BigUnsignedInABase uses ``digits'' (or Digit) where BigUnsigned uses
  20. * ``blocks'' (or Blk).
  21. *
  22. * (2) Both use the management features of NumberlikeArray. (In fact, my desire
  23. * to add a BigUnsignedInABase class without duplicating a lot of code led me to
  24. * introduce NumberlikeArray.)
  25. *
  26. * (3) The only arithmetic operation supported by BigUnsignedInABase is an
  27. * equality test. Use BigUnsigned for arithmetic.
  28. */
  29. class BigUnsignedInABase : protected NumberlikeArray<unsigned short> {
  30. public:
  31. // The digits of a BigUnsignedInABase are unsigned shorts.
  32. typedef unsigned short Digit;
  33. // That's also the type of a base.
  34. typedef Digit Base;
  35. protected:
  36. // The base in which this BigUnsignedInABase is expressed
  37. Base base;
  38. // Creates a BigUnsignedInABase with a capacity; for internal use.
  39. BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {}
  40. // Decreases len to eliminate any leading zero digits.
  41. void zapLeadingZeros() {
  42. while (len > 0 && blk[len - 1] == 0)
  43. len--;
  44. }
  45. public:
  46. // Constructs zero in base 2.
  47. BigUnsignedInABase() : NumberlikeArray<Digit>(), base(2) {}
  48. // Copy constructor
  49. BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray<Digit>(x), base(x.base) {}
  50. // Assignment operator
  51. void operator =(const BigUnsignedInABase &x) {
  52. NumberlikeArray<Digit>::operator =(x);
  53. base = x.base;
  54. }
  55. // Constructor that copies from a given array of digits.
  56. BigUnsignedInABase(const Digit *d, Index l, Base base);
  57. // Destructor. NumberlikeArray does the delete for us.
  58. ~BigUnsignedInABase() {}
  59. // LINKS TO BIGUNSIGNED
  60. BigUnsignedInABase(const BigUnsigned &x, Base base);
  61. operator BigUnsigned() const;
  62. /* LINKS TO STRINGS
  63. *
  64. * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to
  65. * represent digits of 0 through 35. When parsing strings, lowercase is
  66. * also accepted.
  67. *
  68. * All string representations are big-endian (big-place-value digits
  69. * first). (Computer scientists have adopted zero-based counting; why
  70. * can't they tolerate little-endian numbers?)
  71. *
  72. * No string representation has a ``base indicator'' like ``0x''.
  73. *
  74. * An exception is made for zero: it is converted to ``0'' and not the
  75. * empty string.
  76. *
  77. * If you want different conventions, write your own routines to go
  78. * between BigUnsignedInABase and strings. It's not hard.
  79. */
  80. operator std::string() const;
  81. BigUnsignedInABase(const std::string &s, Base base);
  82. public:
  83. // ACCESSORS
  84. Base getBase() const { return base; }
  85. // Expose these from NumberlikeArray directly.
  86. using NumberlikeArray<Digit>::getCapacity;
  87. using NumberlikeArray<Digit>::getLength;
  88. /* Returns the requested digit, or 0 if it is beyond the length (as if
  89. * the number had 0s infinitely to the left). */
  90. Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; }
  91. // The number is zero if and only if the canonical length is zero.
  92. bool isZero() const { return NumberlikeArray<Digit>::isEmpty(); }
  93. /* Equality test. For the purposes of this test, two BigUnsignedInABase
  94. * values must have the same base to be equal. */
  95. bool operator ==(const BigUnsignedInABase &x) const {
  96. return base == x.base && NumberlikeArray<Digit>::operator ==(x);
  97. }
  98. bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); }
  99. };
  100. #endif