• 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: 2D Arrays
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 2D Arrays


  • Subject: Re: 2D Arrays
  • From: Drew McCormack <email@hidden>
  • Date: Sun, 8 Jun 2003 21:57:41 +0200

I suppose I could simulate a 2D array without having using to use a nested NSArray objectAtIndex: method to access each element as follows:

[array objectAtIndex:(i*rowsize + j)]

That way I could get the benefits of using NSArray without the penalty of sending two messages to the NSArray objects.

2d arrays are just a bunch of usual (1d) arrays in a usual array. i
find '2d', '3d'.. arrays a misleading concept and not very helpful.

I think they are useful for modeling 2D or 3D etc real world objects.

Do not hesitate to simply create a standard 2D C-style array, and use
it to point to NSObjects - that works fine. Otherwise, create "arrays
of arrays" using NSArray, as others have suggested.

I'd like to use a Cocoa object so I can take advantage of the built-in functionality, like memory management. If I use C-style arrays, I'll have to use malloc() and free() I suppose to dynamically create and destroy the arrays.
When I need high-performance arrays, I use NSData or NSMutableData. You get the performance of C-style arrays, with the memory management of Cocoa.

NSMutableData *matrixData = [NSMutableData dataWithLength:100 * sizeof(float) ];

...

float *matrix = [matrixData mutableBytes];

for ( i = 0; i < 10; i++ 0 {
for ( j = 0; j < 10; j++ ) {
matrix[10 * i + j] = ...;
}
}


Having said that, what we really need in the Cocoa world is a really powerful multidimensional array class, for high performance purposes. There is MathArray

http://mrtg.planetmirror.com/pub/gnustep/gnustep/contrib/

but I think something along the lines of Numpy in Python would be great.

Drew

----------------------------------
Dr. Drew McCormack
Trade Strategist (www.trade-strategist.com)
Stock Market strategy design platform for Mac OS X.
_______________________________________________
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.

References: 
 >Re: 2D Arrays (From: Graham Wheel <email@hidden>)

  • Prev by Date: Re: C question for you old guys ;-)
  • Next by Date: Re: New Mac Developer looking for advice
  • Previous by thread: Re: 2D Arrays
  • Next by thread: NSCursor changes not sticking
  • Index(es):
    • Date
    • Thread