lundi 23 juin 2014

C++ class design: dynamic typing alternative to template argument?


Vote count:

0




I would like to build a space-efficient modular arithmetic class. The idea is that the modulus M is an immutable attribute that gets fixed during instantiation, so if we have a large array (std::vector or another container) of values with the same M, M only needs to be stored once.


If M can be fixed at compile time, this can be done using templates:



template <typename num, num M> class Mod_template
{
private:
num V;
public:
Mod_template(num v=0)
{
if (M == 0)
V = v;
else
{
V = v % M;
if (V < 0)
V += M;
}
}
// ...
};

Mod_template<int, 5> m1(2); // 2 mod 5


However, in my application, we should be able to express M runtime. What I have looks like this:



template <typename num> class Mod
{
private:
const num M;
num V;
public:
Mod(num m, num v=0): M(abs(m))
{
if (M == 0)
V = v;
else
{
V = v % M;
if (V < 0)
V += M;
}
}
// ...
};

Mod<int> m2(5, 2); // 2 mod 5
Mod<int> m3(3); // 0 mod 3


This works, but a large vector of mod M values uses 2x the space it needs to.


I think the underlying conceptual problem is that Mod's of different moduli are syntactically of the same type even though they "should" be different types. For example, a statement like



m2 = m3;


should raise a runtime error "naturally" (in my version, it does so "manually": check is built into the copy constructor, as well as every binary operator I implement).


So, is there a way to implement some kind of dynamic typing so that the Mod object's type remembers the modulus? I'd really appreciate any idea how to solve this.


This is a recurring problem for me with various mathematical structures (e.g. storing many permutations on the same set, elements of the same group, etc.)



asked 39 secs ago






Aucun commentaire:

Enregistrer un commentaire