Re: CFMutableDictionary vs. NSMapTable
Re: CFMutableDictionary vs. NSMapTable
- Subject: Re: CFMutableDictionary vs. NSMapTable
- From: Bill Bumgarner <email@hidden>
- Date: Mon, 9 Sep 2002 21:36:30 -0400
On Monday, Sep 9, 2002, at 19:13 US/Eastern, Mike Shields wrote:
On Monday, September 9, 2002, at 07:25 AM, Bill Bumgarner wrote:
I need to create a set of maps between integer keys and either
objects or integer values, depending on context (though any one given
dictionary/map will only have values of a single type, obviously).
I have long used NSMapTable's API in this context, but was curious if
CFDictionary's API is now preferable. Looking further at
CFDictionary, it doesn't explicitly document a case where a
non-CFTypeRef is used as the key or values. At the same time, the
key and value callback functions appear to do what is needed to use
integer key/values if you simply pass in NULL for the structure
references.
Right. The NULL values for retain/release are exactly what you're
looking for. You'd probably also want to add in a custom hash and
equal callback to be 100% safe from all edge cases.
But this makes the assumption that sizeof(int) == sizeof(void *) --
i.e. if the code were to move to a 64 bit CPU (no, I'm not speculating,
I'm merely trying to write portable code -- porting the CFDict* or
NSMap* APIs "good enough" isn't hard) and (int) remains 32 bits, the
code breaks unless the automatic type coercion does the right thing
(even then, I still get a bunch of compiler warnings).
However, given that the fact that the number of bits in an (int) is
only coincidentally the same as the number of bits in (void *) --
(void *) being the type of all key and value parameters to the
CFDictionary APIs -- it would appear that CFDictionary is very
explicitly only designed to handle pointer types.
Is there some reason that you need to use ints explicitly? And in my
copy of the Foundation docs, NSMapInsert, NSMapGet, etc all take void*
also. So where's the difficulty of switching?
Yes-- you are correct. I misremembered the API (and also discovered
that I had abused it in the past).
Bottom line: I'll move to CFDictionary because it has an API and
feature set that is actually a bit more flexible than NSMapTable and
fits better with the whole CFType model that is pervasive throughout
the rest of my code.
I'm using int's simply because that is the native data type of the
information that I'm storing (OIDs from a remote system delivered via
XML-RPC, specifically). However, it is trivial to wrap 'em in
CFNumbers and store those via CFDictionary -- also convenient because I
have to deliver CFNumbers into the WebServices API anyway.
This will also smooth a bit of the code used to deal with the data
coming out of the web services API. Unfortunately, it also requires
some ugly modification in that I had to write a bunch of XML tree
traversal code to work around a bug in the Web Services API that caused
it to abort() in certain circumstances (problem reported to
bugreport.apple.com and fixed). I'll be glad when that hunk of code
goes away as it is rather disgusting.
As such, I'm sticking with NSMapTable.
Correct line of thinking or did I miss something?
Not correcting, but just a little confused.
Yup -- I was confused. Thanks for the clarification.
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.