Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!news.mathworks.com!nntp.primenet.com!howland.erols.net!newsfeed.internetmci.com!in3.uu.net!interlan!news
From: w.matveyenko@ieee.ca (Wade Matveyenko)
Subject: Re: Equality of sets
Organization: Intrepid Consulting
Date: Wed, 4 Sep 1996 03:04:02 GMT
Message-ID: <Dx6s8n.7H9@interlan.Interlan.COM>
X-Newsreader: Forte Free Agent 1.0.82
References: <50c6a2$9l2@hermes.is.co.za>
Sender: news@interlan.Interlan.COM (news account)
Lines: 46

roberto@kw.co.za (Roberto Faria) wrote:

>Can anyone explain the following interesting phenomena concerning the
>equality of VisualWorks 2.0 sets:

>((Set with: #test) = (Set with: #test))  evaluates to: false

>...while
>((OrderedCollection with: #test) = (OrderedCollection with: #test))
>evaluates to: true

>Why is this so?

>Thanks in advance: 
>name: Roberto Faria
>company: Knowledge Weavers
>http: www.kw.co.za

If you look at the implementation of = in an OrderedCollection
(actually in SequencableCollection) you will see that it does a
comparison of the size of the collections and then does an element by
element comparison of both collections.  So for equality the 1st
element of collection 1 must equal the 1st element of collection 2,
the 2nd element of collection 1 must equal the 2nd element of
collection 2, etc. 

The implementation of = for a Set inherits from object which actually
does a == on both objects.  Since the two sets are not identical, the
result is false.  In addition, Sets are unordered collections so even
if both Sets contain the same elements, the ordering of the elements
internally might be different so the OrderedCollection implementation
of = would not return a consistent answer.

If you wanted to implement an = for Sets you would probably have to
convert them to SortedCollections and then do a = comparison.
Converting the Sets to SortedCollections, with the same sort blocks,
would ensure that the unordered nature of the Set is removed for the
comparison.

Wade Matveyenko, Senior Consultant
Intrepid Consulting
520 Lake Cook Road, Suite 390
Chicago, Il 60012
(847)267-9019 X81
w.matveyenko@ieee.ca

