• 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: "Adam R. Maxwell" <email@hidden>
  • Date: Thu, 20 Aug 2009 22:19:20 -0700


On Aug 20, 2009, at 9:44 PM, Quincey Morris wrote:

The keys in a dictionary (or other keyed collection, like a map table) need to be immutable objects, but that's unrelated to the hash. Mutability in the keys would be bad, hash or no hash.

What do you mean by immutable? You can put a "mutable" object in a hashing collection as long as its -hash and -isEqual: do not depend on mutable state. For instance, if you configure a CFDictionary to use pointer equality and hash for its keys, you can use an NSMutableString as a key.


[...]

Separately, collections use isEqual: on the object values to determine equality, when they care. (I doubt that NSDictionary cares about object value equality, but NSSet certainly does, as does NSArray, when asked to compare objects.)

Separately, collections may (and presumably sometimes do) use *object* hash values to distribute objects in memory. (Obviously, NSSet does, and for all we know NSDictionary does too, to distribute object values that stored under the same key hash value -- though it would be an implementation detail.)

I use NSMutableStrings as values (not keys) in NSDictionary and expect that to work, even though hash/isEqual: of those strings is certainly changing while in the collection. CFDictionary at least doesn't include a hash function in its value callback, although it does require an equality function. From my quick look at the source, the only place that equality callback is used is in testing CFEqual(dict1, dict2), CFDictionaryGetCountOfValue(), and CFDictionaryContainsValue().



Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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: Quincey Morris <email@hidden>
References: 
 >When do I need to override hash? (From: Gideon King <email@hidden>)
 >Re: When do I need to override hash? (From: Seth Willits <email@hidden>)
 >Re: When do I need to override hash? (From: David Duncan <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Bryan Henry <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Kyle Sluder <email@hidden>)
 >Re: When do I need to override hash? (From: Shawn Erickson <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Kyle Sluder <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Alastair Houghton <email@hidden>)
 >Re: When do I need to override hash? (From: Seth Willits <email@hidden>)
 >Re: When do I need to override hash? (From: Quincey Morris <email@hidden>)

  • Prev by Date: RE: When do I need to override hash?
  • Next by Date: Re: When do I need to override hash?
  • 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