boost::multi_index_container 具有 random_access 和ordered_unique
我在使用 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
,因为 parts
和 used_time
对描述了一个唯一的 intermediate_result
(并且它在我的数据结构中应该是唯一的),但 max_value
是我必须考虑的另一个索引,因为我的算法始终需要具有最高 的
。intermediate_result
最大值
因此,我尝试将 boost::multi_index_container
与 ordered_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用两个索引,您可以编写以下内容:
您可以首先使用
composite_key
来比较used_time
,然后仅在必要时使用vector
。除此之外,请记住您可以使用成员函数作为索引。To use two indices you could write the following:
You could use
composite_key
for comparingused_time
at first andvector
only if necessary. Besides that, keep in mind that you could use member function as index.(我必须使用自己的答案来编写代码块 - 抱歉!)
带有
used_time
和parts
的composite_key
(如 Kirill V. Lyadvinsky建议)基本上是我已经实现的。我想摆脱parts
-向量的字典比较。假设我已经以某种方式存储了need_demand,那么我可以编写一个简单的函数,它在随机访问数据结构中返回正确的索引,如下所示:
显然,这可以更有效地实现,并且这不是唯一可能的索引排序需要的需求。但是我们假设这个函数作为
intermediate_result
的成员函数存在!是否可以编写这样的内容来防止 lexicographyal_compare ?如果这是可能的,并且我用所有可能的
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
withused_time
andparts
(as Kirill V. Lyadvinsky suggested) is basically what I've already implemented. I want to get rid of the lexicographical compare of theparts
-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:
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 preventlexicographical_compare
?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 givenintermediate_result
in constant time (after computing the correct index withget_index
) ?I have to ask this, because I don't quite see, how the
random_access<>
index is linked with theordered_unique<>
index..But thank you for your answers so far!!