lundi 25 février 2019

unordered_map on pair of complex as key fails soon in insert

I have a complex class (class MyComplex) where complex is defined by 2 double variables re and img

I have a function which takes 2 MyComplex objects and does some operations on the same and returns a new MyComplex object.

I wish to keep a cache in the function which first checks if the input has been seen before and if yes return the data from the cache as the calculation is quite expensive in time.

1) Let's define a typedef for the pair of MyComplex

  typedef std::pair<MyComplex, MyComplex> pairOfComplex;

2) An unordered map will need a pair_hash function

 struct pair_hash
{
    //template <class T1, class T2>
    std::size_t operator() (const std::pair<MyComplex, MyComplex> &pairOfComplex) const
    {
        return std::hash<double>()(pairOfComplex.first.re) ^ std::hash<double>()(pairOfComplex.first.im)
            ^ std::hash<double>()(pairOfComplex.second.re) ^std::hash<double>()(pairOfComplex.second.im);
    }
};

3) The actual cache

 std::unordered_map<pairOfComplex, MyComplex, pair_hash> MyCache;

problem: I find that after a few inserts the insert call to the map is failing even though the input is different.

I feel my insert is failing because pair_hash is not proper it seems.

1) What should e an appropriate pair_hash or what strategy should one apply here? 2) Should I change my cache Ds to be something different?

Aucun commentaire:

Enregistrer un commentaire