Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ storing a value in an unordered pair

I want to store a floating point value for an unordered pair of an integers. I am unable to find any kind of easy to understand tutorials for this. E.g for the unordered pair {i,j} I want to store a floating point value f. How do I insert, store and retrieve values like this?

like image 703
AvZ Avatar asked Mar 21 '15 07:03

AvZ


3 Answers

Simple way to handle unordered int pairs is using std::minmax(i,j) to generate std::pair<int,int>. This way you can implement your storage like this:

   std::map<std::pair<int,int>,float> storage;
   storage[std::minmax(i,j)] = 0.f;
   storage[std::minmax(j,i)] = 1.f; //rewrites storage[(i,j)]

Admittedly proper hashing would give you some extra performance, but there is little harm in postponing this kind of optimization.

like image 168
Ap31 Avatar answered Sep 26 '22 15:09

Ap31


Here's some indicative code:

#include <iostream>
#include <unordered_map>
#include <utility>

struct Hasher
{
    int operator()(const std::pair<int, int>& p) const
    {
        return p.first ^ (p.second << 7) ^ (p.second >> 3);
    }
};

int main()
{
    std::unordered_map<std::pair<int,int>, float, Hasher> m =
    { { {1,3}, 2.3 },
      { {2,3}, 4.234 },
      { {3,5}, -2 },
    };

    // do a lookup
    std::cout << m[std::make_pair(2,3)] << '\n';
    // add more data
    m[std::make_pair(65,73)] = 1.23;
    // output everything (unordered)
    for (auto& x : m)
        std::cout << x.first.first << ',' << x.first.second
            << ' ' << x.second << '\n';
}

Note that it relies on the convention that you store the unordered pairs with the lower number first (if they're not equal). You might find it convenient to write a support function that takes a pair and returns it in that order, so you can use that function when inserting new values in the map and when using a pair as a key for trying to find a value in the map.

Output:

4.234
3,5 -2
1,3 2.3
65,73 1.23
2,3 4.234

See it on ideone.com. If you want to make a better hash function, just hunt down an implementation of hash_combine (or use boost's) - plenty of questions here on SO explaining how to do that for std::pair<>s.

like image 34
Tony Delroy Avatar answered Sep 23 '22 15:09

Tony Delroy


You implement a type UPair with your requirements and overload ::std::hash (which is the rare occasion that you are allowed to implement something in std).

#include <utility>
#include <unordered_map>

template <typename T>
class UPair {
  private:
    ::std::pair<T,T> p;
  public:
    UPair(T a, T b) : p(::std::min(a,b),::std::max(a,b)) {
    }   
    UPair(::std::pair<T,T> pair) : p(::std::min(pair.first,pair.second),::std::max(pair.first,pair.second)) {
    }   
    friend bool operator==(UPair const& a, UPair const& b) {
      return a.p == b.p;
    }   
    operator ::std::pair<T,T>() const {
      return p;
    }   
};
namespace std {
  template <typename T>
  struct hash<UPair<T>> {
    ::std::size_t operator()(UPair<T> const& up) const {
      return ::std::hash<::std::size_t>()(
               ::std::hash<T>()(::std::pair<T,T>(up).first)
             ) ^
             ::std::hash<T>()(::std::pair<T,T>(up).second);
      // the double hash is there to avoid the likely scenario of having the same value in .first and .second, resulinting in always 0
      // that would be a problem for the unordered_map's performance
    }   
  };  
}

int main() {
  ::std::unordered_map<UPair<int>,float> um;
  um[UPair<int>(3,7)] = 3.14;
  um[UPair<int>(8,7)] = 2.71;
  return 10*um[::std::make_pair(7,3)]; // correctly returns 31
}
like image 39
bitmask Avatar answered Sep 23 '22 15:09

bitmask