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: Greg Parker <email@hidden>
- Date: Thu, 20 Aug 2009 14:25:14 -0700
On Aug 20, 2009, at 12:00 PM, Seth Willits wrote:
From the docs:
If a mutable object is added to a collection that uses hash values
to determine the object’s position in the collection, the value
returned by thehash method of the object must not change while the
object is in the collection. Therefore, either the hash method must
not rely on any of the object’s internal state information or you
must make sure the object’s internal state information does not
change while the object is in the collection.
That's pretty hard to deal with.
Returning 0 is certainly simpler :p
Simpler, but slower. Performance of NSSet and NSDictionary in
particular will drop dramatically if the -hash values of unequal keys
are always the same.
If two objects are -isEqual: to each other, then they MUST have the
same -hash value. NSSet and NSDictionary will behave incorrectly if
you do this wrong for their keys.
If two objects are not -isEqual: to each other, then they SHOULD have
different -hash values. They MAY have the same -hash value, but NSSet
and NSDictionary will be slower if that happens too often.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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