gmp_override.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #ifndef BOOST_POLYGON_GMP_OVERRIDE_HPP
  8. #define BOOST_POLYGON_GMP_OVERRIDE_HPP
  9. #include <gmpxx.h>
  10. namespace boost { namespace polygon {
  11. class gmp_int {
  12. private:
  13. inline gmp_int(const mpq_class& input) : v_(input) {}
  14. public:
  15. inline gmp_int() {}
  16. explicit inline gmp_int(long input) : v_(input) {}
  17. inline gmp_int(const gmp_int& input) : v_(input.v_) {}
  18. inline gmp_int& operator=(const gmp_int& that) {
  19. v_ = that.v_;
  20. return (*this);
  21. }
  22. inline gmp_int& operator=(long that) {
  23. v_ = that;
  24. return (*this);
  25. }
  26. inline operator int() const {
  27. std::cout << "cast\n";
  28. mpz_class num = v_.get_num();
  29. mpz_class den = v_.get_den();
  30. num /= den;
  31. return num.get_si();
  32. }
  33. inline double get_d() const {
  34. return v_.get_d();
  35. }
  36. inline int get_num() const {
  37. return v_.get_num().get_si();
  38. }
  39. inline int get_den() const {
  40. return v_.get_den().get_si();
  41. }
  42. inline bool operator==(const gmp_int& that) const {
  43. return v_ == that.v_;
  44. }
  45. inline bool operator!=(const gmp_int& that) const {
  46. return v_ != that.v_;
  47. }
  48. inline bool operator<(const gmp_int& that) const {
  49. bool retval = v_ < that.v_;
  50. return retval;
  51. }
  52. inline bool operator<=(const gmp_int& that) const {
  53. return v_ <= that.v_;
  54. }
  55. inline bool operator>(const gmp_int& that) const {
  56. return v_ > that.v_;
  57. }
  58. inline bool operator>=(const gmp_int& that) const {
  59. return v_ >= that.v_;
  60. }
  61. inline gmp_int operator+(const gmp_int& b) {
  62. return gmp_int((*this).v_ + b.v_);
  63. }
  64. inline gmp_int operator-(const gmp_int& b) {
  65. return gmp_int((*this).v_ - b.v_);
  66. }
  67. inline gmp_int operator*(const gmp_int& b) {
  68. return gmp_int((*this).v_ * b.v_);
  69. }
  70. inline gmp_int operator/(const gmp_int& b) {
  71. return gmp_int((*this).v_ / b.v_);
  72. }
  73. inline gmp_int& operator+=(const gmp_int& b) {
  74. (*this).v_ += b.v_;
  75. return (*this);
  76. }
  77. inline gmp_int& operator-=(const gmp_int& b) {
  78. (*this).v_ -= b.v_;
  79. return (*this);
  80. }
  81. inline gmp_int& operator*=(const gmp_int& b) {
  82. (*this).v_ *= b.v_;
  83. return (*this);
  84. }
  85. inline gmp_int& operator/=(const gmp_int& b) {
  86. (*this).v_ /= b.v_;
  87. return (*this);
  88. }
  89. inline gmp_int& operator++() {
  90. ++v_;
  91. return (*this);
  92. }
  93. inline gmp_int& operator--() {
  94. --v_;
  95. return (*this);
  96. }
  97. inline gmp_int operator++(int) {
  98. gmp_int retval(*this);
  99. ++(*this);
  100. return retval;
  101. }
  102. inline gmp_int operator--(int) {
  103. gmp_int retval(*this);
  104. --(*this);
  105. return retval;
  106. }
  107. private:
  108. mpq_class v_;
  109. };
  110. template <>
  111. struct high_precision_type<int> {
  112. typedef mpq_class type;
  113. };
  114. template <>
  115. int convert_high_precision_type<int>(const mpq_class& v) {
  116. mpz_class num = v.get_num();
  117. mpz_class den = v.get_den();
  118. num /= den;
  119. return num.get_si();
  120. };
  121. }
  122. }
  123. #endif