Assume I have the following Order structure:
namespace data
{
using OrderId = int64_t;
using TimePoint = std::chrono::steady_clock::time_point;
struct Order
{
OrderId id;
double price;
TimePoint updateTime;
double average() const
{
return price;
}
};
}
How to by a given price p iterate over the orders as if I have a similar orders table in a database and run the following query?
SELECT * FROM orders WHERE average > p ORDER BY updateTime DESC;
average is SQL is Order::average() that is something different than price in real-life project.
I was able to define a multi-index:
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/key.hpp>
struct IdTag {};
struct DateTag {};
struct PriceTag {};
using IdKey = boost::multi_index::ordered_unique<boost::multi_index::tag<IdTag>, boost::multi_index::key<&data::Order::id>>;
using DateKey = boost::multi_index::ordered_non_unique<boost::multi_index::tag<DateTag>, boost::multi_index::key<&data::Order::updateTime>>;
using PriceKey = boost::multi_index::ordered_non_unique<boost::multi_index::tag<PriceTag>, boost::multi_index::key<&data::Order::average>>;
using DatePriceDic = boost::multi_index_container<
//data::Order,
std::reference_wrapper<data::Order>,
boost::multi_index::indexed_by<IdKey, DateKey, PriceKey>>;
using IdIndex = DatePriceDic::index<IdTag>::type;
using DateIndex = DatePriceDic::index<DateTag>::type;
using PriceIndex = DatePriceDic::index<PriceTag>::type;
and found examples with equal_range and lower_bound, but as far as I can guess I need something else.
You have to do it in two steps:
PriceTagindex and store the results in a vector of reference wrappers.Live Coliru Demo
Output