C++ multi-dimensional data handling

丶灬走出姿态 提交于 2019-12-02 07:16:03

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...

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.

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.

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..
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!