• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Help in 2 Diamensional Array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help in 2 Diamensional Array


  • Subject: Re: Help in 2 Diamensional Array
  • From: William Bumgarner <email@hidden>
  • Date: Fri, 31 Mar 2006 08:32:16 -0800

On Mar 31, 2006, at 6:15 AM, Anoop wrote:
Can I use a 2 two dimensional array in NSArray ?,if yes how is it possible I
want to reuse that array in the method performSelector: withObject: how is
it possible,if I am able to manage a 2 dimensional array in this method
will it create any problems with the memory.
Thanks for all replays in advance.

An NSArray is inherently of one dimension. You could do an array of arrays.


However, it is trivial to do a bit of math to treat it as a two dimensional array. This uses less memory and incurs less CPU overhead. In the end, this overhead is likely small compared to other potential performance issues -- this is a bit of premature optimization. But, what the heck. It is a useful hack that can be extended to any number of dimensions and having everything in a linear array means that you can transform from one representation to another with another small bit of math instead of moving the objects between containers

For example, if you wanted a 4 x 8 2D array of objects, you would do something like the following:

NSMutableArray *a = [NSMutableArray arrayWithCapacity: 4*8]; // this is just a hint, it doesn't "fill slots" or anything
int xIndex;
int yIndex;
for(xIndex = 0; xIndex < 8; xIndex++)
for(yIndex = 0; yIndex < 4; yIndex++)
[a addObject: [NSString stringWithFormat: @"%d, %d", xIndex, yIndex]];


    NSLog(@"Object at 3,2 is (%@)", [a objectAtIndex: (3*4) + 2]);
    NSLog(@"Object at 0,3 is (%@)", [a objectAtIndex: (0*4) + 3]);
    NSLog(@"Object at 7,0 is (%@)", [a objectAtIndex: (7*4) + 0]);

This outputs (I had to compile/run the code because I always confuse the 2D -> 1D transformation):

2006-03-31 08:24:50.022 dfhjdfhjdfjh[855] Object at 3,2 is (3, 2)
2006-03-31 08:24:50.022 dfhjdfhjdfjh[855] Object at 0,3 is (0, 3)
2006-03-31 08:24:50.022 dfhjdfhjdfjh[855] Object at 7,0 is (7, 0)

Now, the one limitation to all of this is that NSMutableArray is not sparse; every slot between 0 and the maximum index must be filled with an object. You could either fill the array with NSNull instances to represent the holes. NSNull is implemented as a singleton so, beyond the preallocated slots in the array, each instance really isn't a memory eating unique instance.

If you have a significant # of holes, you should probably look into any of a number of different sparse array implementations and shove said implementation into your own array class, potentially as a subclass of NSArray (or NSMutableArray).

b.bum


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Help in 2 Diamensional Array
      • From: j o a r <email@hidden>
References: 
 >Help in 2 Diamensional Array (From: Anoop <email@hidden>)

  • Prev by Date: Re: Help in 2 Diamensional Array
  • Next by Date: table view, popup, bindings: more grief...
  • Previous by thread: Re: Help in 2 Diamensional Array
  • Next by thread: Re: Help in 2 Diamensional Array
  • Index(es):
    • Date
    • Thread