0

I am creating a gridcell class which is basically a multi array of cells. I want this gridcell to have any number of dimensions. This means that in the declaration of the boost::multiarray variable I cannot specify the second argument of the template. Concretely, my code looks as follows:

#include "cell.h"
#include <iostream>
#include <vector>
#include <boost/multi_array.hpp>

class GridCell {
public: 
    GridCell(); // Default constructor not used.
    GridCell(const std::vector<int> dims, const float leafsize);
    virtual ~GridCell();

    friend std::ostream& operator << (std::ostream & os, const GridCell & c);

private:
    std::vector<int> dims_; // Vector containing the size of each dimension.
    float leafsize_; // It is assumed that the cells in the grid are cubic.
    boost::multi_array<Cell,ndims> * grid;
};

Concretely, boost::multi_array<Cell,ndims> * grid; The number of dimensions I want it to be specified in the Gridcell class constructor.

Any advice or alternative?

alfC
  • 14,261
  • 4
  • 67
  • 118
Javi
  • 3,440
  • 5
  • 29
  • 43
  • 1
    No way to do this with `boost::multi_array`. The answer to the question I marked this a duplicate of has example code for dynamic dimensions. – pmr Dec 13 '13 at 13:05
  • Do you have any suggestion about a possible alternative? I have thought two possible solutions: -Not having the boost multiarray within the class, just passing it by reference to the methods of the class, - Defining a maximum of dimensions. Let's sayboost::multi_array * grid as I saw in the boost documentation that it is possible to define some of the dimensions with just an int, which means that dimension is actually "flat" (http://www.boost.org/doc/libs/1_55_0/libs/multi_array/doc/reference.html the sentence A degenerate dimension occurs when a single-index is specified...) – Javi Dec 14 '13 at 21:32
  • That would suck. How about rolling your own? A primitive implementation does not seem too hard to me. – pmr Dec 14 '13 at 21:38
  • Well, although I have some experience with C++, I must confess that I have no idea how to face such primitive implementation. Could you give me any advice? Thank you! – Javi Dec 16 '13 at 09:33
  • This really depends on the feature set. If all your dimensions have the same size, this becomes much easier. – pmr Dec 16 '13 at 12:50
  • Well, I would like to stick to the most general case, but I could accept such solution in which dimensions have the same size. However, I have decided to simplify the problem, and create different classes for 2,3 and 6 dimensions, which are the ones more representative for my problem. Thank you very much! – Javi Dec 16 '13 at 16:26

1 Answers1

0

In order to close the question, I add this answer:

Finally I solved the problem by creating a self-made container based on arrays. Initially it was based on vectors, but I preferred to go to arrays by determining the number of dimensions and the maximum size. In this case, I think tis approach is better than using boost multi_array since my container is actually a flat array and the element indexing is generalized by mathematical operations.

Thank you for your replies.

Javi
  • 3,440
  • 5
  • 29
  • 43