Simplified commodity_history_t

This commit is contained in:
John Wiegley 2012-03-06 23:05:06 -06:00
parent 75603edeb4
commit 17782c9cfe
2 changed files with 24 additions and 73 deletions

View file

@ -67,9 +67,6 @@ void commodity_history_t::add_price(const commodity_t& source,
if (! result.second) {
// There is already an entry for this moment, so update it
(*result.first).second = price;
} else {
last_reftime = none; // invalidate the FGraph cache
last_oldest = none;
}
}
@ -85,26 +82,20 @@ void commodity_history_t::remove_price(const commodity_t& source,
// jww (2012-03-04): If it fails, should we give a warning?
prices.erase(date);
last_reftime = none; // invalidate the FGraph cache
last_oldest = none;
}
void commodity_history_t::map_prices(function<void(datetime_t,
const amount_t&)> fn,
const commodity_t& source,
const datetime_t& moment,
const optional<datetime_t>& _oldest)
const optional<datetime_t>& oldest)
{
vertex_descriptor sv = vertex(*source.graph_index(), price_graph);
reftime = moment;
oldest = _oldest;
FGraph fg(price_graph,
recent_edge_weight<EdgeWeightMap, PricePointMap, PriceRatioMap>
(get(edge_weight, price_graph), pricemap, ratiomap,
&reftime, &last_reftime, &oldest, &last_oldest));
moment, oldest));
FNameMap namemap(get(vertex_name, fg));
@ -118,7 +109,7 @@ void commodity_history_t::map_prices(function<void(datetime_t,
foreach (const price_map_t::value_type& pair, prices) {
const datetime_t& when(pair.first);
if ((! _oldest || when >= *_oldest) && when <= moment) {
if ((! oldest || when >= *oldest) && when <= moment) {
if (pair.second.commodity() == source) {
amount_t price(pair.second);
price.in_place_invert();
@ -136,17 +127,14 @@ void commodity_history_t::map_prices(function<void(datetime_t,
optional<price_point_t>
commodity_history_t::find_price(const commodity_t& source,
const datetime_t& moment,
const optional<datetime_t>& _oldest)
const optional<datetime_t>& oldest)
{
vertex_descriptor sv = vertex(*source.graph_index(), price_graph);
reftime = moment;
oldest = _oldest;
FGraph fg(price_graph,
recent_edge_weight<EdgeWeightMap, PricePointMap, PriceRatioMap>
(get(edge_weight, price_graph), pricemap, ratiomap,
&reftime, &last_reftime, &oldest, &last_oldest));
moment, oldest));
FNameMap namemap(get(vertex_name, fg));
@ -188,9 +176,6 @@ commodity_history_t::find_price(const commodity_t& source,
DEBUG("history.find", "price is = " << price.unrounded());
}
last_reftime = reftime; // invalidate the FGraph cache
last_oldest = oldest;
if (price.is_null()) {
DEBUG("history.find", "there is no final price");
return none;
@ -204,18 +189,15 @@ optional<price_point_t>
commodity_history_t::find_price(const commodity_t& source,
const commodity_t& target,
const datetime_t& moment,
const optional<datetime_t>& _oldest)
const optional<datetime_t>& oldest)
{
vertex_descriptor sv = vertex(*source.graph_index(), price_graph);
vertex_descriptor tv = vertex(*target.graph_index(), price_graph);
reftime = moment;
oldest = _oldest;
FGraph fg(price_graph,
recent_edge_weight<EdgeWeightMap, PricePointMap, PriceRatioMap>
(get(edge_weight, price_graph), pricemap, ratiomap,
&reftime, &last_reftime, &oldest, &last_oldest));
moment, oldest));
FNameMap namemap(get(vertex_name, fg));
@ -289,9 +271,6 @@ commodity_history_t::find_price(const commodity_t& source,
DEBUG("history.find", "last target now = " << last_target->symbol());
}
last_reftime = reftime; // invalidate the FGraph cache
last_oldest = oldest;
if (price.is_null()) {
DEBUG("history.find", "there is no final price");
return none;
@ -321,18 +300,10 @@ void commodity_history_t::print_map(std::ostream& out,
const optional<datetime_t>& moment)
{
if (moment) {
reftime = *moment;
oldest = none;
FGraph fg(price_graph,
recent_edge_weight<EdgeWeightMap, PricePointMap, PriceRatioMap>
(get(edge_weight, price_graph), pricemap, ratiomap,
&reftime, &last_reftime, &oldest, &last_oldest));
(get(edge_weight, price_graph), pricemap, ratiomap, *moment));
write_graphviz(out, fg, label_writer<FNameMap>(get(vertex_name, fg)));
last_reftime = reftime;
last_oldest = none;
} else {
write_graphviz(out, price_graph,
label_writer<NameMap>(get(vertex_name, price_graph)));

View file

@ -70,39 +70,25 @@ public:
PricePointMap price_point;
PriceRatioMap ratios;
datetime_t * reftime;
optional<datetime_t> * last_reftime;
optional<datetime_t> * oldest;
optional<datetime_t> * last_oldest;
datetime_t reftime;
optional<datetime_t> oldest;
recent_edge_weight() { }
recent_edge_weight(EdgeWeightMap _weight,
PricePointMap _price_point,
PriceRatioMap _ratios,
datetime_t * _reftime,
optional<datetime_t> * _last_reftime,
optional<datetime_t> * _oldest,
optional<datetime_t> * _last_oldest)
recent_edge_weight(EdgeWeightMap _weight,
PricePointMap _price_point,
PriceRatioMap _ratios,
datetime_t _reftime,
const optional<datetime_t>& _oldest = none)
: weight(_weight), price_point(_price_point), ratios(_ratios),
reftime(_reftime), last_reftime(_last_reftime),
oldest(_oldest), last_oldest(_last_oldest) { }
reftime(_reftime), oldest(_oldest) { }
template <typename Edge>
bool operator()(const Edge& e) const
{
DEBUG("history.find", " reftime = " << *reftime);
if (*last_reftime)
DEBUG("history.find", " last_reftime = " << **last_reftime);
if (*oldest)
DEBUG("history.find", " oldest = " << **oldest);
if (*last_oldest)
DEBUG("history.find", " last_oldest = " << **last_oldest);
#if 0
if (*last_reftime && *reftime == **last_reftime &&
*oldest == *last_oldest) {
DEBUG("history.find", " using previous reftime");
return get(weight, e) != std::numeric_limits<std::size_t>::max();
#if defined(DEBUG_ON)
DEBUG("history.find", " reftime = " << reftime);
if (oldest) {
DEBUG("history.find", " oldest = " << *oldest);
}
#endif
@ -113,22 +99,22 @@ public:
return false;
}
price_map_t::const_iterator low = prices.upper_bound(*reftime);
price_map_t::const_iterator low = prices.upper_bound(reftime);
if (low != prices.end() && low == prices.begin()) {
DEBUG("history.find", " don't use this edge");
put(weight, e, std::numeric_limits<std::size_t>::max());
return false;
} else {
--low;
assert(((*low).first <= *reftime));
assert(((*low).first <= reftime));
if (*oldest && (*low).first < **oldest) {
if (oldest && (*low).first < *oldest) {
DEBUG("history.find", " edge is out of range");
put(weight, e, std::numeric_limits<std::size_t>::max());
return false;
}
long secs = (*reftime - (*low).first).total_seconds();
long secs = (reftime - (*low).first).total_seconds();
assert(secs >= 0);
put(weight, e, secs);
@ -191,12 +177,6 @@ public:
PriceRatioMap> > FGraph;
typedef property_map<FGraph, vertex_name_t>::type FNameMap;
// jww (2012-03-05): Prevents threading
mutable datetime_t reftime;
mutable optional<datetime_t> last_reftime;
mutable optional<datetime_t> oldest;
mutable optional<datetime_t> last_oldest;
commodity_history_t()
: indexmap(get(vertex_index, price_graph)),
pricemap(get(edge_price_point, price_graph)),