在 C++ 中枚举 k 维超立方体顶点的最有效方法是什么?
基本问题:我有 ak 维度盒子。我有一个上限和下限的向量。枚举顶点坐标的最有效方法是什么?
背景:举个例子,假设我有一个 3 维盒子。获得最有效的算法/代码是什么:
vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 )
vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 )
vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 )
vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 )
vertex[4] = ( 1, 0, 0 ) -> ( U_0, L_1, L_2 )
vertex[5] = ( 1, 0, 1 ) -> ( U_0, L_1, U_2 )
vertex[6] = ( 1, 1, 0 ) -> ( U_0, U_1, L_2 )
vertex[7] = ( 1, 1, 1 ) -> ( U_0, U_1, U_2 )
其中L_0对应于下界向量的第0个元素&同样,U_2 是上限向量的第二个元素。
我的代码:
const unsigned int nVertices = ((unsigned int)(floor(std::pow( 2.0, double(nDimensions)))));
for ( unsigned int idx=0; idx < nVertices; ++idx )
{
for ( unsigned int k=0; k < nDimensions; ++k )
{
if ( 0x00000001 & (idx >> k) )
{
bound[idx][k] = upperBound[k];
}
else
{
bound[idx][k] = lowerBound[k];
}
}
}
其中变量bound
声明为:
std::vector< std::vector<double> > bound(nVertices);
但我已经预先调整了它的大小,以免在分配内存的循环中浪费时间。每次运行算法时,我都需要调用上述过程大约 50,000,000 次——所以我需要它非常高效。
可能的子问题:移动 k 是否比总是移动 1 并存储中间结果更快? (我应该使用>>=??)
Basic Question: I have a k dimensional box. I have a vector of upper bounds and lower bounds. What is the most efficient way to enumerate the coordinates of the vertices?
Background: As an example, say I have a 3 dimensional box. What is the most efficient algorithm / code to obtain:
vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 )
vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 )
vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 )
vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 )
vertex[4] = ( 1, 0, 0 ) -> ( U_0, L_1, L_2 )
vertex[5] = ( 1, 0, 1 ) -> ( U_0, L_1, U_2 )
vertex[6] = ( 1, 1, 0 ) -> ( U_0, U_1, L_2 )
vertex[7] = ( 1, 1, 1 ) -> ( U_0, U_1, U_2 )
where L_0 corresponds to the 0'th element of the lower bound vector & likewise U_2 is the 2nd element of the upper bound vector.
My Code:
const unsigned int nVertices = ((unsigned int)(floor(std::pow( 2.0, double(nDimensions)))));
for ( unsigned int idx=0; idx < nVertices; ++idx )
{
for ( unsigned int k=0; k < nDimensions; ++k )
{
if ( 0x00000001 & (idx >> k) )
{
bound[idx][k] = upperBound[k];
}
else
{
bound[idx][k] = lowerBound[k];
}
}
}
where the variable bound
is declared as:
std::vector< std::vector<double> > bound(nVertices);
but I've pre-sized it so as not to waste time in the loop allocating memory. I need to call the above procedure about 50,000,000 times every time I run my algorithm -- so I need this to be really efficient.
Possible Sub-Questions: Does it tend to be faster to shift by k instead of always shifting by 1 and storing an intermediate result? (Should I be using >>= ??)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以减少条件分支,它可能会更快:
如果您可以在单个数组中交错上限和下限,您可能会进一步改进:
我不知道增量移动 idx 是否有帮助。它很容易实现,所以值得一试。
It will probably go faster if you can reduce conditional branching:
You might improve things even more if you can interleave the upper and lower bounds in a single array:
I don't know if shifting
idx
incrementally helps. It's simple enough to implement, so it's worth a try.