• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Implementing isEqual: and hash
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implementing isEqual: and hash


  • Subject: Re: Implementing isEqual: and hash
  • From: Jeff Johnson <email@hidden>
  • Date: Sat, 23 Aug 2008 14:43:35 -0500

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Implementing isEqual: and hash
      • From: Pierre Sandboge <email@hidden>
References: 
 >Implementing isEqual: and hash (From: Graham Cox <email@hidden>)
 >Re: Implementing isEqual: and hash (From: "Michael Ash" <email@hidden>)
 >Re: Implementing isEqual: and hash (From: "Adam R. Maxwell" <email@hidden>)
 >Re: Implementing isEqual: and hash (From: Jeff Johnson <email@hidden>)
 >Re: Implementing isEqual: and hash (From: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: Re: error codes
  • Next by Date: Re: NSApplication _doOpenUntitled raises exception under Leopard, not Tiger
  • Previous by thread: Re: Implementing isEqual: and hash
  • Next by thread: Re: Implementing isEqual: and hash
  • Index(es):
    • Date
    • Thread