繁体   English   中英

在第三方 class 上实现自定义哈希码

[英]Implement a custom hashcode on a third party class

我在 VisualStudio C# 项目中使用来自第三方的 Line class(无法访问源代码) 我的代码正在生成数百/数千个包含重复项的行对象,我需要将它们存储在某种集合中(List、HashSet)用于进一步处理(在屏幕上绘图)。 线 class 具有 Point 类型的 StartPoint 和 EndPoint 属性等。 出于我需要做的目的,线方向无关紧要,如果起点和终点相同,则两个 Line 对象是相同的。 然而,我使用的 class 的行为不同,两个 Line 对象被认为是唯一的,即使它们具有相同的起点/终点。 (GetHashCode 返回不同的值) 问题是 - 如何为第三方 class 实现自定义 IComparer 或 GetHashCode 例程,以便能够使用 HashSet 结构或 List.Distinct() 功能?

谢谢 Ilian 和 Hasan,如果你不介意的话,那就是诀窍快速跟进问题:我不确定是使用 List 还是 HashSet 来保存我的数据,所以我尝试了两者并计时。 结果表明,一个比另一个慢几个数量级。 关于发生了什么的任何见解?

class LineComparer : IEqualityComparer<Line>
{
    public bool Equals(Line l1, Line l2)
    {
        if (l1.EndPoint == l2.EndPoint && l1.StartPoint == l2.StartPoint) return true;
        if (l1.EndPoint == l2.StartPoint && l1.StartPoint == l2.EndPoint) return true;
        return false;
    }
    public int GetHashCode(Line line) => line.StartPoint.GetHashCode() ^ line.EndPoint.GetHashCode();
}

这是我的 HashSet 代码:

var timer = new Stopwatch();

计时器.Start();

var result = new HashSet(new LineComparer());

GenerateAndStore20000Lines();

计时器.停止(); Ed.WriteMessage($"\n生成的 {result.Count} 行,使用哈希集的时间:{timer.ElapsedMilliseconds}");

返回结果;

结果: * 使用 HashSet 的时间:1302 * 使用 HashSet 的时间:1328 * 使用 HashSet 的时间:1314 * 使用 HashSet 的时间:1311 * 使用 HashSet 的时间:1303

带有列表的代码:

var timer = new 秒表(); 计时器.Start();

var 结果 = 新列表();

GenerateAndStore20000Lines();

计时器.停止(); Ed.WriteMessage($"\n生成的 {result.Count} 行,列表时间:{timer.ElapsedMilliseconds}");

返回结果.Distinct(new LineComparer());

结果:

  • 生成 20002 行,列表时间:26
  • 生成 20002 行,列表时间:11
  • 生成 20002 行,列表时间:14
  • 生成 20002 行,列表时间:12
  • 生成 20002 行,列表时间:12

(抱歉格式不好,但这个界面让我发疯......放弃)

将重载与IEqualityComparer一起使用(您必须实现)。

HashSet

我假设,你的意思是Enumerable.Distinct 使用这个

我只是在扩展@Ilian的答案。 试图尽可能多地发表评论,所以我相信代码进行谈话会更好:)

// Mock 3rd Party point
public class ThirdPartyPoint {

}

// Mock 3rd party line
public class ThirdPartyLine {

    public ThirdPartyPoint StartPoint { get; set; }
    public ThirdPartyPoint EndPoint { get; set; }

}

// This class implements IEqualityComparer<ThirdPartyLine>, which compares
// ThirdPartyLine's equality. THis class will be passed as a ctor arument to HashSet<T>
public class CompareLines : IEqualityComparer<ThirdPartyLine> {

    public bool Equals(ThirdPartyLine x, ThirdPartyLine y) {
        // Here check for the equality of the start and end points.
        // I asuumed the following but do not know how the eaulity is implemented in your library.
        return x.EndPoint == y.EndPoint && x.StartPoint == y.StartPoint;
    }

    public int GetHashCode(ThirdPartyLine obj) {
        // Implement an algortihm which must return same hashcode for objects considered the same.
        // I am not sure about the Point class hashcode but I am jsut assuming the following.
        return obj.StartPoint.GetHashCode() ^ obj.EndPoint.GetHashCode();
    }

}


private static void Main(string[] args) {
    // Hashset to hold lines
    var hashSet = new HashSet<ThirdPartyLine>(new Compare());
    // start point
    var starPoint = new ThirdPartyPoint();
    // end point
    var endPoint = new ThirdPartyPoint();

    // Lines with same start and end points
    var line1 = new ThirdPartyLine {
        StartPoint = starPoint,
        EndPoint = endPoint
    };

    var line2 = new ThirdPartyLine {
        StartPoint = starPoint,
        EndPoint = endPoint
    };


    // Check count first
    hashSet.Add(line1);
    var count = hashSet.Count;

    // Check count second, still 1
    hashSet.Add(line2);
    count = hashSet.Count;
}

暂无
暂无

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

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