Passing an operator along with other parameters

2020-02-09 09:33发布

I have some VERY inefficient code in which many lines appear 4 times as I go through permutations with "<" and ">" operations and a variety of variables and constants. It would seem that there is a way to write the function once and pass in the operators along with the necessarily changing values and"ref" variables. What technique do I have to learn? "Delegates" have been suggested but I don't see how to use them in this manner. This is in C# 2.0, VS2005, but if the technique is generic and can be used with C++ too, that would be great.

Request for some code: The following appears in many guises, with different "<" and ">" signs as well as a mix of "+" and "-" signs:

if (move[check].Ypos - move[check].height / 200.0D < LayoutManager.VISIO_HEIGHT - lcac_c.top)
{
  move[check].Ypos = move[check].Ypos + adjust;
.
.
.

3条回答
地球回转人心会变
2楼-- · 2020-02-09 09:53

After defining the Enum Operator in the Comparer class

     public static class Comparer
{
    public static bool IsTrue<T, U>(T value1, Operator comparisonOperator, U value2)
                where T : U
                where U : IComparable
    {
        switch (comparisonOperator)
        {
            case Operator.GreaterThan:
                return value1.CompareTo(value2) > 0;
            case Operator.GreaterThanOrEqual:
                return value1.CompareTo(value2) >= 0;
            case Operator.LessThan:
                return value1.CompareTo(value2) < 0;
            case Operator.LessThanOrEqual:
                return value1.CompareTo(value2) <= 0;
            case Operator.Equal:
                return value1.CompareTo(value2) == 0;
            default:
                return false;
        }
    }

    public enum Operator
    {
        GreaterThan = 1,
        GreaterThanOrEqual = 2,
        LessThan = 3,
        LessThanOrEqual = 4,
        Equal = 5
    }
}

You can make a call like this:

 if (IsTrue(var1, Operator.GreaterThanOrEqual,  var2))
    Console.WriteLine("var1 is greater than var2");
 else
    Console
        .WriteLine("Unfortunately var1 is not greater than or equal var2. Sorry about that.");

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-09 10:07

In C++, use the std::less and std::greater functors. Both of these methods inherit std::binary_function, so your generic function should accept instances of this type.

In .NET, the equivalent to std::binary_function is Func<T, U, R>. There are no equivalents to std::less and std::greater, but it is fairly trivial to create them. See the following example.

static class Functor
{
    static Func<T, T, bool> Greater<T>()
        where T : IComparable<T>
    {
        return delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) > 0; };
    }

    static Func<T, T, bool> Less<T>()
        where T : IComparable<T>
    {
        return delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) < 0; };
    }
}

Note, the above code uses the Func<> class from .NET 3.5. If this is not acceptable, consider defining you own delegate.

C++ invocation example:

void DoWork(const std::binary_function<int, int, bool>& myOperator,
            int arg1, int arg2)
{
    if (myOperator(arg1, arg2)) { /* perform rest of work */ }
}

void main()
{
    DoWork(std::less<int>(), 100, 200);
    DoWork(std::greater<int>(), 100, 200);
}

C# invocation example:

void DoWork(Func<int, int, bool> myOperator, int arg1, int arg2)
{
    if (myOperator(arg1, arg2)) { /* perform rest of work */ }
}

void main()
{
    DoWork(Functor.Less<int>(), 100, 200);
    DoWork(Functor.Greater<int>(), 100, 200);
}

EDIT: I corrected the example of the functor class as applying < or > operators to a generic type doesn't work (in the same manner as it does with C++ templates).

查看更多
Deceive 欺骗
4楼-- · 2020-02-09 10:07

In C# use delegates for passing the "<" and ">" operation to the code that's doing the work.

C# Example:

public delegate bool BooleanOperatorDelegate(int a, int b)

class OperatorsImplementer {
    public bool OperatorLess(int a, int b) {
         return a < b;
    }
}

class AnotherOperatorsImplementer {
    public bool OperatorLess(int a, int b) {
         return (a + 1) < (b - 1);
    }
}

class OperatorUser {
    int DoSomethingObscene(int a, int b, BooleanOperatorDelegate operator) {
        if (operator(a, b)) {
            return 5;
        }
        else {
            return -5;
        }
    }
}

You should also check that the delegate you get as a paramater is not NULL.

This is the C method for doing so:

bool (*operator_func)(float a, float b)
查看更多
登录 后发表回答