当==,CompareTo()和Equals()不同意时会发生什么?

时间:2013-12-09 20:20:34

标签: c# linq operator-overloading icomparable iequatable

我有一个程序,我写了几年回来为字节找到“好”的二元运算符;将字节A乘以字节B以产生字节C。运算符定义为256x256字节矩阵。下面是A类实现的精简版。

Equals()为真IFF阵列中的所有65536个字节都是相同的。

CompareTo()将运算符的线性度比较为线性(对于cryto而言不好)到线性度较低(对于加密有利)。

因此,有两个实例AB可以满足以下两个条件:

A.Equals(B) = false
(A.ComparesTo(B) == 0) = true

我的问题不多了:这是个好主意吗?我知道答案是否定的,但考虑到测量线性度的大量计算成本和我的问题的狭隘性,这个设计是有效的。代码类似于:

if (localMinimumOperator < globalMinimumOperator)
{
  localMinimumOperator = globalMinimumOperator;
}

让我更容易阅读。

我的问题是:这种分歧的后果是什么:==,CompareTo()== 0Equals()?或者: 是否有哪些LINQ扩展方法的列表描述哪个扩展使用哪个接口(IEquatableIComparable)?

Enumerable上的MSDN文章更简洁?

例如:

IEnumerable<BinaryOperation> distinct = orgList.Distinct();

根据Enumerable.Distinct<TSource> Method Equals(BinaryOperator)调用Contains()。我了解Sort()OrderBy()使用对CompareTo()的调用。

但是FindFirst()BinarySearch()呢?

我的示例课程:

using System;
using System.Collections.Generic;
using System.Linq;


namespace Jww05
{
  public class BinaryOperation : IEquatable<BinaryOperation>, IComparable<BinaryOperation>
  {
    #region ClassMembers

    public List<List<byte>> TruthTable
    {
      get
      {
        // I don't like giving out the underlying list if I help it
        var retVal = new List<List<byte>>(OperatorDefintion);
        return retVal;
      }
    }

    // private data store for TruthTable
    private List<List<byte>> OperatorDefintion { get; set; }

    public BinaryOperation()
    {
      // initial state is the Identity operator
      OperatorDefintion = new List<List<byte>>();
      for (int i = 0; i < 256; i++)
      {
        var curRow = new List<byte>();
        for (int j = 0; j < 256; j++)
        {
          curRow.Add((byte)(i + j));
        }
        OperatorDefintion.Add(curRow);
      }
    }

    private long MeasureOperatorLinearity()
    {
      var diagonalOffsets = new byte[] { 255, 0, 1 };

      /*
       * Code that measures linearity in the original code used the Fast Walsh Hadamard Transform.
       * That should go here, but it is removed because the FWHT is clutter for the purposes of this question.
       *
       * Since I needed a stub for this, I decided to exacerbate the differnece
       * between CompareTo() == 0 and Equals()
       * by returning an arbitrary int in lieu of the long CPU intensive Fast Walsh Hadamard Transform.
       *
       * If the matrices are identical on an element-by-element basis, then the Faux Linearity will be the the same.
       * If the faux linearity (sum of terms on the main diagonal and corners) are the same, the underlying matrices could be different on an element-by-element basis.
       */
      long fauxLinearityMeasure = 0;
      for (var currRow = 0; currRow < OperatorDefintion.Count(); ++currRow)
      {
        fauxLinearityMeasure *= 5;
        fauxLinearityMeasure = diagonalOffsets.Select(diagonalOffset => (byte)(currRow + diagonalOffset))
                                              .Aggregate(fauxLinearityMeasure, (current, offestedIndex) => current + (OperatorDefintion[offestedIndex][currRow]));
      }

      return (int)fauxLinearityMeasure;
    }

    #endregion ClassMembers

    #region ComparisonOperations

    public int CompareTo(BinaryOperation other)
    {
      long otherLinearity = other.MeasureOperatorLinearity();
      long thisLinearity = MeasureOperatorLinearity();
      long linearityDiff = thisLinearity - otherLinearity;

      // case the differnece of the linarity measures into {-1, 0, 1}
      return (0 < linearityDiff) ? 1
           : (0 > linearityDiff) ? -1
           : 0;
    }

    public static bool operator >(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }
      return (0 < lhs.CompareTo(rhs));
    }

    public static bool operator <(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }
      return (0 > lhs.CompareTo(rhs));
    }

    public static bool operator <=(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }

      // equals is cheap
      if (lhs.Equals(rhs))
      {
        return true;
      }

      return (0 > lhs.CompareTo(rhs));
    }

    public static bool operator >=(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }

      // equals is cheap
      if (lhs.Equals(rhs))
      {
        return true;
      }

      return (0 < lhs.CompareTo(rhs));
    }

    #endregion ComparisonOperations

    #region EqualityOperators

    public bool Equals(BinaryOperation other)
    {
      if (ReferenceEquals(null, other))
      {
        return false;
      }

      var otherTruthTable = other.TruthTable;
      var thisTruthTable = TruthTable;
      var isEquals = true;
      for (int currRow = 0; currRow < thisTruthTable.Count(); ++currRow)
      {
        isEquals = isEquals && thisTruthTable[currRow].SequenceEqual(otherTruthTable[currRow]);
      }

      return isEquals;
    }

    public override bool Equals(object obj)
    {
      return Equals(obj as BinaryOperation);
    }

    public override int GetHashCode()
    {
      return OperatorDefintion.SelectMany(currRow => currRow)
                              .Aggregate(1, (current, currByte) => current * 5 + currByte);
    }

    public static bool operator ==(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }

      return (0 == lhs.CompareTo(rhs));
    }

    public static bool operator !=(BinaryOperation lhs, BinaryOperation rhs)
    {
      if (ReferenceEquals(null, lhs) ||
          ReferenceEquals(null, rhs))
      {
        return false;
      }

      return (0 != lhs.CompareTo(rhs));
    }

    #endregion EqualityOperators
  }
}

1 个答案:

答案 0 :(得分:4)

  

这种分歧的后果是什么:==,CompareTo()== 0和Equals()?

有人在将来看你的代码会真的很讨厌你。

  

或者替代:是否有哪些linq扩展方法的列表描述哪个扩展使用哪个接口(IEquitable或IComparable)?

我认为你自己找到了大部分内容。一个好的经验法则是,LINQ函数使用的界面通常没有什么令人惊讶的(没有惊喜是优秀设计的特征之一 - 与你的不同)。例如:很明显,要对元素进行排序,必须知道元素应该在哪个特定顺序中,单独的等式/不等式对此不够。 BinarySearch还需要知道搜索过程中“走哪条路” - 如果元素大于当前值,它会重新诅咒到已排序数组的上半部分,如果小则进入较低级别。再次:显然它需要IComparable。对于Distinct EqualsGetHashCode就足够了 - 不需要排序来确定一组唯一元素。等等。