biginteger.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // biginteger.h
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #pragma once
  8. #include <string>
  9. #include <vector>
  10. namespace math {
  11. class biginteger {
  12. private:
  13. template <typename Int>
  14. using is_signed_t =
  15. typename std::enable_if<std::numeric_limits<Int>::is_integer &&
  16. std::numeric_limits<Int>::is_signed,
  17. int>::type;
  18. template <typename Int>
  19. using is_unsigned_t =
  20. typename std::enable_if<std::numeric_limits<Int>::is_integer &&
  21. !std::numeric_limits<Int>::is_signed,
  22. int>::type;
  23. public:
  24. using data_type = std::vector<int32_t>;
  25. static biginteger const ZERO, ONE, NEGATIVE_ONE;
  26. static constexpr int32_t const MAX_SEG{999999999};
  27. static constexpr int32_t const OVER_SEG{1000000000};
  28. static constexpr int32_t const SEG_DIGITS{9};
  29. public:
  30. // Constructors
  31. biginteger();
  32. template <typename Int>
  33. biginteger(Int value, is_signed_t<Int> = 0)
  34. : biginteger(value < 0,
  35. static_cast<uint64_t>(value < 0 ? -value : value)) {}
  36. template <typename Int>
  37. biginteger(Int value, is_unsigned_t<Int> = 0)
  38. : biginteger(false, static_cast<uint64_t>(value)) {}
  39. biginteger(char const *);
  40. // Unary operators
  41. biginteger operator-() const;
  42. // Binary operators
  43. friend biginteger operator+(biginteger, biginteger const &);
  44. friend biginteger operator-(biginteger, biginteger const &);
  45. friend biginteger operator*(biginteger, biginteger const &);
  46. friend biginteger operator/(biginteger, biginteger const &);
  47. friend biginteger operator%(biginteger, biginteger const &);
  48. friend biginteger & operator+=(biginteger &, biginteger const &);
  49. friend biginteger & operator-=(biginteger &, biginteger const &);
  50. friend biginteger & operator*=(biginteger &, biginteger const &);
  51. friend biginteger & operator/=(biginteger &, biginteger const &);
  52. // Output
  53. std::string to_string() const;
  54. // Comparison
  55. friend bool operator==(biginteger const &, biginteger const &);
  56. friend bool operator!=(biginteger const &, biginteger const &);
  57. friend bool operator<=(biginteger const &, biginteger const &);
  58. friend bool operator<(biginteger const &, biginteger const &);
  59. friend bool operator>=(biginteger const &, biginteger const &);
  60. friend bool operator>(biginteger const &, biginteger const &);
  61. private:
  62. biginteger(bool, uint64_t);
  63. void subtract_impl(biginteger const & lhs, bool is_sub);
  64. friend void swap(biginteger & rhs, biginteger & lhs) {
  65. using std::swap;
  66. swap(rhs.is_negative, lhs.is_negative);
  67. swap(rhs.data, lhs.data);
  68. }
  69. bool is_negative;
  70. data_type data{};
  71. };
  72. }