Equality test of Dictionaries
Alejandro F. Reimondo
alereimondo at sugarweb.com
Sun Jan 11 21:10:23 UTC 1998
Equality test is implemented in Set as:
--------------------------------------------------------
!Set methodsFor: 'testing' stamp: 'afr 1/10/98 17:56'!
= aSet
(aSet isKindOf: Set) ifFalse: [^ false].
self size = aSet size ifFalse: [^ false].
self do: [:each | (aSet includes: each) ifFalse: [^ false]].
^ true! !
This causes a great degradation of perfomance when comparing a Set to itself because all elements are traversed.
This perfomance cost is evident when using big Pool Dictionaries (that inherits from Set); each time you see a class using a big pool dictionary in a browser it waste a long time (determining poolDict names in Smalltalk #keyAtValue: ).
I've solved this problem reimplementing #= as:
------------------------------------------------------------------
!Set methodsFor: 'testing' stamp: 'afr 1/10/98 17:56'!
= aSet
aSet == self ifTrue: [ ^true ].
"Modified because perfomance is degradated for big Dictionaries (like BIG pool dictionaries).
Must be revised to conform X3J20 ANSI Smalltalk specification.
(#= in AbstractDictionary conforms to Object protocol)
afr 1/10/98 17:41"
(aSet isKindOf: Set) ifFalse: [^ false].
self size = aSet size ifFalse: [^ false].
self do: [:each | (aSet includes: each) ifFalse: [^ false]].
^ true! !
BUT, I don't know if it is the best aproach because X3J20 Specification determines that Dictionary protocol conforms to Object protocol; then equality test must be identity test!
Comments on this topic will be appreciated.
Ale.
Alejandro F. Reimondo
Feel free to visit Smalltalk User Group of Argentina (SUGAR)
at http://www.sugarWeb.com
More information about the Squeak-dev
mailing list
|