Re: When do I need to override hash?
Re: When do I need to override hash?
- Subject: Re: When do I need to override hash?
- From: Clark Cox <email@hidden>
- Date: Thu, 20 Aug 2009 13:27:49 -0700
On Thu, Aug 20, 2009 at 12:33 PM, David Duncan<email@hidden> wrote:
> On Aug 20, 2009, at 12:00 PM, Seth Willits wrote:
>
>> Returning 0 is certainly simpler :p
>
>
> It is, but you can generally do better than just returning 0, usually by
> just extracting some bits from 'self', ala
>
> -(NSUInteger)hash
> {
> uintptr_t hash = (uintptr_t)self;
> return (hash >> 4);
> }
>
> This satisfies the condition of hash (two equal objects will have the same
> hash code)
No it doesn't. Writing the hash method like that basically prevents
you from having an isEqual that does anything other than a pointer
comparison.
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden