简体   繁体   English

更改没有大条件块的比较运算符

[英]Change comparison operators without large conditional block

I am testing whether a number lies between two values. 我正在测试一个数字是否位于两个值之间。 I leave it up to the user to choose whether the logical comparison should include an equal to on either (or both) of the limits or not. 我将其留给用户来选择逻辑比较是否应该包括equal to或者两个(或两个)限制。 They set this by defining a struct which contains the two edge values and which comparison operator to use: 他们设置这个定义一个struct包含两个边缘值和比较操作使用方法:

typedef struct {
    double low; 
    double high;
    bool low_equal; //false if a greater than operator (`>`) should be used, true if a greater-than-or-equal-to (`>=`) operator should be used
    bool high_equal; //Same as low_equal but for a less-than operator
} Edges;

An array of Edges is created, (termed bins below) and for each input value I check whether it lies within the bin edges. 创建一个Edges阵列(下面称为bins ),并且对于每个输入value我检查它是否位于箱边缘内。 However, in order to use the desired pair of comparison operators, I've ended up with this hideous conditional block: 但是,为了使用所需的一对比较运算符,我最终得到了这个可怕的条件块:

        if (bins[j].low_equal && bins[j].high_equal)
        {
            if (value >= bins[j].low && value <= bins[j].high)
            {
                break;
            }
        }
        else if (bins[j].low_equal)
        {
            if (value >= bins[j].low && value < bins[j].high)
            {
                data[i] = bins[j].value;
                break;
            }
        }
        else if (bins[j].high_equal)
        {
            if (datum > bins[j].low && datum <= bins[j].high)
            {
                break;
            }
        }
        else
        {
            if (value > bins[j].low && value < bins[j].high)
            {
                break;
            }
        }

Is there a better way to do this? 有一个更好的方法吗? Can I somehow set the operators to use and then just call them? 我能以某种方式设置要使用的运算符然后只是调用它们吗?

A simple approach could be: 一个简单的方法可能是:

bool higher = (value > bins[j].low) || (bins[j].low_equal && value == bins[j].low); 
bool lower  = (value < bins[j].high) || (bins[j].high_equal && value == bins[j].high); 

if (higher && lower)
{
    // In range
}

you may use pointer on function 你可以在函数上使用指针

bool less(double lhs, double rhs) { return lhs < rhs; }
bool less_or_equal(double lhs, double rhs) { return lhs <= rhs; }
using comp_double = bool(double, double);

and then 然后

comp_double *low_comp = bins[j].low_equal ? less_or_equal : less;
comp_double *high_comp = bins[j].high_equal ? less_or_equal : less;

if (low_comp(bins[j].low, value) && high_comp(value, bins[j].high)) {
   // In range
}

This would be IMO a good case for the ternary operator 对于三元运营商来说,IMO是一个很好的例子

if ((bins[j].low_equal ? bins[j].low <= value : bins[j].low < value) &&
    (bins[j].high_equal ? value <= bins[j].high : value < bins[j].high)) {
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM