Re: CFMutableDictionary vs. NSMapTable
Re: CFMutableDictionary vs. NSMapTable
- Subject: Re: CFMutableDictionary vs. NSMapTable
- From: Chris Kane <email@hidden>
- Date: Mon, 9 Sep 2002 19:00:36 -0700
On Monday, September 9, 2002, at 06:36 PM, Bill Bumgarner wrote:
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).
There's no problem if, say, pointers go to 64-bits and ints stay at 32
(and you're saying "int" because you are actually planning to use
"int") -- the promotion to pointer will sign-extend the int to a 64-bit
quantity. Presumably it should be the same thing that happens between
a signed short cast to (void *) today, so a tiny experiment can be
tried. But as long as sizeof(int) <= sizeof(void *), all possible int
values will fit in a void *.
Now, if pointers stay 32-bits and int goes to 64, then there are many
int values that won't squeeze unadulterated into a pointer-sized
quantity, obviously, and you might have trouble, unless all your ints
are "small/near zero" -- that is, even if ints were 64 bits, you
wouldn't be exercising the entirety of the range.
"int" is a very generic type that can change. If you really don't care
too much about the range of the values (ie, you're not using int32_t or
similar), then I'd highly recommend using intptr_t and uintptr_t (also
from <stdint.h>, new in C99) -- those types are defined to be integers
of the same "size" as pointer values, and so should match (void *)'s
expressiveness.
Or use CFNumbers as you said.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
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.