views:

555

answers:

4

The documentation for -hash says it must not change while a mutable object is stored in a collection, and similarly the documentation for -isEqual: says the -hash value must be the same for equal objects.

Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0)? Does anybody know how the mutable versions of framework-provided classes do this?

The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.

EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.

A: 

In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.

Bombe
Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
Kevin Ballard
+2  A: 

My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."

To quote directly from the NSObject documentation for hash:

If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, the value returned by the hash method of the object must not change while the object is in the collection. Therefore, either the hash method must not rely on any of the object’s internal state information or you must make sure the object’s internal state information does not change while the object is in the collection.

(Emphasis mine.)

Evan DiBiase
Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
Kevin Ballard
If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state *or* the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
Evan DiBiase
On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
Evan DiBiase
I'm asking the former. I'll update the question to make this clearer
Kevin Ballard
+2  A: 

Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.

In this case, you have 2 choices, neither of which works:

  1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.
  2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.

It seems to me that there's no possible way to do this without using a constant as a hashcode.

Outlaw Programmer
That's what I thought, and I guess it's probably right.
Kevin Ballard
A: 

Since you are already overriding -isEqual: to do a value-based comparison, are you sure you really need to bother with -hash?

I can't guess what exactly you need this for of course, but if you want to do value-based comparison without deviating from the expected implementation of -isEqual: to only return YES when hashes are identical, a better approach might be to mimick NSString's -isEqualToString:, so to create your own -isEqualToFoo: method instead of using or overriding -isEqual:.

Dirk Stoop
The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple%5Fref/doc/uid/TP40002974-CH4-SW25
Quinn Taylor