CocoaDev

Edit AllPages

Object Design: Matrix collection class


Why a Matrix is not an Array or a Dictionary—-

An array (NSArray;DesignArray) is a one-dimensional structure: it takes a single coordinate to refer to a single object. For any sane computer program, http://goo.gl/Cx9sQ this is sufficient as there can only be a finite number of objects anyway. But it is convenient to use something a little more general.

Apple’s answer is NSDictionary (DesignDictionary) - a class that stores objects and refers to them by arbitrary objects, not simple numbers. The very existence of this storage type is intrinsic to all of Cocoa, and weaved into every class you write: the -(unsigned)hash method of NSObject. This intermingling is both a benefit and a curse. Any object can be used as a key with little work. But this is far from efficient, because the number of possible keys vastly outweighs the relatively small number of indices we have available in an array. This means we need to store each key along with the object it references, which can be very memory-inefficient, especially when compared to the zero-key-overhead of the array it is supposed to replace. This occurs both because any object can be a key in any array, and because only one hashing algorithm can be used for each type. So we need a new storage type to fill the void, one I have christened (for now) the Matrix Design. This:

Advantages:

Disadvantages:

What do people think of my criterion for and assessment of the Matrix design? Feel free to add to the advantages/disadvantages lists and add any other comments – KritTer

—-The design in practice: KTMatrix [http://www.geocities.com/kritter_cocoadev/KTMatrix/] —-

I am designing a matrix class (as you may have guessed from ClassClusters), and would appreciate some input.

It implements the design above, using dictionaries for keys, and arbitrary hashing objects to turn them into integer hash values (which are then used as keys in a storage dictionary).

To simplify its use in the role it was intended (cuboid multi-dimensional arrays), it has in-built functionality for easier syntax in those areas, to the point where the equivalent of id matrix[5][3][2]; is KTMutableMatrix *matrix = [KTMutableMatrix matrixWithCuboidBounds:5,3,2,0];and the equivalent of matrix[2][3][1] = [anObject retain];is [matrix setObject:anObject atCoordinates:2,3,1];

Does anyone have any input on this matter? Compliments, comments, insults and directions to code that already does something similar all welcome. I really want to design this thing well if there is interest, so don’t be shy.

– KritTer

—-Issues discovered (and, hopefully, comments)—-

I have minimized this by only having a couple of methods that deal with pre-hashed values, one explicit in the base class, one implicit in the iterator

—-General comments so far…—-

I am very interested in this. As it happens, it popped up at basically the exact moment that it might be useful to me. Serendipity!

Anyhow, I suspect that others will be interested also.

As an aside, does anybody know when the MiscKit is going to release something for OS X? KritTer’s matrix class sounds like it might fit nicely into the MiscKit, but they haven’t released anything in quite a while. If all else fails, a CocoaDev framework might be an option…

– RobRix


Although I don’t think I have any time to devote to beta testing, I will eagerly await the outcome of this project. It seems like a Cool Thing. – DavidRemahl


Ahh… I’d beta test if I could, but outside using it in a program I’m making, I’m probably not going to have enough time, sorry :\

23 -init… methods? That beats my record. – RobRix

Even that would be great. With that many inits, I’d spend days checking it all :( – KritTer

Okie dokie. I gotta learn to use F-Script and ObjCUnit and those sorts of things… – RobRix


OK, I know I’m being naive or something. I’ve looked through KTMutableMatrixes documentation but I still don’t get it. If you send one of the matrix: messages to get a matrix and store it in a KTMutableMatrix *theMatrix it seems you have to declare cuboid bounds. If you do, then, and only declare, say 3,4,2, and access 3,4,3, will an error happen and if so, how do you “add” to it’s bounds? I’m confused. KTMutableMatrix seems almost the same as KTMatrix. Help?


KritTer’s updates:

*4 October 2002 - As noted elsewhere, I am on an extended break from programming while I get Part III Mathematics out of the way. I have passed the baton - and the unfinished fourth alpha - on to RobRix. He will be in charge of future developments. Kritter out.


I have a quick question:

wouldn’t

id myObjCArray[0][0][0] = @”Test String”;

Work? Now that I think about it, you might not even need NSArray for static arrays. Wow.