Dictionary如何在Swift中使用Equatable协议?

为了解决 this question,我一直在玩一个实现Hashable Protocol的自定义结构体.我试图看到等效运算符重载(==)被调用多少次,这取决于填充字典时是否存在哈希冲突.

更新

@matt写了一个更清晰的自定义结构实例,实现了Hashable协议,并显示了hashValue和== get被调用的频率.我正在复制his code.看到我的原始例子,看看edit history.

struct S : Hashable {
    static func ==(lhs:S,rhs:S) -> Bool {
        print("called == for",lhs.id,rhs.id)
        return lhs.id == rhs.id
    }
    let id : Int
    var hashValue : Int {
        print("called hashValue for",self.id)
        return self.id
    }
    init(_ id:Int) {self.id = id}
}
var s = Set<S>()
for i in 1...5 {
    print("inserting",i)
    s.insert(S(i))
}

这产生结果:

/*
inserting 1
called hashValue for 1
inserting 2
called hashValue for 2
called == for 1 2
called hashValue for 1
called hashValue for 2
inserting 3
called hashValue for 3
inserting 4
called hashValue for 4
called == for 3 4
called == for 1 4
called hashValue for 2
called hashValue for 3
called hashValue for 1
called hashValue for 4
called == for 3 4
called == for 1 4
inserting 5
called hashValue for 5
*/

由于Hashable使用Equatable来区分哈希冲突(我假设无论如何),我希望func ==()只有当有哈希冲突时被调用.然而,在上面的@ matt的例子中,根本没有哈希冲突,而==仍在被调用.在我的其他实验中强制哈希冲突(见这个问题的编辑历史),==似乎被称为随机次数.

这里发生了什么?

那么你的答案是:

https://bugs.swift.org/browse/SR-3330?focusedCommentId=19980&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-19980

What’s actually happening:

  • We hash a value only once on insertion.
  • We don’t use hashes for comparison of elements,only ==. Using hashes for comparison is only reasonable if you store the hashes,but
    that means more memory usage for every Dictionary. A compromise that
    needs evaluation.
  • We try to insert the element before evaluating if the Dictionary can fit that element. This is because the element might already be in the
    Dictionary,in which case we don’t need any more capacity.
  • When we resize the Dictionary,we have to rehash everything,because we didn’t store the hashes.

So what you’re seeing is:

  • one hash of the search key
  • some ==’s (searching for a space)
  • hashes of every element in the collection (resize)
  • one hash of the search key (actually totally wasteful,but not a big deal considering it only happens after an O

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...