Re: Multidimensiaon Arrays in Cocoa
Re: Multidimensiaon Arrays in Cocoa
- Subject: Re: Multidimensiaon Arrays in Cocoa
- From: "John C. Randolph" <email@hidden>
- Date: Sun, 23 Sep 2001 05:52:41 -0700
On Sunday, September 23, 2001, at 04:35 AM, email@hidden wrote:
Am I missing something or are there no multidimensional arrays in
Cocoa's NSArray? What do we do, just implement them the old fashioned
way with Objective-C?
One trick that just occurred to me for storage of values of a sparse
array of any number of dimensions, is to keep the values in an
NSDictionary, where the key is the contatenation of the coordinates.
So, if you have a value at (10, 26, 99), and another value at (2, 15,
0), your dictionary would contain:
{
"10:26:99" = <some value>
"2:15:0" = <some other value>
}
And you would use *no* storage for empty or uninitialized values. Your
code could treat any value that the dictionary couldn't find as zero or
null.
Of course, a hash lookup isn't nearly as fast as multiplying addresses
and offsets, but this approach doesn't waste much space.
-jcr
"I fear all we have done is to awaken a sleeping giant and fill him with
a terrible resolve." -Admiral Isoroku Yamamoto, Dec 7, 1941.