Re: 2D Arrays
Re: 2D Arrays
- Subject: Re: 2D Arrays
- From: Simon Stapleton <email@hidden>
- Date: Sun, 8 Jun 2003 10:43:57 +0200
Subject: 2D Arrays
Cc: email@hidden
To: email@hidden
From: email@hidden
What would be the best way to implement a 2 dimensional (or
multi-dimensional) array in Obj-C/Cocoa?
I know I could use the C-language multiple subscript feature, eg
array[i][j], and give it a class wrapper; I'm wondering if there is a
consensus on the best way to do this as I'm sure it is a topic that
must have already been approached.
Thanks in advance,
Graham
Hi Graham.
Depending on your usage requirements, you can probably get away with
arrays in arrays (you might even want to wrap it up in a simple class
to avoid complicating user code)
However (and there had to be a 'however', right ;-)
This can be rather memory-inefficient, especially if your data is
sparse. Let me explain.
Assume you want an array that is 1024 by 1024. The naive implementation
creates 1025 arrays (one for the 'x', and 1024 for each 'y').
We can optimise this somewhat by lazily instantiating the 'y' arrays
(initially we create the 'x' array containing 1024 NSNull objects or
similar, then when we come to try and add an object at a given
coordinate, we create the required 'y' array if necessary.
We can further optimise memory usage by making the dynamically created
arrays only contain objects up to the requested 'y' value, and then
extending the arrays as necessary on demand.
This is pretty simple stuff, really. Basically, what it comes down to
is how clever you want to get, and how much you care about memory usage.
<blatant plug>
Once you start getting into more dimensions, though, you might consider
heading on over to <
http://www.tufty.co.uk/Software/objectware.html>
and download my Data Cube Framework. In fact, it would probably work
quite nicely for your purposes as well.
It's a blazing fast[1], n-dimensional, sparse array class. Read access
approaches the speed of pure 'C' arrays, write access _can_ be a lot
slower. It's not bounded by available memory - potential (and actual)
data storage can be much larger than the available address space for
your process (which can be useful if you're modelling 16-dimensional
data ;-). The memory footprint for non-sparse arrays is a bit heavy -
best memory performance is got from sparse, locally clustered data
(think spreadsheets with great big empty spaces and lots of little
clusters of data)
The only real downside (other than "It's free, but you don't get the
source") is that the interface is somewhat in flux at the moment. It
could (and probably will) change real soon now, which is the main
reason I haven't bothered to publicise it before.
It might be a bit heavyweight for your needs, but could be worth a look.
Hope some of this helps.
Simon
[1] For varying values of 'blazing fast' ;-)
--
PGP Key Id : 0x50D0698D
--
Well, we finally have an indoor toilet. But what new disasters have
struck the Alpenproject?
Find out at : <
http://www.tufty.co.uk/Move/index.html>
_______________________________________________
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.