I have the following basic class structure:
class Distance : public Base {
public:
using Base::Base;
void run(int u, int v); // indices for nodes in graph
void runAll();
};
and
class Base {
protected:
const Graph& G;
Matrix& results;
public:
explicit Base(const Graph& G, Matrix& results);
virtual ~Base() = default;
virtual void run(int u, int v) = 0;
virtual void runAll() = 0;
double getResult(int u, int v) const;
const Matrix& getResults() const;
};
where Distance
basically calculates either for a node-pair (u, v) or for all possible node-pairs a distance-score which gets stored in the results Matrix
.
The problem right now is the separation of the algorithm and the data structure. If I want to reuse the run
-method of Distance
in a second class AnotherDistance
derived from Base
I have to allocated the matrix 2 times (1 in Distance
and 1 in AnotherDistance
) which is not feasible in my case as the matrix could amount to multiple GB.
What would be the best approach to solve this? I could take the results-Matrix as an argument in the constructor (probably bad design) or maybe use move-semantics in another separate getter which would empty the matrix in the class.