快速可哈希协议散列函数是否需要返回唯一值? [英] Do swift hashable protocol hash functions need to return unique values?

查看:85
本文介绍了快速可哈希协议散列函数是否需要返回唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个iOS快速的Tetris教程*,并且它已经完成并且可以工作。
但是我对一个特定方面感到困惑-Hashable协议。
函数:

I am working through an iOS swift Tetris tutorial* and have it completed and working. But I am puzzled about one particular aspect - the Hashable protocol. The function:

class Block: Hashable, Printable {
    [...]    
    var hashValue: Int { return self.column ^ self.row }

行转到0..9 ,以及第0..20列。这些函数的注释说:我们返回行和列属性的异或,以为每个块生成一个唯一的整数。但是我的理解是0 ^ 1与1 ^ 0相同,依此类推...我想知道如果Hash函数不是像这样唯一的问题,或者冲突通常可以吗?就像我说的那样,该应用程序似乎可以正常工作...

Rows go 0..9, and Columns 0..20. The notes says of this function "We return the exclusive-or of our row and column properties to generate a unique integer for each Block.". But my understanding is that 0^1 would be the same as 1^0, etc... I would like to know if it is a problem if the Hash function is not unique like this, or are collisions are generally OK? As I say, the application appears to work fine...

* https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with- swift#!/ chapters / 681

推荐答案

冲突不是 通常可以 。基本假设是,当且仅当<$ c $时, x 的哈希值是 y 的哈希值。 c> x == y 。如果您认为第2列第1行与第1列第2行相同,则可以。但是我不认为你愿意!该应用程序似乎可以正常运行,但是想必您尚未执行任何需要哈希处理的操作-

Collisions are not "generally OK". The underlying assumption is that the hash value of x is the hash value of y if and only if x == y. If you consider column 2, row 1 the same as column 1, row 2, then fine. But I don't think you do! The application may appear to work, but presumably you have done nothing that requires hashability - yet.

这篇关于快速可哈希协议散列函数是否需要返回唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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