简体   繁体   English

如何比较Smalltalk中的集合

[英]How to compare Sets in Smalltalk

I was told that one has to sort them into SortedCollection, but the Set elements are not comparable (only identity compare, which i dont know how to use for sorting). 我被告知必须将它们排序为SortedCollection,但Set元素不具有可比性(只有身份比较,我不知道如何用于排序)。

So is it safe to compare them like set1 = set2, or do i need to sort them (how for identity?). 那么像set1 = set2那样比较它们是安全的,还是我需要对它们进行排序(如何识别?)。

I was thinking about some sortblock like this: [:pre :succ | 我在考虑这样的排序块:[:pre:succ | pre OID < succ OID], would that work? 前OID <succ OID],会有用吗?

How about 怎么样

(set1 size = set2 size) and: [set1 includesAllOf: set2]

Depending on the Smalltalk implementation, you might also use = . 根据Smalltalk实现,您也可以使用= For example, in Squeak it is implemented like this: 例如,在Squeak中,它实现如下:

= aSet
    ...
    self size = aSet size ifFalse: [^ false].
    self do: [:each | (aSet includes: each) ifFalse: [^ false]].
    ^ true

If you want to compare two sets, you can safely use set1 = set2 . 如果要比较两组,可以安全地使用set1 = set2 The elements of the sets are compared using equality. 使用相等性比较集合的元素。 Two sets are equal if they contain equal objects. 如果它们包含相同的对象,则两组相等。

Sorting them does not make sense for equality comparison. 对它们进行排序对于平等比较没有意义。

Mind that Set equality is implemented (roughly) as follows: 请注意,Set(大致)实现如下:

  • if boths sets have equal size 如果两组都有相同的大小
  • if all elements of set2 are included in set1 如果set2的所有元素都包含在set1中

-> since they have the same size and all elements of set1 are in set2, they must be equal. - >因为它们具有相同的大小并且set1的所有元素都在set2中,所以它们必须相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM