From 55d58940ceb6a47e4032bbad83909d1ca82d2e03 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 17 Aug 2004 22:18:38 -0400 Subject: speed improvements; my "bal" script is cut to a third --- amount.cc | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 17 deletions(-) (limited to 'amount.cc') diff --git a/amount.cc b/amount.cc index a8f128cf..1ba5da7f 100644 --- a/amount.cc +++ b/amount.cc @@ -254,16 +254,25 @@ amount_t& amount_t::negate() return *this; } -// comparisons to zero +// integer comparisons +template +static inline void parse_num(amount_t& amt, T num) { + std::string str; + { std::ostringstream strstr(str); + strstr << num; + } + { std::istringstream strstr(str); + amt.parse(strstr); + } +} + bool amount_t::operator<(const int num) const { if (num == 0) { return quantity ? mpz_sgn(MPZ(quantity)) < 0 : false; } else { - std::string str; - std::ostringstream strstr(str); - strstr << num; - amount_t amt(strstr.str()); + amount_t amt; + parse_num(amt, num); return *this < amt; } } @@ -273,10 +282,8 @@ bool amount_t::operator<=(const int num) const if (num == 0) { return quantity ? mpz_sgn(MPZ(quantity)) <= 0 : true; } else { - std::string str; - std::ostringstream strstr(str); - strstr << num; - amount_t amt(strstr.str()); + amount_t amt; + parse_num(amt, num); return *this <= amt; } } @@ -286,10 +293,8 @@ bool amount_t::operator>(const int num) const if (num == 0) { return quantity ? mpz_sgn(MPZ(quantity)) > 0 : false; } else { - std::string str; - std::ostringstream strstr(str); - strstr << num; - amount_t amt(strstr.str()); + amount_t amt; + parse_num(amt, num); return *this > amt; } } @@ -299,14 +304,67 @@ bool amount_t::operator>=(const int num) const if (num == 0) { return quantity ? mpz_sgn(MPZ(quantity)) >= 0 : true; } else { - std::string str; - std::ostringstream strstr(str); - strstr << num; - amount_t amt(strstr.str()); + amount_t amt; + parse_num(amt, num); return *this >= amt; } } +bool amount_t::operator<(const unsigned int num) const +{ + if (num == 0) { + return quantity ? mpz_sgn(MPZ(quantity)) < 0 : false; + } else { + amount_t amt; + parse_num(amt, num); + return *this < amt; + } +} + +bool amount_t::operator<=(const unsigned int num) const +{ + if (num == 0) { + return quantity ? mpz_sgn(MPZ(quantity)) <= 0 : true; + } else { + amount_t amt; + parse_num(amt, num); + return *this <= amt; + } +} + +bool amount_t::operator>(const unsigned int num) const +{ + if (num == 0) { + return quantity ? mpz_sgn(MPZ(quantity)) > 0 : false; + } else { + amount_t amt; + parse_num(amt, num); + return *this > amt; + } +} + +bool amount_t::operator>=(const unsigned int num) const +{ + if (num == 0) { + return quantity ? mpz_sgn(MPZ(quantity)) >= 0 : true; + } else { + amount_t amt; + parse_num(amt, num); + return *this >= amt; + } +} + +bool amount_t::operator==(const unsigned int num) const +{ + if (num == 0) { + return quantity ? mpz_sgn(MPZ(quantity)) == 0 : true; + } else { + amount_t amt; + parse_num(amt, num); + return *this == amt; + } +} + // comparisons between amounts bool amount_t::operator<(const amount_t& amt) const { -- cgit v1.2.3