开发者

Comparator based on integer addresses

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开发者_如何学Go 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_t or uintptr_t. These types are defined in <stdint.h> and are explicitly guaranteed to be large enough to hold the value of a pointer.


I assume that pointers in objective-c are similar to c/c++. I have built sorted arrays of memory addresses to use for looking up a stack trace. On a 64 bit machine, it should be cast to whatever scalar variable is 64 bits, probably a long int. Or if you can use a uint64_t type, it will guarantee the correct size. For a 32 bit machine, use an unsigned 32 bit integer, or a uint32_t. You can use the > < >= <= = and - operators on pointers and you can also use the + operator with a pointer and an integer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜