SortedSet<>.Contains() how to implement own comparation?

微笑、不失礼 提交于 2019-12-02 10:22:10

问题


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?


回答1:


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

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



回答2:


A simple dirty way to do this is with some linq

if(myNode.Where(n => n.X == myNode.X && n.Y == myNode.Y).Count > 0)

You could also do this in an extension method to be able to call this more than once

public static class Extensions
{
    public static bool ContainsNode(this IList<Node> nodes, Node value)
    {
        return nodes.Where(n => n.X == value.X && n.Y == value.Y).Count > 0;
    }
}

Although if you want to be more efficient, you should use a simple foreach loop instead, to possibly shortcut iterating through the whole list once one is found.

Edit: totally forgot about .Any() bassically does the Where for you but does cut out early.

public static class Extensions
{
    public static bool ContainsNode(this IList<Node> nodes, Node value)
    {
        return nodes.Any(n => n.X == value.X && n.Y == value.Y).Count > 0;
    }
}


来源:https://stackoverflow.com/questions/22696329/sortedset-contains-how-to-implement-own-comparation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!