递归布尔函数

发布于 2024-10-29 03:23:51 字数 953 浏览 0 评论 0原文

假设我有以下内容:

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 技术交流群。

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

发布评论

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

评论(2

格子衫的從容 2024-11-05 03:23:51

该函数看起来是正确的,但我有一些风格上的评论。首先,尝试给它起个别的名字,signal 是 POSIX 操作系统中非常常见的函数。

其次,我要么在 if 语句中包含实际的函数调用,要么只有 4 个 if 语句,每个语句都返回,以短路评估并(主观地)稍微清理一下代码。也就是说,要么:

return (signal(orig, n->neChild, tolerance) ||
        signal(orig, n->nwChild, tolerance) ||
        signal(orig, n->swChild, tolerance) ||
        signal(orig, n->seChild, tolerance));

要么:

if (signal(orig, n->neChild, tolerance))
   return true;
if (signal(orig, n->nwChild, tolerance))
   return true;
if (signal(orig, n->swChild, tolerance))
   return true;
if (signal(orig, n->seChild, tolerance))
   return true;
return false;

最后,我想补充一点,我要么创建一个从 QtreeNode 派生的新类,该类实现像 nodeDifference 这样的方法,或者如果您控制 QtreeNode 的源,则只需添加它,这可以进一步清理你的代码,即

bool signal(QtreeNode *&orig, QtreeNode *&n, int tolerance) {
  if (n->isLeaf())
     return (orig->nodeDifference(*n) > tolerance);
  else
     return (signal(orig, n->neChild, tolerance) ||
             signal(orig, n->nwChild, tolerance) ||
             signal(orig, n->swChild, tolerance) ||
             signal(orig, n->seChild, tolerance));
 }

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:

return (signal(orig, n->neChild, tolerance) ||
        signal(orig, n->nwChild, tolerance) ||
        signal(orig, n->swChild, tolerance) ||
        signal(orig, n->seChild, tolerance));

or:

if (signal(orig, n->neChild, tolerance))
   return true;
if (signal(orig, n->nwChild, tolerance))
   return true;
if (signal(orig, n->swChild, tolerance))
   return true;
if (signal(orig, n->seChild, tolerance))
   return true;
return false;

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.

bool signal(QtreeNode *&orig, QtreeNode *&n, int tolerance) {
  if (n->isLeaf())
     return (orig->nodeDifference(*n) > tolerance);
  else
     return (signal(orig, n->neChild, tolerance) ||
             signal(orig, n->nwChild, tolerance) ||
             signal(orig, n->swChild, tolerance) ||
             signal(orig, n->seChild, tolerance));
 }
人生戏 2024-11-05 03:23:51

你会用这种方式击中四叉树中的每一片叶子。相反,你一发现它就想打破它。为此,您需要更改

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;

return signal(orig, n->neChild, tolerance) || 
       signal(orig, n->nwChild, tolerance) ||
       signal(orig, n->swChild, tolerance) ||
       signal(orig, n->seChild, tolerance);

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

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;

to

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