从无操作的集合中删除修改的对象? [英] Deleting a modified object from a set in a no-op?

查看:11
本文介绍了从无操作的集合中删除修改的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的例子

require "set"
s = [[1, 2], [3, 4]].to_set # s = {[1, 2], [3, 4]}
m = s.max_by {|a| a[0]} # m = [3, 4]
m[0] = 9 # m = [9, 4], s = {[1, 2], [9, 4]}
s.delete(m) # s = {[1, 2], [9, 4]} ?????

这与数组的行为不同.(如果我们删除 .to_set,我们将得到预期的 s = [[1, 2]].)这是一个错误吗?

This behaves differently from an array. (If we remove .to_set, we will get s = [[1, 2]] which is expected.) Is this a bug?

推荐答案

是的,这是一个错误,或者至少我会称之为错误.有些人会称之为一个意外泄露给外界的实现细节",但这只是 bug 的花哨的城市男孩谈话.

Yes, this is a bug or at least I'd call it a bug. Some would call this "an implementation detail accidentally leaking to the outside world" but that's just fancy pants city-boy talk for bug.

问题有两个主要原因:

  1. 您在 Set 不知情的情况下修改 Set 的元素.
  2. 标准的 Ruby Set 实现为 Hash.
  1. You're modifying elements of the Set without Set knowing about it.
  2. The standard Ruby Set is implemented as a Hash.

结果是您在 Hash 不知道的情况下修改了内部 Hash 的键,这使可怜的 Hash 变得不知道它有什么键.Hash 类有一个 rehash 方法:

The result is that you're modifying the internal Hash's keys without the Hash knowing about it and that confuses the poor Hash into not really knowing what keys it has anymore. The Hash class has a rehash method:

重新散列→hsh

根据每个键的当前哈希值重建哈希.如果键对象的值在插入后发生了变化,则此方法将重新索引 hsh.

Rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted, this method will reindex hsh.

a = [ "a", "b" ]
c = [ "c", "d" ]
h = { a => 100, c => 300 }
h[a]       #=> 100
a[0] = "z"
h[a]       #=> nil
h.rehash   #=> {["z", "b"]=>100, ["c", "d"]=>300}
h[a]       #=> 100

请注意 rehash 文档中包含的示例中的有趣行为.哈希使用键 kk.hash 值来跟踪事物.如果你有一个数组作为键并且你改变了数组,你也可以改变数组的 hash 值;结果是哈希仍然将该数组作为键,但哈希将无法找到该数组作为键,因为它将在存储桶中查找新的 hash 值,但是数组将在旧 hash 值的存储桶中.但是,如果你 rehash 哈希,它会突然能够再次找到它的所有密钥,并且衰老消失了.非数组键也会出现类似的问题:您只需更改键,使其 hash 值发生变化,并且包含该键的 Hash 会混淆并丢失,直到您 重新散列它.

Notice the interesting behavior in the example included with the rehash documentation. Hashes keep track of things using the k.hash values for the key k. If you have an array as a key and you change the array, you can change the array's hash value as well; the result is that the Hash still has that array as a key but the Hash won't be able to find that array as a key because it will be looking in the bucket for the new hash value but the array will be in the bucket for the old hash value. But, if you rehash the Hash, it will all of a sudden be able to find all of its keys again and the senility goes away. Similar problems will occur with non-array keys: you just have to change the key in such a way that its hash value changes and the Hash containing that key will get confused and wander around lost until you rehash it.

Set 类在内部使用 Hash 来存储它的成员和成员被用作哈希的键.因此,如果您更改成员,则 Set 将变得混乱.如果 Set 有一个 rehash 方法,那么你可以通过用 rehash 将 Set 颠倒过来来解决这个问题.唉,Set中没有这样的方法.但是,您可以在以下位置自行修补:

The Set class uses a Hash internally to store its members and the members are used as the hash's keys. So, if you change a member, the Set will get confused. If Set had a rehash method then you could kludge around the problem by slapping the Set upside the head with rehash to knock some sense into it; alas, there is no such method in Set. However, you can monkey patch your own in:

class Set
  def rehash
    @hash.rehash
  end
end

然后您可以更改密钥,在 Set 上调用 rehash,以及您的 delete(以及其他各种方法,例如 member?)将正常工作.

Then you can change the keys, call rehash on the Set, and your delete (and various other methods such as member?) will work properly.

这篇关于从无操作的集合中删除修改的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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