SortedSet<> .Contains()如何实现自己的比较? [英] SortedSet<>.Contains() how to implement own comparation?

查看:106
本文介绍了SortedSet<> .Contains()如何实现自己的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查SortedSet<>中是否存在具有给定值的对象,但是我不了解自定义比较的工作原理.在List<>.Exists()中,我只能使用lambda,但是我不能在那里使用lambda,并且在msdn说我需要重写int返回函数时,我没有得到整个接口.

I want to check if Object with given values exists in SortedSet<> but I don't understand how custom comparation works here. In List<>.Exists() i could just use lambda, but I cannot do that there and i don't get that whole interface thing while msdn says i need to override int returning function.

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");      
    }
}

有没有简单的方法可以做到这一点?

Is there any simple way to do that?

推荐答案

您有两个选择,创建一个实现IComparer<Node>的类(您也应该执行IEqualityComparer<Node>)并将其传递给排序集的构造函数

You have two options, create a class that implements IComparer<Node> (you should do IEqualityComparer<Node> too) and pass that in to the constructor of the sorted set.

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");      
    }
}

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

Or have Node implement the relevant methods it needs itself.

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);
    }
}

这篇关于SortedSet&lt;&gt; .Contains()如何实现自己的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆