From 009dd3969a5cf965a685702b71aee53bf957b206 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 19 Jan 2009 17:48:27 -0400 Subject: Added a recursive, date-based commodity price searching capability. This makes it possible to find all possible prices for a commodity by walking the map of pricing relationships to all other commodities, even if the relation is distant. --- src/commodity.h | 94 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 27 deletions(-) (limited to 'src/commodity.h') diff --git a/src/commodity.h b/src/commodity.h index 1277b2c2..6c9a7586 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -52,6 +52,12 @@ namespace ledger { DECLARE_EXCEPTION(commodity_error, std::runtime_error); +struct price_point_t +{ + datetime_t when; + amount_t price; +}; + class commodity_t : public delegates_flags<>, public equality_comparable1 @@ -71,10 +77,19 @@ public: history_map prices; ptime last_lookup; - void add_price(const datetime_t& date, const amount_t& price); + void add_price(const datetime_t& date, const amount_t& price, + const bool reflexive = true); bool remove_price(const datetime_t& date); - optional find_price(const optional& moment = none); + optional + find_price(const commodity_t& source, + const optional& commodity, + const optional& moment = none, + const optional& oldest = none +#if defined(DEBUG_ON) + , const int indent = 0 +#endif + ); }; typedef std::map history_by_commodity_map; @@ -83,15 +98,28 @@ public: { history_by_commodity_map histories; - void add_price(const datetime_t& date, const amount_t& price); + void add_price(const datetime_t& date, const amount_t& price, + const bool reflexive = true); bool remove_price(const datetime_t& date, commodity_t& commodity); - optional - find_price(const optional& commodity = none, - const optional& moment = none); - optional - find_price(const std::vector& commodities, - const optional& moment = none); + optional + find_price(const commodity_t& source, + const optional& commodity = none, + const optional& moment = none, + const optional& oldest = none +#if defined(DEBUG_ON) + , const int indent = 0 +#endif + ); + optional + find_price(const commodity_t& source, + const std::vector& commodities, + const optional& moment = none, + const optional& oldest = none +#if defined(DEBUG_ON) + , const int indent = 0 +#endif + ); optional history(const optional& commodity = none); @@ -229,25 +257,19 @@ protected: return none; } - optional - history(const optional& commodity); - optional - history(const std::vector& commodities); - - optional - find_price(commodity_t& commodity, - const optional& moment, - std::vector& bools); + optional history(const optional& commodity); + optional history(const std::vector& commodities); public: // These methods provide a transparent pass-through to the underlying // base->varied_history object. - void add_price(const datetime_t& date, const amount_t& price) { + void add_price(const datetime_t& date, const amount_t& price, + const bool reflexive = true) { if (! base->varied_history) base->varied_history = varied_history_t(); - base->varied_history->add_price(date, price); + base->varied_history->add_price(date, price, reflexive); } bool remove_price(const datetime_t& date, commodity_t& commodity) { if (base->varied_history) @@ -255,19 +277,37 @@ public: return false; } - optional - find_price(const optional& commodity = none, - const optional& moment = none) { + optional + find_price(const optional& commodity = none, + const optional& moment = none, + const optional& oldest = none +#if defined(DEBUG_ON) + , const int indent = 0 +#endif + ) { if (base->varied_history) - return base->varied_history->find_price(commodity, moment); + return base->varied_history->find_price(*this, commodity, moment, oldest +#if defined(DEBUG_ON) + , indent +#endif + ); return none; } - optional + optional find_price(const std::vector& commodities, - const optional& moment = none) { + const optional& moment = none, + const optional& oldest = none +#if defined(DEBUG_ON) + , const int indent = 0 +#endif + ) { if (base->varied_history) - return base->varied_history->find_price(commodities, moment); + return base->varied_history->find_price(*this, commodities, moment, oldest +#if defined(DEBUG_ON) + , indent +#endif + ); return none; } -- cgit v1.2.3