简体   繁体   English

C# GetHashCode 用于分箱

[英]C# GetHashCode for binning

I would like to bin a set of objects, in particular Point types with an x and y value so that I can count the number of points at each x, y pair that exists in the set of Points.我想对一组对象进行分类,特别是具有 x 和 y 值的点类型,以便我可以计算点集中存在的每个 x、y 对的点数。 The max value of x and y is [-1000,1000] so it is easy enough to generate a unique hash code for each possible point. x 和 y 的最大值为 [-1000,1000],因此很容易为每个可能的点生成唯一的 hash 代码。

For the actual binning process, can this be done using a Dictionary.对于实际的分箱过程,可以使用字典来完成。 Does the GetHashCode get used for this when I add/lookup a Point to the dictionary?当我向字典添加/查找点时,GetHashCode 是否会被用于此目的?

Is there a better way to do binning?有没有更好的方法来进行分箱?

Yes, unless you pass a custom instance to provide hash codes and check equality when you construct the Dictionary instance, GetHashCode and Equals methods for your type will be used.是的,除非您在构造Dictionary实例时传递自定义实例以提供 hash 代码并检查相等性,否则将使用您的类型的GetHashCodeEquals方法。

To provide your custom hashing mechanism, you can simply override GetHashCode and Equals .要提供自定义哈希机制,您可以简单地覆盖GetHashCodeEquals

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

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