 |
 |
NSMutableArray and hashing???
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
I have an NSMutable array and I want to remove objects that are both in that and in another array. I understand the isEqual method and I understand the compare methods that i need to make for sorting, but, although I know what a hash table is, I have no idea what it really wants when it says that it wants me to implement both the hash function and the isEqual function to remove another array. Excerpt from the documentation:
This method is similar to removeObject:, but allows you to efficiently remove large sets of objects with a single operation. It assumes that all elements in otherArray-which are the objects to be removed-respond to hash and isEqual:.
So, what should I do for hash? My objects in the array are only really differentiated by FSRef's which are 80 byte quantities and I have no idea what the internal structure of an FSRef is composed of, so how can I hash it?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Status:
Offline
|
|
You shouldn't have to do anything. NSObject has default -hash and -isEqual methods, which you normally don't need to override.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Status:
Offline
|
|
@#$! back button...
[ 06-23-2001: Message edited by: 3.1416 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Oh, okay. I thought that those were unimplemented but I guess that i was wrong. Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
NSObject's hash and isEqual: methods depend on the address of the object. These may work all right if you know that the objects you are removing from the main array are the same objects in the other array, but they emphatically won't work if those objects are merely identical (e.g. two separate instances of an NSString "Hello").
If you can have two separate but identical objects, the proper thing to do is to implement isEqual: to call FSCompareFSRefs. The hash method is supposed to be fast, so a Quick 'n Dirty hash for a FSRef would be to loop over those 80 bytes (or a fixed subset of them) and return their sum mod 31 or some other prime, perhaps a larger one if your arrays are likely to be long.
Hope this helps.
-Peter
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
I'm sorry, I gave you bad advice re: the hash function. Since there's no guarantee that FSRefs that point to the same file have the same byte sequence, you shouldn't hash them by summing over the sequence. Instead, you could use FSRefMakePath and hash over the path:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (<font color = green>unsigned</font>)hash {
<font color = green>static</font> <font color = green>char</font> path[<font color = blue>64</font>];
FSRefMakePath(&myFSRef, path, <font color = green>sizeof</font>(path));
<font color = green>return</font> [[NSString stringWithCString  ath length:<font color = green>sizeof</font>(path)] hash];
}
</font>[/code]
But this may turn out to be just as slow as returning 0 for the hash and relying on the isEqual: method.
-Peter
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|