C++ Question: Arrays of Arbitrary Dimension?

Ah, C programmers. Can’t live with 'em, can’t, well…

I kid, of course. I kid because I love. :stuck_out_tongue:

Thanks for all your replies. I think Sage Rat’s code is the closest to what I had in mind, though I would use C++ style. It has the added benefit of holding the data consecutively in memory, which, if I understand correctly, Stealth Potato’s code doesn’t do.

I’m writing code to do some analysis on a discrete function of d variables. In other words, I have a uniform cubical grid where each cube (square, hypercube) is mapped to a value (think of a digital picture, which is the 2D case) and I find critical sets of cubes, that is maxima, minima and saddles. Since I’m going to work with datasets of varied dimensionality (mostly 2D, 3D and 4D), I’m trying to avoid rewriting the methods for each case. To give an example, I have a method that, starting at a singular cube (i.e. potentially part of a critical set), recursively checks its neighbours to build a set of connected singular cubes. Looping on a cube’s neighbours is done the same way regardless of the grid’s dimension, so I can use the same method for every case. But for this I need to have a data structure containing an array of arbitrary dimension. Boost’s multi_array structure doesn’t work, since while it allows me to define an array of any fixed dimensionality, it doesn’t allow me to define an array of arbitrary (indefinite) dimensionality.

My code has speed constraints, but I don’t really hit the bottleneck accessing the dataset. So even a relatively inefficient solution doesn’t bother me too much in this case. I’m sure my code is full of inefficiencies and dubious programming choices; I’m not an expert. But I’m trying to improve it, hence the reason why I want to find a generic solution in this case. C++ is the language with which I’m most familiar, and I’ve already written something workable, so rewriting it in another language doesn’t really appeal to me now. :wink:

Nope, mine doesn’t, but only because I just tossed a single “data” field into the dimension structure when I was starting out (it was just the first thing that came to mind), and built around that. It would be very easy to factor the data up a level so that you’re storing each “row” of your last dimension contiguously.

It also has the benefit of making it fairly easy to implement the more natural subscripting and assignment syntax, as I’ve shown, but how much a factor that is depends on if you really care about being able to write array[y][z][t] = 42 instead of int subs = { x, y, z, t }; array.set(42, subs); :stuck_out_tongue:

(Or array.set(42, x, y, z, t), since I suppose you could implement get and set as variadic functions.)

If you wanted to go with that sort of “composed dimensions” approach, though, I’d probably do it along the lines ticker suggested – instead of each dimension containing a vector of subdimensions and a vector of data (only one of which would be used), make dimension an abstract base class and have two subclasses, one for containing subdimensions, and one for containing a row of data. That breakdown is much more elegant, in particular when it comes to regulating the behavior of the conversion and assignment operators, which can then be done polymorphically instead of with an explicit leaf check.