I'd ditch the set idea (it is a waste of both memory and time to store two integers in a std::set
) and go with the pair. Then define
template <class A>
struct unordered_pair_hash
{
std::size_t operator()(const std::pair<A, A>& p) const {
using std::min;
using std::max;
return std::hash(min(p.first, p.second))+
17*std::hash(max(p.first, p.second));
}
};
template <class A>
struct unordered_pair_eq
{
bool operator()(const std::pair<A, A>& p1, const std::pair<A, A>& p2) const {
using std::min;
using std::max;
return min(p1.first, p1.second)==min(p2.first, p2.second) &&
max(p1.first, p1.second)==max(p2.first, p2.second);
}
};
and then declare the map with a custom hash and equality.
std::unordered_map<std::pair<int, int>, float, unordered_pair_hash<int>, unordered_pair_eq<int> > ...