存在时无法找到字典键

时间:2014-07-06 23:42:47

标签: c# dictionary double-precision

为什么我的词典在我插入密钥时说密钥不存在?是用我的Equals方法还是双重比较?

以下是代码:

// Test: I know the dictionary contains nCoord but its saying the key doesn't exist
Dictionary<UTMCoordinate, int> planes = new Dictionary<UTMCoordinate, int>();
UTMCoordinate nCoord    = new UTMCoordinate(337394.136407966, 6263820.40182064, 0, 56, UTMCoordinate.Hemisphere.H_SOUTHERN);
planes[nCoord]          = 1;

bool exists = planes.ContainsKey(nCoord);  // always returns false

UTMCoordinate的实施情况如下:

public class UTMCoordinate
{
    public enum                 Hemisphere {H_NOTHERN, H_SOUTHERN};
    public const double         DIF_TOLERANCE    = 0.0005;
    public double               x               { get; set; }
    public double               y               { get; set; }
    public double               elev            { get; set; }
    public uint                 UTMZone         { get; set; }
    public Hemisphere           hemisphere      { get; set; }

    public UTMCoordinate(double x, double y, double elev=double.MinValue, uint utmZone=uint.MinValue, Hemisphere hemisphere=Hemisphere.H_SOUTHERN) {
        this.x          = x;
        this.y          = y;
        this.elev       = elev;
        this.UTMZone    = utmZone;
        this.hemisphere = hemisphere;
    }

    public override int GetHashCode() {
        unchecked // Overflow is fine, just wrap
        {
            int hash = 17;
            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + x.GetHashCode();
            hash = hash * 23 + y.GetHashCode();
            hash = hash * 23 + elev.GetHashCode();
            hash = hash * 23 + UTMZone.GetHashCode();
            hash = hash * 23 + hemisphere.GetHashCode();
            return hash;
        }
    }

    public override bool Equals(object obj)
    {
        UTMCoordinate other = obj as UTMCoordinate;
        if (other == null)
            return false;

        return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere);
    }
}

编辑使用Daniel A. Whites建议我使用了不同的双重比较方法。不幸的是,它还没有确定关键:

public override bool Equals(object obj)
{
    //return base.Equals (obj);
    UTMCoordinate other = obj as UTMCoordinate;
    if (other == null)
        return false;

    //return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere);
    return Math.Abs (x-other.x) <= DIF_TOLERANCE && Math.Abs (y-other.y) <= DIF_TOLERANCE && Math.Abs (elev-other.elev) <= DIF_TOLERANCE && uint.Equals(UTMZone, other.UTMZone) && hemisphere == other.hemisphere;
}

1 个答案:

答案 0 :(得分:4)

如果我从您的问题中提取您的代码:

Dictionary<UTMCoordinate, int> planes = new Dictionary<UTMCoordinate, int>();
UTMCoordinate nCoord    = new UTMCoordinate(337394.136407966, 6263820.40182064, 0, 56, UTMCoordinate.Hemisphere.H_SOUTHERN);
planes[nCoord]          = 1;

bool exists = planes.ContainsKey(nCoord);

exists获得的值为true

但是,如果我这样做:

nCoord.x = 1.0;
exists = planes.ContainsKey(nCoord);

exists的值突然变为false,即使该对象仍在字典中。这是因为GetHashCode的值已更改。它是-1473667404,但在分配属性x后,它变为201352392

字典使用GetHashCode的值来确定将密钥放入哪个存储桶中,因此当哈希代码更改时,字典可能会尝试在错误的存储桶中找到密钥,然后报告它没有#t}包含密钥。

我怀疑你的代码中发生了什么。

所以你需要改变你的对象,使它不可变。

public double               x               { get; private set; }
public double               y               { get; private set; }
public double               elev            { get; private set; }
public uint                 UTMZone         { get; private set; }
public Hemisphere           hemisphere      { get; private set; }

然后不要更改构造函数之外的任何值。