Re: When do I need to override hash?
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