[Glass] Hash implementations

monty via Glass glass at lists.gemtalksystems.com
Thu Mar 15 08:16:35 PDT 2018

First, #= methods should start like this:

= anObject
    self == anObject
        ifTrue: [^ true]. "they're identical; no further comparisons"

    self species == anObject species
        ifFalse: [^ false]. "different kinds of objects; no further comparisons"

    "...compare self's state with anObject's state"

Why #species instead of #class? Because it supports polymorphism. If someone creates a ClassB subclass of your ClassA class, but only changes the implementation slightly and wants instances of both to be #= when they share the same state, they just have to implement ClassB>>#species to return ClassA. Now ClassA>>#= will work polymorphically, since both classes return the same #species (ClassA).

For #hash, since it's non-cryptographic, collisions (two unequal objects reporting the same #hash) are permitted, but undesirable. The main requirement is that __whenever two objects are #=, their #hashes are #= too__.

In fact, it's not uncommon to see #hash implementations only use a proper subset of the state that their corresponding #= method does to make #hash faster, but at the cost of more collisions. Compare ZnUrl #hash with #=.

Sent: Thursday, March 15, 2018 at 6:28 AM
From: "Trussardi Dario Romano via Glass" <glass at lists.gemtalksystems.com>
To: "GemStone Seaside beta discussion" <glass at lists.gemtalksystems.com>
Subject: [Glass] Hash implementations

i have a seaside application development with Pharo ( 4.0 ) and deployment with Gemstone GLASS.
Now  i have some doubts about collisions regarding the hashing and the equality implementation.
Where for collision i intend the same hashing value from two different object.
For example i have a class where redefine the methods :  =   and the relative  hash .
= anItem
anItem ifNil:[^false].
anItem class = self class ifFalse:[ ^false].
^ rfrConsegna = anItem rfrConsegna
and:[ rfrSubTable = anItem rfrSubTable
and:[ referenceTime = anItem referenceTime
and:[ consumer = anItem consumer
and:[ item = anItem item
and:[ opzioniVoce = anItem opzioniVoce ]]]]]
^ self class hash
bitXor:( rfrConsegna hash
bitXor:( rfrSubTable hash
bitXor: ( referenceTime hash
bitXor: ( consumer hash
bitXor: ( item hash
bitXor: ( opzioniVoce hash ))))))
( All variables used define the respective methods:    =    hash )
Now all works fine, 
but i can have collision ?
>From the theoretical point of view it is right ?
Someone can give me indications about it?
Thanks for any considerations.
Dario_______________________________________________ Glass mailing list Glass at lists.gemtalksystems.com http://lists.gemtalksystems.com/mailman/listinfo/glass

More information about the Glass mailing list