A Fresh Look at Comparisons, Take 2

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 18 11:28:05 PDT 2008


"Janice Caron" wrote
> On 18/04/2008, Steven Schveighoffer wrote:
>>  >>  >>  You need to have opEquals in Object in order to support hashing.
>>> > Please could you elaborate, as I don't understand why this is so?
>> ...
>>  It is not necessary in Object
>
> Ah! That would explain why I didn't understand why it was so! :-)
>
> I am confused about one point though. I know that one can implement an
> AA using hashing. I also know that one can implement an AA using
> comparison. However, it seems to me that one does not need /both/. How
> are D AA's implemented - do they use hashing, or do they use
> comparison? And why do the docs say we need both?

That is a good question...

I assume that one should override both opEquals and opCmp because if one 
doesn't, then if opCmp(x) == 0 can return different results than 
opEquals(x).

I'm guessing that the buckets of the hash are somehow sorted lists, maybe 
they are RB trees (that seems terribly inefficient) or just sorted arrays? 
I have no idea.  But strictly speaking, opCmp is not required for a basic 
hash implementation.  only opEquals to see if you got the right element or 
not.

I'd be interested to know why opCmp is required.

-steve 





More information about the Digitalmars-d mailing list