Ваше сообщение не предоставляет MCVE , и код в нем содержит ряд ошибок, которые мешают его компиляции даже после добавления необходимого дополнительного кода (заголовков и прочего).
Итак, я переписал вещь, исправляющую ошибки, которые я нашел по пути, вы можете найти результат ниже. Что касается вашего основного вопроса, вам не нужны ранжированные индексы, для этого достаточно упорядоченных индексов. То, что вы хотите, это сумма orderQuantity
s блоков с одинаковыми productID
, desiredPrice
и маркой покупки / продажи и меньшим значением priorityAtPrice
, чем у данного блока: вам нужен индекс, который группирует блоки по (productID
, desiredPrice
, buy
, priorityAtPrice
):
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<ObtainSublistByProductPriceAndBuy>
,boost::multi_index::composite_key<
Block,
boost::multi_index::member<Block, const int32_t, &Block::productID>
,boost::multi_index::member<Block, const int64_t, &Block::desiredPrice>
,boost::multi_index::member<Block, bool, &Block::buy>
,boost::multi_index::member<Block, const uint64_t, &Block::priorityAtPrice>
>
>
и функция, которая извлекает соответствующий диапазон и выполняет сумму величин:
uint32_t placeInLine(const BlockDatabase& bd, const Block& b)
{
auto& index = bd.get<ObtainSublistByProductPriceAndBuy>();
auto first = index.lower_bound(
std::make_tuple(b.productID, b.desiredPrice, b.buy));
auto last = index.lower_bound(
std::make_tuple(b.productID, b.desiredPrice, b.buy, b.priorityAtPrice));
return std::accumulate(
first, last, uint32_t(0),
[](uint32_t n, const Block* pb){ return n + pb->orderQuantity; });
}
CompleteПример:
Live On Coliru
#include <array>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <iostream>
#include <numeric>
struct Block{
const int32_t productID;
mutable uint32_t orderQuantity;
const int64_t desiredPrice;
mutable uint64_t priorityAtPrice;
const uint64_t blockID;
bool buy;
bool operator<(const Block& lilBlock)const{return blockID<lilBlock.blockID;}
};
struct ObtainElementByBlockID {};
struct RandomAccess {};
struct ObtainSublistByProductAndPrice {};
struct ObtainSublistByProductPriceAndBuy {};
typedef boost::multi_index_container<
Block*,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<
boost::multi_index::tag<ObtainElementByBlockID>
,boost::multi_index::member<Block,const uint64_t,&Block::blockID>
>
,boost::multi_index::random_access<
boost::multi_index::tag<RandomAccess>
>
,boost::multi_index::ordered_non_unique<
boost::multi_index::tag<ObtainSublistByProductAndPrice>
,boost::multi_index::composite_key<
Block,
boost::multi_index::member<Block, const int32_t, &Block::productID>
,boost::multi_index::member<Block, const int64_t, &Block::desiredPrice>
,boost::multi_index::member<Block, const uint64_t, &Block::priorityAtPrice>
>
>
,boost::multi_index::ordered_non_unique<
boost::multi_index::tag<ObtainSublistByProductPriceAndBuy>
,boost::multi_index::composite_key<
Block,
boost::multi_index::member<Block, const int32_t, &Block::productID>
,boost::multi_index::member<Block, const int64_t, &Block::desiredPrice>
,boost::multi_index::member<Block, bool, &Block::buy>
,boost::multi_index::member<Block, const uint64_t, &Block::priorityAtPrice>
>
>
>
> BlockDatabase;
uint32_t placeInLine(const BlockDatabase& bd, const Block& b)
{
auto& index = bd.get<ObtainSublistByProductPriceAndBuy>();
auto first = index.lower_bound(
std::make_tuple(b.productID, b.desiredPrice, b.buy));
auto last = index.lower_bound(
std::make_tuple(b.productID, b.desiredPrice, b.buy, b.priorityAtPrice));
return std::accumulate(
first, last, uint32_t(0),
[](uint32_t n, const Block* pb){ return n + pb->orderQuantity; });
}
int main()
{
const int32_t chairsID = 0;
auto blocks = std::array{
Block{chairsID, 5, 5, 2, 753, true},
Block{chairsID, 12, 5, 6, 54, true},
Block{chairsID, 3, 5, 896, 712, true}
};
BlockDatabase bd;
for(auto& b: blocks) bd.insert(&b);
// retrieve block with blockID 712
auto it = bd.find(712);
// output sum of quantities for blocks with same productID, desiredPrice
// and buy tag and lower priorityAtPrice
std::cout << placeInLine(bd, **it) << "\n";
}
Выход
17