lundi 20 mai 2019

C++: Can we add and remove vectors from a class?

I have a C++ software that performs agent based simulations. In a simulation, there are Populations that contain Patches that contain Individuals that contain two Haplotypes. Each Haplotype contain 10 vectors each serving to track different types of genes.

class Haplotype
{
    std::vector<A> genesA;
    std::vector<B> genesB;
    std::vector<C> genesC;
    std::vector<D> genesD;
    ....

};

In practice, however, a user will never use more than one or two types of genes. This means that each of these unused vectors consume a few bytes. For simulations with lots of genes and few Individuals that is negligible. However, for simulations with few genes and lots of individuals these extra bytes might start to matter. Performance (both CPU time and RAM) is of prime importance.

Is there a nice design pattern that would allow me to deal with this issue? Something that could add the vectors into Haplotype on demand only?

Aucun commentaire:

Enregistrer un commentaire