当通过序列而不是集合时,为什么集合#相交的行为会有所不同?

发布于 2025-02-06 17:09:05 字数 712 浏览 3 评论 0原文

我注意到应用程序中的一个奇数错误,并将其范围缩小到一个令人惊讶的原因:如果将序列传递给set#seter#互动,则与传递set set ,即使两者都是set数据结构的支持方法。

您可以在任何Swift Repl上验证:

  1> Set([3, 6, 0, 1, 5, 2, 4]).intersection([0, 1, 1, 2, 3, 4, 5])
$R0: Set<Int> = 7 values {
  [0] = 5
  [1] = 0
  [2] = 3
  [3] = 1
  [4] = 2
  [5] = 6
  [6] = 4
}
  2> Set([3, 6, 0, 1, 5, 2, 4]).intersection(Set([0, 1, 1, 2, 3, 4, 5]))
$R1: Set<Int> = 6 values {
  [0] = 1
  [1] = 5
  [2] = 0
  [3] = 4
  [4] = 2
  [5] = 3
}

这里的问题是,即使在序列传递给的序列中没有6,第一个结果也具有额外的值,整数 6 交叉点。第二个结果是正确的 - 第一个结果是错误的。两个调用之间的唯一区别是,第二个调用的序列将其序列转换为set

我是失去理智,还是意外的行为?

I noticed an odd bug in an app and narrowed it down to a surprising cause: if you pass a sequence to Set#intersection, you get different behavior than if you pass a Set, even though both are supported methods of the Set data structure.

You can verify at any Swift REPL:

  1> Set([3, 6, 0, 1, 5, 2, 4]).intersection([0, 1, 1, 2, 3, 4, 5])
$R0: Set<Int> = 7 values {
  [0] = 5
  [1] = 0
  [2] = 3
  [3] = 1
  [4] = 2
  [5] = 6
  [6] = 4
}
  2> Set([3, 6, 0, 1, 5, 2, 4]).intersection(Set([0, 1, 1, 2, 3, 4, 5]))
$R1: Set<Int> = 6 values {
  [0] = 1
  [1] = 5
  [2] = 0
  [3] = 4
  [4] = 2
  [5] = 3
}

The issue here is that the first result has an extra value, the integer 6, even though there is no 6 in the sequence passed to intersection. The second result is correct - the first is wrong. And the only difference between the two calls is that the second call has its sequence converted to a Set.

Am I losing my mind, or is this unexpected behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2025-02-13 17:09:05

我认为这是错误 sr-16061 。该报告显示了与您的类似的意外行为,其中使用set 交叉点的过载会产生正确的输出,但是sequence Overload没有。

let s = Set("ae")

print("s: \(s)") // ["a", "e"]

let i = s.intersection("aa")
let j = s.intersection(Set("aa"))

print("i: \(i)") // ["a", "e"]
print("j: \(j)") // ["a"]

我试图稍作研究发生的事情。先前的“ norefollow noreferrer”>“ noreferrer”> committ 修改了 intersection 斯威夫特回到2021年11月。这与可以复制此错误的Swift版本的发行日期相符 - 仅在Swift 5.5和5.6之间。据说,这种更改应该通过使用一些设置来加快set.Intersection

I think this is the bug SR-16061. The report shows a similar unexpected behaviour to yours, where using the Set overload of intersection produces the correct output, but the Sequence overload does not.

let s = Set("ae")

print("s: \(s)") // ["a", "e"]

let i = s.intersection("aa")
let j = s.intersection(Set("aa"))

print("i: \(i)") // ["a", "e"]
print("j: \(j)") // ["a"]

I tried to look a little into what's happened. The previous commit that modified the intersection methods in NativeSet.swift was back in November 2021. This agrees with the release dates of the Swift version in which this bug can be reproduced - just between Swift 5.5 and 5.6. It is said that this change is supposed to speed up Set.intersection by using a bit set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文