• 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: When do I need to override hash?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When do I need to override hash?


  • Subject: Re: When do I need to override hash?
  • From: David Duncan <email@hidden>
  • Date: Thu, 20 Aug 2009 12:33:28 -0700

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) without relying on object state (since a particular object's location in memory will never change). You can probably do even better than this, but as with hash codes in general there is a certain amount of experimentation required.
--
David Duncan
Apple DTS Animation and Printing


_______________________________________________

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: When do I need to override hash?
      • From: Clark Cox <email@hidden>
References: 
 >When do I need to override hash? (From: Gideon King <email@hidden>)
 >Re: When do I need to override hash? (From: Nathan Vander Wilt <email@hidden>)
 >Re: When do I need to override hash? (From: Gideon King <email@hidden>)
 >Re: When do I need to override hash? (From: "Adam R. Maxwell" <email@hidden>)
 >Re: When do I need to override hash? (From: Seth Willits <email@hidden>)

  • Prev by Date: Re: NSSortDescriptor in core-data document app
  • Next by Date: Re: Heartbeat thread is blocked? -- With Code and Movie
  • Previous by thread: Re: When do I need to override hash?
  • Next by thread: Re: When do I need to override hash?
  • Index(es):
    • Date
    • Thread