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.
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...
As advised, I chose to implement full classes:
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.You need to map your domain.
Each vector/map will contain objects of some type. That brings us to your next question :)
Looks like that is what you need at the very least.
Look at both
has-a
andis-implemented-in-terms-of
designs.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 aChip
. That'll help get started.