Using union for structure hashing with std::unordered_map or std::unordered_set

276 views Asked by At

I have a simple structure:

struct data
{
  bool flag;
  std::size_t ix;
  std::size_t iy;
  std::size_t d;
};

I keep all instances of this structure in container, and I need to search it as fast as possible. At the moment I use the std::set with custom comparison functor, based on lexicographical comparison. However, I want to experiment with storing my data in std::unordered_set.

I used boost::hash_combine for creating custom hash functor, but the result was actually even slower, than with std::set. I want now to pack all the data from the structure in a unsigned long long and to use standard std::hash functor. So an idea is to have something like:

struct hashed_data
{
  union
  {
    struct
    {
      bool flag;
      std::size_t ix;
      std::size_t iy;
      std::size_t d;
    } data;
    unsigned long long value_to_hash;
  };
};//hashed_data

The problem is, I want to make sure, that my structure actually fits in unsigned long long type. I can change the type of my data members within limits:

ix and iy are less then a 100,000 (they might not fit into 16 bits integer, but 24 bits is more than enough, however, there is no standard 24 bits integers?).

d can safely be of a char type.

I would be grateful for any views on this problem -- whether idea is sound at all and how to achieve my objective.

0

There are 0 answers