Lookup commodity prices recursively, searching for the newest data.

This commit is contained in:
John Wiegley 2009-01-19 19:59:49 -04:00
parent 85be211f99
commit 52e140d14e
2 changed files with 42 additions and 28 deletions

View file

@ -43,7 +43,8 @@
namespace ledger { namespace ledger {
void commodity_t::base_t::history_t::add_price(const datetime_t& date, void commodity_t::base_t::history_t::add_price(const commodity_t& source,
const datetime_t& date,
const amount_t& price, const amount_t& price,
const bool reflexive) const bool reflexive)
{ {
@ -59,7 +60,9 @@ void commodity_t::base_t::history_t::add_price(const datetime_t& date,
} }
if (reflexive) { if (reflexive) {
price.commodity().add_price(date, *one / price, false); amount_t inverse(*one / price);
inverse.set_commodity(const_cast<commodity_t&>(source));
price.commodity().add_price(date, inverse, false);
} }
} }
@ -73,7 +76,9 @@ bool commodity_t::base_t::history_t::remove_price(const datetime_t& date)
return false; return false;
} }
void commodity_t::base_t::varied_history_t::add_price(const datetime_t& date, void commodity_t::base_t::varied_history_t::
add_price(const commodity_t& source,
const datetime_t& date,
const amount_t& price, const amount_t& price,
const bool reflexive) const bool reflexive)
{ {
@ -90,7 +95,7 @@ void commodity_t::base_t::varied_history_t::add_price(const datetime_t& date,
} }
assert(hist); assert(hist);
hist->add_price(date, price, reflexive); hist->add_price(source, date, price, reflexive);
} }
bool commodity_t::base_t::varied_history_t::remove_price(const datetime_t& date, bool commodity_t::base_t::varied_history_t::remove_price(const datetime_t& date,
@ -112,7 +117,7 @@ optional<price_point_t>
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent , const int indent
#endif #endif
) ) const
{ {
price_point_t point; price_point_t point;
bool found = false; bool found = false;
@ -219,7 +224,7 @@ optional<price_point_t>
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent , const int indent
#endif #endif
) ) const
{ {
optional<price_point_t> point; optional<price_point_t> point;
optional<datetime_t> limit = oldest; optional<datetime_t> limit = oldest;
@ -295,7 +300,8 @@ optional<price_point_t>
assert(! commodity || point->price.commodity() == *commodity); assert(! commodity || point->price.commodity() == *commodity);
DEBUG_INDENT("commodity.prices", indent + 1); DEBUG_INDENT("commodity.prices", indent + 1);
DEBUG("commodity.prices", " saw a price there: " << point->price); DEBUG("commodity.prices",
" saw a price there: " << point->price << " from " << point->when);
if (! limit || point->when > *limit) { if (! limit || point->when > *limit) {
limit = point->when; limit = point->when;
best = *point; best = *point;
@ -325,7 +331,7 @@ optional<price_point_t>
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent , const int indent
#endif #endif
) ) const
{ {
foreach (commodity_t * commodity, commodities) { foreach (commodity_t * commodity, commodities) {
if (optional<price_point_t> point = find_price(source, *commodity, if (optional<price_point_t> point = find_price(source, *commodity,
@ -753,8 +759,7 @@ commodity_pool_t::commodity_pool_t() : default_commodity(NULL)
{ {
TRACE_CTOR(commodity_pool_t, ""); TRACE_CTOR(commodity_pool_t, "");
null_commodity = create(""); null_commodity = create("");
null_commodity->add_flags(COMMODITY_STYLE_NOMARKET | null_commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_STYLE_NOMARKET);
COMMODITY_STYLE_BUILTIN);
} }
commodity_t * commodity_pool_t::create(const string& symbol) commodity_t * commodity_pool_t::create(const string& symbol)

View file

@ -77,7 +77,9 @@ public:
history_map prices; history_map prices;
ptime last_lookup; ptime last_lookup;
void add_price(const datetime_t& date, const amount_t& price, void add_price(const commodity_t& source,
const datetime_t& date,
const amount_t& price,
const bool reflexive = true); const bool reflexive = true);
bool remove_price(const datetime_t& date); bool remove_price(const datetime_t& date);
@ -89,7 +91,7 @@ public:
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent = 0 , const int indent = 0
#endif #endif
); ) const;
}; };
typedef std::map<commodity_t *, history_t> history_by_commodity_map; typedef std::map<commodity_t *, history_t> history_by_commodity_map;
@ -98,7 +100,9 @@ public:
{ {
history_by_commodity_map histories; history_by_commodity_map histories;
void add_price(const datetime_t& date, const amount_t& price, void add_price(const commodity_t& source,
const datetime_t& date,
const amount_t& price,
const bool reflexive = true); const bool reflexive = true);
bool remove_price(const datetime_t& date, commodity_t& commodity); bool remove_price(const datetime_t& date, commodity_t& commodity);
@ -110,7 +114,7 @@ public:
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent = 0 , const int indent = 0
#endif #endif
); ) const;
optional<price_point_t> optional<price_point_t>
find_price(const commodity_t& source, find_price(const commodity_t& source,
const std::vector<commodity_t *>& commodities, const std::vector<commodity_t *>& commodities,
@ -119,7 +123,7 @@ public:
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent = 0 , const int indent = 0
#endif #endif
); ) const;
optional<history_t&> optional<history_t&>
history(const optional<commodity_t&>& commodity = none); history(const optional<commodity_t&>& commodity = none);
@ -270,7 +274,7 @@ public:
if (! base->varied_history) if (! base->varied_history)
base->varied_history = varied_history_t(); base->varied_history = varied_history_t();
base->varied_history->add_price(date, price, reflexive); base->varied_history->add_price(*this, date, price, reflexive);
} }
bool remove_price(const datetime_t& date, commodity_t& commodity) { bool remove_price(const datetime_t& date, commodity_t& commodity) {
if (base->varied_history) if (base->varied_history)
@ -285,13 +289,18 @@ public:
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent = 0 , const int indent = 0
#endif #endif
) { ) const {
if (base->varied_history) if (base->varied_history && ! has_flags(COMMODITY_WALKED)) {
return base->varied_history->find_price(*this, commodity, moment, oldest const_cast<commodity_t&>(*this).add_flags(COMMODITY_WALKED);
optional<price_point_t> point =
base->varied_history->find_price(*this, commodity, moment, oldest
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, indent , indent
#endif #endif
); );
const_cast<commodity_t&>(*this).drop_flags(COMMODITY_WALKED);
return point;
}
return none; return none;
} }
@ -302,7 +311,7 @@ public:
#if defined(DEBUG_ON) #if defined(DEBUG_ON)
, const int indent = 0 , const int indent = 0
#endif #endif
) { ) const {
if (base->varied_history) if (base->varied_history)
return base->varied_history->find_price(*this, commodities, moment, oldest return base->varied_history->find_price(*this, commodities, moment, oldest
#if defined(DEBUG_ON) #if defined(DEBUG_ON)