带有比较运算符的boost :: multi_index_container复合键中的equal_range [英] equal_range in boost::multi_index_container composite key with comparision operator

查看:269
本文介绍了带有比较运算符的boost :: multi_index_container复合键中的equal_range的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从多索引容器中查询结果,其中值类型是三个元素的结构.给定第一个值,但第二个和第三个必须大于或小于查询参数.

I'm trying to query results from a multi index container where the value type is a struct of three elements. The first value is given, but the second and third have to be greater or less than a query parameter.

搜索后,我发现必须实现自定义的密钥提取器,并且此处的某些链接建议相同,但是我无法实现它:

After searching around, I found that a custom Key extractor has to be implemented, and some of the links here suggest the same, but I am unable to implement it:

  • boost::multi_index user-defined key extractor and composite key
  • https://www.boost.org/doc/libs/1_62_0/libs/multi_index/test/test_composite_key.cpp

任何人都可以帮助我使其正常工作吗?

Can anyone help me get it working?

下面是我的struct和multiindex实现:

Below is my struct and multiindex implementation:

#define RANKFILTERVIEW 0

struct TPQ {
    int UID;
    int Value;
    int Rank;
    TPQ():UID(0),Value(0),Rank(0)
    { }
    TPQ(int _T, int _V, int _R):UID(_T),Value(_V),Rank(_R)
    { }
};

typedef bip::allocator<
    TPQ,bip::managed_shared_memory::segment_manager
> shared_struct_allocator;

typedef bmi::multi_index_container<
    TPQ,
    bmi::indexed_by<
        bmi::ordered_unique<bmi::tag<struct Composite>,
            bmi::composite_key<TPQ,
                bmi::member<TPQ, int,&TPQ::UID>,
                bmi::member<TPQ, int,&TPQ::Value>,
                bmi::member<TPQ, int,&TPQ::Rank>
        > >
    >,
    shared_struct_allocator
> Rank_Set;

typedef nth_index<Rank_Set, RANKFILTERVIEW>::type Rank_view;

int main()
{
    bip::managed_shared_memory segment(bip::open_only,"RANKSOTRE");

    int UID =52478;

    std::pair<Rank_Set*, std::size_t> RankOrderRecord=segment.find<Rank_Set>("RANKDATARECORD");

    /// Here I want the result as stated below.
    auto range = RankOrderRecord.first->get<Composite>().equal_range(boost::make_tuple(UID,_2>500,_3>5));

}

我有一组指令,要求忽略排序或重新排列.

I have a set of instruction that sorting or re-arranging shall be ignored.

推荐答案

您几乎明白了.实际上,您可以通过 partial 键查询订单索引.由于这些字段是常规整数类型,因此很容易得出一个好的上下限:

You almost got it. Indeed, in order indices you can query by partial key. Since the fields are regular integral types it's pretty easy to come up with a good lower and upper bound:

auto range = boost::make_iterator_range(
    view.lower_bound(boost::make_tuple(UID, 501, 6)),
    view.lower_bound(boost::make_tuple(UID+1)));

这是完整的演示:

在Coliru上直播

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/ordered_index.hpp>

#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> // for Coliru

#include <boost/range/iterator_range.hpp>

#include <iostream>

namespace bmi = boost::multi_index;
namespace bip = boost::interprocess;

struct TPQ {
    int UID   = 0;
    int Value = 0;
    int Rank  = 0;

    TPQ() = default;
    TPQ(int _T, int _V, int _R) : UID(_T), Value(_V), Rank(_R) {}
};

static inline std::ostream& operator<<(std::ostream& os, TPQ const& tpq) {
    return os << "{ UID: " << tpq.UID
              << ", Value: " << tpq.Value
              << ", Rank: " << tpq.Rank << "}";
}

using shared_struct_allocator = bip::allocator<TPQ, bip::managed_mapped_file::segment_manager>;

using Rank_Set = bmi::multi_index_container<
    TPQ,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct RankFilterView>,
            bmi::composite_key<TPQ,
                bmi::member<TPQ, int, &TPQ::UID>, 
                bmi::member<TPQ, int, &TPQ::Value>,
                bmi::member<TPQ, int, &TPQ::Rank>
            >
        >
    >,
    shared_struct_allocator>;

using Rank_view = bmi::index<Rank_Set, RankFilterView>::type;

int main() {
    bip::managed_mapped_file segment(bip::open_or_create, "RANKSOTRE", 10*1024);
    auto& table = *segment.find_or_construct<Rank_Set>("RANKDATARECORD")(segment.get_segment_manager());

    table.insert({
        {52478, 501, 6}, // Match!
        {52478, 500, 6}, // - Value too small
        {52479, 0,   0}, // - UID too high
        {52478, 502, 6}, // Match!
        {52478, 502, 7}, // Match!
        {52478, 501, 5}, // - Rank too small
        {52477, 502, 7}, // - UID too small
        {52478, 999, 9}, // Match!
    });

    int UID = 52478;
    Rank_view& view = table.get<RankFilterView>(); 

    auto range = boost::make_iterator_range(
        view.lower_bound(boost::make_tuple(UID, 501, 6)),
        view.upper_bound(UID));

    for (TPQ const& tpq : range) {
        std::cout << tpq << "\n";
    }

}

仅会按预期打印:

{ UID: 52478, Value: 501, Rank: 6}
{ UID: 52478, Value: 502, Rank: 6}
{ UID: 52478, Value: 502, Rank: 7}
{ UID: 52478, Value: 999, Rank: 9}

这篇关于带有比较运算符的boost :: multi_index_container复合键中的equal_range的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆