Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSMutableArray and hashing???

NSMutableArray and hashing???
Thread Tools
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jun 23, 2001, 07:30 AM
 
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
Reply With Quote
Jun 23, 2001, 06:31 PM
 
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
Reply With Quote
Jun 23, 2001, 06:35 PM
 
@#$! back button...

[ 06-23-2001: Message edited by: 3.1416 ]
     
Dalgo  (op)
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jun 25, 2001, 07:13 AM
 
Oh, okay. I thought that those were unimplemented but I guess that i was wrong. Thanks.
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jun 25, 2001, 12:33 PM
 
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
Reply With Quote
Jun 25, 2001, 01:24 PM
 
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 stringWithCStringath 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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2