(value): Use `std::map<>::lower_bound' to search for the nearest price
item in a commodity's history list. This is much more efficient than doing a reverse linear search.
This commit is contained in:
parent
6a1071b289
commit
675319a0b0
1 changed files with 11 additions and 6 deletions
17
amount.cc
17
amount.cc
|
|
@ -1173,15 +1173,20 @@ amount_t commodity_t::value(const std::time_t moment)
|
||||||
std::time_t age = 0;
|
std::time_t age = 0;
|
||||||
amount_t price;
|
amount_t price;
|
||||||
|
|
||||||
if (history)
|
if (history) {
|
||||||
for (history_map::reverse_iterator i = history->prices.rbegin();
|
if (moment == 0) {
|
||||||
i != history->prices.rend();
|
history_map::reverse_iterator i = history->prices.rbegin();
|
||||||
i++)
|
age = (*i).first;
|
||||||
if (moment == 0 || std::difftime(moment, (*i).first) >= 0) {
|
price = (*i).second;
|
||||||
|
} else {
|
||||||
|
history_map::iterator i = history->prices.lower_bound(moment);
|
||||||
|
if (i != history->prices.begin()) {
|
||||||
|
--i;
|
||||||
age = (*i).first;
|
age = (*i).first;
|
||||||
price = (*i).second;
|
price = (*i).second;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (updater)
|
if (updater)
|
||||||
(*updater)(*this, moment, age,
|
(*updater)(*this, moment, age,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue