I want to create a custom dictionary that does not copy its keys (just retains).
Internally, I plan to use an NSMutableArray with a special Pair object, where the first object of the pair is the key, and the second is the value.
Keys are all unique.
In order to quickly retrieve objects on keys, I’d use binary search algorithm – so the array should be SORTED (attention!) on memory addresses of pairs’ first objects.
(btw, this is why I refused to use CFDictionaryRef with a special set of callbacks – I suspect it to degrade to O(n) in case when a reasonable hash would not be provided)
Is this a bad idea, assuming that:
- Objects used as keys may change internally (that is, I cannot use
isEqual:instead of address comparison); - Objects used as keys are NOT going to be deallocated (which is reasonable – they are retained by a special Pair object, and the Pair is retained by an internal NSArray).
Why is it bad (or good)?
How do I obtain memory address from a pointer? Just cast it to a long long int or something like that?
Thank you!
To get an integer value from a pointer, cast to
intptr_toruintptr_t. These types are defined in<stdint.h>and are explicitly guaranteed to be large enough to hold the value of a pointer.