Removed some unnecessary variables.

This commit is contained in:
John Wiegley 2007-05-06 10:29:12 +00:00
parent b95aa8d4ca
commit 7868a53b70
3 changed files with 155 additions and 143 deletions

View file

@ -56,18 +56,28 @@ bool amount_t::keep_tag = false;
bool amount_t::full_strings = false; bool amount_t::full_strings = false;
#ifndef THREADSAFE
/**
* These global temporaries are pre-initialized for the sake of
* efficiency, and reused over and over again.
*/
static mpz_t temp;
static mpz_t divisor;
#endif
struct amount_t::bigint_t
{
#define BIGINT_BULK_ALLOC 0x01 #define BIGINT_BULK_ALLOC 0x01
#define BIGINT_KEEP_PREC 0x02 #define BIGINT_KEEP_PREC 0x02
class amount_t::bigint_t
{
public:
mpz_t val; mpz_t val;
precision_t prec; precision_t prec;
flags_t flags; flags_t flags;
uint_least16_t ref; uint_least16_t ref;
uint_fast32_t index; uint_fast32_t index;
#define MPZ(bigint) ((bigint)->val)
bigint_t() : prec(0), flags(0), ref(1), index(0) { bigint_t() : prec(0), flags(0), ref(1), index(0) {
TRACE_CTOR(bigint_t, ""); TRACE_CTOR(bigint_t, "");
mpz_init(val); mpz_init(val);
@ -82,21 +92,12 @@ class amount_t::bigint_t
TRACE_CTOR(bigint_t, "copy"); TRACE_CTOR(bigint_t, "copy");
mpz_init_set(val, other.val); mpz_init_set(val, other.val);
} }
~bigint_t(); ~bigint_t() {
};
#define MPZ(x) ((x)->val)
#ifndef THREADSAFE
static mpz_t temp; // these are the global temp variables
static mpz_t divisor;
#endif
inline amount_t::bigint_t::~bigint_t() {
TRACE_DTOR(bigint_t); TRACE_DTOR(bigint_t);
assert(ref == 0); assert(ref == 0);
mpz_clear(val); mpz_clear(val);
} }
};
void amount_t::initialize() void amount_t::initialize()
{ {
@ -582,6 +583,11 @@ amount_t& amount_t::operator/=(const amount_t& amt)
} }
amount_t::precision_t amount_t::precision() const
{
return quantity->prec;
}
amount_t& amount_t::in_place_negate() amount_t& amount_t::in_place_negate()
{ {
if (quantity) { if (quantity) {
@ -805,7 +811,8 @@ annotation_t amount_t::annotation_details() const
return annotation_t(); return annotation_t();
} }
static void parse_quantity(std::istream& in, string& value) namespace {
void parse_quantity(std::istream& in, string& value)
{ {
char buf[256]; char buf[256];
char c = peek_next_nonws(in); char c = peek_next_nonws(in);
@ -888,7 +895,7 @@ bool parse_annotations(std::istream& in, annotation_t& details)
// may have only specified {$1} or something similar. // may have only specified {$1} or something similar.
if (temp.has_commodity() && if (temp.has_commodity() &&
temp.quantity->prec < temp.commodity().precision()) temp.precision() < temp.commodity().precision())
temp = temp.round(); // no need to retain individual precision temp = temp.round(); // no need to retain individual precision
details.price = temp; details.price = temp;
@ -935,6 +942,7 @@ bool parse_annotations(std::istream& in, annotation_t& details)
return details; return details;
} }
}
void amount_t::parse(std::istream& in, flags_t flags) void amount_t::parse(std::istream& in, flags_t flags)
{ {

View file

@ -148,7 +148,7 @@ protected:
void _clear(); void _clear();
void _release(); void _release();
class bigint_t; struct bigint_t;
bigint_t * quantity; bigint_t * quantity;
commodity_t * commodity_; commodity_t * commodity_;
@ -278,6 +278,11 @@ public:
* Unary arithmetic operators. There are several unary methods * Unary arithmetic operators. There are several unary methods
* support on amounts: * support on amounts:
* *
* precision() return an amount's current, internal precision. To
* find the precision it will be displayed at -- assuming it was not
* created using the static method `amount_t::exact' -- refer to
* commodity().precision.
*
* negate(), also unary minus (- x), returns the negated value of an * negate(), also unary minus (- x), returns the negated value of an
* amount. * amount.
* *
@ -318,6 +323,8 @@ public:
* in_place_reduce() * in_place_reduce()
* in_place_unreduce() * in_place_unreduce()
*/ */
precision_t precision() const;
amount_t negate() const { amount_t negate() const {
amount_t temp = *this; amount_t temp = *this;
temp.in_place_negate(); temp.in_place_negate();
@ -624,9 +631,6 @@ public:
} }
bool valid() const; bool valid() const;
private:
friend bool parse_annotations(std::istream& in, annotation_t& details);
}; };
inline amount_t amount_t::exact(const string& value) { inline amount_t amount_t::exact(const string& value) {