递归布尔函数
假设我有以下内容:
bool signal(QtreeNode * & orig, QtreeNode * & n, int tolerance) {
bool signal1= false, signal2= false, signal3= false, signal4= false;
if(n->isLeaf()){
if(totalDiff>tolerance) //suppose these were defined
return true;
else return false;
}
signal1=signal(orig, n->neChild, tolerance);
signal2=signal(orig, n->nwChild, tolerance);
signal3=signal(orig, n->swChild, tolerance);
signal4=signal(orig, n->seChild, tolerance);
if(signal1 || signal2 || signal3 || signal4)
return true;
else return false;
}
并假设我从某个包装器方法中调用该方法,如下所示:
signal1=signal(orig, n, tolerance);
if(signal1)
//do something
所以我在这里所做的是遍历整个四叉树,仅查找一种情况,其中我为真。我需要这个函数做的就是在 TotalDiff 大于容差的一种情况下返回 true。我担心我所拥有的并没有达到我希望的效果。看看函数,当我在包装器方法中设置 signal1 时,如果它只找到该条件的 1 种情况,我会得到 true
吗?还是我做错了?
Say I have the following:
bool signal(QtreeNode * & orig, QtreeNode * & n, int tolerance) {
bool signal1= false, signal2= false, signal3= false, signal4= false;
if(n->isLeaf()){
if(totalDiff>tolerance) //suppose these were defined
return true;
else return false;
}
signal1=signal(orig, n->neChild, tolerance);
signal2=signal(orig, n->nwChild, tolerance);
signal3=signal(orig, n->swChild, tolerance);
signal4=signal(orig, n->seChild, tolerance);
if(signal1 || signal2 || signal3 || signal4)
return true;
else return false;
}
And say I call that method from some wrapper method like this:
signal1=signal(orig, n, tolerance);
if(signal1)
//do something
So what I'm doing here is traversing an entire quad-tree looking for just one case where I get true. All I need for this function to do is to return true in one case where totalDiff is greater than tolerance. I'm afraid that what I have isn't doing what I hoped it would do. Looking at function, does it seem like, when I set signal1 in my wrapper method, that I will get true
back if it finds just 1 case of that condition? Or am I doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该函数看起来是正确的,但我有一些风格上的评论。首先,尝试给它起个别的名字,
signal
是 POSIX 操作系统中非常常见的函数。其次,我要么在 if 语句中包含实际的函数调用,要么只有 4 个 if 语句,每个语句都返回,以短路评估并(主观地)稍微清理一下代码。也就是说,要么:
要么:
最后,我想补充一点,我要么创建一个从 QtreeNode 派生的新类,该类实现像
nodeDifference
这样的方法,或者如果您控制 QtreeNode 的源,则只需添加它,这可以进一步清理你的代码,即The function looks correct, but I have a few stylistic comments. First, try to call it something else,
signal
is a very common function in POSIX operating systems.Secondly, I'd either include actual function calls in an if statement or just have 4 if statements, each returning, to short circuit evaluation and to (subjectively) clean up the code a bit. That is, either:
or:
Finally, I'd like to add that I would either create a new class deriving from QtreeNode that implemented a method like
nodeDifference
or just add it if you control the source of QtreeNode, which can clean up your code further, i.e.你会用这种方式击中四叉树中的每一片叶子。相反,你一发现它就想打破它。为此,您需要更改
为
You'll hit every leaf in your quadtree this way. Instead you want to break as soon as you've found it. To do that you need to change
to