views:

871

answers:

4

Many times, I find myself having to define a container for multi-dimensional data.

Let's take an example: I have many Chips, each Chip has many Registers, each Register has many Cells, and each Cell has many Transistors.

At some stage of my C++ program I have to read this data, and later I have to use it.

I cannot use any external storage for this data: file, data-base, etc.

So, should I create some multi-dimensional STL container? A map of maps of vectors, or something like that... ?

Or should I create classes (structs) for each of them? Cell class that contains a vector of Transistors, and then a Register class that contains a vector of Cells, etc? But what if later I want to access my data by Transistor, and not by Chip?

Any other way?

Thank you

EDIT: Forgot to mention: I cannot use boost.

+5  A: 

You need to map your domain.

So, should I create some multi-dimensional STL container? A map of maps of vectors, or something like that... ?

Each vector/map will contain objects of some type. That brings us to your next question :)

Or should I create classes (structs) for each of them?

Looks like that is what you need at the very least.

Cell class that contains a vector of Transistors, and then a Register class that contains a vector of Cells, etc?

Look at both has-a and is-implemented-in-terms-of designs.

But what if later I want to sort my data by Transistor, and not by Chip?

What data? You can always pass around comparators depending on the context. Also, ask yourself if you really need to expose the Transistor level details to someone working with a Chip. That'll help get started.

dirkgently
+1  A: 

If you want to access your data along different "dimensions," you may be interested in boost::multi_index_container. I haven't used it myself, but it looks like it fits the bill.

j_random_hacker
+4  A: 

Implement full classes for them. Your code will be cleaner in the end.

Whenever I ignore this axiom, it comes back to haunt me. I implemented a hierarchical 3-tiered string collection in terms of std::pairs of std::strings and std:pairs. It was quick and simple, and when I had to replace one layer and then another with a class to contain extra attributes, it was surprisingly easy to do. But in the end, the code was a mess and I wasn't happy documenting it. Lesson learned again, and again, and again...

phord
Reminds me of a discussion of Charniak et al.'s book on AI programming. After designing a system based on something like that, they couldn't remember what property (cadar x) meant (roughly, x.first.second.first) and couldn't redesign it.
David Thornley
+1  A: 

As advised, I chose to implement full classes:

class Chip
{
    map<RegisterLocation, Register> RegistersPerLocation;
  public:
    void AddRegisterPerLocation(RegisterLocation, Register); 

};

class Register
{
    map<CellLocation, Cell> CellsPerLocation;
  public:
    void AddCellPerLocation(CellLocation, Cell); 
};

// etc..
Igor Oks