Boost Multi Index:基于列表内容的索引

Boost Multi Index: index based on list content

本文关键字:列表 索引 Index Multi 于列表 Boost      更新时间:2023-10-16

我是Boost多索引容器的新手,想知道它是否能以更有效的方式解决我的问题,简化如下:

struct A {
   int id;
}
struct B {
   int id;
   std::list<A> products;
}

每个A有一个严格唯一的ID,我希望能够与一个单一的多索引容器,能够寻找B。B的id和A的id。

此刻,我正在使用很好的std::map,并将A id链接到B id。可以这么说。它工作得很好。但是,对于这样的查询,我有其他参数,它变得非常讨厌:)

编辑:

根据评论要求,我将详细说明:

我有卫星,而卫星又有许多应答器和许多源。我希望能够找到一个给定的应答器id或源id的卫星(这确实是唯一的)

遗憾的是,我没有手在卫星结构,这意味着,我不能改变它。

简单来说就是:

struct Satellite {
 int norad_id;
 std::list<Transponder> transponders;
 std::list<Source> sources;
 ... some more data
}

我想做的就是简单地搜索任何卫星,并找到具有特定应答器-或源-或norad id的卫星。

目前,我正在使用3个漂亮的地图

std::map<int /*norad*/ ,Satellite> satellites;
std::map<int /*transponder  id*/, int/* norad */> transponder_to_satellite;
std::map<int /* source_id */,  int /* norad */ > source_to_satellite;

从@sehe提供的示例中,我看到如果生成一个关系结构体会更容易一些。我想我会试一试……:)

在缺少确切用例的情况下,这里有一些基于您所展示的对索引建模的建议¹

struct Product {
int id;
};
struct Category {
int id;
};
struct ProductCategoryRelation {
    int productId;
    int categoryId;
};
namespace bmi = boost::multi_index;
using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct by_product>,
            bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
        >,
        bmi::ordered_unique<
            bmi::tag<struct by_category>,
            bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>
        >
    >
>;

您也可以使用组合键,这在ordered_*索引中是通用的:

using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct by_product>,
            bmi::composite_key<ProductCategoryRelation,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
            >
        >
    >
>;

这里有一个小的演示:

Live On Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <list>
struct Product {
   int id;
};
struct Category {
   int id;
};
struct ProductCategoryRelation {
    int productId;
    int categoryId;
};
namespace bmi = boost::multi_index;
using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct compound>,
            bmi::composite_key<ProductCategoryRelation,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
            >
        >
    >
>;
#include <iostream>
#include <boost/range/iterator_range.hpp>
int main() {
    RelationTable table {
        ProductCategoryRelation { 1, 7 },
        ProductCategoryRelation { 2, 7 },
        ProductCategoryRelation { 3, 7 },
        ProductCategoryRelation { 4, 6 },
        ProductCategoryRelation { 5, 6 },
        ProductCategoryRelation { 6, 6 },
        ProductCategoryRelation { 7, 5 },
        ProductCategoryRelation { 8, 5 },
        ProductCategoryRelation { 9, 5 },
    };
    // find all products in category 6:
    for (auto& rel : boost::make_iterator_range(table.get<compound>().equal_range(6)))
        std::cout << "Product " << rel.productId << " is in category " << rel.categoryId << "n";
}

打印:

Product 4 is in category 6
Product 5 is in category 6
Product 6 is in category 6