문제

I have a Polygon structure and a function with the following signature

Polygon myfunction(Polygon &pol1, Polygon &pol2, int rot1, int rot2); 

in which pol1 and pol2 are Polygons, rot1 and rot2 are rotation applied to pol1 and pol2.

I need to store all the results of myfunction applied to a set of polygons and a set of admissible rotations.

So, for example, if I have 100 polygons and 4 admissible rotations, I need to store 100*100*4*4 = 160000 polygons. I'm aware that I need a lot of memory, but I'm ok with it.

For the moment I'm storing them in a container like that:

vector<vector<vector<vector<Polygon>> results;

so that calling results[pol1][pol2][rot1][rot2]; I get the right polygon.

I know that accessing a single result could be quite inefficient, so how can I improve it?

도움이 되었습니까?

해결책

It depends on many thing, my solution is mixing std::tuple and std::map

typedef std::tuple<Polygon , Polygon , int, int> Params;

std::map<Params, Polygon> results;

Which maps parameters to results. In this case you can find result of an specific input efficiently.

To use, you have to overload operator< for Polygon because of std::map :

struct Polygon
{
// ...
};

inline bool operator<(const Polygon &p1, const Polygon &p2)
{
    return ...;
}

Polygon p1, p2, p3;

// ...

results.insert(std::make_pair(std::make_tuple(p1, p2, 1, 2), p3));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top