I'm working on a library for doing computations with multivectors and I'm trying to figure out how to properly store the data.
Very quickly, a multivector is the sum of blades, each containing zero or more bases. For example, V = 1 + 2x + 3yz - 4xz is a multivector.
In Python, the structure I settled on was a mapping of tuples of basis indices to scalars; in type syntax that's TermDict = Dict[Tuple[int, ...], float]
- V above would be stored as {(): 1.0, (0,): 2.0, (1, 2): 3.0, (0, 2): -4.0}
However I'm now attempting to port this to C++ and getting uncomfortable. Currently what I have for the purpose is:
// Adapted from https://stackoverflow.com/a/53283994/6605349
struct SetHasher {
int operator()(const std::set<unsigned int>& V) const {
int hash = V.size();
for (auto &i : V) {
hash ^= i + 0x9e3779b9 + (hash << 6) + (hash >> 2);
}
return hash;
}
};
typedef std::unordered_map<std::set<unsigned int>, double, SetHasher> TermDict;
which I believe just emulates the Python behavior. However, with apologies to Chirag, I have a lot less faith in a random SO answer than in the CPython tuple.__hash__
implementation, so I'm wondering if there's a better way to go about this that can avoid the need to hash a collection of indices. I could possibly apply this to the Python implementation too. What might there be? Or have I got the best idea after all?