How to Compare Kits in Smalltalk

I was told that you need to sort them in a SortedCollection, but the Set elements are not comparable (only an identity comparison, which I do not know how to use for sorting).

Is it safe to compare them like set1 = set2, or do I need to sort them (as for identification?).

I was thinking of some kind of similar block: [: pre: succ | pre OID <succ OID], will this work?

+7
source share
2 answers

What about

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

Depending on your Smalltalk implementation, you may also use = . For example, in Squeak it is implemented as follows:

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

If you want to compare two sets, you can safely use set1 = set2 . Set items are compared using equality. Two sets are equal if they contain equal objects.

Sorting them does not make sense for comparing equality.

Note that Set Equality is implemented (roughly) as follows:

  • if both sets are the same size
  • if all elements of set2 are included in set1

->, since they are the same size, and all elements of set1 are in set2, they must be equal.

+3
source

All Articles