加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:19:27 所属栏目:百科 来源:网络整理
导读:为了解决 this question,我一直在玩一个实现Hashable Protocol的自定义结构体.我试图看到等效运算符重载(==)被调用多少次,这取决于填充字典时是否存在哈希冲突. 更新 @matt写了一个更清晰的自定义结构实例,实现了Hashable协议,并显示了hashValue和== get被调
为了解决 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

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读