Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

So is it safe to compare them like set1 = set2, or do i need to sort them (how for identity?).

I was thinking about some sortblock like this: [:pre :succ | pre OID < succ OID], would that work?

like image 720
Jakub Fojtik Avatar asked Jan 26 '13 17:01

Jakub Fojtik


2 Answers

How about

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

Depending on the Smalltalk implementation, you might also use =. For example, in Squeak it is implemented like this:

= aSet
    ...
    self size = aSet size ifFalse: [^ false].
    self do: [:each | (aSet includes: each) ifFalse: [^ false]].
    ^ true
like image 199
Leo Avatar answered Oct 24 '22 06:10

Leo


If you want to compare two sets, you can safely use 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:

  • if boths sets have equal size
  • if all elements of set2 are included in set1

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

like image 25
Johan B Avatar answered Oct 24 '22 07:10

Johan B