Implementing isEqual: and hash

2008-08-23 Thread Graham Cox
I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the docs say I also need to implement -hash. Any pointers on what is a good way to do that? Could I just

Re: Implementing isEqual: and hash

2008-08-23 Thread Phil
On Sat, Aug 23, 2008 at 11:41 PM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the docs say I also need to implement

Re: Implementing isEqual: and hash

2008-08-23 Thread Michael Ash
On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the docs say I also need to implement

Re: Implementing isEqual: and hash

2008-08-23 Thread Graham Cox
On 23 Aug 2008, at 10:13 pm, Michael Ash wrote: On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based

Re: Implementing isEqual: and hash

2008-08-23 Thread Jean-Daniel Dupas
Le 23 août 08 à 13:41, Graham Cox a écrit : I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the docs say I also need to implement -hash. Any pointers on

Re: Implementing isEqual: and hash

2008-08-23 Thread Graham Cox
On 23 Aug 2008, at 9:52 pm, Jean-Daniel Dupas wrote: Le 23 août 08 à 13:41, Graham Cox a écrit : I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the

Re: Implementing isEqual: and hash

2008-08-23 Thread Jean-Daniel Dupas
Le 23 août 08 à 15:39, Graham Cox a écrit : On 23 Aug 2008, at 9:52 pm, Jean-Daniel Dupas wrote: Le 23 août 08 à 13:41, Graham Cox a écrit : I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned- string). I can

Re: Implementing isEqual: and hash

2008-08-23 Thread Jim Correia
On Aug 23, 2008, at 7:41 AM, Graham Cox wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based on that but the docs say I also need to implement -hash. Any pointers

Re: Implementing isEqual: and hash

2008-08-23 Thread Jeff Johnson
On Aug 23, 2008, at 8:57 AM, Jean-Daniel Dupas wrote: Look into the CoreFoundation sources. as Cocoa primitive are tool free bridged, they use the same hash functions. They is even a special case for NSString into the CFString.c file.

Re: Implementing isEqual: and hash

2008-08-23 Thread Adam R. Maxwell
On Aug 23, 2008, at 5:13 AM, Michael Ash wrote: On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a UUID-turned-string). I can easily implement isEqual: based

Re: Implementing isEqual: and hash

2008-08-23 Thread Jeff Johnson
On Aug 23, 2008, at 11:38 AM, Adam R. Maxwell wrote: On Aug 23, 2008, at 5:13 AM, Michael Ash wrote: On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same internal string value (which happens to be a

Re: Implementing isEqual: and hash

2008-08-23 Thread Jeff Johnson
On Aug 23, 2008, at 12:26 PM, Jeff Johnson wrote: If you compare primitives in -isEqual:, you should combine them (using xor or the like) in -hash. If you compare objects by calling - isEqual: on them, you should combine their hashes (using xor or the like). If you do some of each, combine

Re: Implementing isEqual: and hash

2008-08-23 Thread Adam R. Maxwell
On Aug 23, 2008, at 10:26 AM, Jeff Johnson wrote: On Aug 23, 2008, at 11:38 AM, Adam R. Maxwell wrote: On Aug 23, 2008, at 5:13 AM, Michael Ash wrote: On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox [EMAIL PROTECTED] wrote: I have a class for which equality can be defined as having the same

Re: Implementing isEqual: and hash

2008-08-23 Thread Jeff Johnson
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:

Re: Implementing isEqual: and hash

2008-08-23 Thread Pierre Sandboge
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

Re: Implementing isEqual: and hash

2008-08-23 Thread Peter Duniho
Date: Sat, 23 Aug 2008 12:05:44 -0700 From: Adam R. Maxwell [EMAIL PROTECTED] [...] - (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

Re: Implementing isEqual: and hash

2008-08-23 Thread Adam R. Maxwell
On Aug 23, 2008, at 5:20 PM, Peter Duniho wrote: Date: Sat, 23 Aug 2008 12:05:44 -0700 From: Adam R. Maxwell [EMAIL PROTECTED] [...] Note the redundant multiply and add in the first line. Writing the code that way makes it easy to add/remove members if for some reason the data

Re: Implementing isEqual: and hash

2008-08-23 Thread Peter Duniho
Date: Sat, 23 Aug 2008 17:52:21 -0700 From: Adam R. Maxwell [EMAIL PROTECTED] Well, if your hash values collide, they collide regardless of the hash table. As long as you've got a well-distributed hash function, any collisions generated by the hash table implementation should be