Re: 2D Arrays
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.