# cube structure

Discussion in 'C++' started by Philipp Kraus, Apr 21, 2010.

1. ### Philipp KrausGuest

Hello,

I need y n-dimensional cube structur. Is there a solution in the STL or Boost?
The datatypes of the cube must be variable. I think an array is like
this structure
but I need a dynamic allocation:

cube c<int>(i, 10) //this declare a cube with 10^i elements

Thanks for help

Phil

Philipp Kraus, Apr 21, 2010

2. ### John H.Guest

On Apr 21, 4:16 pm, Philipp Kraus <> wrote:
> I need y n-dimensional cube structur.

You should try writing your own, it can be fun.

>Is there a solution in the STL or Boost?

After you wrote your own, throw it away and look at boost's
Multidimensional Array Library:
http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

John H., Apr 21, 2010

3. ### Marcel MüllerGuest

Philipp Kraus wrote:
> I need y n-dimensional cube structur. Is there a solution in the STL or
> Boost?

Directly: no.

But feel free to setup a nested structure. However, you have to take
care of the vector size if you need to synchronize that in the deeper
levels. It depends a bit on what you call 'dynamic allocation'. If the
size is a lifetime constant of the objects, it is quite easy to handle.

> cube c<int>(i, 10) //this declare a cube with 10^i elements

Here you have another implicit constraint. All dimensions are the same.
Is this always true?

Furthermore think about the memory complexity. You have exponential
complexity here. Is this really what you need? Your memory footprint
will literally explode with i. Are all values in the tensor distinct or
are many of them zero? In the latter case you should prefer some sparse
tensor implementation. Also a Perl like create on demand solution might
be sufficient where non-existing elements are zero by definition.

Whatever you do, do not allocate a single chunk of memory and index it
by x,y,z etc. This may cause unwanted memory fragmentation. And, of
course, reallocations are extremely expensive.

Marcel

Marcel Müller, Apr 21, 2010
4. ### red floydGuest

On Apr 21, 3:11 pm, Marcel Müller <>
wrote:
> Philipp Kraus wrote:

> > cube c<int>(i, 10) //this declare a cube with 10^i elements

>
> Here you have another implicit constraint. All dimensions are the same.
> Is this always true?
>

Kind of implied by the problem statemtn -- "cube".

red floyd, Apr 22, 2010
5. ### Marcel MüllerGuest

red floyd wrote:
>> Here you have another implicit constraint. All dimensions are the same.
>> Is this always true?

>
> Kind of implied by the problem statemtn -- "cube".

Well, from mathematical point of view, yes. In context of database and
BI applications cube is used for multi dimensional data containers in
general.

Marcel

Marcel Müller, Apr 24, 2010