可可。对象相等和散列澄清 [英] Cocoa. Object equality and hashing clarification

查看:157
本文介绍了可可。对象相等和散列澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习可可收藏,我的研究已经为Mike Ash的 post 对象平等和散列。

I'm studying Cocoa collections currently and my research has brought to Mike Ash's post on object equality and hashing.

这是从帖子中摘取的:


由于hash的语义,如果你重写isEqual:那么你必须覆盖hash。如果你不这样做,那么你有两个对象是相等的,但是没有相同的哈希值。如果你在字典,集合或其他使用哈希表的对象中使用这些对象,那么将会出现欢闹。

Because of the semantics of hash, if you override isEqual: then you must override hash. If you don't, then you risk having two objects which are equal but which don't have the same hash. If you use these objects in a dictionary, set, or something else which uses a hash table, then hilarity will ensue.

作者没有进一步详细的欢乐会发生什么,我的好奇心不让我离开它,而不想深入挖掘。所以问题是:如果我有两个相同的对象具有不同的哈希值,我把这些对象放到一个集合,究竟会发生什么?我会遇到什么样的问题?

Unfortunately the author doesn't get further in details of what the hilarity will occur and my curiosity doesn't let me just leave it without trying to dig deeper. So the question is: what exactly will happen if i have two equal objects with different hash values and i put these objects into one collection? What sort of problem i will run into?

推荐答案

答案在这一部分从迈克的帖子

The answer is in this section from Mike's post


哈希表基本上是一个具有特殊索引的大数组。对象被放置到具有与其散列相对应的索引的数组中。散列本质上是从对象的属性生成的伪随机数。这个想法是使索引足够随机,使得两个对象不可能具有相同的散列,但是它具有完全可再现性。当插入对象时,使用散列来确定它去哪里。当查找一个对象时,它的哈希用于确定在哪里查找。

A hash table is basically a big array with special indexing. Objects are placed into an array with an index that corresponds to their hash. The hash is essentially a pseudorandom number generated from the object's properties. The idea is to make the index random enough to make it unlikely for two objects to have the same hash, but have it be fully reproducible. When an object is inserted, the hash is used to determine where it goes. When an object is looked up, its hash is used to determine where to look.

在更正式的术语中,定义一个对象的哈希,相同的散列,如果它们相等。注意,相反的不是真的,不能是:两个对象可以有相同的散列和不相等。你想尽量避免这种情况,因为当两个不相等的对象具有相同的哈希(称为冲突)时,哈希表必须采取特殊措施来处理这个,这是慢的。

In more formal terms, the hash of an object is defined such that two objects have an identical hash if they are equal. Note that the reverse is not true, and can't be: two objects can have an identical hash and not be equal. You want to try to avoid this as much as possible, because when two unequal objects have the same hash (called a collision) then the hash table has to take special measures to handle this, which is slow. However, it's provably impossible to avoid it completely.

这意味着你将拥有两个声称相等的对象。您添加第一个作为键在字典与一些值。然后,您尝试使用另一个对象作为键提取该值。它不工作。它应该,因为你的对象是平等的。但是初始哈希查找失败。

What it means is that you will have your 2 objects which claim to be equal. You add the first as the key in a dictionary with some value. Then you try to extract that value using the other object as the key. And it doesn't work. It should, because your objects are equal. But the initial hash lookup failed.

要清楚,这可能不会发生。它可能对一些对象工作正常,对其他对象失败。关键是,如果你不实现这两种方法,你不知道会发生什么。

To be clear, this might not happen. It might work fine for some objects and fail for others. The point is, if you don't implement both methods, you don't know what's going to happen.

这篇关于可可。对象相等和散列澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆