SortedSet <>.包含()如何实现自己的比较?

ddl*_*ddl 1 c# sortedset

我想检查具有给定值的Object是否存在,SortedSet<>但我不明白自定义比较在这里是如何工作的.在List<>.Exists()我可以只使用lambda,但我不能那样做,我没有得到整个界面的东西,而msdn说我需要覆盖int返回功能.

public class Node
{
    public int X, Y;
    public int rand;

    public Node(int x, int y, int r)
    { X = x; Y = y; rand = r; }
}

class Program
{
    static void Main(string[] args)
    {
        SortedSet<Node> mySet = new SortedSet<Node>();
        mySet.Add(new Node(1, 2, 90));
        Node myNode = new Node(1, 2, 50);
        // I want this to check if X and Y are the same
        if (mySet.Contains(myNode, interfaceThing))
            Console.WriteLine("Sth is already on that (X, Y) position");      
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法呢?

Sco*_*ain 5

您有两个选项,创建一个实现的类IComparer<Node>(您也应该这样做 IEqualityComparer<Node>)并将其传递给有序集的构造函数.

public class NodeComparer : IComparer<Node>, IEqualityComparer<Node>
{
    public int Compare(Node node1, Node node2)
    {
        //Sorts by X then by Y

        //perform the X comparison
        var result = node1.X.CompareTo(node2.X);
        if (result != 0)
            return result;

        //Perform the Y Comparison
        return node1.Y.CompareTo(node2.Y);
    }

    public bool Equals(Node x, Node y)
    {
        if (ReferenceEquals(x, y)) return true;
        if (ReferenceEquals(x, null)) return false;
        if (ReferenceEquals(y, null)) return false;
        if (x.GetType() != y.GetType()) return false;
        return x.X == y.X && x.Y == y.Y && x.rand == y.rand;
    }

    public int GetHashCode(Node obj)
    {
        unchecked
        {
            var hashCode = obj.X;
            hashCode = (hashCode * 397) ^ obj.Y;
            hashCode = (hashCode * 397) ^ obj.rand;
            return hashCode;
        }
    }
}

public class Node
{
    public int X, Y;
    public int rand;

    public Node(int x, int y, int r)
    { X = x; Y = y; rand = r; }
}

class Program
{
    static void Main(string[] args)
    {
        SortedSet<Node> mySet = new SortedSet<Node>(new NodeComparer());
        mySet.Add(new Node(1, 2, 90));
        Node myNode = new Node(1, 2, 50);
        // I want this to check if X and Y are the same
        if (mySet.Contains(myNode, interfaceThing))
            Console.WriteLine("Sth is already on that (X, Y) position");      
    }
}
Run Code Online (Sandbox Code Playgroud)

或者让Node实现自己需要的相关方法.

public class Node : IEquatable<Node>, IComparable<Node>
{
    public int X, Y;

    public int rand;

    public Node(int x, int y, int r)
    { X = x; Y = y; rand = r; }

    public bool Equals(Node other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return X == other.X && Y == other.Y && rand == other.rand;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Node)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = X;
            hashCode = (hashCode*397) ^ Y;
            hashCode = (hashCode*397) ^ rand;
            return hashCode;
        }
    }

    public int CompareTo(Node other)
    {
        //First order by X then order by Y then order by rand

        var result = X.CompareTo(other.X);
        if (result != 0)
            return result;

        result = Y.CompareTo(other.Y);
        if (result != 0)
            return result;

        return rand.CompareTo(other.rand);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它对我不起作用 SortedSet 似乎忽略了 `Equals()` 函数,并且只使用 `Compare()` (2认同)