boost::multi_index_container 具有 random_access 和ordered_unique

发布于 2024-08-10 17:56:00 字数 1877 浏览 10 评论 0原文

我在使用 boost::multi_index_container 同时使用随机访问和 orderd_unique 时遇到问题。 (我很抱歉这个冗长的问题,但我认为我应该使用一个例子..)

这里有一个例子:假设我想在工厂中生产 N 个对象,并且对于每个对象我都有一个要满足的需求(这个需求是已知的)创建多索引时)。 好吧,在我的算法中,我得到了中间结果,我将其存储在以下类中:

class intermediate_result
{
private:
    std::vector<int>   parts;     // which parts are produced
    int                used_time; // how long did it take to produce

    ValueType          max_value; // how much is it worth
};

向量 parts 描述了生成的对象(其长度为 N,按字典顺序小于我的 coresp 需求向量) !) - 对于每个这样的向量,我也知道used_time。此外,我还获得了生成对象的向量的值。

我有另一个约束,因此我无法生成每个对象 - 我的算法需要在数据结构中存储多个 intermediate_result 对象。这里使用了 boost::multi_index_container,因为 partsused_time 对描述了一个唯一的 intermediate_result(并且它在我的数据结构中应该是唯一的),但 max_value 是我必须考虑的另一个索引,因为我的算法始终需要具有最高 intermediate_result最大值

因此,我尝试将 boost::multi_index_containerordered_unique<> 一起用于我的“parts&used_time-pair”,并使用 ordered_non_unique<> 进行我的 max_value (不同的 intermediate_result - 对象可能具有相同的值)。

问题是:决定哪个“parts&used_time-pair”较小所需的谓词在我的 parts 向量上使用 std::lexicographyal_compare ,因此是对于许多 intermediate_result 对象来说非常慢。 但会有一个解决方案:我对每个对象的需求不是那么高,因此我可以根据其used_time在每个可能的部分向量上存储唯一的中间结果。

例如:如果我有一个需求向量 ( 2 , 3 , 1) 那么我需要一个存储 (2+1)*(3+1)*(1 +1)=24 可能的部分向量以及每个此类条目上不同的used_times,它们必须是唯一的! (存储最小的时间是不够的 - 例如:如果我的附加约束是:完全满足给定的生产时间)

但是如何将 random_access-index 与 结合起来order_unique<>-index?
Example11 没有帮助我就这个..)

I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the same time. (I'm sorry for the lengthly question, but I think I should use an example..)

Here an example: Suppose I want to produce N objects in a factory and for each object I have a demand to fulfill (this demand is known at creation of the multi-index).
Well, within my algorithm I get intermediate results, which I store in the following class:

class intermediate_result
{
private:
    std::vector<int>   parts;     // which parts are produced
    int                used_time; // how long did it take to produce

    ValueType          max_value; // how much is it worth
};

The vector parts descibes, which objects are produced (its length is N and it is lexicographically smaller then my coresp demand-vector!) - for each such vector I know the used_time as well. Additionally I get a value for this vector of produced objects.

I got another constraint so that I can't produce every object - my algorithm needs to store several intermediate_result-objects in a data-structure. And here boost::multi_index_container is used, because the pair of parts and used_time describes a unique intermediate_result (and it should be unique in my data-structure) but the max_value is another index I'll have to consider, because my algorithm always needs the intermediate_result with the highest max_value.

So I tried to use boost::multi_index_container with ordered_unique<> for my "parts&used_time-pair" and ordered_non_unique<> for my max_value (different intermediate_result-objects may have the same value).

The problem is: the predicate, which is needed to decide which "parts&used_time-pair" is smaller, uses std::lexicographical_compare on my parts-vector and hence is very slow for many intermediate_result-objects.
But there would be a solution: my demand for each object isn't that high, therefore I could store on each possible parts-vector the intermediate results uniquely by its used_time.

For example: if I have a demand-vector ( 2 , 3 , 1) then I need a data-structure which stores (2+1)*(3+1)*(1+1)=24 possible parts-vectors and on each such entry the different used_times, which have to be unique! (storing the smallest time is insufficient - for example: if my additional constraint is: to meet a given time exactly for production)

But how do I combine a random_access<>-index with an ordered_unique<>-index?
(Example11 didn't help me on this one..)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-08-17 17:56:00

要使用两个索引,您可以编写以下内容:

indexed_by<
  random_access< >,      
  ordered_unique< 
    composite_key< 
      intermediate_result,
      member<intermediate_result, int, &intermediate_result::used_time>,
      member<intermediate_result, std::vector<int>, &intermediate_result::parts>
    >
  >
>

您可以首先使用 composite_key 来比较 used_time,然后仅在必要时使用 vector。除此之外,请记住您可以使用成员函数作为索引。

To use two indices you could write the following:

indexed_by<
  random_access< >,      
  ordered_unique< 
    composite_key< 
      intermediate_result,
      member<intermediate_result, int, &intermediate_result::used_time>,
      member<intermediate_result, std::vector<int>, &intermediate_result::parts>
    >
  >
>

You could use composite_key for comparing used_time at first and vector only if necessary. Besides that, keep in mind that you could use member function as index.

谈场末日恋爱 2024-08-17 17:56:00

(我必须使用自己的答案来编写代码块 - 抱歉!)

带有 used_timepartscomposite_key (如 Kirill V. Lyadvinsky建议)基本上是我已经实现的。我想摆脱 parts-向量的字典比较。

假设我已经以某种方式存储了need_demand,那么我可以编写一个简单的函数,它在随机访问数据结构中返回正确的索引,如下所示:

int get_index(intermediate_result &input_result) const
{
    int ret_value  = 0;
    int index_part = 1;
    for(int i=0;i<needed_demand.size();++i)
    {
        ret_value  += input_result.get_part(i) * index_part;
        index_part *= (needed_demand.get_part(i) + 1);
    }
}

显然,这可以更有效地实现,并且这不是唯一可能的索引排序需要的需求。但是我们假设这个函数作为 intermediate_result 的成员函数存在!是否可以编写这样的内容来防止 lexicographyal_compare ?

indexed_by<
  random_access< >,      
  ordered_unique< 
    composite_key< 
      intermediate_result,
      member<intermediate_result, int, &intermediate_result::used_time>,
      const_mem_fun<intermediate_result,int,&intermediate_result::get_index>
    >
  >
>

如果这是可能的,并且我用所有可能的 parts-向量初始化了多重索引(即在上面的评论中,我会在我的数据结构中推送 24 个空映射),这是否找到了正确的在恒定时间内输入给定的 intermediate_result(使用 get_index 计算正确的索引后)?
我必须问这个问题,因为我不太明白 random_access<> 索引是如何与 ordered_unique<> 索引链接的。

但是谢谢你到目前为止你的答案!!

(I had to use an own answer to write code-blocks - sorry!)

The composite_key with used_time and parts (as Kirill V. Lyadvinsky suggested) is basically what I've already implemented. I want to get rid of the lexicographical compare of the parts-vector.

Suppose I've stored the needed_demand somehow then I could write a simple function, which returns the correct index within a random-access data-structure like that:

int get_index(intermediate_result &input_result) const
{
    int ret_value  = 0;
    int index_part = 1;
    for(int i=0;i<needed_demand.size();++i)
    {
        ret_value  += input_result.get_part(i) * index_part;
        index_part *= (needed_demand.get_part(i) + 1);
    }
}

Obviously this can be implemented more efficiently and this is not the only possible index ordering for the needed demand. But let's suppose this function exists as a member-function of intermediate_result! Is it possible to write something like this to prevent lexicographical_compare ?

indexed_by<
  random_access< >,      
  ordered_unique< 
    composite_key< 
      intermediate_result,
      member<intermediate_result, int, &intermediate_result::used_time>,
      const_mem_fun<intermediate_result,int,&intermediate_result::get_index>
    >
  >
>

If this is possible and I initialized the multi-index with all possible parts-vectors (i.e. in my comment above I would've pushed 24 empty maps in my data-structure), does this find the right entry for a given intermediate_result in constant time (after computing the correct index with get_index) ?
I have to ask this, because I don't quite see, how the random_access<> index is linked with the ordered_unique<> index..

But thank you for your answers so far!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文