简体   繁体   English

对象识别的XOR

[英]XOR for Object Identitification

In the code below, I was wondering why XOR (^) is being used to combine the hascodes of the constituent members of the composition (this is source from MonoCross 1.3)? 在下面的代码中,我想知道为什么XOR(^)被用于组合组成成员的hascodes(这是MonoCross 1.3的源代码)?

  1. Is the bitwise XOR of an MXViewPerspective object's Perspective and ModelType member's used to uniquely identify the instance ? MXViewPerspective对象的PerspectiveModelType成员的按位XOR是否用于唯一标识实例?

  2. If so, is there a name for this property of the XOR operation (how XOR-ing two values (ie, hashcodes) guarantees uniqueness) ? 如果是这样,是否有XOR操作的这个属性的名称(如何异或两个值(即,哈希码)保证唯一性)?


public class MXViewPerspective : IComparable
{
    public MXViewPerspective(Type modelType, string perspective)
    {
        this.Perspective = perspective;
        this.ModelType = modelType;
    }
    public string Perspective { get; set; }
    public Type ModelType { get; set; }

    public int CompareTo(object obj)
    {
        MXViewPerspective p =(MXViewPerspective)obj;
        return this.GetHashCode() == p.GetHashCode() ? 0 : -1;
    }
    public static bool operator ==(MXViewPerspective a, MXViewPerspective b)
    {
        return a.CompareTo(b) == 0;
    }
    public static bool operator !=(MXViewPerspective a, MXViewPerspective b)
    {
        return a.CompareTo(b) != 0;
    }
    public override bool Equals(object obj)
    {
        return this == (MXViewPerspective)obj;
    }
    public override int GetHashCode()
    {
        return this.ModelType.GetHashCode() ^ this.Perspective.GetHashCode();
    }

    public override string ToString()
    {
        return string.Format("Model \"{0}\" with perspective  \"{1}\"", ModelType, Perspective);
    }
}

Thank you. 谢谢。

xor'ing hashcodes doesn't guarantee uniqueness, but is usually used to improve the distribution over a table without complicating the hashing. xor'ing哈希码不保证唯一性,但通常用于改善表上的分布,而不会使哈希复杂化。

You want to make 2 different values map to different hash keys if they differ in any of the fields (ie - same ModelType , but different Perspective , or vice versa). 如果它们在任何字段中不同( ModelType ,相同的ModelType ,但不同的Perspective ,反之亦然),您希望将2个不同的值映射到不同的散列键。 So you need to incorporate both values into your hash key. 因此,您需要将两个值合并到哈希键中。 You could have used + for example, or shift and concatenate them (the latter would be better in fact, as it would guarantee uniqueness, but also extend the key length which might complicate hashing). 你可以使用+例如,或者移位和连接它们(事实上后者会更好,因为它可以保证唯一性,但也可以扩展可能使哈希复杂化的密钥长度)。

xor won't guarantee this uniqueness since if you flip the same bit in ModelType and Perspective , you'd get the same hash key, for example 5 ^ 7 = 1 ^ 3 = 2, but it's usually good enough. xor不保证这种唯一性,因为如果你在ModelTypePerspective翻转相同的位,你会得到相同的哈希键,例如5 ^ 7 = 1 ^ 3 = 2,但它通常足够好。 Eventually it all depends on the ranges and distributions of the values you provide. 最终,这一切都取决于您提供的值的范围和分布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM