Re: Implementing isEqual: and hash
Re: Implementing isEqual: and hash
- Subject: Re: Implementing isEqual: and hash
- From: Pierre Sandboge <email@hidden>
- Date: Sun, 24 Aug 2008 01:40:15 +0200
On Aug 23, 2008, at 9:43 PM, Jeff Johnson wrote:
On Aug 23, 2008, at 2:05 PM, Adam R. Maxwell wrote:
If I have
@interface Test : NSObject
{
id ivar1;
id ivar2;
}
@end
@implementation Test
- (BOOL)isEqual:(id)other
{
if ([other isKindOfClass:[self class]] == NO) return NO;
return ([ivar1 isEqual:(Test *)other->ivar1] && [ivar2 isEqual:
(Test *)other->ivar2]);
}
- (unsigned)hash { return [ivar1 hash]; }
@end
I believe it's sufficient to use [ivar1 hash], since the object is
only equal if ivar1 is equal in both objects. I was just curious
to know what you gain by using ([ivar1 hash] ^ [ivar2 hash]); is it
possible to know in general if it reduces collisions? Presumably
that depends on the hash table implementation as well.
--
Adam
Right, it depends on the hash table implementation, which we don't
know. Theoretically, it seems likely to reduce collisions. This
could only be confirmed by real-world performance tests, though.
-Jeff
_______________________________________________
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:
@standin.se
This email sent to email@hidden
I think theoretically, it seems likely to increase collisions. If
ivar1 and ivar2 aren't independent of each other, xor can cluster
different values causing lots of collisions. Consider what happens if
ivars are laid out serially say 0x00000100 and 0x00000200, xor and you
get 0x300, and let's say that another completely different object's
ivars are 0xff000600 and 0xff000700, and you still get 0x300,
collision.
The hash table implementation has nothing to do with this, the problem
occurs before the hash table has any chance to make it even worse. (If
you always return the same hash value, the hash table has nothing to
work with in spreading the stored objects.)
/Pierre
_______________________________________________
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